forked from NationalBankBelgium/stark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-functions.sh
247 lines (212 loc) · 7.67 KB
/
build-functions.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env bash
#######################################
# Verifies a directory isn't in the ignored list
# Arguments:
# param1 - Path to check
# Returns:
# Boolean
#######################################
isIgnoredDirectory() {
#logTrace "${FUNCNAME[0]}: Checking for ${1}" 1
name=$(basename ${1})
if [[ -f "${1}" || "${name}" == "src" || "${name}" == "test" || "${name}" == "integrationtest" || "${name}" == "reports" || "${name}" == "coverage" || "${name}" == "assets" || "${name}" == "typings" || "${name}" == "node_modules" ]]; then
#logTrace "No" 1
return 0
else
#logTrace "Yes" 1
return 1
fi
}
#######################################
# Run "ng build <library_name>"
# Arguments:
# param1 - Library name
# Returns:
# None
#######################################
ngBuild() {
logTrace "Executing function: ${FUNCNAME[0]}: 'ng build ${1}'" 1
logTrace "${FUNCNAME[0]}: command: '${NG} build ${1}'" 2
${NG} build $1
logTrace "${FUNCNAME[0]}: 'ng build ${1}' completed" 1
}
#######################################
# Check if array contains an element
# Arguments:
# param1 - Element to check
# param2 - Array to look for element in
# Returns:
# None
#######################################
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
#######################################
# Adds banners to all files in a directory
# Arguments:
# param1 - Directory to add license banners to
# Returns:
# None
#######################################
addBanners() {
logTrace "${FUNCNAME[0]}" 1
logDebug "Adding banners to all files in $1" 1
for file in ${1}/*; do
if [[ -f ${file} && "${file##*.}" != "map" ]]; then
# TODO pass LICENSE_BANNER as a param
logTrace "Adding banner to : ${file}"
cat ${LICENSE_BANNER} > ${file}.tmp
cat ${file} >> ${file}.tmp
mv ${file}.tmp ${file}
fi
done
}
#######################################
# Update version references
# Arguments:
# param1 - version
# param2 - directory in which version references should be updated
# Returns:
# None
#######################################
updateVersionReferences() {
logTrace "Executing function: ${FUNCNAME[0]}" 1
local NPM_DIR="$2"
(
cd ${NPM_DIR}
local PATTERN="0\.0\.0\-PLACEHOLDER\-VERSION"
perl -p -i.bak -e "s/$PATTERN/$1/g" ./package.json
)
}
#######################################
# Update package name references
# Arguments:
# param1 - package name
# param2 - directory in which version references should be updated
# Returns:
# None
#######################################
updatePackageNameReferences() {
logTrace "Executing function: ${FUNCNAME[0]}" 1
local NPM_DIR="$2"
(
cd ${NPM_DIR}
local PATTERN="PLACEHOLDER\-PACKAGE\-NAME"
perl -p -i.bak -e "s/$PATTERN/$1/g" ./package.json
)
}
#######################################
# Generate NPM ignore file based on the .gitignore at the root
# Arguments:
# param1 - directory in which the .gitignore file is located
# param2 - directory in which to place the generated .npmignore file
# Returns:
# None
#######################################
generateNpmIgnore() {
logTrace "Executing function: ${FUNCNAME[0]}" 1
local PROJECT_ROOT_DIR="$1"
local NPM_DIR="$2"
(
cp $PROJECT_ROOT_DIR/.gitignore $NPM_DIR/.npmignore
)
}
#######################################
# Generate NPM package (tar.gz) file using npm pack
# Arguments:
# param1 - directory where npm pack should be executed
# Returns:
# None
#######################################
generateNpmPackage() {
logTrace "Executing function: ${FUNCNAME[0]}" 1
local NPM_DIR="$1"
(
cd $NPM_DIR > /dev/null
npm pack ./ --silent
)
}
#######################################
# Update a package.json file's dependencies version for the given stark package
# Arguments:
# param1 - name of the stark package
# param2 - version of stark to set
# param3 - path to the package.json file to adapt
# param4 - sub level of the package to adapt
# Returns:
# None
#######################################
adaptNpmPackageDependencies() {
logTrace "Executing function: ${FUNCNAME[0]}" 1
local PACKAGE="$1"
local VERSION="$2"
local PACKAGE_JSON_FILE="$3"
local SUB_LEVEL=$(($4))
local PATH_PARENT=""
index=1
while [[ $index -le $SUB_LEVEL ]]
do
PATH_PARENT="..\/$PATH_PARENT"
index=$index+1
done
local TGZ_PATH="file:${PATH_PARENT}dist\/packages-dist\/$PACKAGE\/nationalbankbelgium-$PACKAGE-$VERSION.tgz"
logTrace "TGZ path: $TGZ_PATH"
local PATTERN="\"\@nationalbankbelgium\/$PACKAGE\"\s*\:\s*\".*\""
local REPLACEMENT="\\\"\@nationalbankbelgium\/$PACKAGE\\\": \\\"$TGZ_PATH\\\""
logTrace "PATTERN: $PATTERN"
logTrace "REPLACEMENT: $REPLACEMENT"
logTrace "Package JSON file: $PACKAGE_JSON_FILE"
# Packages will have dependencies between them. They will so have "devDependencies" and "peerDependencies" with different values.
# We should only replace the value of the devDependency for make it work.
perl -p -i.bak -e "s/$PATTERN/$REPLACEMENT/" $PACKAGE_JSON_FILE
}
#######################################
# Update a package-lock.json file's dependencies version for the given stark package
# Arguments:
# param1 - name of the stark package
# param2 - version of stark to set
# param3 - path to the package-lock.json file to adapt
# param4 - sub level of the package to adapt
# Returns:
# None
#######################################
adaptNpmPackageLockDependencies() {
logTrace "Executing function: ${FUNCNAME[0]}" 1
local PACKAGE="$1"
local VERSION="$2"
local PACKAGE_JSON_FILE="$3"
local SUB_LEVEL=$(($4))
local PATH_PARENT=""
index=1
while [[ $index -le $SUB_LEVEL ]]
do
PATH_PARENT="..\/$PATH_PARENT"
index=$index+1
done
local TGZ_REALPATH="dist/packages-dist/$PACKAGE/nationalbankbelgium-$PACKAGE-$VERSION.tgz"
local TGZ_PATH="file:${PATH_PARENT}dist\/packages-dist\/$PACKAGE\/nationalbankbelgium-$PACKAGE-$VERSION.tgz"
logTrace "TGZ path: $TGZ_PATH"
SHA="$(openssl dgst -sha512 -binary ./$TGZ_REALPATH | openssl enc -A -base64)"
ESCAPED_SHA=${SHA//\//\\/}
logTrace "SHA-512: $SHA"
logTrace "SHA-512 escaped: $ESCAPED_SHA"
local PATTERN="\\\"\@nationalbankbelgium\/$PACKAGE\\\": \\{(\s*)\\\"version\\\": \\\"(\S*)\\\"(,(\s*)\\\"resolved\\\": \\\"(.*))?,(\s*)\\\"integrity\\\": \\\"sha512-(.*)\\\""
local REPLACEMENT='"\@nationalbankbelgium\/'$PACKAGE'": {$1"version": "'$TGZ_PATH'",$4"integrity": "sha512-'$ESCAPED_SHA'"'
logTrace "PATTERN: $PATTERN"
logTrace "REPLACEMENT: $REPLACEMENT"
logTrace "Package JSON file: $PACKAGE_JSON_FILE"
# Packages will have dependencies between them. They will so have "devDependencies" and "peerDependencies" with different values.
# We should only replace the value of the devDependency for make it work.
perl -p -i.bak -0 -e "s/$PATTERN/$REPLACEMENT/m" $PACKAGE_JSON_FILE
# In npm >= v8, the package-lock.json file contains also the path in the node_modules folder.
local PATTERN="\\\"node_modules\/\@nationalbankbelgium\/$PACKAGE\\\": \\{(\s*)\\\"version\\\": \\\"(\S*)\\\"(,(\s*)\\\"resolved\\\": \\\"(.*))?,(\s*)\\\"integrity\\\": \\\"sha512-(.*)\\\""
local REPLACEMENT='"node_modules\/\@nationalbankbelgium\/'$PACKAGE'": {$1"version":"$2","resolved":"'$TGZ_PATH'",$4"integrity": "sha512-'$ESCAPED_SHA'"'
logTrace "PATTERN: $PATTERN"
logTrace "REPLACEMENT: $REPLACEMENT"
logTrace "Package JSON file: $PACKAGE_JSON_FILE"
# Packages will have dependencies between them. They will so have "devDependencies" and "peerDependencies" with different values.
# We should only replace the value of the devDependency for make it work.
perl -p -i.bak -0 -e "s/$PATTERN/$REPLACEMENT/m" $PACKAGE_JSON_FILE
}