-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·265 lines (231 loc) · 10.4 KB
/
build.sh
File metadata and controls
executable file
·265 lines (231 loc) · 10.4 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
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
#!/usr/bin/env bash
set -e
# ── Colors ──────────────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# ── Project root (where this script lives) ──────────────────────────────────
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
DIST_DIR="$ROOT_DIR/dist"
PLUGINS_DIR="$ROOT_DIR/plugins"
# ── Plugin definitions: slug|main-file ──────────────────────────────────────
ALL_PLUGINS=(
"fchub-p24|fchub-p24.php"
"fchub-fakturownia|fchub-fakturownia.php"
"fchub-memberships|fchub-memberships.php"
"fchub-portal-extender|fchub-portal-extender.php"
"fchub-wishlist|fchub-wishlist.php"
"fchub-stream|fchub-stream.php"
"fchub-multi-currency|fchub-multi-currency.php"
"cartshift|cartshift.php"
)
# ── Helpers ──────────────────────────────────────────────────────────────────
info() { printf "${CYAN}▸${NC} %s\n" "$*"; }
success() { printf "${GREEN}✓${NC} %s\n" "$*"; }
warn() { printf "${YELLOW}⚠${NC} %s\n" "$*"; }
error() { printf "${RED}✗${NC} %s\n" "$*"; exit 1; }
human_size() {
local bytes=$1
if (( bytes >= 1048576 )); then printf "%.1f MB" "$(echo "scale=1; $bytes/1048576" | bc)"
elif (( bytes >= 1024 )); then printf "%.1f KB" "$(echo "scale=1; $bytes/1024" | bc)"
else printf "%d B" "$bytes"
fi
}
usage() {
printf "${BOLD}Usage:${NC} ./build.sh [plugin-slug]\n\n"
printf "Build distribution ZIPs for FCHub plugins.\n\n"
printf "${BOLD}Arguments:${NC}\n"
printf " plugin-slug Build only the specified plugin (optional)\n"
printf " Valid slugs: fchub-p24, fchub-fakturownia, fchub-memberships, fchub-portal-extender, fchub-wishlist, fchub-stream, fchub-multi-currency, cartshift\n\n"
printf "${BOLD}Examples:${NC}\n"
printf " ./build.sh Build all plugins\n"
printf " ./build.sh fchub-p24 Build only fchub-p24\n"
printf " ./build.sh fchub-memberships Build only fchub-memberships (runs npm build)\n"
exit 0
}
# ── Parse arguments ──────────────────────────────────────────────────────────
FILTER_SLUG=""
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
fi
if [ -n "$1" ]; then
FILTER_SLUG="$1"
# Validate slug
found=0
for entry in "${ALL_PLUGINS[@]}"; do
IFS='|' read -r slug _ <<< "$entry"
if [ "$slug" = "$FILTER_SLUG" ]; then
found=1
break
fi
done
if [ "$found" -eq 0 ]; then
error "Unknown plugin: $FILTER_SLUG"
fi
fi
# ── Determine which plugins to build ─────────────────────────────────────────
PLUGINS=()
if [ -n "$FILTER_SLUG" ]; then
for entry in "${ALL_PLUGINS[@]}"; do
IFS='|' read -r slug _ <<< "$entry"
if [ "$slug" = "$FILTER_SLUG" ]; then
PLUGINS+=("$entry")
break
fi
done
else
PLUGINS=("${ALL_PLUGINS[@]}")
fi
# ── Clean dist/ ─────────────────────────────────────────────────────────────
printf "\n${BOLD}Building FCHub plugin ZIPs${NC}\n"
printf "%s\n\n" "──────────────────────────────────────"
if [ -n "$FILTER_SLUG" ]; then
info "Building: $FILTER_SLUG"
else
info "Building all plugins"
fi
# Sync shared updater into all plugins
info "Syncing GitHubUpdater into plugins ..."
for dir in "$PLUGINS_DIR"/fchub-* "$PLUGINS_DIR"/cartshift; do
if [ -d "$dir" ]; then
mkdir -p "$dir/lib"
cp "$ROOT_DIR/lib/GitHubUpdater.php" "$dir/lib/GitHubUpdater.php"
fi
done
success "GitHubUpdater synced"
echo ""
if [ -z "$FILTER_SLUG" ] && [ -d "$DIST_DIR" ]; then
info "Cleaning previous dist/ ..."
rm -rf "$DIST_DIR"
fi
mkdir -p "$DIST_DIR"
# ── Build each plugin ───────────────────────────────────────────────────────
declare -a BUILT_ZIPS=()
for entry in "${PLUGINS[@]}"; do
IFS='|' read -r slug main_file <<< "$entry"
plugin_dir="$PLUGINS_DIR/$slug"
printf "${BOLD}── %s ${NC}\n" "$slug"
# Verify plugin directory exists
if [ ! -d "$plugin_dir" ]; then
warn "Plugin directory not found: $plugin_dir — skipping"
echo ""
continue
fi
# Read version from plugin header
version=$(grep -i "^[[:space:]]*\*[[:space:]]*Version:" "$plugin_dir/$main_file" | head -1 | sed 's/.*Version:[[:space:]]*//' | tr -d '[:space:]')
if [ -z "$version" ]; then
warn "Could not read version from $main_file — skipping"
echo ""
continue
fi
info "Version: $version"
# Run npm build for portal-extender
if [ "$slug" = "fchub-portal-extender" ]; then
if [ -f "$plugin_dir/package.json" ]; then
info "Running npm build for $slug ..."
(cd "$plugin_dir" && npm ci --silent && npm run build --silent)
if [ ! -d "$plugin_dir/assets/dist" ] || [ -z "$(ls -A "$plugin_dir/assets/dist" 2>/dev/null)" ]; then
error "npm build failed — assets/dist/ is empty"
fi
success "npm build complete"
fi
fi
# Run npm build for memberships
if [ "$slug" = "fchub-memberships" ]; then
if [ -f "$plugin_dir/package.json" ]; then
info "Running npm build for $slug ..."
(cd "$plugin_dir" && npm ci --silent && npm run build --silent)
if [ ! -d "$plugin_dir/assets/dist" ] || [ -z "$(ls -A "$plugin_dir/assets/dist" 2>/dev/null)" ]; then
error "npm build failed — assets/dist/ is empty"
fi
success "npm build complete"
fi
fi
# Run npm build for fchub-stream (admin-app + portal-app)
if [ "$slug" = "fchub-stream" ]; then
if [ -d "$plugin_dir/admin-app" ]; then
info "Running npm build for $slug/admin-app ..."
(cd "$plugin_dir/admin-app" && npm ci --silent && npm run build --silent)
if [ ! -d "$plugin_dir/admin/dist" ] || [ -z "$(ls -A "$plugin_dir/admin/dist" 2>/dev/null)" ]; then
error "npm build failed — admin/dist/ is empty"
fi
success "admin-app build complete"
fi
if [ -d "$plugin_dir/portal-app" ]; then
info "Running npm build for $slug/portal-app ..."
(cd "$plugin_dir/portal-app" && npm ci --silent && npm run build --silent)
if [ ! -d "$plugin_dir/portal-app/dist" ] || [ -z "$(ls -A "$plugin_dir/portal-app/dist" 2>/dev/null)" ]; then
error "npm build failed — portal-app/dist/ is empty"
fi
success "portal-app build complete"
fi
fi
# Temp working directory
tmp_dir=$(mktemp -d)
trap "rm -rf '$tmp_dir'" EXIT
# Build rsync exclude args from .distignore
exclude_args=()
distignore="$plugin_dir/.distignore"
if [ -f "$distignore" ]; then
while IFS= read -r line || [ -n "$line" ]; do
# Skip empty lines and comments
[[ -z "$line" || "$line" =~ ^# ]] && continue
exclude_args+=(--exclude="$line")
done < "$distignore"
else
# Fallback excludes if no .distignore
warn "No .distignore found, using defaults"
exclude_args=(
--exclude='node_modules/'
--exclude='vendor/'
--exclude='tests/'
--exclude='docs/'
--exclude='.phpunit.cache/'
--exclude='.git/'
--exclude='.gitignore'
--exclude='.distignore'
--exclude='phpunit.xml'
--exclude='phpunit.xml.dist'
--exclude='composer.json'
--exclude='composer.lock'
--exclude='package.json'
--exclude='package-lock.json'
--exclude='vite.config.js'
--exclude='*.md'
--exclude='.DS_Store'
--exclude='Thumbs.db'
)
fi
# rsync plugin files
rsync -a "${exclude_args[@]}" "$plugin_dir/" "$tmp_dir/$slug/"
# Create ZIP (from tmp_dir so the root inside the ZIP is the slug directory)
zip_name="${slug}-${version}.zip"
zip_path="$DIST_DIR/$zip_name"
rm -f "$zip_path"
(cd "$tmp_dir" && zip -qr "$zip_path" "$slug/")
success "Created $zip_name"
BUILT_ZIPS+=("$zip_path")
# Cleanup temp
rm -rf "$tmp_dir"
echo ""
done
# ── Summary ─────────────────────────────────────────────────────────────────
if [ ${#BUILT_ZIPS[@]} -eq 0 ]; then
error "No plugins were built."
fi
printf "${BOLD}Build Summary${NC}\n"
printf "%s\n" "──────────────────────────────────────"
printf "%-35s %10s %-34s %s\n" "File" "Size" "MD5" "Files"
printf "%s\n" "──────────────────────────────────────────────────────────────────────────────────────────────────"
for zip_path in "${BUILT_ZIPS[@]}"; do
fname=$(basename "$zip_path")
fsize=$(stat -f%z "$zip_path" 2>/dev/null || stat --printf="%s" "$zip_path")
fsize_h=$(human_size "$fsize")
md5=$(md5 -q "$zip_path" 2>/dev/null || md5sum "$zip_path" | awk '{print $1}')
file_count=$(zipinfo -t "$zip_path" 2>/dev/null | grep -o '[0-9]* files' | awk '{print $1}')
printf "%-35s %10s %s %s files\n" "$fname" "$fsize_h" "$md5" "$file_count"
done
printf "\n${GREEN}${BOLD}Done!${NC} ZIPs are in ${CYAN}dist/${NC}\n\n"