diff options
author | Sarah German <sgerman@gitlab.com> | 2023-05-05 22:05:23 +0300 |
---|---|---|
committer | Sarah German <sgerman@gitlab.com> | 2023-05-05 22:05:23 +0300 |
commit | 0a602766667fc93591571f37f63b9bc03d568fd3 (patch) | |
tree | 2a315355c3aea1743548213e3ef90952503fa765 /scripts | |
parent | 51939c43cfacbfd520add5472c7223949fd9b5c8 (diff) | |
parent | 759d4dd839e769bd1ccbe96627d830bb4f402b01 (diff) |
Merge branch 'axil-fix-minify-run' into 'main'
Change logic of input and output targets of post-processing scripts
Closes #1612
See merge request https://gitlab.com/gitlab-org/gitlab-docs/-/merge_requests/3791
Merged-by: Sarah German <sgerman@gitlab.com>
Approved-by: Sarah German <sgerman@gitlab.com>
Reviewed-by: Achilleas Pipinellis <axil@gitlab.com>
Co-authored-by: Achilleas Pipinellis <axil@gitlab.com>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/compress_images.sh | 20 | ||||
-rwxr-xr-x | scripts/minify-assets.sh | 77 | ||||
-rwxr-xr-x | scripts/normalize-links.sh | 63 |
3 files changed, 72 insertions, 88 deletions
diff --git a/scripts/compress_images.sh b/scripts/compress_images.sh index dd839d79..5fb10501 100755 --- a/scripts/compress_images.sh +++ b/scripts/compress_images.sh @@ -1,30 +1,18 @@ #!/usr/bin/env bash -TARGET="$1" -VER="$2" +INPUT="$1" PNGQUANT=$(which pngquant) PNG="$PNGQUANT -f --skip-if-larger --ext .png --speed 1" -if [ -z "$TARGET" ]; then - echo "Usage: $0 <target> <version>" +if [ -z "$INPUT" ]; then + echo "Usage: $0 <INPUT>" echo "No target provided. Exiting." exit 1 fi -if [ -z "$VER" ]; then - echo "Usage: $0 <target> <version>" - echo "No version provided. Exiting." - exit 1 -fi - -if ! [ -d "$TARGET/$VER" ]; then - echo "Target directory $TARGET/$VER does not exist. Exiting." - exit 1 -fi - # Compress images # shellcheck disable=SC2044 -for image in $(find "${TARGET}/${VER}/" -name "*.png") +for image in $(find "${INPUT}/" -name "*.png") do echo "Compressing $image" $PNG "$image" done diff --git a/scripts/minify-assets.sh b/scripts/minify-assets.sh index aa27512e..edcb3288 100755 --- a/scripts/minify-assets.sh +++ b/scripts/minify-assets.sh @@ -1,22 +1,22 @@ #!/usr/bin/env bash # shellcheck disable=SC2059 -TARGET="$1" -VER="$2" -MINIFY_FLAGS=("--html-keep-document-tags" "--html-keep-whitespace" "--recursive") +DEST="$1" +SOURCE="$2" +MINIFY_FLAGS=("--html-keep-document-tags" "--html-keep-whitespace" "--recursive" "--sync") COLOR_RED="\e[31m" COLOR_GREEN="\e[32m" COLOR_RESET="\e[39m" -if [ -z "$TARGET" ] || [ -z "$VER" ]; then +if [ -z "$DEST" ] || [ -z "$SOURCE" ]; then echo "Usage: $0 <target> <ver>" - printf "${COLOR_RED}ERROR: Either <target> or <ver> is missing.${COLOR_RESET}\n" + printf "${COLOR_RED}ERROR: Either <DEST> or <SOURCE> is missing.${COLOR_RESET}\n" exit 1 fi -if ! [ -d "$TARGET" ]; then - printf "${COLOR_RED}ERROR: Target directory $TARGET does not exist.${COLOR_RESET}\n" +if ! [ -d "$DEST" ]; then + printf "${COLOR_RED}ERROR: Output directory $DEST does not exist.${COLOR_RESET}\n" exit 1 fi @@ -30,41 +30,46 @@ else then MINIFY_BIN=/scripts/minify else - printf "${COLOR_RED}ERROR: minify not found in PATH. Run 'make setup'.${COLOR_RESET}\n" + printf "${COLOR_RED}ERROR: minify not found in PATH. Run 'make setup'.${COLOR_RESET}\n" exit 1 fi fi # Minify assets -printf "${COLOR_GREEN}INFO: Minifying HTML...${COLOR_RESET}\n" -if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=html --match="\.html$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then - printf "${COLOR_GREEN}INFO: HTML minified!${COLOR_RESET}\n" -else - printf "${COLOR_RED}ERROR: Couldn't minify HTML${COLOR_RESET}\n" - exit 1 -fi - -printf "${COLOR_GREEN}INFO: Minifying CSS...${COLOR_RESET}\n" -if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=css --match="\.css$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then - printf "${COLOR_GREEN}INFO: CSS minified!${COLOR_RESET}\n" -else - printf "${COLOR_RED}ERROR: Couldn't minify CSS${COLOR_RESET}\n" - exit 1 -fi - - -printf "${COLOR_GREEN}INFO: Minifying JSON...${COLOR_RESET}\n" -if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=json --match="\.json$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then - printf "${COLOR_GREEN}INFO: JSON minified!${COLOR_RESET}\n" -else - printf "${COLOR_RED}ERROR: Couldn't minify JSON${COLOR_RESET}\n" - exit 1 -fi +printf "${COLOR_GREEN}INFO: Minifying all assets...${COLOR_RESET}\n" +if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --match="\.html$|\.css$|\.json|\.svg$" -o "${DEST}" "${SOURCE}"; then + printf "${COLOR_GREEN}INFO: Assets minified!${COLOR_RESET}\n" + # Calculate sizes before and after minifying/gzipping the static files (HTML, CSS, JS) + SIZE_BEFORE_MINIFY=$(du -sh "$SOURCE" | awk '{print $1}') + SIZE_AFTER_MINIFY=$(du -sh "$DEST" | awk '{print $1}') + # Print size results + printf "${COLOR_GREEN}INFO: Size before minifying: $SIZE_BEFORE_MINIFY ${COLOR_RESET}\n" + printf "${COLOR_GREEN}INFO: Size after minifying: $SIZE_AFTER_MINIFY ${COLOR_RESET}\n" -printf "${COLOR_GREEN}INFO: Minifying SVGs...${COLOR_RESET}\n" -if $MINIFY_BIN "${MINIFY_FLAGS[@]}" --type=svg --match="\.svg$" -o "${TARGET}/${VER}/" "${TARGET}/${VER}"; then - printf "${COLOR_GREEN}INFO: SVGs minified!${COLOR_RESET}\n" + # + # Test that minification worked by comparing the number of the source and + # destination files. + # + SOURCE_FILES_COUNT=$(find "$SOURCE" -type f | wc -l) + DEST_FILES_COUNT=$(find "$DEST" -type f | wc -l) + SOURCE_DIR_COUNT=$(find "$SOURCE" -type d | wc -l) + # Destination is always one directory deeper (more) than source, so we subtract one + DEST_DIR_COUNT=$(($(find "$DEST" -type d | wc -l) - 1)) + printf "${COLOR_GREEN}INFO: Checking that the number of source and destination files is the same...${COLOR_RESET}\n" + if [[ $SOURCE_FILES_COUNT -eq $DEST_FILES_COUNT ]]; then + printf "${COLOR_GREEN}SUCCESS: File count in $SOURCE and $DEST: $SOURCE_FILES_COUNT/$DEST_FILES_COUNT ${COLOR_RESET}\n" + else + printf "${COLOR_RED}ERROR: File count in $SOURCE and $DEST do not match: $SOURCE_FILES_COUNT/$DEST_FILES_COUNT ${COLOR_RESET}" + exit 1 + fi + printf "${COLOR_GREEN}INFO: Checking that the number of source and destination directories is the same...${COLOR_RESET}\n" + if [[ $SOURCE_DIR_COUNT -eq $DEST_DIR_COUNT ]]; then + printf "${COLOR_GREEN}SUCCESS: Directory count in $SOURCE and $DEST: $SOURCE_DIR_COUNT/$DEST_DIR_COUNT ${COLOR_RESET}\n" + else + printf "${COLOR_RED}ERROR: Directory count in $SOURCE and $DEST do not match: $SOURCE_DIR_COUNT/$DEST_DIR_COUNT ${COLOR_RESET}" + exit 1 + fi else - printf "${COLOR_RED}ERROR: Couldn't minify SVGs${COLOR_RESET}\n" + printf "${COLOR_RED}ERROR: Couldn't minify assets${COLOR_RESET}\n" exit 1 fi diff --git a/scripts/normalize-links.sh b/scripts/normalize-links.sh index 442a5f43..fee57f40 100755 --- a/scripts/normalize-links.sh +++ b/scripts/normalize-links.sh @@ -4,11 +4,8 @@ COLOR_RED="\e[31m" COLOR_GREEN="\e[32m" COLOR_RESET="\e[39m" -TARGET="$1" # The directory that has all the HTML files including versions. - # Usually public/ locally and /site in the Docker image. - -VER="$2" # The docs version which is a directory holding all the respective - # versioned site, for example 13.0/ +INPUT="$1" # The directory that has all the HTML files. +VER="$2" # The docs version to replace the URLs with. ## Check which OS the script runs from since sed behaves differently ## on macOS and Linux. For macOS, check if you're using the built-in sed. @@ -27,8 +24,8 @@ else SED="sed" fi -if [ -z "$TARGET" ]; then - echo "Usage: $0 <target> <version>" +if [ -z "$INPUT" ]; then + echo "Usage: $0 <input> <version>" echo "Example: $0 public 13.0" # shellcheck disable=2059 printf "${COLOR_RED}ERROR: No target provided.${COLOR_RESET}\n" @@ -36,19 +33,13 @@ if [ -z "$TARGET" ]; then fi if [ -z "$VER" ]; then - echo "Usage: $0 <target> <version>" + echo "Usage: $0 <input> <version>" echo "Example: $0 public 13.0" # shellcheck disable=2059 printf "${COLOR_RED}ERROR: No version provided.${COLOR_RESET}\n" exit 1 fi -if ! [ -d "$TARGET/$VER" ]; then - # shellcheck disable=2059 - printf "${COLOR_RED}ERROR: Target directory $TARGET/$VER does not exist.${COLOR_RESET}\n" - exit 1 -fi - ## ## In order for the version to be correct, we need to replace any occurrences ## of relative or full URLs with the respective version. Basically, prefix @@ -59,25 +50,25 @@ fi ## # shellcheck disable=2059 -printf "${COLOR_GREEN}INFO: Replacing relative URLs in $TARGET/$VER for HTML files...${COLOR_RESET}\n" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i -e 's|href="/ee/|href="/'"$VER"'/ee/|g' \ - -e 's|href="/runner/|href="/'"$VER"'/runner/|g' \ - -e 's|href="/omnibus/|href="/'"$VER"'/omnibus/|g' \ - -e 's|href="/charts/|href="/'"$VER"'/charts/|g' \ - -e 's|href="/operator/|href="/'"$VER"'/operator/|g' \ - -e 's|="/assets/|="/'"$VER"'/assets/|g' \ - -e 's|="/frontend/|="/'"$VER"'/frontend/|g' \ - -e 's|<a href="/">|<a href="/'"$VER"'/">|g' \ - -e 's|="/opensearch.xml|="/'"$VER"'/opensearch.xml|g' +printf "${COLOR_GREEN}INFO: Replacing relative URLs in $INPUT for HTML files...${COLOR_RESET}\n" +find "${INPUT}" -type f -name '*.html' -print0 | xargs -0 "$SED" -i -e 's|href="/ee/|href="/'"$VER"'/ee/|g' \ + -e 's|href="/runner/|href="/'"$VER"'/runner/|g' \ + -e 's|href="/omnibus/|href="/'"$VER"'/omnibus/|g' \ + -e 's|href="/charts/|href="/'"$VER"'/charts/|g' \ + -e 's|href="/operator/|href="/'"$VER"'/operator/|g' \ + -e 's|="/assets/|="/'"$VER"'/assets/|g' \ + -e 's|="/frontend/|="/'"$VER"'/frontend/|g' \ + -e 's|<a href="/">|<a href="/'"$VER"'/">|g' \ + -e 's|="/opensearch.xml|="/'"$VER"'/opensearch.xml|g' # shellcheck disable=2059 -printf "${COLOR_GREEN}INFO: Replacing relative URLs in $TARGET/$VER for CSS files...${COLOR_RESET}\n" -find "${TARGET}/$VER" -type f -name '*.css' -print0 | xargs -0 "$SED" -i 's|/assets/|/'"$VER"'/assets/|g' +printf "${COLOR_GREEN}INFO: Replacing relative URLs in $INPUT for CSS files...${COLOR_RESET}\n" +find "${INPUT}" -type f -name '*.css' -print0 | xargs -0 "$SED" -i 's|/assets/|/'"$VER"'/assets/|g' # shellcheck disable=2059 -printf "${COLOR_GREEN}INFO: Replacing relative URLs in $TARGET/$VER for JavaScript files...${COLOR_RESET}\n" -find "${TARGET}/$VER" -type f -name '*.js' -print0 | xargs -0 "$SED" -i -e 's|/search/|/'"$VER"'/search/|g' \ - -e 's|/assets/|/'"$VER"'/assets/|g' +printf "${COLOR_GREEN}INFO: Replacing relative URLs in $INPUT for JavaScript files...${COLOR_RESET}\n" +find "${INPUT}" -type f -name '*.js' -print0 | xargs -0 "$SED" -i -e 's|/search/|/'"$VER"'/search/|g' \ + -e 's|/assets/|/'"$VER"'/assets/|g' # # Full URLs # @@ -88,13 +79,13 @@ find "${TARGET}/$VER" -type f -name '*.js' -print0 | xargs -0 "$SED" -i -e 's|/s # See https://gitlab.com/gitlab-org/gitlab-docs/-/issues/1568 # # shellcheck disable=2059 -printf "${COLOR_GREEN}INFO: Replacing full URLs in $TARGET/$VER for HTML files...${COLOR_RESET}\n" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/ee/|href="/'"$VER"'/ee/|g' \ - -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/runner/|href="/'"$VER"'/runner/|g' \ - -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/omnibus/|href="/'"$VER"'/omnibus/|g' \ - -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/charts/|href="/'"$VER"'/charts/|g' \ - -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/operator/|href="/'"$VER"'/operator/|g' +printf "${COLOR_GREEN}INFO: Replacing full URLs in $INPUT for HTML files...${COLOR_RESET}\n" +find "${INPUT}" -type f -name '*.html' -print0 | xargs -0 "$SED" -i -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/ee/|href="/'"$VER"'/ee/|g' \ + -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/runner/|href="/'"$VER"'/runner/|g' \ + -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/omnibus/|href="/'"$VER"'/omnibus/|g' \ + -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/charts/|href="/'"$VER"'/charts/|g' \ + -e '/\(rel="canonical"\|property="og:url"\)/! s|href="https://docs.gitlab.com/operator/|href="/'"$VER"'/operator/|g' # shellcheck disable=2059 printf "${COLOR_GREEN}INFO: Fixing URLs inside the sitemap...${COLOR_RESET}\n" -find "${TARGET}/$VER" -type f -name 'sitemap.xml' -print0 | xargs -0 "$SED" -i 's|docs.gitlab.com/|docs.gitlab.com/'"$VER"'/|g' +find "${INPUT}" -type f -name 'sitemap.xml' -print0 | xargs -0 "$SED" -i 's|docs.gitlab.com/|docs.gitlab.com/'"$VER"'/|g' |