返回 DeepSeek-Reasonix
prod_test
根目录 / prod_test
1 #!/usr/bin/env bash
2 # Build a local production-style Reasonix desktop package from this checkout.
3 #
4 # This is intentionally worktree-relative: run ./prod_test from any clone or
5 # git worktree and it will package that exact tree, including dirty local changes.
6 set -euo pipefail
7
8 usage() {
9 cat <<'EOF'
10 Usage:
11 ./prod_test [platform] [version] [channel]
12
13 Examples:
14 ./prod_test
15 ./prod_test darwin/arm64
16 ./prod_test darwin/universal v1.5.0-prodtest stable
17 ./prod_fast_test
18
19 Defaults:
20 platform auto-detected from this machine; macOS defaults to darwin/universal
21 version v0.0.0-prodtest.<branch-or-detached>.<short-sha>[.dirty]
22 channel stable
23
24 Environment overrides:
25 PROD_TEST_PLATFORM Same as first argument.
26 PROD_TEST_VERSION Same as second argument.
27 PROD_TEST_CHANNEL Same as third argument.
28 PROD_TEST_FAST 1 enables a faster local iteration build:
29 native host architecture on macOS and no DMG
30 packaging unless overridden below.
31 PROD_TEST_INSTALL_TOOLS 1 installs missing create-dmg/nfpm when possible.
32 Set to 0 to only check and print missing deps.
33 PROD_TEST_OPEN_DIST 1 opens dist/ after a successful build when supported.
34 DESKTOP_BUILD_SKIP_DMG 1 skips macOS DMG creation.
35 EOF
36 }
37
38 if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
39 usage
40 exit 0
41 fi
42
43 SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
44 ROOT="$SCRIPT_DIR"
45 cd "$ROOT"
46
47 fail() {
48 echo "prod_test: $*" >&2
49 exit 1
50 }
51
52 need_cmd() {
53 command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1"
54 }
55
56 go_bin_dir() {
57 local gobin
58 gobin="$(go env GOBIN)"
59 if [[ -z "$gobin" ]]; then
60 gobin="$(go env GOPATH)/bin"
61 fi
62 printf '%s' "$gobin"
63 }
64
65 sanitize_semver_id() {
66 local value="$1"
67 value="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^0-9A-Za-z]+/-/g; s/^-+//; s/-+$//; s/-+/-/g')"
68 printf '%s' "${value:-local}"
69 }
70
71 detect_platform() {
72 local os arch machine
73 case "$(uname -s)" in
74 Darwin) os="darwin" ;;
75 Linux) os="linux" ;;
76 MINGW*|MSYS*|CYGWIN*) os="windows" ;;
77 *) fail "unsupported host OS: $(uname -s)" ;;
78 esac
79
80 machine="$(uname -m)"
81 case "$machine" in
82 arm64|aarch64) arch="arm64" ;;
83 x86_64|amd64) arch="amd64" ;;
84 *) fail "unsupported host architecture: $machine" ;;
85 esac
86
87 if [[ "$os" == "darwin" ]]; then
88 # Match the release workflow's single macOS artifact by default.
89 printf 'darwin/universal'
90 else
91 printf '%s/%s' "$os" "$arch"
92 fi
93 }
94
95 detect_native_platform() {
96 local os arch machine
97 case "$(uname -s)" in
98 Darwin) os="darwin" ;;
99 Linux) os="linux" ;;
100 MINGW*|MSYS*|CYGWIN*) os="windows" ;;
101 *) fail "unsupported host OS: $(uname -s)" ;;
102 esac
103
104 machine="$(uname -m)"
105 case "$machine" in
106 arm64|aarch64) arch="arm64" ;;
107 x86_64|amd64) arch="amd64" ;;
108 *) fail "unsupported host architecture: $machine" ;;
109 esac
110 printf '%s/%s' "$os" "$arch"
111 }
112
113 default_version() {
114 local ref sha dirty
115 ref="$(git branch --show-current 2>/dev/null || true)"
116 if [[ -z "$ref" ]]; then
117 ref="$(git describe --tags --exact-match 2>/dev/null || true)"
118 fi
119 ref="${ref:-detached}"
120 sha="$(git rev-parse --short=8 HEAD 2>/dev/null || echo unknown)"
121 dirty=""
122 if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
123 dirty=".dirty"
124 fi
125 printf 'v0.0.0-prodtest.%s.%s%s' "$(sanitize_semver_id "$ref")" "$sha" "$dirty"
126 }
127
128 install_or_check_tools() {
129 local os install_tools
130 os="${1%/*}"
131 install_tools="${PROD_TEST_INSTALL_TOOLS:-1}"
132
133 need_cmd git
134 need_cmd go
135 need_cmd node
136 export PATH="$(go_bin_dir):$PATH"
137
138 if ! command -v pnpm >/dev/null 2>&1; then
139 if [[ "$install_tools" == "1" ]]; then
140 echo "==> installing pnpm"
141 if command -v corepack >/dev/null 2>&1; then
142 corepack enable
143 corepack prepare pnpm@10 --activate
144 else
145 need_cmd npm
146 npm install -g pnpm@10
147 fi
148 else
149 fail "pnpm not found. Install with: corepack enable && corepack prepare pnpm@10 --activate"
150 fi
151 fi
152
153 case "$os" in
154 darwin)
155 if [[ "${DESKTOP_BUILD_SKIP_DMG:-0}" == "1" ]]; then
156 return
157 fi
158 if ! command -v create-dmg >/dev/null 2>&1; then
159 if [[ "$install_tools" == "1" && -x "$(command -v brew 2>/dev/null || true)" ]]; then
160 echo "==> installing create-dmg"
161 brew install create-dmg
162 else
163 fail "create-dmg not found. Install with: brew install create-dmg"
164 fi
165 fi
166 ;;
167 linux)
168 if ! command -v nfpm >/dev/null 2>&1; then
169 if [[ "$install_tools" == "1" ]]; then
170 echo "==> installing nfpm"
171 go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.46.3
172 else
173 fail "nfpm not found. Install with: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.46.3"
174 fi
175 fi
176 ;;
177 windows)
178 command -v makensis >/dev/null 2>&1 || fail "makensis not found. Install NSIS first."
179 ;;
180 esac
181 }
182
183 FAST="${PROD_TEST_FAST:-0}"
184 if [[ "$FAST" == "1" ]]; then
185 export DESKTOP_BUILD_SKIP_DMG="${DESKTOP_BUILD_SKIP_DMG:-1}"
186 fi
187
188 if [[ -n "${1:-}" ]]; then
189 PLATFORM="$1"
190 elif [[ -n "${PROD_TEST_PLATFORM:-}" ]]; then
191 PLATFORM="$PROD_TEST_PLATFORM"
192 elif [[ "$FAST" == "1" ]]; then
193 PLATFORM="$(detect_native_platform)"
194 else
195 PLATFORM="$(detect_platform)"
196 fi
197 VERSION="${2:-${PROD_TEST_VERSION:-$(default_version)}}"
198 CHANNEL="${3:-${PROD_TEST_CHANNEL:-stable}}"
199 OPEN_DIST="${PROD_TEST_OPEN_DIST:-1}"
200
201 case "$PLATFORM" in
202 darwin/*|linux/*|windows/*) ;;
203 *) fail "platform must look like os/arch, got: $PLATFORM" ;;
204 esac
205
206 case "$CHANNEL" in
207 stable|canary) ;;
208 *) fail "channel must be stable or canary, got: $CHANNEL" ;;
209 esac
210
211 install_or_check_tools "$PLATFORM"
212
213 if [[ -n "$(git status --porcelain)" ]]; then
214 echo "==> worktree has uncommitted changes; this build will include them"
215 git status --short
216 fi
217
218 echo "==> root: ."
219 echo "==> branch: $(git branch --show-current 2>/dev/null || echo detached)"
220 echo "==> commit: $(git rev-parse --short=8 HEAD 2>/dev/null || echo unknown)"
221 echo "==> platform: $PLATFORM"
222 echo "==> version: $VERSION"
223 echo "==> channel: $CHANNEL"
224 if [[ "$FAST" == "1" ]]; then
225 echo "==> fast mode: DESKTOP_BUILD_SKIP_DMG=$DESKTOP_BUILD_SKIP_DMG"
226 fi
227
228 bash scripts/desktop-build.sh "$PLATFORM" "$VERSION" "$CHANNEL"
229
230 echo "==> production-style artifacts are in dist/"
231
232 if [[ "$OPEN_DIST" == "1" ]]; then
233 case "$(uname -s)" in
234 Darwin) open dist ;;
235 Linux) command -v xdg-open >/dev/null 2>&1 && xdg-open dist >/dev/null 2>&1 || true ;;
236 MINGW*|MSYS*|CYGWIN*) explorer.exe dist 2>/dev/null || true ;;
237 esac
238 fi
239
239 lines Plain Text