-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.sh
More file actions
executable file
·235 lines (193 loc) · 6.91 KB
/
bundle.sh
File metadata and controls
executable file
·235 lines (193 loc) · 6.91 KB
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
#!/bin/bash
set -e
echo "🚀 Starting bundle process..."
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install minisign if not available
ensure_minisign() {
if ! command_exists minisign; then
echo "📦 Installing minisign via npm..."
if ! npm install -g minisign; then
echo "❌ Failed to install minisign via npm" >&2
exit 1
fi
# Verify minisign is now available
if ! command_exists minisign; then
echo "❌ Failed to install minisign" >&2
exit 1
fi
echo "✅ minisign installed successfully"
fi
}
# Function to detect OS and architecture
detect_platform() {
OS=$(uname -s)
ARCH=$(uname -m)
case "$OS" in
Linux*)
case "$ARCH" in
x86_64) echo "linux-x86_64" ;;
aarch64|arm64) echo "linux-aarch64" ;;
*) echo "Unsupported Linux architecture: $ARCH" >&2; exit 1 ;;
esac
;;
Darwin*)
case "$ARCH" in
x86_64) echo "macos-x86_64" ;;
arm64) echo "macos-aarch64" ;;
*) echo "Unsupported macOS architecture: $ARCH" >&2; exit 1 ;;
esac
;;
MINGW*|CYGWIN*|MSYS*)
case "$ARCH" in
x86_64) echo "windows-x86_64" ;;
*) echo "Unsupported Windows architecture: $ARCH" >&2; exit 1 ;;
esac
;;
*)
echo "Unsupported operating system: $OS" >&2
exit 1
;;
esac
}
# Function to verify minisign signature
verify_minisign() {
local tarball="$1"
local signature="$2"
local pubkey="$3"
echo "🔐 Verifying minisign signature..."
if minisign -V -m "$tarball" -s "$signature" -P "$pubkey" >/dev/null 2>&1; then
echo "✅ Minisign signature verified successfully"
return 0
else
echo "❌ Minisign signature verification failed!"
return 1
fi
}
# Function to shuffle lines from input
shuffle_lines() {
local input="$1"
echo "$input" | shuf 2>/dev/null || echo "$input" | perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);' 2>/dev/null || echo "$input"
}
# Function to map platform to Zig platform string
map_to_zig_platform() {
local platform="$1"
case "$platform" in
linux-x86_64) echo "x86_64-linux" ;;
linux-aarch64) echo "aarch64-linux" ;;
macos-x86_64) echo "x86_64-macos" ;;
macos-aarch64) echo "aarch64-macos" ;;
windows-x86_64) echo "x86_64-windows" ;;
*) echo "$platform" ;;
esac
}
# Function to install Zig using community mirrors and minisign verification
install_zig() {
echo "📦 Installing Zig with minisign signature verification..."
PLATFORM=$(detect_platform)
ZIG_PLATFORM=$(map_to_zig_platform "$PLATFORM")
ZIG_VERSION="0.15.1"
ZIG_DIR="$HOME/.local/zig"
# Zig's official public key for minisign verification
PUBKEY="RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U"
# Construct tarball name
TARBALL_NAME="zig-$ZIG_PLATFORM-$ZIG_VERSION.tar.xz"
# Fetch community mirrors list
echo "🌐 Fetching community mirrors..."
MIRRORS=$(curl -s "https://ziglang.org/download/community-mirrors.txt")
if [ $? -ne 0 ] || [ -z "$MIRRORS" ]; then
echo "❌ Failed to fetch community mirrors list" >&2
exit 1
fi
# Shuffle mirrors for load balancing
echo "🔀 Shuffling mirrors for load balancing..."
SHUFFLED_MIRRORS=$(shuffle_lines "$MIRRORS")
# Try each mirror in shuffled order
SUCCESS=false
for MIRROR_URL in $SHUFFLED_MIRRORS; do
# Skip empty lines
[ -z "$MIRROR_URL" ] && continue
echo "📥 Trying mirror: $MIRROR_URL"
# Download tarball with source parameter
TARBALL_URL="${MIRROR_URL}/${TARBALL_NAME}?source=tablemd_automation"
if curl -L "$TARBALL_URL" -o "/tmp/$TARBALL_NAME" --connect-timeout 10 --max-time 60; then
echo "✅ Downloaded tarball from $MIRROR_URL"
# Download signature
SIGNATURE_URL="${MIRROR_URL}/${TARBALL_NAME}.minisig?source=tablemd_automation"
if curl -L "$SIGNATURE_URL" -o "/tmp/$TARBALL_NAME.minisig" --connect-timeout 10 --max-time 30; then
echo "✅ Downloaded signature from $MIRROR_URL"
# Verify signature
if verify_minisign "/tmp/$TARBALL_NAME" "/tmp/$TARBALL_NAME.minisig" "$PUBKEY"; then
echo "✅ Signature verification passed for $MIRROR_URL"
SUCCESS=true
break
else
echo "❌ Signature verification failed for $MIRROR_URL, trying next mirror..."
rm -f "/tmp/$TARBALL_NAME" "/tmp/$TARBALL_NAME.minisig"
fi
else
echo "❌ Failed to download signature from $MIRROR_URL, trying next mirror..."
rm -f "/tmp/$TARBALL_NAME"
fi
else
echo "❌ Failed to download tarball from $MIRROR_URL, trying next mirror..."
fi
done
if [ "$SUCCESS" = false ]; then
echo "❌ Failed to download and verify Zig from any mirror" >&2
exit 1
fi
# Extract Zig
echo "📦 Extracting Zig..."
mkdir -p "$HOME/.local"
if [[ "$TARBALL_NAME" == *.zip ]]; then
unzip "/tmp/$TARBALL_NAME" -d "/tmp/"
mv "/tmp/zig-$ZIG_PLATFORM-$ZIG_VERSION" "$ZIG_DIR"
else
tar -xJ -f "/tmp/$TARBALL_NAME" -C "/tmp/"
mv "/tmp/zig-$ZIG_PLATFORM-$ZIG_VERSION" "$ZIG_DIR"
fi
# Clean up
rm -f "/tmp/$TARBALL_NAME" "/tmp/$TARBALL_NAME.minisig"
# Add to PATH for this session
export PATH="$ZIG_DIR:$PATH"
echo "✅ Zig installed successfully at $ZIG_DIR"
echo "Successfully fetched Zig $ZIG_VERSION!"
}
# Check if Zig is available first
if command_exists zig; then
echo "✅ Zig is already available"
zig version
else
echo "❌ Zig not found, installing..."
# Ensure minisign is available for signature verification
ensure_minisign
install_zig
fi
# Verify Zig is now available
if ! command_exists zig; then
echo "❌ Failed to install or find Zig" >&2
exit 1
fi
echo "🔨 Building with Zig..."
zig build -Dtarget=wasm32-emscripten --release=safe
# Check if build was successful
if [ ! -d "zig-out/web" ]; then
echo "❌ Build failed - zig-out/web directory not found" >&2
exit 1
fi
echo "📁 Creating dist directory..."
mkdir -p dist
mkdir -p dist/assets
echo "📋 Copying build artifacts to dist..."
cp -r zig-out/web/* dist/
cp -r src/web/assets/* dist/assets/
echo "✅ Bundle process completed successfully!"
echo "📦 Build artifacts copied to dist/"
echo "Renaming dist/root.html to dist/index.html..."
mv dist/root.html dist/index.html
# List contents of dist for verification
echo "📋 Contents of dist/:"
ls -la dist/