This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
/
hbw.sh
251 lines (198 loc) · 5.32 KB
/
hbw.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
248
249
250
251
#!/bin/bash
#
# Hornet-Builder-Wrapper
#
# --------------------------------------------------------
# Bootstrap phase: non sourced mode
#
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
set -e
set -u
PROGDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
else
echo "Sourced mode is not supported" >&2; exit 127
fi
# --------------------------------------------------------
# LOG4HBW
#
LOG_PREFIX="[hbw]"
function log_debug() {
local message="$@"
[ -n "${DEBIAN_SCRIPT_DEBUG:-}" ] && log "DEBUG" "${message}";
return $?
}
function log_info() {
local message="$@"
log "INFO" "${message}"
return $?
}
log_error() {
local message="$@"
log "ERROR" "${message}"
}
function log() {
local level="$1"
shift
local message="$@"
case "${level}" in
"DEBUG")
[ -n "${DEBIAN_SCRIPT_DEBUG:-}" ] && echo "${LOG_PREFIX} DEBUG ${message}"
;;
*)
echo "${LOG_PREFIX} ${level} ${message}"
;;
esac
return $?
}
# --------------------------------------------------------
#
#
NPM_EXEC="npm"
NODEJS_EXEC="node"
[ -z "${HB_PULLED_DIR:-}" ] && HB_PULLED_DIR="./node_modules/hornet-js-builder"
[ -z "${HB_BUILDER_CLI:-}" ] && HB_BUILDER_CLI="bin/builder-cli.js"
[ -z "${HB_INSTALL_BASEDIR:-}" ] && HB_INSTALL_BASEDIR=$(cd ~ ; pwd)"/.hbw"
function hbw_install_pulled_builder() {
local pulledVersion=$(${NODEJS_EXEC} ${HB_PULLED_DIR}/${HB_BUILDER_CLI} --version 2>/dev/null)
log_info "Pulled version: ${pulledVersion}"
rm -Rf "${HB_INSTALL_BASEDIR}/${pulledVersion}"
mkdir -p "${HB_INSTALL_BASEDIR}/${pulledVersion}/node_modules"
rsync -ah ${HB_PULLED_DIR}/ ${HB_INSTALL_BASEDIR}/${pulledVersion}/
rsync -ah ./node_modules/ ${HB_INSTALL_BASEDIR}/${pulledVersion}/node_modules/
rm -Rf ./node_modules
touch hb_version
HB_VERSION="${pulledVersion}"
echo "${HB_VERSION}" > hb_version
log_info "Install pulled version DONE"
return 0
}
function hbw_pull_builder() {
local builderVersion="${1}"
[ -z "${builderVersion}" ] && builderVersion="latest"
local installReportLog=$(mktemp)
${NPM_EXEC} install hornet-js-builder@${builderVersion} 2>&1 1>${installReportLog}
local returnCode=$?
if [ ! ${returnCode} -eq 0 ]; then
log_error "Hornet-js-builder version ${builderVersion} is not pulled properly"
cat ${installReportLog}
fi
rm -f ${installReportLog}
log_info "Builder version ${builderVersion} pulled DONE"
return ${returnCode}
}
function hbw_is_version_installed() {
local targetVersion="${1:-}"
case "${targetVersion}" in
"latest")
if [ ! -z "${HB_VERSION:-}" ] && [ ! "${HB_VERSION:-}" == "latest" ]; then
local installedVersion=$(${NODEJS_EXEC} ${HB_INSTALL_BASEDIR}/${HB_VERSION}/${HB_BUILDER_CLI} --version 2>/dev/null || true)
if [[ "${HB_VERSION}" == "${installedVersion}" ]]; then
return 0
fi
targetVersion="${installedVersion}"
fi
;;
*)
if [ "${targetVersion}" == "${HB_VERSION:-}" ]; then
local installedVersion=$(${NODEJS_EXEC} ${HB_INSTALL_BASEDIR}/${HB_VERSION}/${HB_BUILDER_CLI} --version 2>/dev/null || true)
if [[ "${HB_VERSION}" == "${installedVersion}" ]]; then
return 0
fi
fi
;;
esac
hbw_pull_builder "${targetVersion}" \
&& hbw_install_pulled_builder
return $?
}
function hbw_run() {
if [ -z "${HB_VERSION:-}" ]; then
hbw_die "Variable HB_VERSION must be set"
else
hbw_is_version_installed "${HB_VERSION}";
fi
${NODEJS_EXEC} ${HB_INSTALL_BASEDIR}/${HB_VERSION}/${HB_BUILDER_CLI} $@
return $?
}
###
#
#
function hbw_die() {
[ $# -ne 0 ] && log_error "$@" >&2
hbw_help
exit 1
}
###
# Script version
#
function hbw_version() {
echo -e "${HBW_MAIN_SCRIPT} version ${HBW_VERSION}"
echo -e "builder-cli version" $(hbw_run --version)
}
###
# Script usage
#
function hbw_usage() {
hbw_version
echo -e " --------------------------------------------------------"
echo -e ""
echo -e " Usage: ${HBW_MAIN_SCRIPT} [-wv|--wrapper-version] [-wh|--wrapper-help]"
echo -e " [--is-version-installed <hbVersion>]"
echo -e ""
}
###
# Script help
#
function hbw_help() {
hbw_usage
echo -e " Options ${HBW_MAIN_SCRIPT}:"
echo -e ""
echo -e " -wv, --wrapper-version Afficher la version de cet outil"
echo -e " -wh, --wrapper-help Afficher cette aide"
echo -e ""
echo -e " --is-version-installed Vérifier si la version de hornet-builder est installée"
echo -e " Si aucun argument, la variable HB_VERSION"
echo -e " est lue pour tenter cette vérification."
echo -e ""
echo -e " --------------------------------------------------------"
echo -e ""
hbw_run --help
}
function hbw_main() {
HBW_TASK="${1:-}"
[ -z "${HB_VERSION:-}" ] && HB_VERSION=$(cat hb_version 2>/dev/null || true);
###
# Command line router
#
case "${HBW_TASK}" in
"--wrapper-version"|"-wv")
hbw_version
;;
"--wrapper-help"|"-wh")
hbw_help
;;
"--is-version-installed")
shift
local builderVersion="${1:-}"
hbw_is_version_installed "${builderVersion}"
return $?
;;
*)
hbw_run $*
return $?
;;
esac
return 0
}
# --------------------------------------------------------
# Globals
#
HBW_MAIN_SCRIPT="hbw"
HBW_VERSION="1.0.0"
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
hbw_main $*
exit $?
fi
#
# Fin de hornet-builder-wrapper
# --------------------------------------------------------