-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.sh
executable file
·340 lines (279 loc) · 9.01 KB
/
build.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
# Exit on any error
set -e
# Colors for better output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Show an error message and exit
error() {
echo -e "${RED}ERROR: $1${NC}" >&2
exit 1
}
# Show a success message
success() {
echo -e "${GREEN}SUCCESS: $1${NC}"
}
# Show an info message
info() {
echo -e "${BLUE}INFO: $1${NC}"
}
# Show a warning message
warning() {
echo -e "${YELLOW}WARNING: $1${NC}"
}
# Check for required tools
check_requirements() {
info "Checking requirements..."
# Check for Rust
if ! command -v rustc &> /dev/null; then
error "Rust is not installed. Please install it from https://rustup.rs/"
fi
# Check for wasm-pack
if ! command -v wasm-pack &> /dev/null; then
info "wasm-pack is not installed. Installing..."
cargo install wasm-pack || error "Failed to install wasm-pack"
fi
# Check for npm
if ! command -v npm &> /dev/null; then
error "Node.js/npm is not installed. Please install it from https://nodejs.org/"
fi
# Change to www directory and install npm dependencies if needed
if [ -d "www" ] && [ -f "www/package.json" ]; then
cd www
if [ ! -d "node_modules" ]; then
info "Installing npm dependencies..."
npm install || error "Failed to install npm dependencies"
fi
cd ..
fi
success "All requirements satisfied"
}
# Clean function to remove temporary files
clean() {
info "Cleaning build artifacts..."
# Remove target directory
if [ -d "target" ]; then
rm -rf target
info "Removed 'target' directory"
fi
# Remove wasm-pack generated files
if [ -d "pkg" ]; then
rm -rf pkg
info "Removed 'pkg' directory"
fi
success "Clean complete!"
return 0
}
# Copy and fix paths in index.html for docs directory
fix_index_paths() {
local src="$1"
local dst="$2"
info "Fixing paths in index.html for GitHub Pages..."
# Copy the file first
cp "$src" "$dst" || error "Failed to copy index.html"
# No path fixes needed as all resources use relative paths already
# If needed, this is where we would adjust paths for GitHub Pages
}
# Build the WASM package and setup web files
build_wasm() {
info "Building WASM Package..."
# Build WebAssembly package
wasm-pack build --target web --out-dir pkg || error "Failed to build WebAssembly package"
# Check for www directory
if [ ! -d "www" ]; then
error "www directory does not exist. Please create it first."
fi
# Copy WASM files to www/js
cd www
mkdir -p js
cp ../pkg/*.js js/ || error "Failed to copy JS files"
cp ../pkg/*.wasm js/ || error "Failed to copy WASM files"
# Fix import paths in JS files
for jsfile in js/*.js; do
sed -i.bak 's|import.meta.url, \"../pkg/|import.meta.url, \"|g' "$jsfile" && rm -f "$jsfile.bak"
done
cd ..
success "WASM build complete!"
return 0
}
# Build Tailwind CSS
build_css() {
info "Building CSS..."
cd www
npm run build || error "Failed to build CSS"
cd ..
success "CSS build complete!"
return 0
}
# Build for distribution (GitHub Pages)
build_dist() {
info "Building project for distribution (GitHub Pages)..."
# First build the WASM package
info "Building WebAssembly package..."
wasm-pack build --target web --out-dir pkg || error "Failed to build WebAssembly package"
# Change to www directory
cd www || error "Failed to change to www directory"
# Copy WASM files to js directory
info "Copying WebAssembly files..."
mkdir -p js
cp ../pkg/*.js js/ || error "Failed to copy JS files"
cp ../pkg/*.wasm js/ || error "Failed to copy WASM files"
# Fix import paths in JS files
info "Fixing module paths in JavaScript files..."
for jsfile in js/*.js; do
sed -i.bak 's|import.meta.url, \"../pkg/|import.meta.url, \"|g' "$jsfile" && rm -f "$jsfile.bak"
done
# Build CSS
info "Building CSS..."
npm run build || error "Failed to build CSS"
# Return to project root
cd ..
# Clean and prepare docs directory
info "Preparing docs directory for GitHub Pages..."
if [ -d "docs" ]; then
# Simply clean the directory - docs/ is only for GitHub Pages
rm -rf docs/* || error "Failed to clean docs directory"
else
mkdir -p docs || error "Failed to create docs directory"
fi
# Copy index.html with path fixes
fix_index_paths "www/index.html" "docs/index.html"
# Copy all necessary files to docs directory
info "Copying files to docs directory..."
mkdir -p docs/css
cp www/css/styles.css docs/css/ || error "Failed to copy CSS files"
mkdir -p docs/js
cp www/js/*.js docs/js/ || error "Failed to copy JS files"
cp www/js/*.wasm docs/js/ || error "Failed to copy WASM files"
# Check for other assets that might need to be copied
if [ -d "www/assets" ]; then
mkdir -p docs/assets
cp -r www/assets/* docs/assets/ || error "Failed to copy assets directory"
fi
# Success message
success "Distribution build complete! Files are ready in the docs directory."
info "You can now commit the docs directory for GitHub Pages deployment."
info "Your site will be available at: https://sayom.me/obadh_engine/"
return 0
}
# Serve the web application
serve() {
info "Starting development server..."
# Change to www directory
cd www
# Setup signal handling
# This is a cleaner approach than using background processes with trap
exec npm run serve
}
# Development mode with watch
dev() {
info "Starting development environment with watch..."
# Change to www directory
cd www
# Run npm dev command that handles CSS watch and server
exec npm run dev
}
# Build everything and start the server
start() {
info "Building and starting the server..."
cd www
npm run build-wasm && npm run build && npm run serve
cd ..
}
# Build the native Rust binary
build_bin() {
info "Building native Rust binary..."
# Build in release mode for optimization
cargo build --release --bin obadh || error "Failed to build native binary"
success "Native binary built successfully at: target/release/obadh"
info "You can install it to your system with: cargo install --path ."
return 0
}
# Build everything (bin, wasm, css, dist)
build_all() {
info "Building everything (native binary, WASM, CSS, and distribution files)..."
# First clean everything
clean
# Build the native binary
build_bin
# Build for distribution (this includes building WASM and CSS)
build_dist
success "All components built successfully!"
info "Native binary is available at: target/release/obadh"
info "Web files are ready in the docs/ directory for GitHub Pages."
info ""
info "To serve the web interface locally:"
info "cd docs && python -m http.server 8000"
info "Then visit: http://localhost:8000"
info ""
info "Or you can just deploy to GitHub Pages:"
info "git add docs/"
info "git commit -m \"Update deployment files\""
info "git push"
return 0
}
# Display the help information
show_help() {
echo "Obadh Engine Build Tool"
echo "======================="
echo "Usage:"
echo " ./build.sh bin # Build the native Rust binary (bin/obadh)"
echo " ./build.sh wasm # Build the WASM package"
echo " ./build.sh css # Build Tailwind CSS"
echo " ./build.sh serve # Start the development server only"
echo " ./build.sh dev # Start development mode with file watching"
echo " ./build.sh start # Build everything and start the server"
echo " ./build.sh dist # Build for distribution (GitHub Pages)"
echo " ./build.sh clean # Clean up build artifacts"
echo " ./build.sh all # Build everything (bin, wasm, css, dist)"
echo ""
echo "Note: Using 'dev' or 'serve' is the recommended way for development."
echo " Use 'dist' to prepare files for GitHub Pages deployment."
echo " Use 'bin' to build the command-line tool."
echo " Use 'all' to build everything for production and distribution."
}
# Main execution
case "$1" in
"clean")
clean
;;
"wasm")
check_requirements
build_wasm
;;
"css")
check_requirements
build_css
;;
"serve")
check_requirements
serve
;;
"dev")
check_requirements
dev
;;
"dist")
check_requirements
build_dist
;;
"start")
check_requirements
clean && build_wasm && build_css && serve
;;
"bin")
check_requirements
build_bin
;;
"all")
check_requirements
build_all
;;
*)
show_help
;;
esac
exit $?