From 8efa12cda06adf00ff34eca084228ac3da5b408f Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Tue, 4 Feb 2025 09:25:58 -0500 Subject: [PATCH 1/4] Add shell script linter --- lint-requirements.txt | 1 + taskfile.yaml | 1 + taskfiles/lint-sh.yaml | 66 +++++++++++++++++++ taskfiles/lint.yaml | 7 ++ .../centos-stream-9/centos-script.sh | 3 + tools/scripts/lib_install/general-script.sh | 3 + .../scripts/lib_install/linux/linux-script.sh | 3 + .../scripts/lib_install/macos/macos-script.sh | 3 + 8 files changed, 87 insertions(+) create mode 100644 taskfiles/lint-sh.yaml create mode 100644 tools/scripts/lib_install/centos-stream-9/centos-script.sh create mode 100644 tools/scripts/lib_install/general-script.sh create mode 100644 tools/scripts/lib_install/linux/linux-script.sh create mode 100644 tools/scripts/lib_install/macos/macos-script.sh diff --git a/lint-requirements.txt b/lint-requirements.txt index 94193f3e..03fb1dd6 100644 --- a/lint-requirements.txt +++ b/lint-requirements.txt @@ -2,4 +2,5 @@ clang-format>=19.1.6 clang-tidy>=19.1.0 colorama>=0.4.6 gersemi>=0.16.2 +shellcheck-py>=0.10.0 yamllint>=1.35.1 diff --git a/taskfile.yaml b/taskfile.yaml index 77d6ceea..3b04e6f6 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -9,6 +9,7 @@ vars: G_BUILD_DIR: "{{.ROOT_DIR}}/build" G_CMAKE_CACHE: "{{.G_BUILD_DIR}}/CMakeCache.txt" G_COMPILE_COMMANDS_DB: "{{.G_BUILD_DIR}}/compile_commands.json" + G_SCRIPT_DIR: "{{.ROOT_DIR}}/tools/scripts" # Project-specific variables G_YSTDLIB_CPP_SRC_DIR: "{{.ROOT_DIR}}/src/ystdlib" diff --git a/taskfiles/lint-sh.yaml b/taskfiles/lint-sh.yaml new file mode 100644 index 00000000..60a13b08 --- /dev/null +++ b/taskfiles/lint-sh.yaml @@ -0,0 +1,66 @@ +version: "3" + +tasks: + sh: + # NOTE: shellcheck does not have the ability to fix errors. + aliases: + - "sh-check" + - "sh-fix" + desc: "Runs the shell script linters. Only checks for warnings and violations." + vars: + SHELLCHECK_FLAGS: "--enable=all --severity=style --external-sources --norc" + sources: + - "{{.G_LINT_VENV_CHECKSUM_FILE}}" + - "{{.G_SCRIPT_DIR}}/**/*.sh" + - "{{.TASKFILE}}" + deps: + - "venv" + cmds: + - task: "linux-shellcheck" + vars: + FLAGS: "{{.SHELLCHECK_FLAGS}}" + SRC_PATHS: + ref: ".G_LINT_SH_DIRS" + VENV_DIR: "{{.G_LINT_VENV_DIR}}" + - task: "macos-shellcheck" + vars: + FLAGS: "{{.SHELLCHECK_FLAGS}}" + SRC_PATHS: + ref: ".G_LINT_SH_DIRS" + VENV_DIR: "{{.G_LINT_VENV_DIR}}" + + linux-shellcheck: + internal: true + vars: + EXCLUDES: + - "**/macos" + - "**/centos-stream-9" + requires: + vars: ["FLAGS", "SRC_PATHS", "VENV_DIR"] + platforms: ["linux"] # Requires raw strings and cannot interpret variable substitutions + cmd: |- + . "{{.VENV_DIR}}/bin/activate" + find {{- range .SRC_PATHS}} "{{.}}" {{- end}} \ + \( {{- range $i, $path := .EXCLUDES }}{{if $i}} -o {{end}} -path '{{ $path }}'{{end}} \) \ + -prune -o \ + -type f -iname "*.sh" \ + -print0 | \ + xargs -0 shellcheck {{.FLAGS}} + + macos-shellcheck: + internal: true + vars: + EXCLUDES: + - "**/linux" + - "**/centos-stream-9" + requires: + vars: ["FLAGS", "SRC_PATHS", "VENV_DIR"] + platforms: ["darwin"] + cmd: |- + . "{{.VENV_DIR}}/bin/activate" + find {{- range .SRC_PATHS}} "{{.}}" {{- end}} \ + \( {{- range $i, $path := .EXCLUDES }}{{if $i}} -o {{end}} -path '{{ $path }}'{{end}} \) \ + -prune -o \ + -type f -iname "*.sh" \ + -print0 | \ + xargs -0 shellcheck {{.FLAGS}} diff --git a/taskfiles/lint.yaml b/taskfiles/lint.yaml index 143fa4ce..d2084d4b 100644 --- a/taskfiles/lint.yaml +++ b/taskfiles/lint.yaml @@ -7,11 +7,16 @@ includes: cpp: flatten: true taskfile: "./lint-cpp.yaml" + sh: + flatten: true + taskfile: "./lint-sh.yaml" yaml: flatten: true taskfile: "./lint-yaml.yaml" vars: + G_LINT_SH_DIRS: + - "{{.G_SCRIPT_DIR}}" G_LINT_VENV_DIR: "{{.G_BUILD_DIR}}/lint-venv" G_LINT_VENV_CHECKSUM_FILE: "{{.G_BUILD_DIR}}/lint#venv.md5" G_LINT_CPP_DIRS: @@ -23,6 +28,7 @@ tasks: cmds: - task: "cmake-check" - task: "cpp-check" + - task: "sh-check" - task: "yaml-check" fix: @@ -30,6 +36,7 @@ tasks: cmds: - task: "cmake-fix" - task: "cpp-fix" + - task: "sh-fix" - task: "yaml-fix" venv: diff --git a/tools/scripts/lib_install/centos-stream-9/centos-script.sh b/tools/scripts/lib_install/centos-stream-9/centos-script.sh new file mode 100644 index 00000000..68d4debb --- /dev/null +++ b/tools/scripts/lib_install/centos-stream-9/centos-script.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +source /etc/centos-release diff --git a/tools/scripts/lib_install/general-script.sh b/tools/scripts/lib_install/general-script.sh new file mode 100644 index 00000000..02cf0a19 --- /dev/null +++ b/tools/scripts/lib_install/general-script.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo "Hello world" diff --git a/tools/scripts/lib_install/linux/linux-script.sh b/tools/scripts/lib_install/linux/linux-script.sh new file mode 100644 index 00000000..71fa4d14 --- /dev/null +++ b/tools/scripts/lib_install/linux/linux-script.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +source /etc/os-release diff --git a/tools/scripts/lib_install/macos/macos-script.sh b/tools/scripts/lib_install/macos/macos-script.sh new file mode 100644 index 00000000..7e8325ae --- /dev/null +++ b/tools/scripts/lib_install/macos/macos-script.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +sw_vers From 16551282cbf1f5604200fcfdb89eb02c49516fb8 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Tue, 4 Feb 2025 09:33:45 -0500 Subject: [PATCH 2/4] Clarify testing --- tools/scripts/lib_install/centos-stream-9/centos-script.sh | 1 + tools/scripts/lib_install/linux/linux-script.sh | 1 + tools/scripts/lib_install/macos/macos-script.sh | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/scripts/lib_install/centos-stream-9/centos-script.sh b/tools/scripts/lib_install/centos-stream-9/centos-script.sh index 68d4debb..561de354 100644 --- a/tools/scripts/lib_install/centos-stream-9/centos-script.sh +++ b/tools/scripts/lib_install/centos-stream-9/centos-script.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash +# This should only be lintable on centos. source /etc/centos-release diff --git a/tools/scripts/lib_install/linux/linux-script.sh b/tools/scripts/lib_install/linux/linux-script.sh index 71fa4d14..2d4654b4 100644 --- a/tools/scripts/lib_install/linux/linux-script.sh +++ b/tools/scripts/lib_install/linux/linux-script.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash +# This should only be lintable on linux. source /etc/os-release diff --git a/tools/scripts/lib_install/macos/macos-script.sh b/tools/scripts/lib_install/macos/macos-script.sh index 7e8325ae..44162d8e 100644 --- a/tools/scripts/lib_install/macos/macos-script.sh +++ b/tools/scripts/lib_install/macos/macos-script.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash -sw_vers +# This should only be lintable on macos. +source /Applications From 17534f1263690b959d770cc5e886c33d8ff33159 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Tue, 4 Feb 2025 09:37:38 -0500 Subject: [PATCH 3/4] Fix --- tools/scripts/lib_install/macos/macos-script.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/scripts/lib_install/macos/macos-script.sh b/tools/scripts/lib_install/macos/macos-script.sh index 44162d8e..2dbcfc94 100644 --- a/tools/scripts/lib_install/macos/macos-script.sh +++ b/tools/scripts/lib_install/macos/macos-script.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash # This should only be lintable on macos. -source /Applications +source /System/Library/CoreServices/SystemVersion.plist From 059c02768d9e7e9ed729f60381b87de0f7c228e6 Mon Sep 17 00:00:00 2001 From: Bingran Hu Date: Tue, 4 Feb 2025 09:40:02 -0500 Subject: [PATCH 4/4] Fix --- tools/scripts/lib_install/macos/macos-script.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/scripts/lib_install/macos/macos-script.sh b/tools/scripts/lib_install/macos/macos-script.sh index 2dbcfc94..ac999fbe 100644 --- a/tools/scripts/lib_install/macos/macos-script.sh +++ b/tools/scripts/lib_install/macos/macos-script.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash # This should only be lintable on macos. +# shellcheck disable=SC1094 source /System/Library/CoreServices/SystemVersion.plist