Files
git.taskfile.build/task
T
Janik von Rotz fc09e848fd feat: enhance shellcheck integration and improve code quality across task files
- Added new prompt file `prompts/07_run-shellcheck-and-fix-issues.md` for shellcheck linting and fixing
- Updated `help()` function in `task`, `task.template`, and `README.md` to use `local grep_pattern` variable for improved readability and maintainability
- Updated `lint()` function in `task` to run `shellcheck` on both `task` and `task.template` files
- Added `export` statements for `BUILDX_PLATFORM`, `CONTAINER_REGISTRY`, `CONTAINER_TAG`, and `DOCKERFILE` variables in `task` script
- Added `# shellcheck disable=SC1090` comment before sourcing files in `task` and `task.template` to suppress shellcheck warnings
- Updated `update-template()` function in `task` to use `local temp_file` declaration before assignment for better variable scoping
- Updated `help()` function in `task.template` to use `help "$@"` instead of `help` when displaying unknown command error
- Removed `disable=SC2059` from `.shellcheckrc` as it's no longer needed after code improvements
2026-02-14 11:36:44 +01:00

342 lines
9.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
help-table() {
local -a rows
local max_cmd=7 max_opt=6 max_desc=11
rows+=("Command|Option|Description")
rows+=("all||Run all tasks.")
rows+=("build-11ty||Build the 11ty website.")
rows+=("build-image|[output]|Build Docker image with buildx.")
rows+=("dev-11ty||Run 11ty server.")
rows+=("help|[grep]|Show help for commands.")
rows+=("install||Install Node packages.")
rows+=("lint||Lint code with prettier.")
rows+=("run||Run Docker image.")
rows+=("run-tests||Test with shellcheck.")
rows+=("serve-11ty||Serve the 11ty output folder.")
rows+=("setup-completion|[shell]|Show shell completion instructions.")
rows+=("update-template||Update task.template from library.md.")
rows+=("version||Show version of required tools.")
for row in "${rows[@]}"; do
IFS='|' read -r cmd opt desc <<< "$row"
(( ${#cmd} > max_cmd )) && max_cmd=${#cmd}
(( ${#opt} > max_opt )) && max_opt=${#opt}
(( ${#desc} > max_desc )) && max_desc=${#desc}
done
printf '| %-*s | %-*s | %-*s |\n' \
"$max_cmd" "Command" \
"$max_opt" "Option" \
"$max_desc" "Description"
printf '|-%*s-|-%*s-|-%*s-|\n' \
"$max_cmd" "$(printf '%*s' "$max_cmd" '' | tr ' ' '-')" \
"$max_opt" "$(printf '%*s' "$max_opt" '' | tr ' ' '-')" \
"$max_desc" "$(printf '%*s' "$max_desc" '' | tr ' ' '-')"
for row in "${rows[@]:1}"; do
IFS='|' read -r cmd opt desc <<< "$row"
printf '| %-*s | %-*s | %-*s |\n' \
"$max_cmd" "$cmd" \
"$max_opt" "$opt" \
"$max_desc" "$desc"
done
}
help() {
local grep_pattern
echo
if [[ -n "$1" ]]; then
grep_pattern="$1"
help-table | grep -i "$grep_pattern" | column -t -s'|'
else
echo "task <command> [options]"
echo
echo "commands:"
echo
help-table
fi
echo
}
clone-taskfile(){
if [[ ! -d "$HOME/taskfile.build" ]]; then
echo -e '\033[38;5;214mGit\033[0m: Clone taskfile repo'
git clone https://git.taskfile.build "$HOME/taskfile.build"
else
echo -e '\033[38;5;214mGit\033[0m: Pull taskfile repo'
git -C "$HOME/taskfile.build" pull
fi
}
if [[ -d "$HOME/taskfile.build/bin" ]]; then
for file in "$HOME/taskfile.build/bin/"*; do
if [[ -f "$file" ]]; then
# shellcheck disable=SC1090
source "$file"
fi
done
fi
# Static vars
BUILDX_PLATFORM="linux/amd64"
CONTAINER_REGISTRY="janikvonrotz"
CONTAINER_TAG="taskfile.build:latest"
DOCKERFILE="image/Dockerfile"
# Export variables for external use
export BUILDX_PLATFORM
export CONTAINER_REGISTRY
export CONTAINER_TAG
export DOCKERFILE
# Project commands
run() {
echo "Open http://localhost:8080 in your browser"
docker run -p 8080:80 \
-e GIT_URL="https://git.taskfile.build" \
-e TASK_COMMANDS="install,build-11ty" \
-e CADDY_ROOT="_site" \
janikvonrotz/taskfile.build:latest
}
install() {
pnpm install
}
dev-11ty() {
open-with-os 'http://localhost:8080/' & pnpm dlx @11ty/eleventy --serve
}
lint() {
pnpm exec prettier . --write
shellcheck task task.template
}
build-11ty() {
pnpm dlx @11ty/eleventy
}
serve-11ty() {
pnpx serve ./_site
}
version() {
echo "pnpm: $(pnpm --version)"
echo "eleventy: $(pnpm dlx @11ty/eleventy --version)"
echo "shellcheck: $(shellcheck --version)"
}
setup-completion() {
local shell_type="$1"
# Set default shell type if not provided
if [[ -z "$shell_type" ]]; then
shell_type="bash" # Default to bash
fi
# Validate shell type
case "$shell_type" in
bash|zsh|fish)
# Valid shell type
;;
*)
echo "Error: Unsupported shell type '$shell_type'. Use 'bash', 'zsh', or 'fish'."
echo "Usage: task setup-completion [bash|zsh|fish]"
return 1
;;
esac
# Output the actual completion script
output_completion_script "$shell_type"
# Add instructions as comments at the end
echo ""
echo "# ============================================"
echo "# Taskfile completion setup instructions"
echo "# ============================================"
echo "# To enable completion, add this script to your shell configuration:"
if [[ "$shell_type" == "fish" ]]; then
echo "# task setup-completion fish >> ~/.config/fish/config.fish"
echo "# source ~/.config/fish/config.fish"
elif [[ "$shell_type" == "zsh" ]]; then
echo "# task setup-completion zsh >> ~/.zshrc"
echo "# source ~/.zshrc"
else
echo "# task setup-completion bash >> ~/.bashrc"
echo "# source ~/.bashrc"
fi
echo "# ============================================"
}
output_completion_script() {
local shell_type="$1"
# Default to bash if no shell type provided
if [[ -z "$shell_type" ]]; then
shell_type="bash"
fi
if [[ "$shell_type" == "fish" ]]; then
# Fish completion script
cat << 'EOF'
# Taskfile completion for fish
function _task_completions
# Get the current word being typed
set cur (commandline -p)
# Get the previous word
set prev (commandline -oprev)
# Get the available commands from `task help`
set commands (./task help | grep -A100 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$' | tr '\n' ' ')
if test (count (commandline -opc)) -eq 1
# Provide completions for the command part (second word)
complete -c task -f -n "__fish_seen_subcommand_from $prev" -a "$commands"
else if test (count (commandline -opc)) -eq 2
# Provide file path completions for the third parameter
complete -c task -f -n "__fish_seen_subcommand_from $prev" -a (ls)
end
end
EOF
elif [[ "$shell_type" == "zsh" ]]; then
# ZSH completion script
cat << 'EOF'
# Taskfile completion for zsh
_task_completions() {
local reply
local si=$IFS
IFS=$'
' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" ./task help | grep -A999 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$' | tr '
' ' '))
IFS=$si
if [[ ${#reply} -gt 0 ]]; then
_describe 'values' reply
else
_default
fi
}
if [[ "${zsh_eval_context[-1]}" == "loadautofunc" ]]; then
_task_completions "$@"
else
compdef _task_completions task
compdef _task_completions t
fi
EOF
else
# Bash completion script
cat << 'EOF'
# Taskfile completion for bash
_task_completions() {
local cur prev commands
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands=$(./task help | grep -A999 -e '---' | awk '{print $2}' | sed 's/^[[:space:]]*//' | grep -v '^$' | tr '\n' ' ')
if [[ ${COMP_CWORD} == 1 ]]; then
COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
elif [[ ${COMP_CWORD} == 2 ]]; then
COMPREPLY=( $(compgen -f -- "${cur}") )
else
COMPREPLY=()
fi
}
complete -F _task_completions task
complete -F _task_completions t
EOF
fi
}
update-template() {
echo "Updating task.template from library.md"
# Extract all rows from library.md that match the pattern rows+=("...")
local -a new_rows
while IFS= read -r line; do
if [[ $line =~ rows\+=\(\"(.*)\"\) ]]; then
new_rows+=("${BASH_REMATCH[1]}")
fi
done < library.md
# Create a temporary file
local temp_file
temp_file=$(mktemp)
# Read the task.template file and update the rows section
local in_rows_section=false
local found_header=false
while IFS= read -r line || [[ -n "$line" ]]; do
# Check if we're at the header row
if [[ $line == *"rows+=(\"Command|Option|Description\")"* ]]; then
echo "$line" >> "$temp_file"
found_header=true
in_rows_section=true
# Add all the new rows after the header
for row in "${new_rows[@]}"; do
echo " rows+=(\"$row\")" >> "$temp_file"
done
continue
fi
# Check if we're at the for-loop that ends the rows section
if [[ $line == *"for row in \"\${rows[@]}\""* ]]; then
in_rows_section=false
fi
# Skip existing rows between header and for-loop
if $in_rows_section && $found_header; then
if [[ $line =~ rows\+=\(\"(.*)\"\) ]]; then
continue
fi
fi
echo "$line" >> "$temp_file"
done < task.template
# Replace task.template with the updated content
mv "$temp_file" task.template
echo "task.template updated successfully"
}
if [[ "$1" == *","* ]]; then
# Split comma-separated commands
IFS=',' read -ra COMMANDS <<< "$1"
for cmd in "${COMMANDS[@]}"; do
# Trim whitespace
cmd=$(echo "$cmd" | xargs)
if declare -f "$cmd" > /dev/null; then
"$cmd"
else
echo "Unknown command: $1"
help "$@"
exit 1
fi
done
elif declare -f "$1" > /dev/null; then
"$1" "${@:2}"
else
case "$1" in
all)
install
build-11ty
;;
*)
echo "Unknown command: $1"
help
exit 1
;;
esac
fi