Skip to content

Commit

Permalink
Merge pull request #422 from per1234/unify-markdown-link-check
Browse files Browse the repository at this point in the history
Avoid platform-specific code in `markdown:check-links` task
  • Loading branch information
per1234 authored Dec 1, 2023
2 parents 4eb4e89 + fa253b2 commit 7530cab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 104 deletions.
84 changes: 30 additions & 54 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -625,65 +625,41 @@ tasks:
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
markdown:check-links:
desc: Check for broken links
vars:
# The command is defined in a Taskfile variable to allow it to be broken into multiple lines for readability.
# This can't be done in the `cmd` object of the Taskfile because `npx --call` uses the native shell, which causes
# standard newline escaping syntax to not work when the task is run on Windows.
#
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
# \ characters special treatment on Windows in an attempt to support them as path separators.
#
# prettier-ignore
CHECK_LINKS_COMMAND:
"
find . \
-type d -name \".git\" -prune -o \
-type d -name \".licenses\" -prune -o \
-type d -name \"__pycache__\" -prune -o \
-type d -name \"node_modules\" -prune -o \
-path \"./{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples\" -prune -o \
-path \"./{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples\" -prune -o \
-regex \".*[.]md\" \
-exec \
markdown-link-check \
--quiet \
--config \"./.markdown-link-check.json\" \
\\{\\} \
+
"
deps:
- task: docs:generate
- task: npm:install-deps
cmds:
- |
if [[ "{{.OS}}" == "Windows_NT" ]]; then
# npx --call uses the native shell, which makes it too difficult to use npx for this application on Windows,
# so the Windows user is required to have markdown-link-check installed and in PATH.
if ! which markdown-link-check &>/dev/null; then
echo "markdown-link-check not found or not in PATH."
echo "Please install: https://github.com/tcort/markdown-link-check#readme"
exit 1
fi
# Default behavior of the task on Windows is to exit the task when the first broken link causes a non-zero
# exit status, but it's better to check all links before exiting.
set +o errexit
STATUS=0
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
# \ characters special treatment on Windows in an attempt to support them as path separators.
for file in $(
find . \
-type d -name '.git' -prune -o \
-type d -name '.licenses' -prune -o \
-type d -name '__pycache__' -prune -o \
-type d -name 'node_modules' -prune -o \
-path './{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples' -prune -o \
-path './{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples' -prune -o \
-regex ".*[.]md" -print
); do
markdown-link-check \
--quiet \
--config "./.markdown-link-check.json" \
"$file"
STATUS=$(( $STATUS + $? ))
done
exit $STATUS
else
npx --package=markdown-link-check --call='
STATUS=0
for file in $(
find . \
-type d -name '.git' -prune -o \
-type d -name '.licenses' -prune -o \
-type d -name '__pycache__' -prune -o \
-type d -name 'node_modules' -prune -o \
-path './{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples' -prune -o \
-path './{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples' -prune -o \
-regex ".*[.]md" -print
); do
markdown-link-check \
--quiet \
--config "./.markdown-link-check.json" \
"$file"
STATUS=$(( $STATUS + $? ))
done
exit $STATUS
'
fi
npx \
--package=markdown-link-check \
--call='{{.CHECK_LINKS_COMMAND}}'
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
markdown:fix:
Expand Down
78 changes: 28 additions & 50 deletions workflow-templates/assets/check-markdown-task/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,39 @@ tasks:
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
markdown:check-links:
desc: Check for broken links
vars:
# The command is defined in a Taskfile variable to allow it to be broken into multiple lines for readability.
# This can't be done in the `cmd` object of the Taskfile because `npx --call` uses the native shell, which causes
# standard newline escaping syntax to not work when the task is run on Windows.
#
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
# \ characters special treatment on Windows in an attempt to support them as path separators.
#
# prettier-ignore
CHECK_LINKS_COMMAND:
"
find . \
-type d -name \".git\" -prune -o \
-type d -name \".licenses\" -prune -o \
-type d -name \"__pycache__\" -prune -o \
-type d -name \"node_modules\" -prune -o \
-regex \".*[.]md\" \
-exec \
markdown-link-check \
--quiet \
--config \"./.markdown-link-check.json\" \
\\{\\} \
+
"
deps:
- task: docs:generate
- task: npm:install-deps
cmds:
- |
if [[ "{{.OS}}" == "Windows_NT" ]]; then
# npx --call uses the native shell, which makes it too difficult to use npx for this application on Windows,
# so the Windows user is required to have markdown-link-check installed and in PATH.
if ! which markdown-link-check &>/dev/null; then
echo "markdown-link-check not found or not in PATH."
echo "Please install: https://github.com/tcort/markdown-link-check#readme"
exit 1
fi
# Default behavior of the task on Windows is to exit the task when the first broken link causes a non-zero
# exit status, but it's better to check all links before exiting.
set +o errexit
STATUS=0
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
# \ characters special treatment on Windows in an attempt to support them as path separators.
for file in $(
find . \
-type d -name '.git' -prune -o \
-type d -name '.licenses' -prune -o \
-type d -name '__pycache__' -prune -o \
-type d -name 'node_modules' -prune -o \
-regex ".*[.]md" -print
); do
markdown-link-check \
--quiet \
--config "./.markdown-link-check.json" \
"$file"
STATUS=$(( $STATUS + $? ))
done
exit $STATUS
else
npx --package=markdown-link-check --call='
STATUS=0
for file in $(
find . \
-type d -name '.git' -prune -o \
-type d -name '.licenses' -prune -o \
-type d -name '__pycache__' -prune -o \
-type d -name 'node_modules' -prune -o \
-regex ".*[.]md" -print
); do
markdown-link-check \
--quiet \
--config "./.markdown-link-check.json" \
"$file"
STATUS=$(( $STATUS + $? ))
done
exit $STATUS
'
fi
npx \
--package=markdown-link-check \
--call='{{.CHECK_LINKS_COMMAND}}'
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
markdown:fix:
Expand Down

0 comments on commit 7530cab

Please sign in to comment.