-
Notifications
You must be signed in to change notification settings - Fork 8
/
copyrights.sh
executable file
·200 lines (173 loc) · 6.54 KB
/
copyrights.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
################################################################################
# Copyright 2021 General Electric Company
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
#!/usr/bin/env bash
{ set -e; } 2> /dev/null
function usage {
echo "Usage: $( python -c "import os; print os.path.abspath('${BASH_SOURCE[0]}')" ) [--debug] [(-d | --delete-copyright-headers) | (-u | --upsert-copyright-headers)] [(-f | --file) <filename>]"
}
function read_args {
while (( "$#" )); do
case "$1" in
-d|--delete-copyright-headers)
DELETE_COPYRIGHTS='true'
;;
-u|--upsert-copyright-headers)
UPSERT_COPYRIGHTS='true'
;;
--debug)
DEBUG='true'
;;
-f|--file)
shift
SRC_FILE="$1"
echo -e "Modifying source file: ${SRC_FILE}\n"
;;
*)
echo "Unknown option: ${1}"
usage
exit 2
;;
esac
shift
done
}
NEWLINE='
'
COPYRIGHT_HEADER_TITLE="Copyright $( date +'%Y' ) General Electric Company"
COPYRIGHT_HEADER_BODY="${COPYRIGHT_HEADER_TITLE}
\n
Licensed under the Apache License, Version 2.0 (the \"License\");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
\n
http://www.apache.org/licenses/LICENSE-2.0
\n
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an \"AS IS\" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"
function generate_copyright_header {
if [[ "$1" == 'java' ]]; then
BEGINNING_COMMENT_MARKER='/'
CONTINUING_COMMENT_MARKER='*'
ENDING_COMMENT_MARKER='/'
BEGINNING_COMMENT_LINE="$( printf -- "${CONTINUING_COMMENT_MARKER}%.0s" $( seq 1 79 ) )"
elif [[ "$1" == 'xml' ]]; then
BEGINNING_COMMENT_MARKER='<!'
CONTINUING_COMMENT_MARKER='-'
ENDING_COMMENT_MARKER='>'
BEGINNING_COMMENT_LINE="$( printf -- "${CONTINUING_COMMENT_MARKER}%.0s" $( seq 1 2 ) )"
elif [[ "$1" == 'sh' || "$1" == 'properties' ]]; then
BEGINNING_COMMENT_MARKER='#'
CONTINUING_COMMENT_MARKER="$BEGINNING_COMMENT_MARKER"
ENDING_COMMENT_MARKER="$CONTINUING_COMMENT_MARKER"
BEGINNING_COMMENT_LINE="$( printf -- "${CONTINUING_COMMENT_MARKER}%.0s" $( seq 1 79 ) )"
else
return
fi
ENDING_COMMENT_LINE="$BEGINNING_COMMENT_LINE"
COPYRIGHT_HEADER="$BEGINNING_COMMENT_MARKER"
COPYRIGHT_HEADER="${COPYRIGHT_HEADER}${BEGINNING_COMMENT_LINE}${NEWLINE}"
IFS="$NEWLINE"
for l in $COPYRIGHT_HEADER_BODY; do
if [[ "$BEGINNING_COMMENT_MARKER" != "$CONTINUING_COMMENT_MARKER" ]]; then
COPYRIGHT_HEADER="${COPYRIGHT_HEADER} "
fi
COPYRIGHT_HEADER="${COPYRIGHT_HEADER}${CONTINUING_COMMENT_MARKER}"
if [[ "$l" == '\n' ]]; then
COPYRIGHT_HEADER="${COPYRIGHT_HEADER}${NEWLINE}"
else
COPYRIGHT_HEADER="${COPYRIGHT_HEADER} ${l}${NEWLINE}"
fi
done
if [[ "$BEGINNING_COMMENT_MARKER" != "$CONTINUING_COMMENT_MARKER" ]]; then
COPYRIGHT_HEADER="${COPYRIGHT_HEADER} "
fi
COPYRIGHT_HEADER="${COPYRIGHT_HEADER}${ENDING_COMMENT_LINE}${ENDING_COMMENT_MARKER}${NEWLINE}"
echo "$COPYRIGHT_HEADER"
}
function delete_copyright {
if [[ "$1" == 'java' ]]; then
perl -i -pe 'BEGIN{undef $/;} s/\/\*{79}?\s*?\*\s*?Copyright.*?(\n\s*?\*.*?)+?\n\s*?\*{79}?\/\n{1,}//' "$2"
elif [[ "$1" == 'xml' ]]; then
perl -i -pe 'BEGIN{undef $/;} s/<!-{2}?\s*?-\s*?Copyright.*?(\n\s*?-.*?)+?\n\s*?-{2}?>\n{1,}//' "$2"
elif [[ "$1" == 'sh' || "$1" == 'properties' ]]; then
perl -i -pe 'BEGIN{undef $/;} s/#{80}?\s*?#\s*?Copyright.*?(\n\s*?#.*?)+?\n\s*?#{80}?\n{1,}//' "$2"
fi
}
function upsert_copyright {
delete_copyright "$1" "$2"
local TMP_FILE="${2}.__tmp__"
if [[ "$1" == 'xml' ]]; then
echo -e '<?xml version="1.0" encoding="UTF-8"?>\n' > "$TMP_FILE"
fi
echo "$( generate_copyright_header "$1" )" >> "$TMP_FILE"
echo '' >> "$TMP_FILE"
if [[ "$1" == 'xml' ]]; then
cat "$2" | awk '/<\?xml/{y=1;next}y' | sed '/./,$!d' >> "$TMP_FILE"
else
cat "$2" >> "$TMP_FILE"
fi
mv "$TMP_FILE" "$2"
if [[ "$1" == 'sh' ]]; then
chmod a+x "$2"
fi
}
function modify_copyright_in_file {
FILENAME="$( basename "$1" )"
EXTENSION="${FILENAME##*.}"
if [[ -n "$DELETE_COPYRIGHTS" ]]; then
echo "Deleting copyrights from file: ${1} with extension: .${EXTENSION}"
delete_copyright "$EXTENSION" "$1"
elif [[ -n "$UPSERT_COPYRIGHTS" ]]; then
echo "Upserting copyrights from file: ${1} with extension: .${EXTENSION}"
upsert_copyright "$EXTENSION" "$1"
fi
}
unset DELETE_COPYRIGHTS
unset UPSERT_COPYRIGHTS
unset DEBUG
unset SRC_FILE
read_args "$@"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ -n "$DEBUG" ]]; then
echo 'The following options are set:'
echo " DELETE_COPYRIGHTS: ${DELETE_COPYRIGHTS}"
echo " UPSERT_COPYRIGHTS: ${UPSERT_COPYRIGHTS}"
echo " DEBUG: ${DEBUG}"
echo " SRC_FILE: ${SRC_FILE}"
echo " DIR: ${DIR}"
echo ''
fi
if [[ -n "$DELETE_COPYRIGHTS" && -n "$UPSERT_COPYRIGHTS" ]]; then
echo "Can't specify the deletion and upsert options at the same time"
usage
exit 2
fi
if [[ -n "$DEBUG" ]]; then
generate_copyright_header 'java'
generate_copyright_header 'sh'
generate_copyright_header 'xml'
fi
if [[ -z "$SRC_FILE" ]]; then
for f in $( find "$DIR" \( -not -path '*/\.*' -and -not -path '*/failsafe*' -and -not -path '*/surefire*' \) -type f \( -iname '*.java' -or -iname '*.sh' -or -iname '*.properties' -or -iname '*.xml' \) ); do
modify_copyright_in_file "$f"
done
else
modify_copyright_in_file "$SRC_FILE"
fi