File size: 7,641 Bytes
3fbbdd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
#
# Standalone, reproducible build script for massCode (Electron + Vue + Vite).
#
# On a fresh `git clone` into which only build.sh and launch.sh have been copied,
# `bash build.sh` installs all dependencies, applies the required source patch
# inline, builds the renderer, packages the app with electron-builder as an
# unsigned .app bundle, ad-hoc signs it, and copies it to the output directory.
#
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
PNPM_VERSION="8.15.9"                       # pnpm-lock.yaml is lockfileVersion 6.0 -> pnpm 8
APP_NAME="massCode.app"
OUT_DIR="${APP_OUTPUT_DIR:-$SCRIPT_DIR}"

# Map host arch -> electron-builder mac arch.
case "$(uname -m)" in
  arm64)  MAC_ARCH="arm64" ;;
  x86_64) MAC_ARCH="x64" ;;
  *)      echo "!! Unsupported architecture: $(uname -m)"; exit 1 ;;
esac

echo ">> massCode build starting"
echo ">>   script dir : $SCRIPT_DIR"
echo ">>   output dir : $OUT_DIR"
echo ">>   mac arch   : $MAC_ARCH"

# -----------------------------------------------------------------------------
# 1. Toolchain check
# -----------------------------------------------------------------------------
command -v node >/dev/null 2>&1 || { echo "!! node is required but not found"; exit 1; }
command -v npx  >/dev/null 2>&1 || { echo "!! npx is required but not found"; exit 1; }
echo ">> node: $(node --version)"

# -----------------------------------------------------------------------------
# 2. Reset tracked source files to their pristine committed state.
#    electron-builder (step 5) rewrites the root package.json in place as a side
#    effect of `extraMetadata` (it strips devDependencies and sets main), and
#    the inline patch below edits config/electron-builder.ts. Resetting first
#    makes this script safe to re-run: a corrupted package.json would otherwise
#    fail the frozen-lockfile install on the next invocation.
# -----------------------------------------------------------------------------
if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  git -C "$SCRIPT_DIR" checkout -- package.json config/electron-builder.ts 2>/dev/null || true
fi

# -----------------------------------------------------------------------------
# 3. Install JavaScript dependencies
#    - The lockfile is pnpm v6.0 format, which requires pnpm 8. We invoke a
#      pinned pnpm via npx so the build works regardless of the globally
#      installed pnpm version.
#    - .npmrc sets node-linker=hoisted, giving a flat node_modules that
#      electron-builder can package.
# -----------------------------------------------------------------------------
export HUSKY=0                              # skip git-hook installation in CI
echo ">> Installing dependencies with pnpm@$PNPM_VERSION (this can take a few minutes) ..."
npx --yes "pnpm@$PNPM_VERSION" install --frozen-lockfile

