-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·108 lines (99 loc) · 3.3 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·108 lines (99 loc) · 3.3 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
#!/usr/bin/env bash
set -euo pipefail
# Build script for node-module-man
# Usage examples:
# ./build.sh # build macOS (arm64, amd64) into ./dist
# GOOS=linux GOARCH=amd64 ./build.sh # build one target based on env
# ./build.sh all # build common targets (darwin/linux/win)
# ./build.sh release # build + package archives for release (multi-OS)
APP_NAME="node-module-man"
OUT_DIR="dist"
VERSION=${VERSION:-dev}
LDFLAGS="-s -w -X main.version=${VERSION}"
PKG="./cmd/node-module-man"
mkdir -p "${OUT_DIR}"
build_target() {
local goos="$1" arch="$2" ext="" outbin
outbin="${OUT_DIR}/${APP_NAME}-${goos}-${arch}"
if [[ "$goos" == "windows" ]]; then ext=".exe"; fi
echo "==> Building ${APP_NAME}-${goos}-${arch}${ext}"
CGO_ENABLED=0 GOOS="$goos" GOARCH="$arch" \
go build -trimpath -ldflags "$LDFLAGS" -o "${outbin}${ext}" "$PKG"
}
# Package a built artifact into tar.gz (unix) or zip (windows)
package_target() {
local goos="$1" arch="$2" ext="" base name archive tmpbin
if [[ "$goos" == "windows" ]]; then ext=".exe"; fi
base="${APP_NAME}-${goos}-${arch}${ext}"
name="${APP_NAME}_${VERSION}_${goos}_${arch}"
archive="${OUT_DIR}/${name}"
tmpbin="${OUT_DIR}/${APP_NAME}${ext}"
# Repack binary without GOOS/GOARCH suffix inside archive for a clean layout
cp "${OUT_DIR}/${APP_NAME}-${goos}-${arch}${ext}" "$tmpbin"
if [[ "$goos" == "windows" ]]; then
echo "==> Packaging ${name}.zip"
(cd "${OUT_DIR}" && zip -q -9 "${name}.zip" "${APP_NAME}${ext}")
else
echo "==> Packaging ${name}.tar.gz"
(cd "${OUT_DIR}" && tar -czf "${name}.tar.gz" "${APP_NAME}${ext}")
fi
rm -f "$tmpbin"
}
# Compute sha256 checksums for release files
checksum_files() {
local files=("$@")
local sumfile="${OUT_DIR}/checksums_${VERSION}.txt"
: > "$sumfile"
if command -v sha256sum >/dev/null 2>&1; then
(cd "$OUT_DIR" && sha256sum "${files[@]##${OUT_DIR}/}") >> "$sumfile"
else
# macOS fallback
(cd "$OUT_DIR" && shasum -a 256 "${files[@]##${OUT_DIR}/}") >> "$sumfile"
fi
echo "==> Wrote checksums to ${sumfile}"
}
case "${1:-}" in
all)
build_target darwin arm64
build_target darwin amd64
build_target linux amd64
build_target linux arm64
build_target windows amd64
build_target windows arm64
echo "Binaries in ${OUT_DIR}/"
exit 0
;;
release)
# Build and package for common OS/arch combos
targets=(
"darwin arm64"
"darwin amd64"
"linux amd64"
"linux arm64"
"windows amd64"
"windows arm64"
)
archives=()
for t in "${targets[@]}"; do
read -r goos arch <<< "$t"
build_target "$goos" "$arch"
package_target "$goos" "$arch"
if [[ "$goos" == "windows" ]]; then
archives+=("${OUT_DIR}/${APP_NAME}_${VERSION}_${goos}_${arch}.zip")
else
archives+=("${OUT_DIR}/${APP_NAME}_${VERSION}_${goos}_${arch}.tar.gz")
fi
done
checksum_files "${archives[@]}"
echo "Release artifacts in ${OUT_DIR}/"
exit 0
;;
esac
# If GOOS/GOARCH provided, build that single target; otherwise default to macOS universal pair
if [[ -n "${GOOS:-}" && -n "${GOARCH:-}" ]]; then
build_target "$GOOS" "$GOARCH"
else
build_target darwin arm64
build_target darwin amd64
fi
echo "Binaries in ${OUT_DIR}/"