From ace89713d7d36aaf93f9eed58521c241ee7e40c9 Mon Sep 17 00:00:00 2001 From: cool-mist Date: Sat, 17 May 2025 14:39:59 +0530 Subject: [PATCH] Fix scripts --- build-web.sh | 15 ---- clean | 3 - deploy-ltpd.sh | 10 --- dev | 1 - run-local.sh | 11 --- tools/scripts.sh | 126 ++++++++++++++++++++++++++++++++++ {web => tools/web}/index.html | 0 7 files changed, 126 insertions(+), 40 deletions(-) delete mode 100755 build-web.sh delete mode 100755 clean delete mode 100755 deploy-ltpd.sh delete mode 100755 dev delete mode 100755 run-local.sh create mode 100644 tools/scripts.sh rename {web => tools/web}/index.html (100%) diff --git a/build-web.sh b/build-web.sh deleted file mode 100755 index f3a90de..0000000 --- a/build-web.sh +++ /dev/null @@ -1,15 +0,0 @@ -cargo build --target wasm32-unknown-unknown --release - -if [ $? -ne 0 ]; then - echo "Wasm build failed" - exit 1 -fi - -rm -rf ./dist && mkdir -p ./dist && mv ./target/wasm32-unknown-unknown/release/sol_chess.wasm ./dist/sol_chess.wasm && cp ./web/index.html ./dist/index.html - -if [ $? -ne 0 ]; then - echo "Failed to create create dist directory" - exit 1 -fi - -tar -czvf ./sol_chess.tar.gz -C ./dist . && rm -rf ./dist && echo "Web build complete" diff --git a/clean b/clean deleted file mode 100755 index 3c59ca0..0000000 --- a/clean +++ /dev/null @@ -1,3 +0,0 @@ -rm -rf ./local-deploy/sol_chess.wasm -rm -rf ./sol_chess.tar.gz -rm -rf ./target diff --git a/deploy-ltpd.sh b/deploy-ltpd.sh deleted file mode 100755 index 8d42579..0000000 --- a/deploy-ltpd.sh +++ /dev/null @@ -1,10 +0,0 @@ -./build-web.sh - -serve_root=$1 - -sudo mv ./sol_chess.tar.gz $serve_root/sol_chess.tar.gz && \ -sudo tar -xzvf $serve_root/sol_chess.tar.gz -C $serve_root && \ -sudo rm $serve_root/sol_chess.tar.gz - -echo "Deployment complete" - diff --git a/dev b/dev deleted file mode 100755 index e7f8efb..0000000 --- a/dev +++ /dev/null @@ -1 +0,0 @@ -TESTING=1 cargo run diff --git a/run-local.sh b/run-local.sh deleted file mode 100755 index d80051e..0000000 --- a/run-local.sh +++ /dev/null @@ -1,11 +0,0 @@ -cargo build --target wasm32-unknown-unknown --release - -if [ $? -ne 0 ]; then - echo "Wasm build failed" - exit 1 -fi - -mkdir -p ./local-deploy && \ -cp ./web/index.html ./local-deploy/index.html && \ -cp ./target/wasm32-unknown-unknown/release/sol_chess.wasm ./local-deploy/sol_chess.wasm && \ -basic-http-server ./local-deploy diff --git a/tools/scripts.sh b/tools/scripts.sh new file mode 100644 index 0000000..020e099 --- /dev/null +++ b/tools/scripts.sh @@ -0,0 +1,126 @@ +#! /usr/bin/bash + +rootd() { + pushd $(git rev-parse --show-toplevel) 2>&1 > /dev/null +} + +restored() { + popd 2>&1 > /dev/null +} + +get_parameter() { + DEFAULT="${1}" + if [ -z ${2} ]; then + echo "${DEFAULT}" + elif [ ! -d "${2}" ]; then + echo "${DEFAULT}" + else + echo "${2}" + fi +} + +# Usage: sol_chess_build_web debug|release target_dir [archive_dir] +# Build profile location to place files location to place compressed archive +sol_chess_build_web() { + rootd + + local BUILD_PROFILE="debug" + local BUILD_PROFILE_SWITCH="" + if [ -n "${1}" ]; then + if [ "${1}" = "release" ]; then + local BUILD_PROFILE="release" + local BUILD_PROFILE_SWITCH="--release" + fi + fi + + local TARGET_DIR=$(get_parameter "./target/dist" ${2}) + local ARCHIVE_DIR=$(get_parameter "" ${3}) + + echo "Build profile: ${BUILD_PROFILE}" + echo "Build profile switch: ${BUILD_PROFILE_SWITCH}" + echo "Target directory: ${TARGET_DIR}" + echo "Archive directory: ${ARCHIVE_DIR}" + + set -x + cargo build --target wasm32-unknown-unknown ${BUILD_PROFILE_SWITCH} + set +x + if [ $? -ne 0 ]; then + echo "Wasm build failed" + return 1 + fi + + rm -rf ${TARGET_DIR} && mkdir -p ${TARGET_DIR} && mv ./target/wasm32-unknown-unknown/${BUILD_PROFILE}/sol_chess.wasm ${TARGET_DIR}/sol_chess.wasm && cp ./tools/web/index.html ${TARGET_DIR}/index.html + if [ $? -ne 0 ]; then + echo "Failed to assemble the build in ${TARGET_DIR}" + return 1 + fi + + if [ -n "${ARCHIVE_DIR}" ]; then + local TAR_NAME="${ARCHIVE_DIR}/sol_chess.tar.gz" + set -x + tar -czvf ${TAR_NAME} -C ${TARGET_DIR} . && echo "Created ${TAR_NAME}" + set +x + fi + + restored +} + +sol_chess_web_local() { + rootd + + local TARGET_DIR=$(get_parameter "./target/dist" ${1}) + echo "Building web app in ${TARGET_DIR}" + sol_chess_build_web "debug" $TARGET_DIR + if [ $? -ne 0 ]; then + echo "Failed to build the web app" + return 1 + fi + + basic-http-server $TARGET_DIR + + restored +} + +sol_chess_dev() { + rootd + + TESTING=1 cargo run + + restored +} + +sol_chess_deploy() { + rootd + + if [ $# -ne 1 ]; then + echo "Usage: sol_chess_deploy " + return 1 + fi + + if [ ! -d $1 ]; then + echo "Directory $1 does not exist" + return 1 + fi + + local serve_root=$1 + sol_chess_build_web "release" "./target/dist" "./target" + if [ $? -ne 0 ]; then + echo "Failed to build the web app" + return 1 + fi + + sudo mv ./target/sol_chess.tar.gz $serve_root/sol_chess.tar.gz && \ + sudo tar -xzvf $serve_root/sol_chess.tar.gz -C $serve_root && \ + sudo rm $serve_root/sol_chess.tar.gz + echo "Deployment complete" + + restored +} + +sol_chess_clean() { + rootd + + rm -rf ./target + + restored +} diff --git a/web/index.html b/tools/web/index.html similarity index 100% rename from web/index.html rename to tools/web/index.html