Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·221 lines (188 loc) · 4.95 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·221 lines (188 loc) · 4.95 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env bash
#
# MieruWebUI build script
#
# Usage:
# ./build.sh # build backend + frontend (release)
# ./build.sh all # same as default
# ./build.sh prod # production bundle → dist/prod/
# ./build.sh backend # Rust release build only
# ./build.sh frontend # Vue production build only
# ./build.sh dev # debug backend + frontend deps
#
# Requires bash (not plain sh):
# ./build.sh prod
# bash build.sh prod
#
if [ -z "${BASH_VERSION:-}" ]; then
exec bash "$0" "$@"
fi
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="${ROOT_DIR}/backend"
FRONTEND_DIR="${ROOT_DIR}/frontend"
OUT_DIR="${OUT_DIR:-${ROOT_DIR}/dist/prod}"
log() { printf '\033[1;32m[build]\033[0m %s\n' "$*"; }
err() { printf '\033[1;31m[build]\033[0m %s\n' "$*" >&2; }
usage() {
cat <<EOF
Usage: ./build.sh [target]
Targets:
all Build frontend + backend (default, release, in-tree)
prod Production bundle: optimized build + package to dist/prod/
docker Build Docker image via docker compose
backend Build Rust backend only (cargo build --release)
frontend Build Vue frontend only (npm run build)
dev Debug backend build + install frontend deps
Environment:
OUT_DIR Output directory for prod bundle (default: dist/prod)
Examples:
./build.sh
./build.sh prod
./build.sh backend
OUT_DIR=/opt/mieruwebui ./build.sh prod
EOF
}
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
err "Required command not found: $1"
exit 1
fi
}
load_rust_env() {
if [[ -f "${HOME}/.cargo/env" ]]; then
# shellcheck disable=SC1091
source "${HOME}/.cargo/env"
fi
}
build_frontend() {
log "Building frontend (production)..."
need_cmd npm
cd "${FRONTEND_DIR}"
if [[ -f package-lock.json ]]; then
npm ci --no-audit --no-fund
else
npm install --no-audit --no-fund
fi
NODE_ENV=production npm run build
log "Frontend ready: ${FRONTEND_DIR}/dist"
}
build_backend() {
local profile="${1:-release}"
log "Building backend (${profile})..."
need_cmd cargo
load_rust_env
cd "${BACKEND_DIR}"
if [[ "${profile}" == "release" ]]; then
RUSTFLAGS="${RUSTFLAGS:--C strip=symbols}" cargo build --release
log "Backend ready: ${BACKEND_DIR}/target/release/mieru-webui"
else
cargo build
log "Backend ready: ${BACKEND_DIR}/target/debug/mieru-webui"
fi
}
build_all() {
build_frontend
build_backend release
log "Done. Run:"
log " cd backend && cargo run --release"
}
write_run_script() {
local dest="$1"
cat >"${dest}/run.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${DIR}/backend"
mkdir -p data
exec ./mieru-webui
EOF
chmod +x "${dest}/run.sh"
}
write_prod_readme() {
local dest="$1"
cat >"${dest}/README.txt" <<EOF
MieruWebUI production bundle
Generated: $(date -u +"%Y-%m-%d %H:%M:%S UTC")
Layout:
backend/mieru-webui — server binary
backend/data/ — SQLite database
frontend/dist/ — web UI static files
.env.example — configuration template
Quick start:
1. cp .env.example .env && nano .env
2. ./run.sh
Or with systemd — see install.sh in source repository.
EOF
}
build_prod() {
log "Production build → ${OUT_DIR}"
build_frontend
build_backend release
log "Packaging..."
rm -rf "${OUT_DIR}"
mkdir -p "${OUT_DIR}/backend/data" "${OUT_DIR}/frontend/dist"
cp "${BACKEND_DIR}/target/release/mieru-webui" "${OUT_DIR}/backend/mieru-webui"
chmod +x "${OUT_DIR}/backend/mieru-webui"
rsync -a "${FRONTEND_DIR}/dist/" "${OUT_DIR}/frontend/dist/"
if [[ -f "${ROOT_DIR}/.env.example" ]]; then
cp "${ROOT_DIR}/.env.example" "${OUT_DIR}/.env.example"
fi
write_run_script "${OUT_DIR}"
write_prod_readme "${OUT_DIR}"
# Record build info
cat >"${OUT_DIR}/BUILD_INFO" <<EOF
built_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
rustc=$(rustc --version 2>/dev/null || echo unknown)
node=$(node --version 2>/dev/null || echo unknown)
EOF
log "Production bundle ready: ${OUT_DIR}"
log " Binary: ${OUT_DIR}/backend/mieru-webui"
log " Static: ${OUT_DIR}/frontend/dist"
log " Run: cd ${OUT_DIR} && cp .env.example .env && ./run.sh"
}
build_dev() {
need_cmd npm
load_rust_env
cd "${FRONTEND_DIR}"
npm install --no-audit --no-fund
cd "${BACKEND_DIR}"
cargo build
log "Dev build complete (debug backend + frontend deps)"
}
main() {
local target="${1:-all}"
case "${target}" in
all)
build_all
;;
prod|production)
build_prod
;;
docker)
need_cmd docker
log "Building Docker image..."
docker compose build
log "Image ready. Run: docker compose up -d"
;;
backend|back|rust)
build_backend release
;;
frontend|front|vue)
build_frontend
;;
dev)
build_dev
;;
-h|--help|help)
usage
;;
*)
err "Unknown target: ${target}"
echo
usage
exit 1
;;
esac
}
main "$@"