initial commit
This commit is contained in:
42
scripts/build.sh
Executable file
42
scripts/build.sh
Executable file
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
|
||||
# Defaults
|
||||
OUTDIR=""
|
||||
RELEASE=0
|
||||
CARGO_ARGS=""
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $(basename "$0") [options]
|
||||
|
||||
Options:
|
||||
--release Use cargo --release
|
||||
--outdir <dir> Output directory (passed to cargo --dist)
|
||||
--cargo-args "..." Extra arguments forwarded to cargo build
|
||||
-h, --help Show this help
|
||||
EOF
|
||||
}
|
||||
|
||||
# Parse args
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--release) RELEASE=1; shift;;
|
||||
--outdir) OUTDIR="$2"; shift 2;;
|
||||
--cargo-args) CARGO_ARGS="$2"; shift 2;;
|
||||
-h|--help) usage; exit 0;;
|
||||
*) echo "❌ Unknown option: $1"; echo; usage; exit 1;;
|
||||
esac
|
||||
done
|
||||
|
||||
"$SCRIPT_DIR/install.sh"
|
||||
|
||||
set -x
|
||||
cmd=(cargo build)
|
||||
if [[ $RELEASE -eq 1 ]]; then cmd+=(--release); fi
|
||||
if [[ -n "$OUTDIR" ]]; then cmd+=(--dist "$OUTDIR"); fi
|
||||
if [[ -n "$CARGO_ARGS" ]]; then cmd+=($CARGO_ARGS); fi
|
||||
"${cmd[@]}"
|
||||
set +x
|
76
scripts/environment.sh
Normal file
76
scripts/environment.sh
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# This script prepares the dev environment and (when sourced) exports env vars.
|
||||
# Usage:
|
||||
# source ./scripts/environment.sh # export env vars to current shell
|
||||
# ./scripts/environment.sh # runs setup checks; prints sourcing hint
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
# --- Helper: print next steps -------------------------------------------------
|
||||
print_next_steps() {
|
||||
echo ""
|
||||
echo "Next steps:"
|
||||
echo " 1) Start server (in ../server): cargo run -- --from-env --verbose"
|
||||
echo " 2) Start portal: ./scripts/start.sh (or ./scripts/start.sh --port 8088)"
|
||||
echo " 3) Dev (Trunk): set -a; source .env; set +a; trunk serve"
|
||||
}
|
||||
|
||||
# --- Ensure .env exists (key=value style) -------------------------------------
|
||||
if [ ! -f ".env" ]; then
|
||||
echo "📝 Creating .env file..."
|
||||
cat > .env << EOF
|
||||
# Portal Client Configuration
|
||||
# This file configures the frontend portal app
|
||||
|
||||
## Export-style so that 'source .env' exports to current shell
|
||||
|
||||
# API Key for server authentication (must match one of the API_KEYS in the server .env)
|
||||
export API_KEY=dev_key_123
|
||||
|
||||
# Optional: Override server API base URL (defaults to http://127.0.0.1:3001/api)
|
||||
# Example: API_URL=http://localhost:3001/api
|
||||
# export API_URL=
|
||||
EOF
|
||||
echo "✅ Created .env file with default API key"
|
||||
else
|
||||
echo "✅ .env file already exists"
|
||||
fi
|
||||
|
||||
# --- Install prerequisites ----------------------------------------------------
|
||||
if ! command -v trunk >/dev/null 2>&1; then
|
||||
echo "📦 Installing trunk..."
|
||||
cargo install trunk
|
||||
else
|
||||
echo "✅ trunk is installed"
|
||||
fi
|
||||
|
||||
if ! rustup target list --installed | grep -q "wasm32-unknown-unknown"; then
|
||||
echo "🔧 Adding wasm32-unknown-unknown target..."
|
||||
rustup target add wasm32-unknown-unknown
|
||||
else
|
||||
echo "✅ wasm32-unknown-unknown target present"
|
||||
fi
|
||||
|
||||
# --- Detect if sourced vs executed --------------------------------------------
|
||||
# Works for bash and zsh
|
||||
is_sourced=false
|
||||
# shellcheck disable=SC2296
|
||||
if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then
|
||||
case $ZSH_EVAL_CONTEXT in *:file:*) is_sourced=true;; esac
|
||||
elif [ -n "${BASH_SOURCE:-}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then
|
||||
is_sourced=true
|
||||
fi
|
||||
|
||||
if $is_sourced; then
|
||||
echo "🔐 Sourcing .env (export-style) into current shell..."
|
||||
# shellcheck disable=SC1091
|
||||
source .env
|
||||
echo "✅ Environment exported (API_KEY, optional API_URL)"
|
||||
else
|
||||
echo "ℹ️ Run 'source ./scripts/environment.sh' or 'source .env' to export env vars to your shell."
|
||||
print_next_steps
|
||||
fi
|
8
scripts/install.sh
Executable file
8
scripts/install.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
ROOT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
|
||||
|
||||
pushd "$ROOT_DIR"
|
||||
cargo update
|
161
scripts/release.sh
Executable file
161
scripts/release.sh
Executable file
@@ -0,0 +1,161 @@
|
||||
#!/bin/bash
|
||||
# release.sh - Build optimized WASM and serve with Caddy + Brotli compression
|
||||
set -e
|
||||
|
||||
###############################################################################
|
||||
# Freezone Portal Release Script
|
||||
# - Builds the WASM app with trunk in release mode
|
||||
# - Optionally optimizes .wasm with wasm-opt (-Oz, strip)
|
||||
# - Precompresses assets with gzip and brotli for efficient static serving
|
||||
# - Generates a manifest (manifest.json) with sizes and SHA-256 checksums
|
||||
#
|
||||
# Usage:
|
||||
# ./release.sh [--outdir dist] [--no-opt] [--compress] [--no-manifest]
|
||||
# [--trunk-args "--public-url /portal/"]
|
||||
#
|
||||
# Notes:
|
||||
# - Precompression is OFF by default; enable with --compress
|
||||
# - Only modifies files within the output directory (default: dist)
|
||||
# - Non-destructive to your source tree
|
||||
###############################################################################
|
||||
|
||||
set -u
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
PROJECT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
|
||||
BUILD_SCRIPT="$SCRIPT_DIR/build.sh"
|
||||
|
||||
# Defaults
|
||||
OUTDIR="dist"
|
||||
DO_OPT=1
|
||||
DO_COMPRESS=0
|
||||
DO_MANIFEST=1
|
||||
TRUNK_ARGS=""
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: $(basename "$0") [options]
|
||||
|
||||
Options:
|
||||
--outdir <dir> Output directory (default: dist)
|
||||
--no-opt Skip wasm-opt optimization
|
||||
--compress Enable gzip/brotli precompression
|
||||
--no-manifest Skip manifest generation
|
||||
--trunk-args "..." Extra arguments forwarded to trunk build
|
||||
-h, --help Show this help
|
||||
|
||||
Examples:
|
||||
$(basename "$0") --outdir dist --trunk-args "--public-url /"
|
||||
$(basename "$0") --no-opt --no-compress
|
||||
EOF
|
||||
}
|
||||
|
||||
# Parse args
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--outdir)
|
||||
OUTDIR="$2"; shift 2;;
|
||||
--no-opt)
|
||||
DO_OPT=0; shift;;
|
||||
--compress)
|
||||
DO_COMPRESS=1; shift;;
|
||||
--no-manifest)
|
||||
DO_MANIFEST=0; shift;;
|
||||
--trunk-args)
|
||||
TRUNK_ARGS="$2"; shift 2;;
|
||||
-h|--help)
|
||||
usage; exit 0;;
|
||||
*)
|
||||
echo "❌ Unknown option: $1"; echo; usage; exit 1;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Tool checks
|
||||
if [[ ! -x "$BUILD_SCRIPT" ]]; then
|
||||
echo "❌ build.sh not found or not executable at: $BUILD_SCRIPT"
|
||||
echo " Ensure portal/scripts/build.sh exists and is chmod +x."
|
||||
exit 1
|
||||
fi
|
||||
if ! command -v trunk >/dev/null 2>&1; then
|
||||
echo "❌ trunk not found. Install with: cargo install trunk"; exit 1;
|
||||
fi
|
||||
|
||||
HAS_WASM_OPT=0
|
||||
if command -v wasm-opt >/dev/null 2>&1; then HAS_WASM_OPT=1; fi
|
||||
if [[ $DO_OPT -eq 1 && $HAS_WASM_OPT -eq 0 ]]; then
|
||||
echo "⚠️ wasm-opt not found. Skipping WASM optimization."
|
||||
DO_OPT=0
|
||||
fi
|
||||
|
||||
if [[ $DO_COMPRESS -eq 1 ]]; then
|
||||
if ! command -v gzip >/dev/null 2>&1; then
|
||||
echo "⚠️ gzip not found. Skipping gzip compression."; GZIP_OK=0; else GZIP_OK=1; fi
|
||||
if ! command -v brotli >/dev/null 2>&1; then
|
||||
echo "⚠️ brotli not found. Skipping brotli compression."; BR_OK=0; else BR_OK=1; fi
|
||||
else
|
||||
GZIP_OK=0; BR_OK=0
|
||||
fi
|
||||
|
||||
echo "🔧 Building optimized WASM bundle (via build.sh)..."
|
||||
set -x
|
||||
"$BUILD_SCRIPT" --release --outdir "$OUTDIR" ${TRUNK_ARGS:+--trunk-args "$TRUNK_ARGS"}
|
||||
set +x
|
||||
|
||||
DIST_DIR="$PROJECT_DIR/$OUTDIR"
|
||||
if [[ ! -d "$DIST_DIR" ]]; then
|
||||
echo "❌ Build failed: output directory not found: $DIST_DIR"; exit 1;
|
||||
fi
|
||||
|
||||
# Optimize .wasm files
|
||||
if [[ $DO_OPT -eq 1 && $HAS_WASM_OPT -eq 1 ]]; then
|
||||
echo "🛠️ Optimizing WASM with wasm-opt (-Oz, strip)..."
|
||||
while IFS= read -r -d '' wasm; do
|
||||
echo " • $(basename "$wasm")"
|
||||
tmp="$wasm.opt"
|
||||
wasm-opt -Oz --strip-dwarf "$wasm" -o "$tmp"
|
||||
mv "$tmp" "$wasm"
|
||||
done < <(find "$DIST_DIR" -type f -name "*.wasm" -print0)
|
||||
fi
|
||||
|
||||
# Precompress assets
|
||||
if [[ $DO_COMPRESS -eq 1 ]]; then
|
||||
echo "🗜️ Precompressing assets (gzip/brotli)..."
|
||||
while IFS= read -r -d '' f; do
|
||||
if [[ $GZIP_OK -eq 1 ]]; then
|
||||
gzip -kf9 "$f"
|
||||
fi
|
||||
if [[ $BR_OK -eq 1 ]]; then
|
||||
brotli -f -q 11 "$f"
|
||||
fi
|
||||
done < <(find "$DIST_DIR" -type f \( -name "*.wasm" -o -name "*.js" -o -name "*.css" \) -print0)
|
||||
fi
|
||||
|
||||
# Manifest with sizes and SHA-256
|
||||
if [[ $DO_MANIFEST -eq 1 ]]; then
|
||||
echo "🧾 Generating manifest.json (sizes, sha256)..."
|
||||
manifest="$DIST_DIR/manifest.json"
|
||||
echo "{" > "$manifest"
|
||||
first=1
|
||||
while IFS= read -r -d '' f; do
|
||||
rel="${f#"$DIST_DIR/"}"
|
||||
size=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f")
|
||||
if command -v shasum >/dev/null 2>&1; then
|
||||
hash=$(shasum -a 256 "$f" | awk '{print $1}')
|
||||
else
|
||||
hash=$(openssl dgst -sha256 -r "$f" | awk '{print $1}')
|
||||
fi
|
||||
[[ $first -eq 1 ]] || echo "," >> "$manifest"
|
||||
first=0
|
||||
printf " \"%s\": { \"bytes\": %s, \"sha256\": \"%s\" }" "$rel" "$size" "$hash" >> "$manifest"
|
||||
done < <(find "$DIST_DIR" -type f ! -name "manifest.json" -print0 | sort -z)
|
||||
echo "\n}" >> "$manifest"
|
||||
fi
|
||||
|
||||
echo "📦 Checking bundle sizes ($OUTDIR)..."
|
||||
if [ -d "$OUTDIR" ]; then
|
||||
echo "Bundle sizes:"
|
||||
find "$OUTDIR" -name "*.wasm" -exec ls -lh {} \; | awk '{print " WASM: " $5 " - " $9}'
|
||||
find "$OUTDIR" -name "*.js" -exec ls -lh {} \; | awk '{print " JS: " $5 " - " $9}'
|
||||
find "$OUTDIR" -name "*.css" -exec ls -lh {} \; | awk '{print " CSS: " $5 " - " $9}'
|
||||
echo ""
|
||||
fi
|
1
scripts/run.sh
Executable file
1
scripts/run.sh
Executable file
@@ -0,0 +1 @@
|
||||
cargo run
|
8
scripts/test.sh
Executable file
8
scripts/test.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# serve.sh - Build optimized WASM and serve with Caddy + Brotli compression
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
|
||||
cargo check
|
||||
cargo test
|
Reference in New Issue
Block a user