Skip to content

Commit 94acece

Browse files
committed
Create separate rules for explicitly running shellcheck
1 parent b9c8de2 commit 94acece

File tree

3 files changed

+129
-55
lines changed

3 files changed

+129
-55
lines changed

tools/make/common.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ COVERAGE_SERVICE ?= codecov
116116
# Define the linter to use when linting Markdown files:
117117
MARKDOWN_LINTER ?= remark
118118

119+
# Define the linter to use when linting shell script files:
120+
SHELL_LINTER ?= shellcheck
121+
119122

120123
# COMMANDS #
121124

tools/make/lib/lint/shell/Makefile

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,14 @@
1616
# limitations under the License.
1717
#/
1818

19-
# VARIABLES #
19+
# DEPENDENCIES #
2020

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 ?=
21+
# Note: keep in alphabetical order...
22+
include $(TOOLS_MAKE_LIB_DIR)/lint/shell/shellcheck.mk
2623

2724

2825
# RULES #
2926

30-
#/
31-
# Checks whether [shellcheck][1] is installed.
32-
#
33-
# [1]: https://github.com/koalaman/shellcheck
34-
#
35-
# @private
36-
#
37-
# @example
38-
# make check-shellcheck
39-
#/
40-
check-shellcheck:
41-
ifeq (, $(shell command -v $(SHELLCHECK) 2>/dev/null))
42-
$(QUIET) echo ''
43-
$(QUIET) echo 'shellcheck is not installed. Please install shellcheck and try again.'
44-
$(QUIET) echo 'For install instructions, see https://github.com/koalaman/shellcheck'
45-
$(QUIET) echo 'and the project development guide.'
46-
$(QUIET) echo ''
47-
$(QUIET) exit 1
48-
else
49-
$(QUIET) echo 'shellcheck is installed.'
50-
$(QUIET) exit 0
51-
endif
52-
53-
.PHONY: check-shellcheck
54-
5527
#/
5628
# Lints shell script files.
5729
#
@@ -69,18 +41,8 @@ endif
6941
# make lint-shell SHELL_FILTER=.*/_tools/.*
7042
#/
7143
lint-shell:
72-
ifeq ($(FAIL_FAST), true)
73-
$(QUIET) $(FIND_SHELL_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
74-
echo ''; \
75-
echo "Linting file: $$file"; \
76-
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || exit 1; \
77-
done
78-
else
79-
$(QUIET) $(FIND_SHELL_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
80-
echo ''; \
81-
echo "Linting file: $$file"; \
82-
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || echo 'Linting failed.'; \
83-
done
44+
ifeq ($(SHELL_LINTER), shellcheck)
45+
$(QUIET) $(MAKE) -f $(this_file) shellcheck
8446
endif
8547

8648
.PHONY: lint-shell
@@ -99,18 +61,8 @@ endif
9961
# make lint-shell-files FILES='/foo/file.sh /bar/file.sh'
10062
#/
10163
lint-shell-files:
102-
ifeq ($(FAIL_FAST), true)
103-
$(QUIET) for file in $(FILES); do \
104-
echo ''; \
105-
echo "Linting file: $$file"; \
106-
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || exit 1; \
107-
done
108-
else
109-
$(QUIET) for file in $(FILES); do \
110-
echo ''; \
111-
echo "Linting file: $$file"; \
112-
$(SHELL_LINTER) $(SHELL_LINTER_FLAGS) $$file || echo 'Linting failed.'; \
113-
done
64+
ifeq ($(SHELL_LINTER), shellcheck)
65+
$(QUIET) FILES="$(FILES)" $(MAKE) -f $(this_file) shellcheck-files
11466
endif
11567

11668
.PHONY: lint-shell-files
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 command-line options to use when invoking the shellcheck executable:
22+
SHELLCHECK_FLAGS ?=
23+
24+
25+
# RULES #
26+
27+
#/
28+
# Checks whether [shellcheck][1] is installed.
29+
#
30+
# [1]: https://github.com/koalaman/shellcheck
31+
#
32+
# @private
33+
#
34+
# @example
35+
# make check-shellcheck
36+
#/
37+
check-shellcheck:
38+
ifeq (, $(shell command -v $(SHELLCHECK) 2>/dev/null))
39+
$(QUIET) echo ''
40+
$(QUIET) echo 'shellcheck is not installed. Please install shellcheck and try again.'
41+
$(QUIET) echo 'For install instructions, see https://github.com/koalaman/shellcheck'
42+
$(QUIET) echo 'and the project development guide.'
43+
$(QUIET) echo ''
44+
$(QUIET) exit 1
45+
else
46+
$(QUIET) echo 'shellcheck is installed.'
47+
$(QUIET) exit 0
48+
endif
49+
50+
.PHONY: check-shellcheck
51+
52+
#/
53+
# Lints shell script files using [shellcheck][1].
54+
#
55+
# ## Notes
56+
#
57+
# - 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.
58+
#
59+
# [1]: https://github.com/koalaman/shellcheck
60+
#
61+
# @private
62+
# @param {string} [SHELL_FILTER] - file path pattern (e.g., `.*/_tools/.*`)
63+
# @param {*} [FAST_FAIL] - flag indicating whether to stop linting upon encountering a lint error
64+
#
65+
# @example
66+
# make shellcheck
67+
#
68+
# @example
69+
# make shellcheck SHELL_FILTER=.*/_tools/.*
70+
#/
71+
shellcheck:
72+
ifeq ($(FAIL_FAST), true)
73+
$(QUIET) $(FIND_SHELL_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
74+
echo ''; \
75+
echo "Linting file: $$file"; \
76+
$(SHELLCHECK) $(SHELLCHECK_FLAGS) $$file || exit 1; \
77+
done
78+
else
79+
$(QUIET) $(FIND_SHELL_CMD) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
80+
echo ''; \
81+
echo "Linting file: $$file"; \
82+
$(SHELLCHECK) $(SHELLCHECK_FLAGS) $$file || echo 'Linting failed.'; \
83+
done
84+
endif
85+
86+
.PHONY: shellcheck
87+
88+
#/
89+
# Lints a specified list of shell script files using [shellcheck][1].
90+
#
91+
# ## Notes
92+
#
93+
# - 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`).
94+
#
95+
# [1]: https://github.com/koalaman/shellcheck
96+
#
97+
# @private
98+
# @param {string} FILES - list of shell script file paths
99+
# @param {*} [FAST_FAIL] - flag indicating whether to stop linting upon encountering a lint error
100+
#
101+
# @example
102+
# make shellcheck-files FILES='/foo/file.sh /bar/file.sh'
103+
#/
104+
shellcheck-files:
105+
ifeq ($(FAIL_FAST), true)
106+
$(QUIET) for file in $(FILES); do \
107+
echo ''; \
108+
echo "Linting file: $$file"; \
109+
$(SHELLCHECK) $(SHELLCHECK_FLAGS) $$file || exit 1; \
110+
done
111+
else
112+
$(QUIET) for file in $(FILES); do \
113+
echo ''; \
114+
echo "Linting file: $$file"; \
115+
$(SHELLCHECK) $(SHELLCHECK_FLAGS) $$file || echo 'Linting failed.'; \
116+
done
117+
endif
118+
119+
.PHONY: shellcheck-files

0 commit comments

Comments
 (0)