From 840787f8c749519f0569b1d88c5b97dfd4821ffa Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 7 Nov 2024 18:28:58 -0600 Subject: [PATCH] Add shellchecker script task for VSCode workspace --- .vscode/shellchecker.sh | 42 +++++++++++++++++++++++++++++ .vscode/tasks.json | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100755 .vscode/shellchecker.sh create mode 100644 .vscode/tasks.json diff --git a/.vscode/shellchecker.sh b/.vscode/shellchecker.sh new file mode 100755 index 0000000..77bc8e2 --- /dev/null +++ b/.vscode/shellchecker.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# set -u + +ROOT="$1" +SHELLCHECK_OPTIONS="--color=never --format=gcc" + +# Function to invoke shellcheck or chezmoi execute-template based on file type +invoke_checker() { + filepath="$1" + + # If the file is a .tmpl file, use chezmoi execute-template + if [[ $filepath == *.tmpl ]]; then + RENDERED_TEMPLATE=$(chezmoi execute-template <$filepath) + if [ $? -eq 0 ]; then + echo "$RENDERED_TEMPLATE" | shellcheck - $SHELLCHECK_OPTIONS | sed "s|^-|$filepath|" + fi + else + # Otherwise, use shellcheck directly + shellcheck "$filepath" $SHELLCHECK_OPTIONS + fi +} + +echo "inotify watcher started" + +# Run an initial scan of all shell scripts +while IFS= read -rd $'\0' file; do + invoke_checker "$file" +done < <(find "$ROOT" \( -name "*.sh" -o -name "*.sh.tmpl" \) -type f -print0) + +echo "inotifywait invoking" +inotifywait -mr --quiet --timefmt '%d/%m/%y %H:%M:%S' --format '%T %w %f' -e modify $1 | + while read -r date time dir file; do + absolute_path=${dir}${file} + + # Check if the changed file ends with .sh or .sh.tmpl + if [[ $absolute_path == *.sh || $absolute_path == *.sh.tmpl ]]; then + invoke_checker $absolute_path + fi + + done + +echo "inotify watcher stopped" diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0a3e679 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,58 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Shellcheck", + "type": "shell", + "isBackground": true, + "command": "${workspaceFolder}/.vscode/shellchecker.sh ${workspaceFolder}", + "problemMatcher": [ + + { + "source": "shellcheck", + "owner": "bash", + "fileLocation": ["autoDetect", "${workspaceFolder}"], + "pattern": { + // info/style joined into 'note' + "regexp": "^(.*):(\\d+):(\\d+):\\s*(error|warning|note):\\s*(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + }, + "background": { + "activeOnStart": true, + "beginsPattern": "^inotify watcher started$", + "endsPattern": "^inotify watcher stopped$" + } + }, + { + "source": "chezmoi", + "owner": "chezmoi", + "fileLocation": ["autoDetect", "${workspaceFolder}"], + "pattern": { + "regexp": "^chezmoi: template: (.*):(\\d+):(\\d+): executing \"stdin\" at <.*>: (.*)$", + "file": 1, + "line": 2, + "column": 3, + "message": 4 + } + } + ], + "runOptions": { + "runOn": "folderOpen" + }, + "presentation": { + "echo": true, + "reveal": "always", + "revealProblems": "never", + "focus": false, + "panel": "dedicated", + "clear": false + }, + } + ] +} \ No newline at end of file