From 56d87dd51a53e8dda3ab2ac3fe73a1a055e7491f Mon Sep 17 00:00:00 2001 From: Patrice Chalin Date: Sat, 20 Jan 2024 05:35:46 -0500 Subject: [PATCH] [CI] Smoke test new site from Hugo-module Docsy as well --- .github/workflows/smoke.yaml | 7 +- tools/make-site.sh | 229 +++++++++++++++++++++++++---------- 2 files changed, 169 insertions(+), 67 deletions(-) diff --git a/.github/workflows/smoke.yaml b/.github/workflows/smoke.yaml index d1e291e326..c267bfda93 100644 --- a/.github/workflows/smoke.yaml +++ b/.github/workflows/smoke.yaml @@ -12,11 +12,12 @@ on: workflow_dispatch: jobs: - new-site-from-npm: + new-site: runs-on: ${{ matrix.os }} strategy: matrix: os: [windows-latest, ubuntu-latest] + docsy-src: [NPM, HUGO_MODULE] env: BASE_REPO: ${{ github.repository }} BRANCH: ${{ github.head_ref }} @@ -37,11 +38,11 @@ jobs: if: github.event_name != 'pull_request' run: | mkdir tmp && cd tmp && set -x - ../tools/make-site.sh -p $BASE_REPO -v $SHA + ../tools/make-site.sh -s ${{ matrix.docsy-src }} -r $BASE_REPO -v $SHA shell: bash - name: Make site from PR if: github.event_name == 'pull_request' run: | mkdir tmp && cd tmp && set -x - ../tools/make-site.sh -p $PR_REPO -v $BRANCH + ../tools/make-site.sh -s ${{ matrix.docsy-src }} -r $PR_REPO -v $BRANCH shell: bash diff --git a/tools/make-site.sh b/tools/make-site.sh index 30ac261282..413d24643a 100755 --- a/tools/make-site.sh +++ b/tools/make-site.sh @@ -3,29 +3,40 @@ set -eo pipefail DEPS="autoprefixer postcss-cli" +DOCSY_REPO_DEFAULT="google/docsy" +DOCSY_REPO=$DOCSY_REPO_DEFAULT +DOCSY_VERS="" +DOCSY_SRC="NPM" FORCE_DELETE=false : ${HUGO:=npx hugo} -NPM_PKG_VERS= -NPM_PKG="google/docsy" SITE_NAME="test-site" -THEMESDIR=node_modules +THEMESDIR="node_modules" +VERBOSE=1 +OUTPUT_REDIRECT="" # Use along with VERBOSE function _usage() { - echo - echo "Usage: `basename $0` [options]" - echo - echo " Creates a Docsy-themed site under SITE_NAME using the Hugo new command." - echo " Docsy is fetched as an NPM module from GitHub, unless the -l flag is used." - echo - echo " -f Force delete SITE_NAME if it exists before recreating it" - echo " -h Output this usage info" - echo " -l PATH Use Docsy at PATH rather than as an NPM package" - echo " -n SITE_NAME Name of directory to create for the Hugo generated site. Default: $SITE_NAME" - echo " -p NPM_PKG GitHub repo to fetch Docsy from as an NPM module" - echo " Format: GITHUB_USER/DOCSY_REPO. Default: $NPM_PKG" - echo " -v VERS Docsy NPM package version. Default: ''." - echo " Examples: semver:0.8.0, some-branch-name" - echo + cat <&2 + usage 1; + fi +} + +# Create site directory, checking if it exists first +function create_site_directory() { + if [ -e "$SITE_NAME" ]; then + if [ "$FORCE_DELETE" = true ]; then + echo "[INFO] Directory '$SITE_NAME' already exists. Deleting it as requested (-f)." + ([[ $VERBOSE ]] && set -x; rm -rf "$SITE_NAME") + else + echo "[ERROR] Directory '$SITE_NAME' already exists. Remove it or use -f to force delete." + exit 1 + fi + fi +} + +function _npm_install() { + npm init -y > /dev/null + npm install --omit dev --save $DEPS +} + +function set_up_and_cd_into_site() { + $HUGO new site --format yaml --quiet "$SITE_NAME" + cd "$SITE_NAME" + eval _npm_install $OUTPUT_REDIRECT + + if [[ "$DOCSY_SRC" == HUGO* ]]; then + _set_up_site_using_hugo_modules else - echo "[ERROR] Directory '$SITE_NAME' already exists. Remove it or use -f to force delete." - exit 1 + echo "theme: docsy" >> hugo.yaml + echo "themesDir: $THEMESDIR" >> hugo.yaml fi -fi +} -DOCSY_NPM_PKG=$NPM_PKG$NPM_PKG_VERS +function _set_up_site_using_hugo_modules() { + local user_name=$(whoami) + # : ${user_name:=$USER} + # : ${user_name:="me"} -set -x + HUGO_MOD_WITH_VERS=$DOCSY_REPO + if [[ -n $DOCSY_VERS ]]; then + HUGO_MOD_WITH_VERS+="@$DOCSY_VERS" + fi + + echo "[INFO] Getting Docsy as Hugo module $HUGO_MOD_WITH_VERS" -# Setup site -$HUGO new site --format yaml --quiet "$SITE_NAME" -cd "$SITE_NAME" -npm init -y > /dev/null -npm install --save-dev $DOCSY_NPM_PKG $DEPS + eval "$HUGO mod init github.com/$user_name/$SITE_NAME" $OUTPUT_REDIRECT + + if [[ "$DOCSY_REPO" == "$DOCSY_REPO_DEFAULT" ]]; then + eval "$HUGO mod get github.com/$HUGO_MOD_WITH_VERS" $OUTPUT_REDIRECT + else + echo "[INFO] Fetch Docsy GitHub repo '$DOCSY_REPO' @ '$DOCSY_VERS'" + mkdir tmp + BRANCH_SPEC="" + if [[ -n $DOCSY_VERS ]]; then + BRANCH_SPEC="-b $DOCSY_VERS" + fi + git clone --depth=1 https://github.com/$DOCSY_REPO $BRANCH_SPEC tmp/docsy + (cd tmp/docsy && git log -1) -echo "theme: docsy" >> hugo.yaml -echo "themesDir: $THEMESDIR" >> hugo.yaml + echo "replace github.com/$DOCSY_REPO_DEFAULT => ./tmp/docsy" >> go.mod + eval "$HUGO mod get github.com/$DOCSY_REPO_DEFAULT" $OUTPUT_REDIRECT + fi + + echo "module: {proxy: direct, hugoVersion: {extended: true}, imports: [{path: github.com/$DOCSY_REPO_DEFAULT, disable: false}]}" >> hugo.yaml +} -# Generate site -$HUGO +function main() { + process_CLI_args "$@" + create_site_directory -set +x + if [[ "$DOCSY_SRC" == "NPM" ]]; then + NPM_PKG=$DOCSY_REPO + if [[ -n $DOCSY_VERS ]]; then + NPM_PKG+="#$DOCSY_VERS" + fi + echo "[INFO] Getting Docsy as NPM package '$NPM_PKG'" + DEPS+=" $NPM_PKG" + elif [[ "$DOCSY_SRC" == "LOCAL" ]]; then + echo "[INFO] Getting Docsy through a local directory '$THEMESDIR" + fi + + [[ $VERBOSE ]] && set -x + set_up_and_cd_into_site + eval $HUGO $OUTPUT_REDIRECT # Generate site + [[ $VERBOSE ]] && set +x + cd .. + + echo "[INFO] '$SITE_NAME' successfully created, set up, and built." + + if [[ $VERBOSE ]]; then + echo "[INFO] Here are the site files:" + echo + set -x + ls -l "$SITE_NAME" + echo + ls -l "$SITE_NAME/public" + fi +} -echo "[INFO] $SITE_NAME successfully created, set up, and built." +main "$@"