From bdae4a6118afec3217c708ea400ba0b60a49893f Mon Sep 17 00:00:00 2001 From: Achilleas Pipinellis Date: Mon, 27 Mar 2023 15:15:06 +0000 Subject: Add support for macOS sed in normalize-links.sh script --- Brewfile | 1 + scripts/normalize-links.sh | 53 ++++++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Brewfile b/Brewfile index a6fca8a3..12fc10be 100644 --- a/Brewfile +++ b/Brewfile @@ -7,3 +7,4 @@ brew "yamllint" # Install on Mac only brew "gnupg" if OS.mac? +brew "gnu-sed" if OS.mac? diff --git a/scripts/normalize-links.sh b/scripts/normalize-links.sh index d239e095..01ef116d 100755 --- a/scripts/normalize-links.sh +++ b/scripts/normalize-links.sh @@ -6,6 +6,23 @@ TARGET="$1" # The directory that has all the HTML files including versions. VER="$2" # The docs version which is a directory holding all the respective # versioned site, for example 13.0/ +## 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. +## gnu-sed is preferred https://medium.com/@bramblexu/install-gnu-sed-on-mac-os-and-set-it-as-default-7c17ef1b8f64. +## For more information about their differences, see +## https://unix.stackexchange.com/questions/13711/differences-between-sed-on-mac-osx-and-other-standard-sed +if [ "$(uname)" == "Darwin" ]; then + if hash gsed 2>/dev/null; then + SED="gsed" + else + echo "✖ ERROR: The built-in sed in macOS is not supported. + Install gnu-sed instead: 'brew install gnu-sed'." >&2 + exit 1 + fi +else + SED="sed" +fi + if [ -z "$TARGET" ]; then echo "Usage: $0 " echo "Example: $0 public 13.0" @@ -34,54 +51,54 @@ fi ## Relative URLs ## echo "Replace relative URLs in $TARGET/$VER for /ee/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/ee/#="/'"$VER"'/ee/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/ee/#="/'"$VER"'/ee/#g' echo "Replace relative URLs in $TARGET/$VER for /runner/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/runner/#="/'"$VER"'/runner/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/runner/#="/'"$VER"'/runner/#g' echo "Replace relative URLs in $TARGET/$VER for /omnibus/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/omnibus/#="/'"$VER"'/omnibus/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/omnibus/#="/'"$VER"'/omnibus/#g' echo "Replace relative URLs in $TARGET/$VER for /charts/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/charts/#="/'"$VER"'/charts/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/charts/#="/'"$VER"'/charts/#g' echo "Replace relative URLs in $TARGET/$VER for /operator/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/operator/#="/'"$VER"'/operator/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/operator/#="/'"$VER"'/operator/#g' echo "Replace relative URLs in $TARGET/$VER for /assets/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/assets/#="/'"$VER"'/assets/#g' -find "${TARGET}/$VER" -type f -name '*.css' -print0 | xargs -0 sed -i 's#/assets/#/'"$VER"'/assets/#g' -find "${TARGET}/$VER" -type f -name '*.js' -print0 | xargs -0 sed -i 's#/assets/#/'"$VER"'/assets/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/assets/#="/'"$VER"'/assets/#g' +find "${TARGET}/$VER" -type f -name '*.css' -print0 | xargs -0 "$SED" -i 's#/assets/#/'"$VER"'/assets/#g' +find "${TARGET}/$VER" -type f -name '*.js' -print0 | xargs -0 "$SED" -i 's#/assets/#/'"$VER"'/assets/#g' echo "Replace relative URLs in $TARGET/$VER for /frontend/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/frontend/#="/'"$VER"'/frontend/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/frontend/#="/'"$VER"'/frontend/#g' echo "Replace relative URLs in $TARGET/$VER for /search/" -find "${TARGET}/$VER" -type f -name '*.js' -print0 | xargs -0 sed -i 's#/search/#/'"$VER"'/search/#g' +find "${TARGET}/$VER" -type f -name '*.js' -print0 | xargs -0 "$SED" -i 's#/search/#/'"$VER"'/search/#g' echo "Replace relative URLs in $TARGET/$VER for /" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's###g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's###g' echo "Replace relative URLs in $TARGET/$VER for opensearch.xml" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="/opensearch.xml#="/'"$VER"'/opensearch.xml#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="/opensearch.xml#="/'"$VER"'/opensearch.xml#g' ## ## Full URLs ## echo "Replace full URLs in $TARGET/$VER for /ee/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="https://docs.gitlab.com/ee/#="/'"$VER"'/ee/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="https://docs.gitlab.com/ee/#="/'"$VER"'/ee/#g' echo "Replace full URLs in $TARGET/$VER for /runner/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="https://docs.gitlab.com/runner/#="/'"$VER"'/runner/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="https://docs.gitlab.com/runner/#="/'"$VER"'/runner/#g' echo "Replace full URLs in $TARGET/$VER for /omnibus/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="https://docs.gitlab.com/omnibus/#="/'"$VER"'/omnibus/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="https://docs.gitlab.com/omnibus/#="/'"$VER"'/omnibus/#g' echo "Replace full URLs in $TARGET/$VER for /charts/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="https://docs.gitlab.com/charts/#="/'"$VER"'/charts/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="https://docs.gitlab.com/charts/#="/'"$VER"'/charts/#g' echo "Replace full URLs in $TARGET/$VER for /operator/" -find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 sed -i 's#="https://docs.gitlab.com/operator/#="/'"$VER"'/operator/#g' +find "${TARGET}/$VER" -type f -name '*.html' -print0 | xargs -0 "$SED" -i 's#="https://docs.gitlab.com/operator/#="/'"$VER"'/operator/#g' echo "Fix URLs inside the sitemap" -find "${TARGET}/$VER" -type f -name 'sitemap.xml' -print0 | xargs -0 sed -i 's#docs.gitlab.com/#docs.gitlab.com/'"$VER"'/#g' +find "${TARGET}/$VER" -type f -name 'sitemap.xml' -print0 | xargs -0 "$SED" -i 's#docs.gitlab.com/#docs.gitlab.com/'"$VER"'/#g' -- cgit v1.2.3