|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -o errexit #abort if any command fails |
| 3 | + |
| 4 | +me=$(basename "$0") |
| 5 | + |
| 6 | +help_message="\ |
| 7 | +Usage: $me [<options>] <command> [<command-options>] |
| 8 | +Run commands related to the slate process. |
| 9 | +
|
| 10 | +Commands: |
| 11 | +
|
| 12 | + serve Run the middleman server process, useful for |
| 13 | + development. |
| 14 | + build Run the build process. |
| 15 | + deploy Will build and deploy files to branch. Use |
| 16 | + --no-build to only deploy. |
| 17 | +
|
| 18 | +Global Options: |
| 19 | +
|
| 20 | + -h, --help Show this help information. |
| 21 | + -v, --verbose Increase verbosity. Useful for debugging. |
| 22 | +
|
| 23 | +Deploy options: |
| 24 | + -e, --allow-empty Allow deployment of an empty directory. |
| 25 | + -m, --message MESSAGE Specify the message used when committing on the |
| 26 | + deploy branch. |
| 27 | + -n, --no-hash Don't append the source commit's hash to the deploy |
| 28 | + commit's message. |
| 29 | + --no-build Do not build the source files. |
| 30 | +" |
| 31 | + |
| 32 | + |
| 33 | +run_serve() { |
| 34 | + exec bundle exec middleman serve --watcher-force-polling |
| 35 | +} |
| 36 | + |
| 37 | +run_build() { |
| 38 | + bundle exec middleman build --clean |
| 39 | +} |
| 40 | + |
| 41 | +parse_args() { |
| 42 | + # Set args from a local environment file. |
| 43 | + if [ -e ".env" ]; then |
| 44 | + source .env |
| 45 | + fi |
| 46 | + |
| 47 | + command= |
| 48 | + |
| 49 | + # Parse arg flags |
| 50 | + # If something is exposed as an environment variable, set/overwrite it |
| 51 | + # here. Otherwise, set/overwrite the internal variable instead. |
| 52 | + while : ; do |
| 53 | + if [[ $1 = "-h" || $1 = "--help" ]]; then |
| 54 | + echo "$help_message" |
| 55 | + exit 0 |
| 56 | + elif [[ $1 = "-v" || $1 = "--verbose" ]]; then |
| 57 | + verbose=true |
| 58 | + shift |
| 59 | + elif [[ $1 = "-e" || $1 = "--allow-empty" ]]; then |
| 60 | + allow_empty=true |
| 61 | + shift |
| 62 | + elif [[ ( $1 = "-m" || $1 = "--message" ) && -n $2 ]]; then |
| 63 | + commit_message=$2 |
| 64 | + shift 2 |
| 65 | + elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then |
| 66 | + GIT_DEPLOY_APPEND_HASH=false |
| 67 | + shift |
| 68 | + elif [[ $1 = "--no-build" ]]; then |
| 69 | + no_build=true |
| 70 | + shift |
| 71 | + elif [[ $1 = "serve" || $1 = "build" || $1 = "deploy" ]]; then |
| 72 | + if [ ! -z "${command}" ]; then |
| 73 | + >&2 echo "You can only specify one command." |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + command=$1 |
| 77 | + shift |
| 78 | + elif [ -z $1 ]; then |
| 79 | + break |
| 80 | + fi |
| 81 | + done |
| 82 | + |
| 83 | + if [ -z "${command}" ]; then |
| 84 | + >&2 echo "Command not specified." |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + |
| 88 | + # Set internal option vars from the environment and arg flags. All internal |
| 89 | + # vars should be declared here, with sane defaults if applicable. |
| 90 | + |
| 91 | + # Source directory & target branch. |
| 92 | + deploy_directory=build |
| 93 | + deploy_branch=gh-pages |
| 94 | + |
| 95 | + #if no user identity is already set in the current git environment, use this: |
| 96 | + default_username=${GIT_DEPLOY_USERNAME:-deploy.sh} |
| 97 | + default_email=${GIT_DEPLOY_EMAIL:-} |
| 98 | + |
| 99 | + #repository to deploy to. must be readable and writable. |
| 100 | + repo=origin |
| 101 | + |
| 102 | + #append commit hash to the end of message by default |
| 103 | + append_hash=${GIT_DEPLOY_APPEND_HASH:-true} |
| 104 | +} |
| 105 | + |
| 106 | +main() { |
| 107 | + enable_expanded_output |
| 108 | + |
| 109 | + if ! git diff --exit-code --quiet --cached; then |
| 110 | + echo Aborting due to uncommitted changes in the index >&2 |
| 111 | + return 1 |
| 112 | + fi |
| 113 | + |
| 114 | + commit_title=`git log -n 1 --format="%s" HEAD` |
| 115 | + commit_hash=` git log -n 1 --format="%H" HEAD` |
| 116 | + |
| 117 | + #default commit message uses last title if a custom one is not supplied |
| 118 | + if [[ -z $commit_message ]]; then |
| 119 | + commit_message="publish: $commit_title" |
| 120 | + fi |
| 121 | + |
| 122 | + #append hash to commit message unless no hash flag was found |
| 123 | + if [ $append_hash = true ]; then |
| 124 | + commit_message="$commit_message"$'\n\n'"generated from commit $commit_hash" |
| 125 | + fi |
| 126 | + |
| 127 | + previous_branch=`git rev-parse --abbrev-ref HEAD` |
| 128 | + |
| 129 | + if [ ! -d "$deploy_directory" ]; then |
| 130 | + echo "Deploy directory '$deploy_directory' does not exist. Aborting." >&2 |
| 131 | + return 1 |
| 132 | + fi |
| 133 | + |
| 134 | + # must use short form of flag in ls for compatibility with macOS and BSD |
| 135 | + if [[ -z `ls -A "$deploy_directory" 2> /dev/null` && -z $allow_empty ]]; then |
| 136 | + echo "Deploy directory '$deploy_directory' is empty. Aborting. If you're sure you want to deploy an empty tree, use the --allow-empty / -e flag." >&2 |
| 137 | + return 1 |
| 138 | + fi |
| 139 | + |
| 140 | + if git ls-remote --exit-code $repo "refs/heads/$deploy_branch" ; then |
| 141 | + # deploy_branch exists in $repo; make sure we have the latest version |
| 142 | + |
| 143 | + disable_expanded_output |
| 144 | + git fetch --force $repo $deploy_branch:$deploy_branch |
| 145 | + enable_expanded_output |
| 146 | + fi |
| 147 | + |
| 148 | + # check if deploy_branch exists locally |
| 149 | + if git show-ref --verify --quiet "refs/heads/$deploy_branch" |
| 150 | + then incremental_deploy |
| 151 | + else initial_deploy |
| 152 | + fi |
| 153 | + |
| 154 | + restore_head |
| 155 | +} |
| 156 | + |
| 157 | +initial_deploy() { |
| 158 | + git --work-tree "$deploy_directory" checkout --orphan $deploy_branch |
| 159 | + git --work-tree "$deploy_directory" add --all |
| 160 | + commit+push |
| 161 | +} |
| 162 | + |
| 163 | +incremental_deploy() { |
| 164 | + #make deploy_branch the current branch |
| 165 | + git symbolic-ref HEAD refs/heads/$deploy_branch |
| 166 | + #put the previously committed contents of deploy_branch into the index |
| 167 | + git --work-tree "$deploy_directory" reset --mixed --quiet |
| 168 | + git --work-tree "$deploy_directory" add --all |
| 169 | + |
| 170 | + set +o errexit |
| 171 | + diff=$(git --work-tree "$deploy_directory" diff --exit-code --quiet HEAD --)$? |
| 172 | + set -o errexit |
| 173 | + case $diff in |
| 174 | + 0) echo No changes to files in $deploy_directory. Skipping commit.;; |
| 175 | + 1) commit+push;; |
| 176 | + *) |
| 177 | + echo git diff exited with code $diff. Aborting. Staying on branch $deploy_branch so you can debug. To switch back to main, use: git symbolic-ref HEAD refs/heads/main && git reset --mixed >&2 |
| 178 | + return $diff |
| 179 | + ;; |
| 180 | + esac |
| 181 | +} |
| 182 | + |
| 183 | +commit+push() { |
| 184 | + set_user_id |
| 185 | + git --work-tree "$deploy_directory" commit -m "$commit_message" |
| 186 | + |
| 187 | + disable_expanded_output |
| 188 | + #--quiet is important here to avoid outputting the repo URL, which may contain a secret token |
| 189 | + git push --quiet $repo $deploy_branch |
| 190 | + enable_expanded_output |
| 191 | +} |
| 192 | + |
| 193 | +#echo expanded commands as they are executed (for debugging) |
| 194 | +enable_expanded_output() { |
| 195 | + if [ $verbose ]; then |
| 196 | + set -o xtrace |
| 197 | + set +o verbose |
| 198 | + fi |
| 199 | +} |
| 200 | + |
| 201 | +#this is used to avoid outputting the repo URL, which may contain a secret token |
| 202 | +disable_expanded_output() { |
| 203 | + if [ $verbose ]; then |
| 204 | + set +o xtrace |
| 205 | + set -o verbose |
| 206 | + fi |
| 207 | +} |
| 208 | + |
| 209 | +set_user_id() { |
| 210 | + if [[ -z `git config user.name` ]]; then |
| 211 | + git config user.name "$default_username" |
| 212 | + fi |
| 213 | + if [[ -z `git config user.email` ]]; then |
| 214 | + git config user.email "$default_email" |
| 215 | + fi |
| 216 | +} |
| 217 | + |
| 218 | +restore_head() { |
| 219 | + if [[ $previous_branch = "HEAD" ]]; then |
| 220 | + #we weren't on any branch before, so just set HEAD back to the commit it was on |
| 221 | + git update-ref --no-deref HEAD $commit_hash $deploy_branch |
| 222 | + else |
| 223 | + git symbolic-ref HEAD refs/heads/$previous_branch |
| 224 | + fi |
| 225 | + |
| 226 | + git reset --mixed |
| 227 | +} |
| 228 | + |
| 229 | +filter() { |
| 230 | + sed -e "s|$repo|\$repo|g" |
| 231 | +} |
| 232 | + |
| 233 | +sanitize() { |
| 234 | + "$@" 2> >(filter 1>&2) | filter |
| 235 | +} |
| 236 | + |
| 237 | +parse_args "$@" |
| 238 | + |
| 239 | +if [ "${command}" = "serve" ]; then |
| 240 | + run_serve |
| 241 | +elif [[ "${command}" = "build" ]]; then |
| 242 | + run_build |
| 243 | +elif [[ ${command} = "deploy" ]]; then |
| 244 | + if [[ ${no_build} != true ]]; then |
| 245 | + run_build |
| 246 | + fi |
| 247 | + main "$@" |
| 248 | +fi |
0 commit comments