Add shellchecker script task for VSCode workspace

This commit is contained in:
2024-11-07 18:28:58 -06:00
parent c9d23e3c73
commit 840787f8c7
2 changed files with 100 additions and 0 deletions

42
.vscode/shellchecker.sh vendored Executable file
View File

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

58
.vscode/tasks.json vendored Normal file
View File

@@ -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
},
}
]
}