From d45421e79ad117f0d06b9f3b21b19c9a82d50366 Mon Sep 17 00:00:00 2001 From: naskya Date: Tue, 14 Nov 2023 23:04:56 +0900 Subject: [PATCH] dev: make upgrading scripts POSIX compliant --- neko/update/docker.sh | 9 +++++---- neko/update/native.sh | 33 ++++++++++++++++++++------------- neko/update/utils | 8 ++++++-- update.sh | 23 +++++++++++++++-------- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/neko/update/docker.sh b/neko/update/docker.sh index 7211b69f8..89e4478ad 100644 --- a/neko/update/docker.sh +++ b/neko/update/docker.sh @@ -1,7 +1,7 @@ -#!/usr/bin/env bash +#!/bin/sh set -eu -source neko/update/utils +. neko/update/utils pull() { say "Pulling the image..." @@ -11,8 +11,9 @@ pull() { if ! pull; then say "awawa, the image may not be compatible with your environment..." say "Gonnya try building the image locally!" - COMMIT_HASH=$(git rev-parse --short HEAD) - docker build --tag docker.io/naskya/firefish --build-arg COMMIT="${COMMIT_HASH:0:7}" . + + COMMIT_HASH=$(printf "%s" "$(git rev-parse HEAD)" | cut -c 1-7) + docker build --tag docker.io/naskya/firefish --build-arg COMMIT="${COMMIT_HASH}" . fi say "Done!" diff --git a/neko/update/native.sh b/neko/update/native.sh index b02648c43..627a4bd77 100755 --- a/neko/update/native.sh +++ b/neko/update/native.sh @@ -1,19 +1,22 @@ -#!/usr/bin/env bash +#!/bin/sh set -eu -source neko/update/utils +. neko/update/utils # Confirm that the server is stopped if [ $# != 1 ] || [ "$1" != "--skip-all-confirmations" ]; then say "Did you stop your server?" - read -r -p "[Y/n] > " yn + printf "[Y/n] > " + read -r yn + case "${yn}" in [Nn]|[Nn][Oo]) say "You must stop your server first!" exit 1 ;; *) - say "uwu~ erai erai!\n" + say "uwu~ erai erai!" + br ;; esac fi @@ -21,12 +24,12 @@ fi # write version info say "Writing version info to package.json..." -COMMIT_HASH=$(git rev-parse --short HEAD) +COMMIT_HASH=$(printf "%s" "$(git rev-parse HEAD)" | cut -c 1-7) +running "sed -e \"s/\\\"version\\\": \\\"\\([^+][^+]*\\).*\\\",$/\\\"version\\\": \\\"\\\\1+neko:${COMMIT_HASH}\\\",/\" package.json > package.json.new && mv -- package.json.new package.json" +sed -e "s/\"version\": \"\([^+][^+]*\).*\",$/\"version\": \"\\1+neko:${COMMIT_HASH}\",/" package.json > package.json.new && mv -- package.json.new package.json -running "sed -i -r 's/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${COMMIT_HASH:0:7}\",/' package.json" -sed -i -r "s/\"version\": \"([^+]+).*\",$/\"version\": \"\\1+neko:${COMMIT_HASH:0:7}\",/" package.json - -say "Done!\n" +say "Done!" +br # install dependencies say "Upgrading dependencies..." @@ -37,7 +40,8 @@ corepack prepare pnpm@latest --activate running "pnpm install" pnpm install -say "Done!\n" +say "Done!" +br # build say "Start building Firefish." @@ -46,7 +50,8 @@ say "It takes some time! Why not brew a cup of cofe?" running "NODE_OPTIONS=\"--max_old_space_size=3072\" NODE_ENV=\"production\" pnpm run rebuild" NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run rebuild -say "Done! We're almost there.\n" +say "Done! We're almost there." +br # prevent migration errors if [ ! -f packages/backend/native-utils/built/index.js ]; then @@ -54,7 +59,8 @@ if [ ! -f packages/backend/native-utils/built/index.js ]; then running "cp neko/index.js packages/backend/native-utils/built/index.js" cp neko/index.js packages/backend/native-utils/built/index.js else - say "It's going well so far!\n" + say "It's going well so far!" + br fi # migrate @@ -63,4 +69,5 @@ say "Database migration time!" running "NODE_OPTIONS=\"--max_old_space_size=3072\" NODE_ENV=\"production\" pnpm run migrate" NODE_OPTIONS="--max_old_space_size=3072" NODE_ENV="production" pnpm run migrate -say "Done!\n" +say "Done!" +br diff --git a/neko/update/utils b/neko/update/utils index f6aa0def6..701fd517d 100644 --- a/neko/update/utils +++ b/neko/update/utils @@ -6,12 +6,16 @@ color() { say() { color 5 # magenta - echo -e "( ^-^) < $1" + printf "( ^-^) < %s\n" "$1" color } running() { color 3 # yellow - echo "[running] \$ $1" + printf "[running] \$ %s\n" "$1" color } + +br() { + printf "\\n" +} diff --git a/update.sh b/update.sh index 2daaee42f..6c7949c7f 100755 --- a/update.sh +++ b/update.sh @@ -1,13 +1,14 @@ -#!/usr/bin/env bash +#!/bin/sh set -eu -source neko/update/utils +. neko/update/utils say "Start upgrading Firefish!" +br # Pull changes ## git pull -OLD_COMMIT=$(git rev-parse --short HEAD) +OLD_COMMIT=$(git rev-parse HEAD) say "Pulling changes from the remote repo..." @@ -17,8 +18,9 @@ git checkout -- package.json packages/backend/assets running "git pull --ff --no-edit --autostash --strategy-option theirs" git pull --ff --no-edit --autostash --strategy-option theirs -NEW_COMMIT=$(git rev-parse --short HEAD) -say "Pulled successfully!\n" +NEW_COMMIT=$(git rev-parse HEAD) +say "Pulled successfully!" +br ## check if the update script itself is updated say "Checking if the update script itself has been modified by this update..." @@ -27,7 +29,8 @@ if [ "$(git diff "${OLD_COMMIT}" "${NEW_COMMIT}" update.sh)" != "" ]; then say "I'm sorry to bother you, but please run this script again!" exit 1 else - say "This script seems to be up-to-date!\n" + say "This script seems to be up-to-date!" + br fi ## show messages @@ -37,8 +40,11 @@ for message in neko/messages/*; do if [ $# != 1 ] || [ "$1" != "--skip-all-confirmations" ]; then say "There is an important notice!" cat "${message}" + say "Continue? (Are you ready for upgrading?)" - read -r -p "[y/N] > " yn + printf "[y/N] > " + read -r yn + case "${yn}" in [Yy]|[Yy][Ee][Ss]) touch "neko/flags/${file}" @@ -57,7 +63,8 @@ for message in neko/messages/*; do done say "Do you use Docker?" -read -r -p "[y/N] > " yn +printf "[y/N] > " +read -r yn case "${yn}" in [Yy]|[Yy][Ee][Ss])