|
| 1 | + |
| 2 | +#!/usr/bin/env bash |
| 3 | +# |
| 4 | +# Init the environment for new user. |
| 5 | + |
| 6 | +set -eu |
| 7 | + |
| 8 | +# CLI Dependencies |
| 9 | +CLI=("git" "npm") |
| 10 | + |
| 11 | +ACTIONS_WORKFLOW=pages-deploy.yml |
| 12 | + |
| 13 | +RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H") |
| 14 | + |
| 15 | +# temporary file suffixes that make `sed -i` compatible with BSD and Linux |
| 16 | +TEMP_SUFFIX="to-delete" |
| 17 | + |
| 18 | +_no_gh=false |
| 19 | + |
| 20 | +help() { |
| 21 | + echo "Usage:" |
| 22 | + echo |
| 23 | + echo " bash /path/to/init [options]" |
| 24 | + echo |
| 25 | + echo "Options:" |
| 26 | + echo " --no-gh Do not deploy to Github." |
| 27 | + echo " -h, --help Print this help information." |
| 28 | +} |
| 29 | + |
| 30 | +# BSD and GNU compatible sed |
| 31 | +_sedi() { |
| 32 | + regex=$1 |
| 33 | + file=$2 |
| 34 | + sed -i.$TEMP_SUFFIX -E "$regex" "$file" |
| 35 | + rm -f "$file".$TEMP_SUFFIX |
| 36 | +} |
| 37 | + |
| 38 | +_check_cli() { |
| 39 | + for i in "${!CLI[@]}"; do |
| 40 | + cli="${CLI[$i]}" |
| 41 | + if ! command -v "$cli" &>/dev/null; then |
| 42 | + echo "Command '$cli' not found! Hint: you should install it." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + done |
| 46 | +} |
| 47 | + |
| 48 | +_check_status() { |
| 49 | + if [[ -n $(git status . -s) ]]; then |
| 50 | + echo "Error: Commit unstaged files first, and then run this tool again." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +_check_init() { |
| 56 | + if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then |
| 57 | + echo "Already initialized." |
| 58 | + exit 0 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +check_env() { |
| 63 | + _check_cli |
| 64 | + _check_status |
| 65 | + _check_init |
| 66 | +} |
| 67 | + |
| 68 | +reset_latest() { |
| 69 | + git reset --hard "$RELEASE_HASH" |
| 70 | + git clean -fd |
| 71 | + git submodule update --init --recursive |
| 72 | +} |
| 73 | + |
| 74 | +init_files() { |
| 75 | + if $_no_gh; then |
| 76 | + rm -rf .github |
| 77 | + else |
| 78 | + ## Change the files of `.github/` |
| 79 | + temp="$(mktemp -d)" |
| 80 | + find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \; |
| 81 | + rm -rf .github && mkdir -p .github/workflows |
| 82 | + mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW" |
| 83 | + rm -rf "$temp" |
| 84 | + fi |
| 85 | + |
| 86 | + # Cleanup image settings in site config |
| 87 | + _sedi "s/(^timezone:).*/\1/;s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml |
| 88 | + |
| 89 | + # remove the other files |
| 90 | + rm -rf tools/init.sh tools/release.sh _posts/* |
| 91 | + |
| 92 | + # build assets |
| 93 | + npm i && npm run build |
| 94 | + |
| 95 | + # track the CSS/JS output |
| 96 | + _sedi "/.*\/dist$/d" .gitignore |
| 97 | +} |
| 98 | + |
| 99 | +commit() { |
| 100 | + git add -A |
| 101 | + git commit -m "chore: initialize the environment" -q |
| 102 | + echo -e "\n> Initialization successful!\n" |
| 103 | +} |
| 104 | + |
| 105 | +main() { |
| 106 | + check_env |
| 107 | + reset_latest |
| 108 | + init_files |
| 109 | + commit |
| 110 | +} |
| 111 | + |
| 112 | +while (($#)); do |
| 113 | + opt="$1" |
| 114 | + case $opt in |
| 115 | + --no-gh) |
| 116 | + _no_gh=true |
| 117 | + shift |
| 118 | + ;; |
| 119 | + -h | --help) |
| 120 | + help |
| 121 | + exit 0 |
| 122 | + ;; |
| 123 | + *) |
| 124 | + # unknown option |
| 125 | + help |
| 126 | + exit 1 |
| 127 | + ;; |
| 128 | + esac |
| 129 | +done |
| 130 | + |
| 131 | +main |
0 commit comments