web/tools/init.sh

108 lines
2.1 KiB
Bash
Raw Normal View History

2021-01-25 01:20:51 +03:00
#!/usr/bin/env bash
2020-07-28 21:27:40 +03:00
#
# Init the evrionment for new user.
set -eu
ACTIONS_WORKFLOW=pages-deploy.yml
help() {
echo "Usage:"
echo
echo " bash /path/to/init.sh [options]"
echo
echo "Options:"
echo " --no-gh Do not deploy to Github."
echo " -h, --help Print this help information."
}
2021-09-28 16:34:07 +03:00
check_status() {
if [[ -n $(git status . -s) ]]; then
echo "Error: Commit unstaged files first, and then run this tool againt."
exit -1
fi
}
2020-07-28 21:27:40 +03:00
check_init() {
local _has_inited=false
if [[ ! -d docs ]]; then
if [[ ! -d .github ]]; then
_has_inited=true # --no-gh
else
if [[ -f .github/workflows/$ACTIONS_WORKFLOW ]]; then
# on BSD, the `wc` could contains blank
local _count="$(find .github/workflows/ -type f -name "*.yml" | wc -l)"
if [[ ${_count//[[:blank:]]/} == 1 ]]; then
_has_inited=true
fi
fi
2020-07-28 21:27:40 +03:00
fi
fi
2020-09-01 09:54:29 +03:00
if $_has_inited; then
2020-07-28 21:27:40 +03:00
echo "Already initialized."
exit 0
fi
}
init_files() {
2020-09-01 09:54:29 +03:00
if $_no_gh; then
2020-07-28 21:27:40 +03:00
rm -rf .github
else
2021-09-28 16:34:07 +03:00
# change the files of `.github`
2020-07-28 21:27:40 +03:00
mv .github/workflows/$ACTIONS_WORKFLOW.hook .
rm -rf .github
mkdir -p .github/workflows
mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW}
2021-09-28 16:34:07 +03:00
# 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"
2020-07-28 21:27:40 +03:00
fi
2021-09-28 16:34:07 +03:00
# trace the gem lockfile on user-end
sed -i "/Gemfile.lock/d" .gitignore
# remove the other fies
2020-07-28 21:27:40 +03:00
rm -f .travis.yml
rm -rf _posts/* docs
2021-09-28 16:34:07 +03:00
# save changes
2020-09-01 09:54:29 +03:00
git add -A && git add .github -f
2020-07-28 21:27:40 +03:00
git commit -m "[Automation] Initialize the environment." -q
echo "[INFO] Initialization successful!"
}
2021-09-28 16:34:07 +03:00
check_status
2020-07-28 21:27:40 +03:00
check_init
_no_gh=false
2020-09-01 09:54:29 +03:00
while (($#)); do
2020-07-28 21:27:40 +03:00
opt="$1"
case $opt in
--no-gh)
_no_gh=true
shift
;;
2020-09-01 09:54:29 +03:00
-h | --help)
2020-07-28 21:27:40 +03:00
help
exit 0
;;
2020-09-01 09:54:29 +03:00
*)
# unknown option
help
exit 1
;;
2020-07-28 21:27:40 +03:00
esac
done
init_files