From bc27a51f1631b1a374e8633e456f466c61e19866 Mon Sep 17 00:00:00 2001 From: Stasinos Konstantopoulos Date: Wed, 12 Oct 2022 10:30:43 +0300 Subject: [PATCH] Support for different syntaxes Add a mechanism for defining different comment syntaxes. Use this mechanism to define shell and C style comments and add testcses for Python (shell style) and JS (C style). Add a mechanism for controlling which files to add the copyright statement to. Update copyright statement to 2022 and remove the UTF-8 character to make it easier to get clean tests reghardless of locale. --- addcopyright.sh | 57 ++++++++++++++++++++++++++++++++++++++++++++----- copyright.txt | 16 ++++++-------- drill/abcd.js | 3 ++- drill/abcd.py | 5 +++++ test.js | 13 +++++------ test.py | 12 +++++++++++ 6 files changed, 85 insertions(+), 21 deletions(-) mode change 100644 => 100755 addcopyright.sh create mode 100644 drill/abcd.py create mode 100644 test.py diff --git a/addcopyright.sh b/addcopyright.sh old mode 100644 new mode 100755 index f196383..c10fd1d --- a/addcopyright.sh +++ b/addcopyright.sh @@ -1,8 +1,55 @@ #!/bin/bash -## Solution found as part of this stackoverflow discussion: https://stackoverflow.com/questions/151677/tool-for-adding-license-headers-to-source-files +declare -A applyRE +declare -A addpre +declare -A addpost -for x in $*; do -head -$COPYRIGHTLEN $x | diff copyright.txt - || ( ( cat copyright.txt; echo; cat $x) > /tmp/file; -mv /tmp/file $x ) -done +addpre[c]='/*' +applyRE[c]='s/^/ * /' +addpost[c]=' */' + +applyRE[shell]='s/^/# /' +applyRE[matlab]='s/^/% /' +applyRE[lua]='s/^/-- /' + +if [[ x$1 == x ]]; then + echo 'Must provide comments style as first argument' >&2 + exit 1 +elif [[ -v "applyRE[$1]" ]] ; then + STYLE=$1 +else + echo "Unknown style" + exit 2 +fi + +if [[ x$2 == x ]]; then + # Apply to all files, excluding directories + FILES=$(find . -type f) +else + D=$(dirname "$2") + F="${2#${D}/}" + FILES=$(find $D -type f -name "$F" -print) +fi + + +tmpcopyright=$(mktemp /tmp/addcopyright.XXXXXX) + +if [[ -v "addpre[$STYLE]" ]] ; then echo "${addpre[$STYLE]}" > $tmpcopyright ; fi +sed "${applyRE[$STYLE]}" < copyright.txt >> $tmpcopyright +if [[ -v "addpost[$STYLE]" ]] ; then echo "${addpost[$STYLE]}" >> $tmpcopyright; fi +COPYRIGHTLEN=$(wc -l < $tmpcopyright) + +# Solution found as part of this stackoverflow discussion: +# https://stackoverflow.com/questions/151677/tool-for-adding-license-headers-to-source-files +for x in $FILES; do + tmpsrcfile=$(mktemp /tmp/addcopyright.XXXXXX) + head -$COPYRIGHTLEN $x | diff -q $tmpcopyright - > /dev/null + if [[ $? == 1 ]]; then + (cat $tmpcopyright; cat $x) > $tmpsrcfile + mv $tmpsrcfile $x + fi +done + +#rm -f $tmpcopyright $tmpsrcfile + +exit 0 diff --git a/copyright.txt b/copyright.txt index 9146707..7f51e5c 100644 --- a/copyright.txt +++ b/copyright.txt @@ -1,9 +1,7 @@ -/* - © Copyright 2020 tapasadhikary.com or one of its affiliates. - * Some Sample Copyright Text Line - * Some Sample Copyright Text Line - * Some Sample Copyright Text Line - * Some Sample Copyright Text Line - * Some Sample Copyright Text Line - * Some Sample Copyright Text Line -*/ \ No newline at end of file +(c) Copyright 2022 tapasadhikary.com or one of its affiliates. +Some Sample Copyright Text Line +Some Sample Copyright Text Line +Some Sample Copyright Text Line +Some Sample Copyright Text Line +Some Sample Copyright Text Line +Some Sample Copyright Text Line diff --git a/drill/abcd.js b/drill/abcd.js index d5a47cd..d65ec38 100644 --- a/drill/abcd.js +++ b/drill/abcd.js @@ -3,4 +3,5 @@ const abcd = () => { console.log('abcd'); } -abcd(); \ No newline at end of file +abcd(); + diff --git a/drill/abcd.py b/drill/abcd.py new file mode 100644 index 0000000..033add7 --- /dev/null +++ b/drill/abcd.py @@ -0,0 +1,5 @@ + +def abcd(): + print('abcd') + +abcd() diff --git a/test.js b/test.js index 982762c..e0fa055 100644 --- a/test.js +++ b/test.js @@ -1,15 +1,16 @@ -/* - © Copyright 2020 tapasadhikary.com or one of its affiliates. +/* + * (c) Copyright 2022 tapasadhikary.com or one of its affiliates. * Some Sample Copyright Text Line * Some Sample Copyright Text Line * Some Sample Copyright Text Line * Some Sample Copyright Text Line * Some Sample Copyright Text Line * Some Sample Copyright Text Line -*/ + */ -const test = () => { - console.log('test'); +const abcd = () => { + console.log('abcd'); } -test(); +abcd(); + diff --git a/test.py b/test.py new file mode 100644 index 0000000..8911a33 --- /dev/null +++ b/test.py @@ -0,0 +1,12 @@ +# (c) Copyright 2022 tapasadhikary.com or one of its affiliates. +# Some Sample Copyright Text Line +# Some Sample Copyright Text Line +# Some Sample Copyright Text Line +# Some Sample Copyright Text Line +# Some Sample Copyright Text Line +# Some Sample Copyright Text Line + +def abcd(): + print('abcd') + +abcd()