# -----------------------------------------------------------------------------
# 4. Apply inline source patch (idempotent)
#    massCode's electron-builder config produces signed .dmg installers for both
#    arches. For a local, unsigned build we switch the mac target to 'dir'
#    (an unpacked .app bundle) and disable code signing (identity: null).
# -----------------------------------------------------------------------------
PATCH_EB=$(cat <<'PATCH'
diff --git a/config/electron-builder.ts b/config/electron-builder.ts
index e2f8505..df7ee3c 100644
--- a/config/electron-builder.ts
+++ b/config/electron-builder.ts
@@ -11,8 +11,8 @@ const artifactName = isSponsored
   : undefined

 const macTarget = [
-  { target: 'dmg', arch: 'arm64' },
-  { target: 'dmg', arch: 'x64' }
+  { target: 'dir', arch: 'arm64' },
+  { target: 'dir', arch: 'x64' }
 ]

 if (isTestBuild) {
@@ -38,7 +38,8 @@ export default {
     target: macTarget,
     icon: 'config/icons/icon.icns',
     category: 'public.app-category.productivity',
-    hardenedRuntime: true,
+    hardenedRuntime: false,
+    identity: null,
     entitlements: 'build/entitlements.mac.inherit.plist'
   },
   win: {
PATCH
)

apply_patch() {
  local patch="$1"
  if printf '%s\n' "$patch" | git apply --reverse --check >/dev/null 2>&1; then
    echo ">> Source patch already applied, skipping."
  elif printf '%s\n' "$patch" | git apply --check >/dev/null 2>&1; then
    printf '%s\n' "$patch" | git apply
    echo ">> Source patch applied."
  else
    echo "!! Source patch does not apply cleanly; aborting."
    exit 1
  fi
}
apply_patch "$PATCH_EB"

# -----------------------------------------------------------------------------
# 5. Compile TypeScript (electron main process + config + build scripts) -> build/
#    NOTE: we deliberately skip `npm run build`'s `vue-tsc` type-check step. That
#    step runs BEFORE Vite generates the git-ignored
#    src/renderer/types/{auto-imports,components}.d.ts files, so on a fresh clone
#    it fails on missing auto-imported globals. It is purely advisory (--noEmit)
#    and not required to produce artifacts.
# -----------------------------------------------------------------------------
echo ">> Compiling TypeScript (tsconfig.electron.json) ..."
node_modules/.bin/tsc -p tsconfig.electron.json || true
test -f build/scripts/build.js || { echo "!! TS build did not emit build/scripts/build.js"; exit 1; }

# electron-builder references build/entitlements.mac.inherit.plist
node_modules/.bin/copyfiles '*.plist' build

# -----------------------------------------------------------------------------
# 6. Build the renderer (Vite) and package the app (electron-builder).
#    TEST_BUILD + TEST_MAC_ARCH limit packaging to the host arch only.
#    CSC_IDENTITY_AUTO_DISCOVERY=false prevents any attempt to auto-discover a
#    signing identity.
# -----------------------------------------------------------------------------
echo ">> Building renderer and packaging app ..."
export TEST_BUILD=true
export TEST_MAC_ARCH="$MAC_ARCH"
export CSC_IDENTITY_AUTO_DISCOVERY=false
export NODE_ENV=production
node build/scripts/build.js

# -----------------------------------------------------------------------------
# 7. Locate the built .app
# -----------------------------------------------------------------------------
SRC_APP="$(find "$SCRIPT_DIR/dist" -maxdepth 2 -name "$APP_NAME" -type d -print -quit 2>/dev/null || true)"
test -n "$SRC_APP" || { echo "!! Could not find built $APP_NAME under dist/"; exit 1; }
echo ">> Built app: $SRC_APP"

# -----------------------------------------------------------------------------
# 8. Ad-hoc code sign.
#    Required so the app runs on Apple Silicon (unsigned arm64 binaries are
#    killed on launch); harmless on Intel.
# -----------------------------------------------------------------------------
echo ">> Ad-hoc signing ..."
codesign --force --deep --sign - "$SRC_APP" || echo "!! ad-hoc signing failed (continuing)"

# -----------------------------------------------------------------------------
# 9. Copy the final .app to the output directory
# -----------------------------------------------------------------------------
mkdir -p "$OUT_DIR"
OUT_APP="$OUT_DIR/$APP_NAME"
SRC_APP_REAL="$(cd "$(dirname "$SRC_APP")" && pwd)/$APP_NAME"
OUT_APP_REAL="$(cd "$OUT_DIR" && pwd)/$APP_NAME"
if [ "$SRC_APP_REAL" != "$OUT_APP_REAL" ]; then
  rm -rf "$OUT_APP"
  ditto "$SRC_APP" "$OUT_APP"
  echo ">> Copied app to: $OUT_APP"
else
  OUT_APP="$SRC_APP"
fi

echo ""
echo ">> BUILD SUCCESS"
echo ">> App bundle: $OUT_APP"