|
| 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