make-site.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/bash
  2. set -eo pipefail
  3. DEPS="autoprefixer postcss-cli"
  4. DOCSY_REPO_DEFAULT="google/docsy"
  5. DOCSY_REPO=$DOCSY_REPO_DEFAULT
  6. DOCSY_VERS=""
  7. DOCSY_SRC="NPM"
  8. FORCE_DELETE=false
  9. : ${HUGO:=npx hugo}
  10. SITE_NAME="test-site"
  11. THEMESDIR="node_modules"
  12. VERBOSE=1
  13. OUTPUT_REDIRECT="" # Use along with VERBOSE
  14. function _usage() {
  15. cat <<EOS
  16. Usage: `basename $0` [options]
  17. Creates a Docsy-themed site under SITE_NAME using the Hugo new command.
  18. Docsy is fetched as an NPM package from $DOCSY_REPO in GitHub,
  19. unless the -l or -s HUGO_MOD flags are used.
  20. -f Force delete SITE_NAME if it exists before recreating it
  21. -h Output this usage info
  22. -l PATH Use local Docsy from PATH. Default: '$THEMESDIR'
  23. -n SITE_NAME Name of directory to create for the Hugo generated site.
  24. Default: '$SITE_NAME'
  25. -q Run a bit more quietly.
  26. -r REPO GitHub org+repo to fetch Docsy from.
  27. Format: GITHUB_USER/DOCSY_REPO. Default: $DOCSY_REPO_DEFAULT
  28. -s MOD_OR_PKG Docsy source: from a Hugo module or NPM package named '$DOCSY_REPO', where
  29. MOD_OR_PKG is NPM or HUGO_MODULE (HUGO for short). Default: $DOCSY_SRC
  30. -v VERS Docsy Hugo module or NPM package version. Default: '$DOCSY_VERS'.
  31. Examples for Hugo modules: v1.1.1, some-branch-name
  32. Examples for NPM: semver:1.1.1, some-branch-name
  33. EOS
  34. }
  35. function usage() {
  36. local status=${1:-0}
  37. _usage 1>&2
  38. exit $status
  39. }
  40. function process_CLI_args() {
  41. while getopts ":fhl:n:qr:s:v:" opt; do
  42. case $opt in
  43. f)
  44. FORCE_DELETE=true
  45. ;;
  46. h)
  47. usage
  48. ;;
  49. l)
  50. DOCSY_SRC="LOCAL"
  51. THEMESDIR="$OPTARG"
  52. ;;
  53. n)
  54. SITE_NAME="$OPTARG"
  55. ;;
  56. q)
  57. VERBOSE=""
  58. OUTPUT_REDIRECT="> /dev/null 2>&1"
  59. ;;
  60. r)
  61. DOCSY_REPO="$OPTARG"
  62. ;;
  63. s)
  64. DOCSY_SRC=$(echo "$OPTARG" | tr '[:lower:]' '[:upper:]')
  65. if [[ $DOCSY_SRC != "NPM" && $DOCSY_SRC != HUGO* ]]; then
  66. echo "ERROR: invalid argument to -s flag: $OPTARG"
  67. usage 1;
  68. fi
  69. ;;
  70. v)
  71. DOCSY_VERS="$OPTARG"
  72. ;;
  73. \?)
  74. echo "ERROR: unrecognized flag: -$OPTARG"
  75. usage 1;
  76. ;;
  77. esac
  78. done
  79. shift $((OPTIND-1))
  80. if [ "$#" -gt 0 ]; then
  81. echo "ERROR: extra argument(s): $*" >&2
  82. usage 1;
  83. fi
  84. }
  85. # Create site directory, checking if it exists first
  86. function create_site_directory() {
  87. if [ -e "$SITE_NAME" ]; then
  88. if [ "$FORCE_DELETE" = true ]; then
  89. echo "[INFO] Directory '$SITE_NAME' already exists. Deleting it as requested (-f)."
  90. ([[ $VERBOSE ]] && set -x; rm -rf "$SITE_NAME")
  91. else
  92. echo "[ERROR] Directory '$SITE_NAME' already exists. Remove it or use -f to force delete."
  93. exit 1
  94. fi
  95. fi
  96. }
  97. function _npm_install() {
  98. npm init -y > /dev/null
  99. npm install --omit dev --save $DEPS
  100. }
  101. function set_up_and_cd_into_site() {
  102. $HUGO new site --format yaml --quiet "$SITE_NAME"
  103. cd "$SITE_NAME"
  104. eval _npm_install $OUTPUT_REDIRECT
  105. if [[ "$DOCSY_SRC" == HUGO* ]]; then
  106. _set_up_site_using_hugo_modules
  107. else
  108. echo "theme: docsy" >> hugo.yaml
  109. echo "themesDir: $THEMESDIR" >> hugo.yaml
  110. fi
  111. }
  112. function _set_up_site_using_hugo_modules() {
  113. local user_name=$(whoami)
  114. # : ${user_name:=$USER}
  115. # : ${user_name:="me"}
  116. HUGO_MOD_WITH_VERS=$DOCSY_REPO
  117. if [[ -n $DOCSY_VERS ]]; then
  118. HUGO_MOD_WITH_VERS+="@$DOCSY_VERS"
  119. fi
  120. echo "[INFO] Getting Docsy as Hugo module $HUGO_MOD_WITH_VERS"
  121. eval "$HUGO mod init github.com/$user_name/$SITE_NAME" $OUTPUT_REDIRECT
  122. if [[ "$DOCSY_REPO" == "$DOCSY_REPO_DEFAULT" ]]; then
  123. eval "$HUGO mod get github.com/$HUGO_MOD_WITH_VERS" $OUTPUT_REDIRECT
  124. else
  125. echo "[INFO] Fetch Docsy GitHub repo '$DOCSY_REPO' @ '$DOCSY_VERS'"
  126. mkdir tmp
  127. local BRANCH_SPEC=""
  128. local DEPTH=10
  129. local SWITCH_NEEDED=
  130. local CLONE="git clone --depth=$DEPTH https://github.com/$DOCSY_REPO tmp/docsy"
  131. if [[ -n $DOCSY_VERS ]]; then
  132. BRANCH_SPEC="-b $DOCSY_VERS"
  133. fi
  134. if ! $CLONE $BRANCH_SPEC; then
  135. SWITCH_NEEDED=1
  136. $CLONE
  137. fi
  138. ( \
  139. cd tmp/docsy && \
  140. git log --oneline -$DEPTH && \
  141. if [[ -n $SWITCH_NEEDED ]]; then git switch --detach $DOCSY_VERS; fi \
  142. )
  143. echo "replace github.com/$DOCSY_REPO_DEFAULT => ./tmp/docsy" >> go.mod
  144. eval "$HUGO mod get github.com/$DOCSY_REPO_DEFAULT" $OUTPUT_REDIRECT
  145. fi
  146. echo "module: {proxy: direct, hugoVersion: {extended: true}, imports: [{path: github.com/$DOCSY_REPO_DEFAULT, disable: false}]}" >> hugo.yaml
  147. }
  148. function main() {
  149. process_CLI_args "$@"
  150. create_site_directory
  151. if [[ "$DOCSY_SRC" == "NPM" ]]; then
  152. NPM_PKG=$DOCSY_REPO
  153. if [[ -n $DOCSY_VERS ]]; then
  154. NPM_PKG+="#$DOCSY_VERS"
  155. fi
  156. echo "[INFO] Getting Docsy as NPM package '$NPM_PKG'"
  157. DEPS+=" $NPM_PKG"
  158. elif [[ "$DOCSY_SRC" == "LOCAL" ]]; then
  159. echo "[INFO] Getting Docsy through a local directory '$THEMESDIR"
  160. fi
  161. [[ $VERBOSE ]] && set -x
  162. set_up_and_cd_into_site
  163. eval $HUGO $OUTPUT_REDIRECT # Generate site
  164. [[ $VERBOSE ]] && set +x
  165. cd ..
  166. echo "[INFO] '$SITE_NAME' successfully created, set up, and built."
  167. if [[ $VERBOSE ]]; then
  168. echo "[INFO] Here are the site files:"
  169. echo
  170. set -x
  171. ls -l "$SITE_NAME"
  172. echo
  173. ls -l "$SITE_NAME/public"
  174. fi
  175. }
  176. main "$@"