-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathblazew
executable file
·322 lines (257 loc) · 8 KB
/
blazew
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bash
VERSION="1.0.0-alpha.1"
set +e
if [ -z "$DEBUG" ]; then
set +x
fi
argv_0=$0
projectdir=$(dirname $0)
projectdir=$(readlink -f "$projectdir")
tmp_dir="$projectdir/.blaze"
nvm_dir="$tmp_dir/nvm"
node_dir="$tmp_dir/nvm/versions/node"
bun_dir="$tmp_dir/bun"
nvm_home="$tmp_dir/nvm_home"
bun="bun"
node="node"
npm="npm"
mkdir -p "$tmp_dir"
mkdir -p "$nvm_home"
getprop() {
value=$(cat "$projectdir/blaze/wrapper/blaze_wrapper.properties" | grep "$1" | cut -d'=' -f2)
echo $value
}
print() {
echo -en "\033[${1}m$2\033[0m"
echo -e " $3"
}
blaze_srcpath=$(getprop "blaze.srcpath")
if [ -z "$blaze_srcpath" ]; then
blaze_srcpath="build_src"
fi
blaze_srcpath="$projectdir/$blaze_srcpath"
if [ ! -d "$blaze_srcpath" ]; then
print "1;31" "error " "BlazeBuild source path \"$blaze_srcpath\" not found."
exit 1
fi
startup() {
echo -e "\033[1;34mBlazeBuild Wrapper version $VERSION\033[0m\n"
}
help() {
startup
echo "Usage: $argv_0 [options] [tasks...]"
echo ""
echo "Options:"
echo " --setup Setup BlazeBuild"
echo " --help Show this help message"
echo " --version Show the version of BlazeBuild"
echo ""
echo "Tasks:"
echo " Any arguments or task names that should"
echo " be passed to BlazeBuild."
echo " Run \`$argv_0 tasks' to see a list of"
echo " available tasks."
exit 0
}
source_bashrc() {
if [ -f "$HOME/.bashrc" ]; then
source "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
source "$HOME/.bash_profile"
fi
}
export_nvm() {
NVM_DIR=$nvm_dir
if [ ! -d "$nvm_dir" ]; then
mkdir -p "$nvm_dir"
fi
if [ -f "$NVM_DIR/nvm.sh" ]; then
source "$NVM_DIR/nvm.sh"
fi
}
setup_nvm() {
has_curl=$(command -v curl)
has_wget=$(command -v wget)
if [ ! -z "$(command -v nvm)" ]; then
print 34 "info " "NVM already installed."
export_nvm
return
fi
print 34 "info " "Installing NVM..."
if [ ! -d "$nvm_dir" ]; then
mkdir -p "$nvm_dir"
fi
if [ ! -z "$has_curl" ]; then
(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
elif [ ! -z "$has_wget" ]; then
(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
else
print "1;31" "error " "No curl or wget found."
exit 1
fi
source_bashrc
export_nvm
}
node_version=$(getprop "node.version")
bun_version=$(getprop "bun.version")
setup_done=0
setup_node() {
export_nvm
setup_node=$1
node_on_path=$(command -v node)
path=""
if [ -z "$node_on_path" ]; then
setup_node=1
else
node_version_installed=$(node -v)
if [[ "$node_version_installed" != "v$node_version"* ]]; then
setup_node=1
fi
fi
if [ $setup_node -eq 1 ] && [ -d "$node_dir" ]; then
find_result=$(find "$node_dir" -maxdepth 1 -type d -name "v$node_version*" | tail -n1)
if [ ! -z "$find_result" ]; then
node="$find_result/bin/node"
npm="$find_result/bin/npm"
path=$(readlink -f "$find_result/bin")
setup_node=2
fi
fi
if [ $setup_node -eq 1 ]; then
if [ ! -e "$nvm_dir/current" ]; then
setup_nvm
fi
print 34 "info " "Installing Node.js ($node_version)..."
if [ -z $DEBUG ]; then
nvm install "$node_version" > /dev/null 2>&1
nvm use "$node_version" > /dev/null 2>&1
else
nvm install "$node_version"
nvm use "$node_version"
fi
find_result=$(find "$node_dir" -maxdepth 1 -type d -name "v$node_version*" | tail -n1)
node="$find_result/bin/node"
npm="$find_result/bin/npm"
path=$(readlink -f "$find_result/bin")
setup_done=1
print 34 "info " "Node.js version: $($node -v)"
print 34 "info " "NPM version: $($npm -v)"
print 34 "info " "NVM version: $(nvm -v)"
elif [ $setup_node -eq 0 ]; then
node=$(command -v node)
npm=$(command -v npm)
fi
if [ ! -z "$path" ]; then
export PATH="$path":$PATH:"$path"
fi
}
setup_bun() {
setup_bun=$1
bun_on_path=$(command -v bun)
if [ -d "$bun_dir" ]; then
bun="$bun_dir/bin/bun"
elif [ -z "$bun_on_path" ]; then
setup_bun=1
else
bun_version_installed=$(bun -v)
if [ "$bun_version_installed" != "$bun_version" ]; then
setup_bun=1
else
bun="$(command -v bun)"
fi
fi
if [ $setup_bun -eq 1 ]; then
print 34 "info " "Installing Bun ($bun_version)..."
args=()
if [ "$bun_version" != "latest" ]; then
args=(-s "bun-v$bun_version")
fi
mkdir -p "$bun_dir"
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
bun=$(readlink -f "$bun_dir/bin/bun")
setup_done=1
print 34 "info " "Bun version: $($bun -v)"
elif [ -d "$bun_dir" ]; then
bun=$(readlink -f "$bun_dir/bin/bun")
else
bun=$(command -v bun)
fi
}
setup_runtime() {
do_setup_node=0
do_setup_bun=0
setup_node $do_setup_node
setup_bun $do_setup_bun
}
setup=0
argsetup=0
for arg in "$@"; do
if [ "$arg" == "--setup" ]; then
setup=1
argsetup=1
fi
done
if [ ! -d "$blaze_srcpath/node_modules" ] || [ ! -d "$projectdir/node_modules/blazebuild" ]; then
setup=1
fi
setup_runtime
has_bun=$(command -v bun)
cmd="$nvm_dir/versions/node/$node_version/bin/npm"
if [ "$node" == "node" ]; then
cmd="npm"
fi
file="$blaze_srcpath/build/index.js"
if [ ! -z "$has_bun" ] || [ $setup_done -eq 1 ] || [ -x "$bun" ]; then
cmd=$bun
has_bun=1
file="$blaze_srcpath/src/index.ts"
fi
blaze_cmd() {
node_path=$(dirname "$node")
final_path=$(readlink -f $node_path)
wrapper=$(readlink -f "$projectdir/blaze/wrapper/blaze_wrapper.mjs")
if [ -z "$has_bun" ]; then
print "1;31" "error " "Bun not found. Please install Bun to use BlazeBuild."
exit 1
export PATH="$final_path":$PATH:"$final_path" && bash -c "exec -a \"$argv_0\" $node $wrapper $file --- $*"
else
export PATH="$final_path":$PATH:"$final_path" && bash -c "exec -a \"$argv_0\" $cmd $wrapper $file --- $*"
fi
}
if [ ! -e "$projectdir/.blaze/build.d.ts" ]; then
print 34 "info " "Creating $projectdir/.blaze/build.d.ts..."
mkdir -p "$projectdir/.blaze"
cp "$blaze_srcpath/templates/build.d.ts" "$projectdir/.blaze/build.d.ts"
if [ $? -ne 0 ]; then
print "1;31" "error " "Failed to create build.d.ts."
exit 1
fi
fi
if [ $setup -eq 1 ]; then
print 34 "info " "Project directory: $projectdir"
print 34 "info " "Setting up BlazeBuild in: $blaze_srcpath"
rm -rf "$blaze_srcpath/node_modules"
path=$(readlink -f "$cmd")
pushd "$blaze_srcpath" > /dev/null 2>&1 || exit 1
print 34 "info " "Installing dependencies for BlazeBuild..."
print 36 "command" "$path install"
$path install
popd > /dev/null 2>&1 || exit 1
print 34 "info " "Finishing up..."
mkdir -p "$projectdir/node_modules"
if [ -L "$projectdir/node_modules/blazebuild" ] || [ -d "$projectdir/node_modules/blazebuild" ]; then
rm -rf "$projectdir/node_modules/blazebuild" || exit 1
fi
ln -s "$(readlink -f "$blaze_srcpath")" "$projectdir/node_modules/blazebuild" || exit 1
if [ -z "$has_bun" ]; then
print 34 "info " "Building BlazeBuild (for node)..."
pushd "$blaze_srcpath" > /dev/null 2>&1 || exit 1
npx tsc
popd > /dev/null 2>&1 || exit 1
fi
print "1;32" "success" "Setup completed."
fi
if [ $argsetup -eq 1 ]; then
exit 0
fi
blaze_cmd $@