krata/tools/init.sh

116 lines
2.4 KiB
Bash
Raw Normal View History

2021-01-24 23:20:51 +01:00
#!/usr/bin/env bash
2020-07-28 20:27:40 +02:00
#
# Init the evrionment for new user.
set -eu
ACTIONS_WORKFLOW=pages-deploy.yml
2021-09-29 19:57:28 +02:00
TEMP_SUFFIX="to-delete" # temporary file suffixes that make `sed -i` compatible with BSD and Linux
2020-07-28 20:27:40 +02:00
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 15:34:07 +02: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 20:27:40 +02:00
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="$(find .github/workflows/ -type f -name "*.yml" | wc -l)"
if [[ ${_count//[[:blank:]]/} == 1 ]]; then
_has_inited=true
fi
2020-07-28 20:27:40 +02:00
fi
fi
2020-09-01 08:54:29 +02:00
if $_has_inited; then
2020-07-28 20:27:40 +02:00
echo "Already initialized."
exit 0
fi
}
init_files() {
2020-09-01 08:54:29 +02:00
if $_no_gh; then
2020-07-28 20:27:40 +02:00
rm -rf .github
else
## Change the files of `.github`
2020-07-28 20:27:40 +02: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 15:34:07 +02:00
## Ensure the gh-actions trigger branch
2021-09-28 15:34:07 +02:00
_workflow=".github/workflows/${ACTIONS_WORKFLOW}"
_default_branch="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
_lineno="$(sed -n "/branches:/=" "$_workflow")"
2021-09-29 19:57:28 +02:00
sed -i.$TEMP_SUFFIX "$((_lineno + 1))s/- .*/- ${_default_branch}/" "$_workflow"
rm -f "$_workflow.$TEMP_SUFFIX"
2021-09-28 15:34:07 +02:00
## Cleanup image settings in site config
sed -i.$TEMP_SUFFIX "s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml
rm -f _config.yml.$TEMP_SUFFIX
2020-07-28 20:27:40 +02:00
fi
2021-09-28 15:34:07 +02:00
# trace the gem lockfile on user-end
2021-09-29 19:57:28 +02:00
sed -i.$TEMP_SUFFIX "/Gemfile.lock/d" .gitignore
rm -f ".gitignore.$TEMP_SUFFIX"
2021-09-28 15:34:07 +02:00
# remove the other fies
2020-07-28 20:27:40 +02:00
rm -f .travis.yml
rm -rf _posts/*
2020-07-28 20:27:40 +02:00
2021-09-28 15:34:07 +02:00
# save changes
git add -A
2020-07-28 20:27:40 +02:00
git commit -m "[Automation] Initialize the environment." -q
echo "[INFO] Initialization successful!"
}
2021-09-28 15:34:07 +02:00
check_status
2020-07-28 20:27:40 +02:00
check_init
_no_gh=false
2020-09-01 08:54:29 +02:00
while (($#)); do
2020-07-28 20:27:40 +02:00
opt="$1"
case $opt in
--no-gh)
_no_gh=true
shift
;;
-h | --help)
help
exit 0
;;
*)
# unknown option
help
exit 1
;;
2020-07-28 20:27:40 +02:00
esac
done
init_files