web/tools/init.sh

86 lines
1.5 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."
}
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
mv .github/workflows/$ACTIONS_WORKFLOW.hook .
rm -rf .github
mkdir -p .github/workflows
mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW}
fi
rm -f .travis.yml
rm -rf _posts/* docs
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!"
}
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