Skip to content

Commit 7ef0be0

Browse files
committed
Simplify file discovery code in markdown:check-links task
Since projects often contain many Markdown files and new files may be added or the paths of the existing files changed frequently, the best approach for validating Markdown files is to search the project file tree recursively for all Markdown files, with exclusions configured for the paths of any externally maintained files. The `markdown:check-links` task uses the markdown-link-check tool. This tool does not have a capability for discovering Markdown files so it is necessary to use the `find` command to discover the files, then pass their paths to the markdown-link-check tool. Previously the discovery code used `find` to generate an array of paths, which was iterated over passed individually to markdown-link-check in a `for` loop. The `for` loop is unnecessary because `find` has an `-exec` flag that can be used to execute commands using the discovered paths. Although the syntax and behavior of this flag is unintuitive, these disadvantages that come from its use are outweighed by the benefits of the significant amount of code that can be replaced by it. Since the `-exec`/`-execdir` flags are already in use in the assets and project infrastructure, the maintainer will be forced to work with them regardless.
1 parent 4eb4e89 commit 7ef0be0

File tree

2 files changed

+50
-72
lines changed

2 files changed

+50
-72
lines changed

Taskfile.yml

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -638,50 +638,39 @@ tasks:
638638
echo "Please install: https://github.com/tcort/markdown-link-check#readme"
639639
exit 1
640640
fi
641-
# Default behavior of the task on Windows is to exit the task when the first broken link causes a non-zero
642-
# exit status, but it's better to check all links before exiting.
643-
set +o errexit
644-
STATUS=0
645641
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
646642
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
647643
# \ characters special treatment on Windows in an attempt to support them as path separators.
648-
for file in $(
649-
find . \
650-
-type d -name '.git' -prune -o \
651-
-type d -name '.licenses' -prune -o \
652-
-type d -name '__pycache__' -prune -o \
653-
-type d -name 'node_modules' -prune -o \
654-
-path './{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples' -prune -o \
655-
-path './{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples' -prune -o \
656-
-regex ".*[.]md" -print
657-
); do
658-
markdown-link-check \
659-
--quiet \
660-
--config "./.markdown-link-check.json" \
661-
"$file"
662-
STATUS=$(( $STATUS + $? ))
663-
done
664-
exit $STATUS
665-
else
666-
npx --package=markdown-link-check --call='
667-
STATUS=0
668-
for file in $(
669-
find . \
670-
-type d -name '.git' -prune -o \
671-
-type d -name '.licenses' -prune -o \
672-
-type d -name '__pycache__' -prune -o \
673-
-type d -name 'node_modules' -prune -o \
674-
-path './{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples' -prune -o \
675-
-path './{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples' -prune -o \
676-
-regex ".*[.]md" -print
677-
); do
644+
find . \
645+
-type d -name ".git" -prune -o \
646+
-type d -name ".licenses" -prune -o \
647+
-type d -name "__pycache__" -prune -o \
648+
-type d -name "node_modules" -prune -o \
649+
-path "./{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples" -prune -o \
650+
-path "./{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples" -prune -o \
651+
-regex ".*[.]md" \
652+
-exec \
678653
markdown-link-check \
679654
--quiet \
680655
--config "./.markdown-link-check.json" \
681-
"$file"
682-
STATUS=$(( $STATUS + $? ))
683-
done
684-
exit $STATUS
656+
\{\} \
657+
+
658+
else
659+
npx --package=markdown-link-check --call='
660+
find . \
661+
-type d -name ".git" -prune -o \
662+
-type d -name ".licenses" -prune -o \
663+
-type d -name "__pycache__" -prune -o \
664+
-type d -name "node_modules" -prune -o \
665+
-path "./{{.CLANG_FORMAT_GOLDEN_TEST_DATA_FOLDER}}/samples" -prune -o \
666+
-path "./{{.CLANG_FORMAT_INPUT_TEST_DATA_FOLDER}}/samples" -prune -o \
667+
-regex ".*[.]md" \
668+
-exec \
669+
markdown-link-check \
670+
--quiet \
671+
--config "./.markdown-link-check.json" \
672+
\{\} \
673+
+
685674
'
686675
fi
687676

workflow-templates/assets/check-markdown-task/Taskfile.yml

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,35 @@ tasks:
2323
echo "Please install: https://github.com/tcort/markdown-link-check#readme"
2424
exit 1
2525
fi
26-
# Default behavior of the task on Windows is to exit the task when the first broken link causes a non-zero
27-
# exit status, but it's better to check all links before exiting.
28-
set +o errexit
29-
STATUS=0
3026
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
3127
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
3228
# \ characters special treatment on Windows in an attempt to support them as path separators.
33-
for file in $(
34-
find . \
35-
-type d -name '.git' -prune -o \
36-
-type d -name '.licenses' -prune -o \
37-
-type d -name '__pycache__' -prune -o \
38-
-type d -name 'node_modules' -prune -o \
39-
-regex ".*[.]md" -print
40-
); do
41-
markdown-link-check \
42-
--quiet \
43-
--config "./.markdown-link-check.json" \
44-
"$file"
45-
STATUS=$(( $STATUS + $? ))
46-
done
47-
exit $STATUS
48-
else
49-
npx --package=markdown-link-check --call='
50-
STATUS=0
51-
for file in $(
52-
find . \
53-
-type d -name '.git' -prune -o \
54-
-type d -name '.licenses' -prune -o \
55-
-type d -name '__pycache__' -prune -o \
56-
-type d -name 'node_modules' -prune -o \
57-
-regex ".*[.]md" -print
58-
); do
29+
find . \
30+
-type d -name ".git" -prune -o \
31+
-type d -name ".licenses" -prune -o \
32+
-type d -name "__pycache__" -prune -o \
33+
-type d -name "node_modules" -prune -o \
34+
-regex ".*[.]md" \
35+
-exec \
5936
markdown-link-check \
6037
--quiet \
6138
--config "./.markdown-link-check.json" \
62-
"$file"
63-
STATUS=$(( $STATUS + $? ))
64-
done
65-
exit $STATUS
39+
\{\} \
40+
+
41+
else
42+
npx --package=markdown-link-check --call='
43+
find . \
44+
-type d -name ".git" -prune -o \
45+
-type d -name ".licenses" -prune -o \
46+
-type d -name "__pycache__" -prune -o \
47+
-type d -name "node_modules" -prune -o \
48+
-regex ".*[.]md" \
49+
-exec \
50+
markdown-link-check \
51+
--quiet \
52+
--config "./.markdown-link-check.json" \
53+
\{\} \
54+
+
6655
'
6756
fi
6857

0 commit comments

Comments
 (0)