build: improve init tool

This commit is contained in:
Cotes Chung 2024-04-28 02:51:16 +08:00
parent e09831ba3e
commit 72d93b132f
No known key found for this signature in database
GPG key ID: 0D9E54843167A808

View file

@ -9,6 +9,8 @@ CLI=("git" "npm")
ACTIONS_WORKFLOW=pages-deploy.yml ACTIONS_WORKFLOW=pages-deploy.yml
RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H")
# temporary file suffixes that make `sed -i` compatible with BSD and Linux # temporary file suffixes that make `sed -i` compatible with BSD and Linux
TEMP_SUFFIX="to-delete" TEMP_SUFFIX="to-delete"
@ -28,7 +30,7 @@ help() {
_sedi() { _sedi() {
regex=$1 regex=$1
file=$2 file=$2
sed -i.$TEMP_SUFFIX "$regex" "$file" sed -i.$TEMP_SUFFIX -E "$regex" "$file"
rm -f "$file".$TEMP_SUFFIX rm -f "$file".$TEMP_SUFFIX
} }
@ -50,22 +52,7 @@ _check_status() {
} }
_check_init() { _check_init() {
local _has_inited=false if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then
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
_count=$(find .github/workflows/ -type f -name "*.yml" | wc -l)
if [[ ${_count//[[:blank:]]/} == 1 ]]; then
_has_inited=true
fi
fi
fi
if $_has_inited; then
echo "Already initialized." echo "Already initialized."
exit 0 exit 0
fi fi
@ -77,22 +64,25 @@ check_env() {
_check_init _check_init
} }
checkout_latest_release() { reset_latest() {
hash=$(git log --grep="chore(release):" -1 --pretty="%H") git reset --hard "$RELEASE_HASH"
git reset --hard "$hash" git clean -fd
git submodule update --init --recursive
} }
init_files() { init_files() {
if $_no_gh; then if $_no_gh; then
rm -rf .github rm -rf .github
else else
## Change the files of `.github` ## Change the files of `.github/`
mv .github/workflows/starter/$ACTIONS_WORKFLOW . temp="$(mktemp -d)"
find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \;
rm -rf .github && mkdir -p .github/workflows rm -rf .github && mkdir -p .github/workflows
mv ./$ACTIONS_WORKFLOW .github/workflows/${ACTIONS_WORKFLOW} mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW"
rm -rf "$temp"
## Cleanup image settings in site config ## Cleanup image settings in site config
_sedi "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml _sedi "s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
fi fi
# remove the other files # remove the other files
@ -108,12 +98,12 @@ init_files() {
commit() { commit() {
git add -A git add -A
git commit -m "chore: initialize the environment" -q git commit -m "chore: initialize the environment" -q
echo -e "\n[INFO] Initialization successful!\n" echo -e "\n> Initialization successful!\n"
} }
main() { main() {
check_env check_env
checkout_latest_release reset_latest
init_files init_files
commit commit
} }