Skip to content

Commit

Permalink
Allow updating headers in add-file-headers.sh
Browse files Browse the repository at this point in the history
* Ignore changing amount of white space (avoids adding headers to files which do not have leading whitespace on empty line)
* add update option which allows to update any string in the first four lines of a header
  • Loading branch information
jfaltermeier authored and edgarmueller committed Mar 15, 2019
1 parent dffb57b commit cb71f96
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions add-file-headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
# Helper script to add headers to source files.
# Invoke without arguments to display usage.
#
# date: 2018-19-01
# date: 2019-14-03
# author: Mat Hansen <mhansen@eclipsesource.com>
# author: Johannes Faltermeier <mhansen@eclipsesource.com>
#

MODE="undefined"
HEADER_FILE="LICENSE"
SRC_PATH="/dev/null"
INDENT=2 # two spaces
REPLACEMENT="undefined"

declare -a EXT_LIST=(ts:c tsx:c)

function print_usage {
show_status usage "$0 [all|git-dirty] [-h|--header=/path/to/header] [-p|--path=/path/to/src]"
show_status usage "$0 [all|git-dirty] [-h|--header=/path/to/header] [-p|--path=/path/to/src] [-u|--update=\"toreplace/replacement\"]"
show_status usage "Example: $0 all --header=./LICENSE --path=/lib # Will add header from LICENSE file and it to all supported file types in /lib directory"
show_status usage "Example: $0 all --header=./LICENSE --path=/lib --update=\"2018 /2018-2019 \" # replace \"2018 \" with \"2018-2019 \" in existing headers"
}

function show_status {
Expand Down Expand Up @@ -60,6 +63,7 @@ function add_header {
indent=$(printf ' %.0s' $(seq 0 $(($2-1))))
header=$3
file=$4
replace=${@:5}

tmp_header=.~$style-style.header.tmp

Expand All @@ -68,8 +72,8 @@ function add_header {
if [ ! -f $tmp_header ]; then
cat $header | sed -e 's/^\(.*\)/ \1/g; 1 s#^\(.*\)$#/*\n\1#; $ s#^\(.*\)$#\1\n*/#;' > $tmp_header
fi
# check whether the first 3 lines are the same
if [ $(diff -q <(head -n 3 $tmp_header) <(head -n 3 $file) | grep differ | wc -l) -eq 1 ]; then
# check whether the first 3 lines are the same. ignore whitespace amount
if [ $(diff -qb <(head -n 3 $tmp_header) <(head -n 3 $file) | grep differ | wc -l) -eq 1 ]; then
cat $tmp_header > $file.new
cat $file >> $file.new
mv $file.new $file
Expand All @@ -80,6 +84,16 @@ function add_header {
show_status error "Unsupported header style '$1'"
exit 1
esac

if [ "$replace" != "undefined" ]; then
# XXX: trailing spaces cut off
if [[ $replace == *" /"* ]]; then
replace="$replace "
fi

# replace in first 4 lines
bash -c "sed -i '1,4 s/$replace/g' $file"
fi

}

Expand All @@ -98,6 +112,9 @@ while [ $# -gt 0 ]; do
-p=*|--path=*)
SRC_PATH="${1#*=}"
;;
-u=*|--update=*)
REPLACEMENT="${1#*=}"
;;
*)
fail_arg "Invalid argument '$1'. Forgot the '=' sign or trailing spaces?"
print_usage
Expand All @@ -119,11 +136,11 @@ do
show_status info "Searching for $ext files... (applying $style style headers)"
case "$MODE" in
all)
find $SRC_PATH -name "*.$ext" -exec bash -c 'add_header $0 $1 $2 "$3" $4' $style $INDENT $HEADER_FILE {} \;
find $SRC_PATH -name "*.$ext" -exec bash -c 'add_header $0 $1 $2 "$3" $4 $5' $style $INDENT $HEADER_FILE {} $REPLACEMENT \;
;;
git-dirty)
for file in $(git ls-files --others --exclude-standard --modified $SRC_PATH/**/*.$ext); do
add_header $style $INDENT $HEADER_FILE $file
add_header $style $INDENT $HEADER_FILE $file $REPLACEMENT
done
;;
esac
Expand Down

0 comments on commit cb71f96

Please sign in to comment.