File size: 7,008 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
#!/usr/bin/env bash
#
# Reproducible build script for the Logseq desktop app (macOS).
#
# Logseq is a ClojureScript + Electron application. The build pipeline is:
#   1. yarn install            (root JS deps; postinstall builds tldraw + amplify)
#   2. gulp build              (sync resources/ -> static/, build CSS)
#   3. cljs:release-electron   (compile ClojureScript -> static/js + static/electron.js)
#   4. yarn install (static/)  (Electron deps; postinstall rebuilds native modules
#                               such as better-sqlite3 for the Electron ABI)
#   5. electron-forge package  (produce Logseq.app)
#
# Notes / fixes baked in:
#   * Node 18 is used for the whole build. better-sqlite3 9.3.0 (and other native
#     addons) fail to compile against the V8 headers shipped with Node >= 20/26
#     (C++20 `requires`/concepts). Node 18 is what upstream CI uses.
#   * resources/forge.config.js is patched in-place to remove osxSign/osxNotarize
#     so packaging works without Apple Developer certificates.
#   * The finished .app is ad-hoc code-signed so it launches on both Intel and
#     Apple Silicon (unsigned arm64 bundles are killed by the kernel).
#
# Self-contained: on a fresh `git clone` with only build.sh + launch.sh copied in,
# `bash build.sh` produces the app. All dependency installs are idempotent.

set -euo pipefail

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

APP_OUTPUT_DIR="${APP_OUTPUT_DIR:-$SCRIPT_DIR}"

log() { printf '\n\033[1;34m[build]\033[0m %s\n' "$*"; }

# ---------------------------------------------------------------------------
# 0. Prerequisites
# ---------------------------------------------------------------------------
if ! command -v brew >/dev/null 2>&1; then
  echo "ERROR: Homebrew is required but was not found on PATH." >&2
  exit 1
fi

# ---------------------------------------------------------------------------
# 1. Dependencies (idempotent)
# ---------------------------------------------------------------------------

# Clojure CLI (compiles the ClojureScript frontend + electron main process).
if ! command -v clojure >/dev/null 2>&1; then
  log "Installing Clojure CLI..."
  brew install clojure/tools/clojure
fi
log "Clojure: $(clojure --version 2>/dev/null || echo 'installed')"

# Node 18 (native addon compatibility -- see header note).
if ! brew list node@18 >/dev/null 2>&1; then
  log "Installing node@18..."
  brew install node@18
fi
NODE18_PREFIX="$(brew --prefix node@18)"
export PATH="$NODE18_PREFIX/bin:$PATH"
hash -r
log "Node: $(node --version)  (from $NODE18_PREFIX)"

# Yarn (classic). Install via node@18's npm if missing.
if ! command -v yarn >/dev/null 2>&1; then
  log "Installing yarn..."
  npm install -g yarn
fi
log "Yarn: $(yarn --version)"

# ---------------------------------------------------------------------------
# 2. Patch forge.config.js -- disable code signing / notarization
#    (no Apple Developer certificates available in this environment).
#    gulp copies resources/forge.config.js -> static/forge.config.js, so we
#    patch the source of truth. Guarded so the script is safe to re-run.
# ---------------------------------------------------------------------------
if grep -q "osxSign" resources/forge.config.js; then
  log "Patching resources/forge.config.js to disable code signing..."
  git apply <<'PATCH'
diff --git a/resources/forge.config.js b/resources/forge.config.js
index eaee978..9414019 100644
--- a/resources/forge.config.js
+++ b/resources/forge.config.js
@@ -12,19 +12,6 @@ module.exports = {
         "schemes": "logseq"
       }
     ],
-    osxSign: {
-      identity: 'Developer ID Application: Tiansheng Qin',
-      'hardened-runtime': true,
-      entitlements: 'entitlements.plist',
-      'entitlements-inherit': 'entitlements.plist',
-      'signature-flags': 'library'
-    },
-    osxNotarize: {
-      tool: 'notarytool',
-      appleId: process.env['APPLE_ID'],
-      appleIdPassword: process.env['APPLE_ID_PASSWORD'],
-      teamId: process.env['APPLE_TEAM_ID']
-    },
   },
   makers: [
     {
PATCH
else
  log "forge.config.js already patched (no osxSign) -- skipping."
fi

# ---------------------------------------------------------------------------
# 3. Root JavaScript dependencies (also builds tldraw + amplify via postinstall)
# ---------------------------------------------------------------------------
log "Installing root JS dependencies (yarn install)..."
yarn install

# ---------------------------------------------------------------------------
# 4. Build static assets: sync resources/ -> static/ and compile CSS
# ---------------------------------------------------------------------------
log "Building static assets (gulp build)..."
yarn gulp:build

# ---------------------------------------------------------------------------
# 5. Compile ClojureScript (app + electron + publishing). Takes several minutes.
# ---------------------------------------------------------------------------
log "Compiling ClojureScript (yarn cljs:release-electron)..."
yarn cljs:release-electron

# ---------------------------------------------------------------------------
# 6. Electron dependencies + native module rebuild (better-sqlite3, etc.)
# ---------------------------------------------------------------------------
log "Installing Electron dependencies (static/)..."
( cd "$SCRIPT_DIR/static" && yarn install )

# ---------------------------------------------------------------------------
# 7. Package the Electron app into a .app bundle (no makers, no signing)
# ---------------------------------------------------------------------------
log "Packaging Electron app (electron-forge package)..."
( cd "$SCRIPT_DIR/static" && ./node_modules/.bin/electron-forge package )

# ---------------------------------------------------------------------------
# 8. Locate and ad-hoc sign the produced .app
# ---------------------------------------------------------------------------
APP_PATH="$(find "$SCRIPT_DIR/static/out" -maxdepth 2 -name 'Logseq.app' -type d 2>/dev/null | head -1 || true)"
if [ -z "$APP_PATH" ]; then
  echo "ERROR: Logseq.app was not produced under static/out/." >&2
  exit 1
fi
log "Built app: $APP_PATH"

log "Ad-hoc code signing (so it launches without a Developer ID)..."
codesign --force --deep --sign - "$APP_PATH" || {
  echo "WARNING: ad-hoc codesign failed; app may not launch on Apple Silicon." >&2
}

# ---------------------------------------------------------------------------
# 9. Copy the .app to the output directory
# ---------------------------------------------------------------------------
mkdir -p "$APP_OUTPUT_DIR"
APP_OUTPUT_DIR_ABS="$(cd "$APP_OUTPUT_DIR" && pwd)"
if [ "$APP_OUTPUT_DIR_ABS" != "$(dirname "$APP_PATH")" ]; then
  log "Copying Logseq.app -> $APP_OUTPUT_DIR_ABS/"
  rm -rf "$APP_OUTPUT_DIR_ABS/Logseq.app"
  cp -R "$APP_PATH" "$APP_OUTPUT_DIR_ABS/Logseq.app"
fi

log "Build complete."
log "  Packaged app : $APP_PATH"
log "  Output copy  : $APP_OUTPUT_DIR_ABS/Logseq.app"