Skip to content

Commit 91f7bb4

Browse files
committed
feat(examples): add smart setup.sh with caching
1 parent 0802c1f commit 91f7bb4

3 files changed

Lines changed: 82 additions & 22 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ node_modules
55
dist/
66
coverage/
77
.capacitor-sdk-source.stamp
8+
.cap-sync.stamp
89

910
# macOS
1011
.DS_Store

examples/demo/package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@
3838
"@types/react-router": "^5.1.20",
3939
"@types/react-router-dom": "^5.3.3",
4040
"@vitejs/plugin-react": "^6.0.1",
41-
"typescript": "^6.0.2",
42-
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
43-
"vite-plus": "0.1.17"
44-
},
45-
"overrides": {
46-
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
47-
"vitest": "npm:@voidzero-dev/vite-plus-test@latest"
41+
"typescript": "^6.0.2"
4842
},
4943
"packageManager": "bun@1.3.11"
5044
}

examples/setup.sh

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,89 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

4+
# Run from inside any examples/<demo> directory.
45
ORIGINAL_DIR=$(pwd)
6+
SDK_ROOT=$(cd "$ORIGINAL_DIR/../.." && pwd)
57

6-
# Build and pack the plugin
7-
cd ../../
8-
bun run build
8+
info() { echo -e "\033[0;32m[setup]\033[0m $*"; }
99

10-
rm -f onesignal-capacitor-plugin*.tgz
11-
bun pm pack
12-
mv onesignal-capacitor-plugin-*.tgz onesignal-capacitor-plugin.tgz
10+
# ── Plugin tarball cache ─────────────────────────────────────────────────────
11+
# Skip rebuild/repack/`vp add` when plugin sources haven't changed.
12+
SDK_STAMP="$SDK_ROOT/.capacitor-sdk-source.stamp"
13+
INSTALLED_DIR="$ORIGINAL_DIR/node_modules/onesignal-capacitor-plugin"
1314

14-
# Install the packed plugin in the demo app
15-
cd "$ORIGINAL_DIR"
15+
SDK_SRC_HASH=$(find "$SDK_ROOT/src" "$SDK_ROOT/android" "$SDK_ROOT/ios" \
16+
"$SDK_ROOT/package.json" \
17+
"$SDK_ROOT/OneSignalCapacitorPlugin.podspec" \
18+
"$SDK_ROOT/Package.swift" \
19+
"$SDK_ROOT/vite.config.ts" \
20+
-type f 2>/dev/null \
21+
| sort \
22+
| xargs shasum 2>/dev/null \
23+
| shasum \
24+
| awk '{print $1}')
1625

17-
bun pm cache rm
26+
if [[ -d "$INSTALLED_DIR" ]] && [[ -f "$SDK_STAMP" ]] && [[ "$(cat "$SDK_STAMP")" == "$SDK_SRC_HASH" ]]; then
27+
info "Capacitor SDK source unchanged, skipping rebuild + repack"
28+
else
29+
info "Building Capacitor plugin & packing tarball..."
30+
(cd "$SDK_ROOT" && vp run build)
31+
(cd "$SDK_ROOT" && rm -f onesignal-capacitor-plugin*.tgz && vp pm pack && mv onesignal-capacitor-plugin-*.tgz onesignal-capacitor-plugin.tgz)
1832

19-
bun remove onesignal-capacitor-plugin
20-
bun add file:../../onesignal-capacitor-plugin.tgz
33+
# Remove before add so bun.lock's integrity hash refreshes against the new
34+
# tarball; otherwise `vp add` hits a dependency-loop error under bun 1.3+.
35+
# Keep the relative `file:../../...` path to match package.json's spec.
36+
info "Registering tarball with vp (refreshes bun.lock integrity hash)..."
37+
vp remove onesignal-capacitor-plugin 2>/dev/null || true
38+
vp add file:../../onesignal-capacitor-plugin.tgz
2139

22-
# Build the web app and sync native projects
23-
bun run build
24-
bunx cap sync
40+
echo "$SDK_SRC_HASH" > "$SDK_STAMP"
41+
fi
42+
43+
# ── Web bundle ───────────────────────────────────────────────────────────────
44+
info "Building web bundle (vite)..."
45+
# Same reasoning as the SDK build above: invoke `vp build` directly to skip
46+
# `vp run`'s task cache, which can be incompatible between vite-plus versions.
47+
vp run build
48+
49+
# ── Capacitor sync cache ─────────────────────────────────────────────────────
50+
# `cap sync` runs `pod install` + `xcodebuild clean` (~30-60s); skip when
51+
# inputs are unchanged. Hash sources (not `dist/`) since bundlers emit
52+
# content-hashed chunk names that can drift between identical builds.
53+
SYNC_STAMP="$ORIGINAL_DIR/.cap-sync.stamp"
54+
SYNC_HASH=$(find "$ORIGINAL_DIR/src" "$ORIGINAL_DIR/index.html" \
55+
"$ORIGINAL_DIR/capacitor.config.ts" "$ORIGINAL_DIR/vite.config.ts" \
56+
"$ORIGINAL_DIR/package.json" "$ORIGINAL_DIR/bun.lock" \
57+
"$ORIGINAL_DIR/ios/App" \
58+
-type f \
59+
! -path "*/node_modules/*" \
60+
! -path "*/Pods/*" \
61+
! -path "*/build/*" \
62+
! -path "*/DerivedData/*" \
63+
! -path "*/xcuserdata/*" \
64+
\( -name "Podfile" -o -name "Package.swift" -o -name "build.gradle" \
65+
-o -name "*.ts" -o -name "*.tsx" \
66+
-o -name "*.json" -o -name "*.html" -o -name "*.js" \
67+
-o -name "*.css" -o -name "*.svg" -o -name "*.xml" \
68+
-o -name "*.swift" -o -name "*.kt" \
69+
-o -name "*.lock" \) \
70+
2>/dev/null \
71+
| sort \
72+
| xargs shasum 2>/dev/null \
73+
| shasum \
74+
| awk '{print $1}')
75+
SYNC_HASH="${SYNC_HASH}-${SDK_SRC_HASH}"
76+
77+
if [[ -d "$ORIGINAL_DIR/ios/App/App/public" ]] && [[ -f "$SYNC_STAMP" ]] && [[ "$(cat "$SYNC_STAMP")" == "$SYNC_HASH" ]]; then
78+
info "Capacitor sync inputs unchanged, skipping cap sync"
79+
elif ! command -v pod >/dev/null 2>&1; then
80+
# CI Android jobs run on Linux where CocoaPods isn't installed.
81+
# Sync only Android so plain `cap sync` doesn't shell out to pod.
82+
info "CocoaPods not found, syncing Android only..."
83+
vpx cap sync android
84+
echo "$SYNC_HASH" > "$SYNC_STAMP"
85+
else
86+
info "Syncing Capacitor..."
87+
vpx cap sync
88+
echo "$SYNC_HASH" > "$SYNC_STAMP"
89+
fi

0 commit comments

Comments
 (0)