2021-01-25 01:20:51 +03:00
|
|
|
#!/usr/bin/env bash
|
2020-07-28 21:27:40 +03:00
|
|
|
#
|
2023-02-28 19:48:25 +03:00
|
|
|
# Init the environment for new user.
|
2020-07-28 21:27:40 +03:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# CLI Dependencies
|
|
|
|
CLI=("git" "npm")
|
|
|
|
|
2020-07-28 21:27:40 +03:00
|
|
|
ACTIONS_WORKFLOW=pages-deploy.yml
|
|
|
|
|
2024-04-27 21:51:16 +03:00
|
|
|
RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H")
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# temporary file suffixes that make `sed -i` compatible with BSD and Linux
|
|
|
|
TEMP_SUFFIX="to-delete"
|
|
|
|
|
|
|
|
_no_gh=false
|
2021-09-29 20:57:28 +03:00
|
|
|
|
2020-07-28 21:27:40 +03:00
|
|
|
help() {
|
|
|
|
echo "Usage:"
|
|
|
|
echo
|
2023-06-28 08:49:13 +03:00
|
|
|
echo " bash /path/to/init [options]"
|
2020-07-28 21:27:40 +03:00
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " --no-gh Do not deploy to Github."
|
|
|
|
echo " -h, --help Print this help information."
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# BSD and GNU compatible sed
|
|
|
|
_sedi() {
|
|
|
|
regex=$1
|
|
|
|
file=$2
|
2024-04-27 21:51:16 +03:00
|
|
|
sed -i.$TEMP_SUFFIX -E "$regex" "$file"
|
2023-03-13 20:25:42 +03:00
|
|
|
rm -f "$file".$TEMP_SUFFIX
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_cli() {
|
|
|
|
for i in "${!CLI[@]}"; do
|
|
|
|
cli="${CLI[$i]}"
|
|
|
|
if ! command -v "$cli" &>/dev/null; then
|
|
|
|
echo "Command '$cli' not found! Hint: you should install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_status() {
|
2021-09-28 16:34:07 +03:00
|
|
|
if [[ -n $(git status . -s) ]]; then
|
2022-12-02 17:50:38 +03:00
|
|
|
echo "Error: Commit unstaged files first, and then run this tool again."
|
2023-02-16 00:02:22 +03:00
|
|
|
exit 1
|
2021-09-28 16:34:07 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
_check_init() {
|
2024-04-27 21:51:16 +03:00
|
|
|
if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then
|
2020-07-28 21:27:40 +03:00
|
|
|
echo "Already initialized."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
check_env() {
|
|
|
|
_check_cli
|
|
|
|
_check_status
|
|
|
|
_check_init
|
2022-12-27 20:11:27 +03:00
|
|
|
}
|
|
|
|
|
2024-04-27 21:51:16 +03:00
|
|
|
reset_latest() {
|
|
|
|
git reset --hard "$RELEASE_HASH"
|
|
|
|
git clean -fd
|
|
|
|
git submodule update --init --recursive
|
2023-03-17 19:49:26 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 21:27:40 +03:00
|
|
|
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
|
2024-04-27 21:51:16 +03:00
|
|
|
## Change the files of `.github/`
|
|
|
|
temp="$(mktemp -d)"
|
|
|
|
find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \;
|
2024-04-13 23:45:52 +03:00
|
|
|
rm -rf .github && mkdir -p .github/workflows
|
2024-04-27 21:51:16 +03:00
|
|
|
mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW"
|
|
|
|
rm -rf "$temp"
|
2021-09-28 16:34:07 +03:00
|
|
|
|
2021-09-29 18:14:59 +03:00
|
|
|
## Cleanup image settings in site config
|
2024-04-27 21:51:16 +03:00
|
|
|
_sedi "s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
|
2020-07-28 21:27:40 +03:00
|
|
|
fi
|
|
|
|
|
2023-05-03 18:51:34 +03:00
|
|
|
# remove the other files
|
2022-02-03 18:58:52 +03:00
|
|
|
rm -rf _posts/*
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2023-05-03 18:51:34 +03:00
|
|
|
# build assets
|
2023-03-13 20:25:42 +03:00
|
|
|
npm i && npm run build
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# track the js output
|
2024-02-13 18:35:32 +03:00
|
|
|
_sedi "/^assets.*\/dist/d" .gitignore
|
2020-07-28 21:27:40 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
commit() {
|
|
|
|
git add -A
|
|
|
|
git commit -m "chore: initialize the environment" -q
|
2024-04-27 21:51:16 +03:00
|
|
|
echo -e "\n> Initialization successful!\n"
|
2023-03-13 20:25:42 +03:00
|
|
|
}
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
main() {
|
|
|
|
check_env
|
2024-04-27 21:51:16 +03:00
|
|
|
reset_latest
|
2023-03-13 20:25:42 +03:00
|
|
|
init_files
|
|
|
|
commit
|
|
|
|
}
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
while (($#)); do
|
2020-07-28 21:27:40 +03:00
|
|
|
opt="$1"
|
|
|
|
case $opt in
|
2022-02-03 18:58:52 +03:00
|
|
|
--no-gh)
|
|
|
|
_no_gh=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-h | --help)
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# unknown option
|
|
|
|
help
|
|
|
|
exit 1
|
|
|
|
;;
|
2020-07-28 21:27:40 +03:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
main
|