From 72d93b132f47cd25d46fadefc059ef0d7a281a6b Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Sun, 28 Apr 2024 02:51:16 +0800 Subject: [PATCH] build: improve init tool --- tools/init | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/tools/init b/tools/init index dd15c69..be8ac01 100755 --- a/tools/init +++ b/tools/init @@ -9,6 +9,8 @@ CLI=("git" "npm") ACTIONS_WORKFLOW=pages-deploy.yml +RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H") + # temporary file suffixes that make `sed -i` compatible with BSD and Linux TEMP_SUFFIX="to-delete" @@ -28,7 +30,7 @@ help() { _sedi() { regex=$1 file=$2 - sed -i.$TEMP_SUFFIX "$regex" "$file" + sed -i.$TEMP_SUFFIX -E "$regex" "$file" rm -f "$file".$TEMP_SUFFIX } @@ -50,22 +52,7 @@ _check_status() { } _check_init() { - local _has_inited=false - - if [[ ! -d .github ]]; then # using option `--no-gh` - _has_inited=true - else - if [[ -f .github/workflows/$ACTIONS_WORKFLOW ]]; then - # on BSD, the `wc` could contains blank - local _count - _count=$(find .github/workflows/ -type f -name "*.yml" | wc -l) - if [[ ${_count//[[:blank:]]/} == 1 ]]; then - _has_inited=true - fi - fi - fi - - if $_has_inited; then + if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then echo "Already initialized." exit 0 fi @@ -77,22 +64,25 @@ check_env() { _check_init } -checkout_latest_release() { - hash=$(git log --grep="chore(release):" -1 --pretty="%H") - git reset --hard "$hash" +reset_latest() { + git reset --hard "$RELEASE_HASH" + git clean -fd + git submodule update --init --recursive } init_files() { if $_no_gh; then rm -rf .github else - ## Change the files of `.github` - mv .github/workflows/starter/$ACTIONS_WORKFLOW . + ## Change the files of `.github/` + temp="$(mktemp -d)" + find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \; rm -rf .github && mkdir -p .github/workflows - mv ./$ACTIONS_WORKFLOW .github/workflows/${ACTIONS_WORKFLOW} + mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW" + rm -rf "$temp" ## Cleanup image settings in site config - _sedi "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml + _sedi "s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml fi # remove the other files @@ -108,12 +98,12 @@ init_files() { commit() { git add -A git commit -m "chore: initialize the environment" -q - echo -e "\n[INFO] Initialization successful!\n" + echo -e "\n> Initialization successful!\n" } main() { check_env - checkout_latest_release + reset_latest init_files commit }