#!/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 ' ' '-')" local first_row=true for row in "${rows[@]:1}"; do IFS='|' read -r cmd opt desc <<< "$row" if [[ "$cmd" =~ ^===.*===$ ]]; then group_name="${cmd/===/}" group_name="${group_name/===/}" if [ "$first_row" = true ]; then first_row=false else 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 ' ' '-')" fi local left_pad=$(( (max_cmd - ${#group_name}) / 2 )) local right_pad=$(( max_cmd - left_pad - ${#group_name} )) printf '| %*s%s%*s | %*s | %*s |\n' \ "$left_pad" "" \ "$group_name" \ "$right_pad" "" \ "$max_opt" "" \ "$max_desc" "" 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 ' ' '-')" else first_row=false printf '| %-*s | %-*s | %-*s |\n' \ "$max_cmd" "$cmd" \ "$max_opt" "$opt" \ "$max_desc" "$desc" fi 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 [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 chmod +x task.template echo "task.template updated successfully" # Update the install script with bin commands echo "Updating install script with bin commands" # Generate the list of bin scripts excluding install local scripts_list scripts_list=$(ls -1 bin | sed '/install/d' | sort) # Create a temporary file for install script local install_temp_file install_temp_file=$(mktemp) # Read the install file and update the scripts section local in_scripts_section=false while IFS= read -r line || [[ -n "$line" ]]; do # Check if we're at the scripts=( if [[ $line == "scripts=(" ]]; then echo "$line" >> "$install_temp_file" in_scripts_section=true # Add all the scripts from bin directory while IFS= read -r script; do echo " $script" >> "$install_temp_file" done <<< "$scripts_list" continue fi # Check if we're at the closing ) if [[ $line == ")" ]] && $in_scripts_section; then in_scripts_section=false fi # Skip existing scripts between scripts=( and ) if $in_scripts_section; then if [[ $line != "scripts=(" ]] && [[ $line != ")" ]]; then continue fi fi echo "$line" >> "$install_temp_file" done < install # Replace install with the updated content mv "$install_temp_file" install chmod +x install echo "install script 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