From a053c454e4760fbee1786695f33899249543bec8 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 28 Sep 2021 21:34:07 +0800 Subject: [PATCH] Improve the init tool --- tools/init.sh | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tools/init.sh b/tools/init.sh index 2ab78df..5035ff2 100755 --- a/tools/init.sh +++ b/tools/init.sh @@ -16,6 +16,13 @@ help() { echo " -h, --help Print this help information." } +check_status() { + if [[ -n $(git status . -s) ]]; then + echo "Error: Commit unstaged files first, and then run this tool againt." + exit -1 + fi +} + check_init() { local _has_inited=false @@ -40,25 +47,40 @@ check_init() { } init_files() { - if $_no_gh; then rm -rf .github else + # change the files of `.github` mv .github/workflows/$ACTIONS_WORKFLOW.hook . rm -rf .github mkdir -p .github/workflows mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW} + + # ensure the gh-actions trigger branch + + _workflow=".github/workflows/${ACTIONS_WORKFLOW}" + _default_branch="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')" + _lineno="$(sed -n "/branches:/=" "$_workflow")" + sed -i "$((_lineno + 1))s/- .*/- ${_default_branch}/" "$_workflow" + fi + # trace the gem lockfile on user-end + sed -i "/Gemfile.lock/d" .gitignore + + # remove the other fies rm -f .travis.yml rm -rf _posts/* docs + # save changes git add -A && git add .github -f git commit -m "[Automation] Initialize the environment." -q echo "[INFO] Initialization successful!" } +check_status + check_init _no_gh=false