1 |
#!/usr/bin/env bash |
2 |
|
3 |
VERSION="1.0.0-alpha.1" |
4 |
|
5 |
set +e |
6 |
|
7 |
if [ -z "$DEBUG" ]; then |
8 |
set +x |
9 |
fi |
10 |
|
11 |
argv_0=$0 |
12 |
projectdir=$(dirname $0) |
13 |
projectdir=$(readlink -f "$projectdir") |
14 |
tmp_dir="$projectdir/.blaze" |
15 |
nvm_dir="$tmp_dir/nvm" |
16 |
node_dir="$tmp_dir/nvm/versions/node" |
17 |
bun_dir="$tmp_dir/bun" |
18 |
nvm_home="$tmp_dir/nvm_home" |
19 |
bun="bun" |
20 |
node="node" |
21 |
npm="npm" |
22 |
|
23 |
mkdir -p "$tmp_dir" |
24 |
mkdir -p "$nvm_home" |
25 |
|
26 |
getprop() { |
27 |
value=$(cat "$projectdir/blaze/wrapper/blaze_wrapper.properties" | grep "$1" | cut -d'=' -f2) |
28 |
echo $value |
29 |
} |
30 |
|
31 |
print() { |
32 |
echo -en "\033[${1}m$2\033[0m" |
33 |
echo -e " $3" |
34 |
} |
35 |
|
36 |
blaze_srcpath=$(getprop "blaze.srcpath") |
37 |
|
38 |
if [ -z "$blaze_srcpath" ]; then |
39 |
blaze_srcpath="build_src" |
40 |
fi |
41 |
|
42 |
blaze_srcpath="$projectdir/$blaze_srcpath" |
43 |
|
44 |
if [ ! -d "$blaze_srcpath" ]; then |
45 |
print "1;31" "error " "BlazeBuild source path \"$blaze_srcpath\" not found." |
46 |
exit 1 |
47 |
fi |
48 |
|
49 |
startup() { |
50 |
echo -e "\033[1;34mBlazeBuild Wrapper version $VERSION\033[0m\n" |
51 |
} |
52 |
|
53 |
help() { |
54 |
startup |
55 |
echo "Usage: $argv_0 [options] [tasks...]" |
56 |
echo "" |
57 |
echo "Options:" |
58 |
echo " --setup Setup BlazeBuild" |
59 |
echo " --help Show this help message" |
60 |
echo " --version Show the version of BlazeBuild" |
61 |
echo "" |
62 |
echo "Tasks:" |
63 |
echo " Any arguments or task names that should" |
64 |
echo " be passed to BlazeBuild." |
65 |
echo " Run \`$argv_0 tasks' to see a list of" |
66 |
echo " available tasks." |
67 |
exit 0 |
68 |
} |
69 |
|
70 |
source_bashrc() { |
71 |
if [ -f "$HOME/.bashrc" ]; then |
72 |
source "$HOME/.bashrc" |
73 |
elif [ -f "$HOME/.bash_profile" ]; then |
74 |
source "$HOME/.bash_profile" |
75 |
fi |
76 |
} |
77 |
|
78 |
export_nvm() { |
79 |
NVM_DIR=$nvm_dir |
80 |
|
81 |
if [ ! -d "$nvm_dir" ]; then |
82 |
mkdir -p "$nvm_dir" |
83 |
fi |
84 |
|
85 |
if [ -f "$NVM_DIR/nvm.sh" ]; then |
86 |
source "$NVM_DIR/nvm.sh" |
87 |
fi |
88 |
} |
89 |
|
90 |
setup_nvm() { |
91 |
has_curl=$(command -v curl) |
92 |
has_wget=$(command -v wget) |
93 |
|
94 |
if [ ! -z "$(command -v nvm)" ]; then |
95 |
print 34 "info " "NVM already installed." |
96 |
export_nvm |
97 |
return |
98 |
fi |
99 |
|
100 |
print 34 "info " "Installing NVM..." |
101 |
|
102 |
if [ ! -d "$nvm_dir" ]; then |
103 |
mkdir -p "$nvm_dir" |
104 |
fi |
105 |
|
106 |
if [ ! -z "$has_curl" ]; then |
107 |
(export NVM_DIR="$nvm_dir" && export HOME="$nvm_home" && (curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash)) > /dev/null 2>&1 |
108 |
elif [ ! -z "$has_wget" ]; then |
109 |
(export NVM_DIR="$nvm_dir" && export HOME="$nvm_home" && (wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash)) > /dev/null 2>&1 |
110 |
else |
111 |
print "1;31" "error " "No curl or wget found." |
112 |
exit 1 |
113 |
fi |
114 |
|
115 |
source_bashrc |
116 |
export_nvm |
117 |
} |
118 |
|
119 |
node_version=$(getprop "node.version") |
120 |
bun_version=$(getprop "bun.version") |
121 |
setup_done=0 |
122 |
|
123 |
setup_node() { |
124 |
export_nvm |
125 |
|
126 |
setup_node=$1 |
127 |
node_on_path=$(command -v node) |
128 |
path="" |
129 |
|
130 |
if [ -z "$node_on_path" ]; then |
131 |
setup_node=1 |
132 |
else |
133 |
node_version_installed=$(node -v) |
134 |
|
135 |
if [[ "$node_version_installed" != "v$node_version"* ]]; then |
136 |
setup_node=1 |
137 |
fi |
138 |
fi |
139 |
|
140 |
if [ $setup_node -eq 1 ] && [ -d "$node_dir" ]; then |
141 |
find_result=$(find "$node_dir" -maxdepth 1 -type d -name "v$node_version*" | tail -n1) |
142 |
|
143 |
if [ ! -z "$find_result" ]; then |
144 |
node="$find_result/bin/node" |
145 |
npm="$find_result/bin/npm" |
146 |
path=$(readlink -f "$find_result/bin") |
147 |
setup_node=2 |
148 |
fi |
149 |
fi |
150 |
|
151 |
if [ $setup_node -eq 1 ]; then |
152 |
if [ ! -e "$nvm_dir/current" ]; then |
153 |
setup_nvm |
154 |
fi |
155 |
|
156 |
print 34 "info " "Installing Node.js ($node_version)..." |
157 |
|
158 |
if [ -z $DEBUG ]; then |
159 |
nvm install "$node_version" > /dev/null 2>&1 |
160 |
nvm use "$node_version" > /dev/null 2>&1 |
161 |
else |
162 |
nvm install "$node_version" |
163 |
nvm use "$node_version" |
164 |
fi |
165 |
|
166 |
find_result=$(find "$node_dir" -maxdepth 1 -type d -name "v$node_version*" | tail -n1) |
167 |
node="$find_result/bin/node" |
168 |
npm="$find_result/bin/npm" |
169 |
path=$(readlink -f "$find_result/bin") |
170 |
setup_done=1 |
171 |
|
172 |
print 34 "info " "Node.js version: $($node -v)" |
173 |
print 34 "info " "NPM version: $($npm -v)" |
174 |
print 34 "info " "NVM version: $(nvm -v)" |
175 |
elif [ $setup_node -eq 0 ]; then |
176 |
node=$(command -v node) |
177 |
npm=$(command -v npm) |
178 |
fi |
179 |
|
180 |
if [ ! -z "$path" ]; then |
181 |
export PATH="$path":$PATH:"$path" |
182 |
fi |
183 |
} |
184 |
|
185 |
setup_bun() { |
186 |
setup_bun=$1 |
187 |
bun_on_path=$(command -v bun) |
188 |
|
189 |
if [ -d "$bun_dir" ]; then |
190 |
bun="$bun_dir/bin/bun" |
191 |
elif [ -z "$bun_on_path" ]; then |
192 |
setup_bun=1 |
193 |
else |
194 |
bun_version_installed=$(bun -v) |
195 |
|
196 |
if [ "$bun_version_installed" != "$bun_version" ]; then |
197 |
setup_bun=1 |
198 |
else |
199 |
bun="$(command -v bun)" |
200 |
fi |
201 |
fi |
202 |
|
203 |
if [ $setup_bun -eq 1 ]; then |
204 |
print 34 "info " "Installing Bun ($bun_version)..." |
205 |
args=() |
206 |
|
207 |
if [ "$bun_version" != "latest" ]; then |
208 |
args=("-s" "bun-v$bun_version") |
209 |
fi |
210 |
|
211 |
mkdir -p "$bun_dir" |
212 |
export BUN_INSTALL="$bun_dir" && export PATH="$BUN_INSTALL":"$PATH":"$BUN_INSTALL" && export SHELL=blazew && curl -fsSL https://bun.sh/install | bash $args > /dev/null 2>&1 |
213 |
bun=$(readlink -f "$bun_dir/bin/bun") |
214 |
setup_done=1 |
215 |
|
216 |
print 34 "info " "Bun version: $($bun -v)" |
217 |
elif [ -d "$bun_dir" ]; then |
218 |
bun=$(readlink -f "$bun_dir/bin/bun") |
219 |
else |
220 |
bun=$(command -v bun) |
221 |
fi |
222 |
} |
223 |
|
224 |
setup_runtime() { |
225 |
do_setup_node=0 |
226 |
do_setup_bun=0 |
227 |
|
228 |
setup_node $do_setup_node |
229 |
setup_bun $do_setup_bun |
230 |
} |
231 |
|
232 |
setup=0 |
233 |
argsetup=0 |
234 |
|
235 |
for arg in "$@"; do |
236 |
if [ "$arg" == "--setup" ]; then |
237 |
setup=1 |
238 |
argsetup=1 |
239 |
fi |
240 |
done |
241 |
|
242 |
if [ ! -d "$blaze_srcpath/node_modules" ] || [ ! -d "$projectdir/node_modules/blazebuild" ]; then |
243 |
setup=1 |
244 |
fi |
245 |
|
246 |
setup_runtime |
247 |
|
248 |
has_bun=$(command -v bun) |
249 |
cmd="$nvm_dir/versions/node/$node_version/bin/npm" |
250 |
|
251 |
if [ "$node" == "node" ]; then |
252 |
cmd="npm" |
253 |
fi |
254 |
|
255 |
file="$blaze_srcpath/build/out/main/typescript/cli.js" |
256 |
if [ ! -z "$has_bun" ] || [ $setup_done -eq 1 ] || [ -x "$bun" ]; then |
257 |
cmd=$bun |
258 |
has_bun=1 |
259 |
file="$blaze_srcpath/src/main/typescript/cli.ts" |
260 |
fi |
261 |
|
262 |
blaze_cmd() { |
263 |
node_path=$(dirname "$node") |
264 |
final_path=$(readlink -f "$node_path") |
265 |
wrapper=$(readlink -f "$projectdir/blaze/wrapper/blaze_wrapper.mjs") |
266 |
|
267 |
if [ -z "$has_bun" ]; then |
268 |
print "1;31" "error " "Bun not found. Please install Bun to use BlazeBuild." |
269 |
exit 1 |
270 |
else |
271 |
export PATH="$final_path":$PATH:"$final_path" && bash -c "exec -a \"$argv_0\" $cmd $wrapper $file --- $*" |
272 |
fi |
273 |
} |
274 |
|
275 |
if [ ! -e "$projectdir/.blaze" ]; then |
276 |
print 34 "info " "Creating $projectdir/.blaze directory..." |
277 |
mkdir -p "$projectdir/.blaze" |
278 |
|
279 |
if [ $? -ne 0 ]; then |
280 |
print "1;31" "error " "Failed to create .blaze/ directory!" |
281 |
exit 1 |
282 |
fi |
283 |
fi |
284 |
|
285 |
if [ $setup -eq 1 ]; then |
286 |
print 34 "info " "Project directory: $projectdir" |
287 |
print 34 "info " "Setting up BlazeBuild in: $blaze_srcpath" |
288 |
rm -rf "$blaze_srcpath/node_modules" |
289 |
|
290 |
path=$(readlink -f "$cmd") |
291 |
pushd "$blaze_srcpath" > /dev/null 2>&1 || exit 1 |
292 |
print 34 "info " "Installing dependencies for BlazeBuild..." |
293 |
print 36 "command" "$path install" |
294 |
$path install |
295 |
popd > /dev/null 2>&1 || exit 1 |
296 |
|
297 |
print 34 "info " "Finishing up..." |
298 |
mkdir -p "$projectdir/node_modules" |
299 |
|
300 |
if [ -L "$projectdir/node_modules/blazebuild" ] || [ -d "$projectdir/node_modules/blazebuild" ]; then |
301 |
rm -rf "$projectdir/node_modules/blazebuild" || exit 1 |
302 |
fi |
303 |
|
304 |
ln -s "$(readlink -f "$blaze_srcpath")" "$projectdir/node_modules/blazebuild" || exit 1 |
305 |
|
306 |
if [ -z "$has_bun" ]; then |
307 |
print 34 "info " "Building BlazeBuild (for node)..." |
308 |
pushd "$blaze_srcpath" > /dev/null 2>&1 || exit 1 |
309 |
npx tsc |
310 |
popd > /dev/null 2>&1 || exit 1 |
311 |
fi |
312 |
|
313 |
print "1;32" "success" "Setup completed." |
314 |
fi |
315 |
|
316 |
if [ $argsetup -eq 1 ]; then |
317 |
exit 0 |
318 |
fi |
319 |
|
320 |
# shellcheck disable=SC2068 |
321 |
blaze_cmd $@ |