Skip to content

Commit bd82871

Browse files
committed
Add rules for linting shell scripts
1 parent e13824b commit bd82871

3 files changed

Lines changed: 131 additions & 1 deletion

File tree

tools/make/lib/lint/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ include $(TOOLS_MAKE_LIB_DIR)/lint/markdown/Makefile
2828
include $(TOOLS_MAKE_LIB_DIR)/lint/package_json.mk
2929
include $(TOOLS_MAKE_LIB_DIR)/lint/python/Makefile
3030
include $(TOOLS_MAKE_LIB_DIR)/lint/r/Makefile
31+
include $(TOOLS_MAKE_LIB_DIR)/lint/shell/Makefile
3132

3233

3334
# RULES #
@@ -43,7 +44,7 @@ include $(TOOLS_MAKE_LIB_DIR)/lint/r/Makefile
4344
# @example
4445
# make lint
4546
#/
46-
lint: lint-filenames lint-javascript lint-markdown lint-python lint-julia lint-r lint-conf lint-pkg-json lint-license-headers
47+
lint: lint-filenames lint-javascript lint-markdown lint-python lint-julia lint-r lint-shell lint-conf lint-pkg-json lint-license-headers
4748

4849
.PHONY: lint
4950

tools/make/lib/lint/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,44 @@ This command is useful when wanting to lint a list of R files generated by some
528528

529529
* * *
530530

531+
### Shell
532+
533+
#### lint-shell
534+
535+
Lints shell script files.
536+
537+
<!-- run-disable -->
538+
539+
```bash
540+
$ make lint-shell
541+
```
542+
543+
The command supports the following environment variables:
544+
545+
- **SHELL_FILTER**: file path pattern; e.g., `.*/_tools/.*`.
546+
- **FAST_FAIL**: flag indicating whether to stop linting upon encountering a lint error.
547+
548+
This command is useful when wanting to glob for shell script files (e.g., lint all shell scripts files in a particular directory).
549+
550+
#### lint-shell-files
551+
552+
Lints a specified list of shell script files.
553+
554+
<!-- run-disable -->
555+
556+
```bash
557+
$ make lint-shell-files FILES='/foo/file.sh /bar/file.sh'
558+
```
559+
560+
The command supports the following environment variables:
561+
562+
- **FILES**: list of shell script files.
563+
- **FAST_FAIL**: flag indicating whether to stop linting upon encountering a lint error.
564+
565+
This command is useful when wanting to lint a list of shell script files generated by some other command (e.g., a list of changed shell script files obtained via `git diff`).
566+
567+
* * *
568+
531569
### Markdown
532570

533571
#### lint-markdown

tools/make/lib/lint/shell/Makefile

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2018 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
# VARIABLES #
20+
21+
# Define the shell script linter:
22+
SHELL_LINTER ?= $(SHELLCHECK)
23+
24+
# Define the command-line options to be used when invoking the executable:
25+
SHELL_LINTER_FLAGS ?=
26+
27+
28+
# RULES #
29+
30+
#/
31+
# Lints shell script files.
32+
#
33+
# ## Notes
34+
#
35+
# - This rule is useful when wanting to glob for files, irrespective of context, for a particular directory in order to lint all contained shell script files.
36+
#
37+
# @param {string} [SHELL_FILTER] - file path pattern (e.g., `.*/_tools/.*`)
38+
# @param {*} [FAST_FAIL] - flag indicating whether to stop linting upon encountering a lint error
39+
#
40+
# @example
41+
# make lint-shell
42+
#
43+
# @example
44+
# make lint-shell SHELL_FILTER=.*/_tools/.*
45+
#/
46+
lint-shell:
47+
ifeq ($(FAIL_FAST), true)
48+
$(QUIET) $(FIND_SHELL_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
49+
echo ''; \
50+
echo "Linting file: $$file"; \
51+
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || exit 1; \
52+
done
53+
else
54+
$(QUIET) $(FIND_SHELL_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
55+
echo ''; \
56+
echo "Linting file: $$file"; \
57+
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || echo 'Linting failed.'; \
58+
done
59+
endif
60+
61+
.PHONY: lint-shell
62+
63+
#/
64+
# Lints a specified list of shell script files.
65+
#
66+
# ## Notes
67+
#
68+
# - This rule is useful when wanting to lint a list of shell script files generated by some other command (e.g., a list of changed shell script files obtained via `git diff`).
69+
#
70+
# @param {string} FILES - list of shell script file paths
71+
# @param {*} [FAST_FAIL] - flag indicating whether to stop linting upon encountering a lint error
72+
#
73+
# @example
74+
# make lint-shell-files FILES='/foo/file.sh /bar/file.sh'
75+
#/
76+
lint-shell-files:
77+
ifeq ($(FAIL_FAST), true)
78+
$(QUIET) for file in $(FILES); do \
79+
echo ''; \
80+
echo "Linting file: $$file"; \
81+
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || exit 1; \
82+
done
83+
else
84+
$(QUIET) for file in $(FILES); do \
85+
echo ''; \
86+
echo "Linting file: $$file"; \
87+
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || echo 'Linting failed.'; \
88+
done
89+
endif
90+
91+
.PHONY: lint-shell-files

0 commit comments

Comments
 (0)