|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Prepare wasm files for the web host |
| 4 | +# Usage: ./scripts/prepare-wasm-files.sh --mode debug|release [--target-dir <dir>] |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +usage() { |
| 9 | + echo "Usage: $0 --mode <mode> [--target-dir <dir>]" |
| 10 | + echo " mode: debug or release" |
| 11 | + echo " target-dir: optional custom target directory (default: packages/web-host/public/plugins)" |
| 12 | + echo "" |
| 13 | + echo "Example:" |
| 14 | + echo " $0 --mode debug" |
| 15 | + echo " $0 --mode release" |
| 16 | + echo " $0 --mode debug --target-dir ./custom/plugins" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +validate_mode() { |
| 21 | + local mode="$1" |
| 22 | + if [[ "$mode" != "debug" && "$mode" != "release" ]]; then |
| 23 | + echo "Error: --mode must be one of: debug, release." |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +prepare_wasm_files() { |
| 29 | + local mode="$1" |
| 30 | + local target_dir="$2" |
| 31 | + echo "Preparing wasm files for mode: $mode" |
| 32 | + |
| 33 | + local workspace_root="$(cd "$(dirname "$0")/.." && pwd)" |
| 34 | + |
| 35 | + mkdir -p "$target_dir" |
| 36 | + |
| 37 | + local wasm_files=( |
| 38 | + "target/wasm32-wasip1/$mode/plugin_echo.wasm" |
| 39 | + "target/wasm32-wasip1/$mode/plugin_greet.wasm" |
| 40 | + "target/wasm32-wasip1/$mode/plugin_ls.wasm" |
| 41 | + "target/wasm32-wasip1/$mode/plugin_cat.wasm" |
| 42 | + "target/wasm32-wasip1/$mode/plugin_weather.wasm" |
| 43 | + "c_modules/plugin-echo/plugin-echo-c.wasm" |
| 44 | + "target/wasm32-wasip1/$mode/repl_logic_guest.wasm" |
| 45 | + ) |
| 46 | + |
| 47 | + # Copy each wasm file |
| 48 | + for wasm_file in "${wasm_files[@]}"; do |
| 49 | + local copy_from="$workspace_root/$wasm_file" |
| 50 | + local copy_to="$target_dir/$(basename "$wasm_file")" |
| 51 | + |
| 52 | + if [[ ! -f "$copy_from" ]]; then |
| 53 | + echo "" |
| 54 | + echo "Failed to copy $copy_from to $copy_to" |
| 55 | + echo "" |
| 56 | + if [[ "$mode" == "debug" ]]; then |
| 57 | + echo "Please run the command: just build" |
| 58 | + else |
| 59 | + echo "Please run the command: just build-release" |
| 60 | + fi |
| 61 | + echo "" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + |
| 65 | + cp "$copy_from" "$copy_to" |
| 66 | + echo "Copied $copy_from to $copy_to" |
| 67 | + done |
| 68 | +} |
| 69 | + |
| 70 | +# Parse command line arguments |
| 71 | +MODE="" |
| 72 | +TARGET_DIR="" |
| 73 | + |
| 74 | +while [[ $# -gt 0 ]]; do |
| 75 | + case $1 in |
| 76 | + --mode) |
| 77 | + MODE="$2" |
| 78 | + shift 2 |
| 79 | + ;; |
| 80 | + --target-dir) |
| 81 | + TARGET_DIR="$2" |
| 82 | + shift 2 |
| 83 | + ;; |
| 84 | + -h|--help) |
| 85 | + usage |
| 86 | + ;; |
| 87 | + *) |
| 88 | + echo "Unknown option: $1" |
| 89 | + usage |
| 90 | + ;; |
| 91 | + esac |
| 92 | +done |
| 93 | + |
| 94 | +# Check if mode is provided |
| 95 | +if [[ -z "$MODE" ]]; then |
| 96 | + echo "Error: --mode is required" |
| 97 | + usage |
| 98 | +fi |
| 99 | + |
| 100 | +# Set default target directory if not provided |
| 101 | +if [[ -z "$TARGET_DIR" ]]; then |
| 102 | + WORKSPACE_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 103 | + TARGET_DIR="$WORKSPACE_ROOT/packages/web-host/public/plugins" |
| 104 | +fi |
| 105 | + |
| 106 | +# Validate mode and prepare files |
| 107 | +validate_mode "$MODE" |
| 108 | +prepare_wasm_files "$MODE" "$TARGET_DIR" |
0 commit comments