forked from eclipsesource/jsonforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-file-headers.sh
executable file
·153 lines (126 loc) · 3.52 KB
/
add-file-headers.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
#
# Helper script to add headers to source files.
# Invoke without arguments to display usage.
#
# 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] [-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 {
case "$1" in
error)
echo -e "\e[1m\e[31m[ERROR]\e[0m $2"
;;
usage|info|*)
echo -e "\e[1m[$(echo $1 | awk '{print toupper($0)}')]\e[0m $2"
;;
esac
}
function fail {
show_status error "$1"
exit 1
}
function fail_arg {
show_status error "$1. Unable to continue."
print_usage
exit 1
}
function verify_args {
if [ ! -d $SRC_PATH ]; then
fail_arg "Invalid path"
fi
if [[ -z $HEADER_FILE || ! -f $HEADER_FILE ]]; then
fail_arg "Header file not found"
fi
}
function add_header {
style=$1
indent=$(printf ' %.0s' $(seq 0 $(($2-1))))
header=$3
file=$4
replace=${@:5}
tmp_header=.~$style-style.header.tmp
case "$style" in
c) # add c style /* ... */ 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. 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
fi
;;
*)
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
}
while [ $# -gt 0 ]; do
case "$1" in
all|git-dirty)
if [ $MODE == "undefined" ]; then
MODE="${1}"
else
fail_arg "Mode can only specified once!"
fi
;;
-h=*|--header=*)
HEADER_FILE="${1#*=}"
;;
-p=*|--path=*)
SRC_PATH="${1#*=}"
;;
-u=*|--update=*)
REPLACEMENT="${1#*=}"
;;
*)
fail_arg "Invalid argument '$1'. Forgot the '=' sign or trailing spaces?"
print_usage
exit 1
esac
shift
done
verify_args
export -f add_header show_status
for pattern in "${EXT_LIST[@]}"
do
ext=$(echo $pattern | cut -d":" -f1)
style=$(echo $pattern | cut -d":" -f2)
rm .~$style-style.header.tmp > /dev/null
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 $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 $REPLACEMENT
done
;;
esac
done
show_status info "Completed"
unset -f add_header show_status
exit 0