Files
git.taskfile.build/task
T
2026-07-15 21:22:30 +02:00

246 lines
6.6 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-container||Run Docker image.")
rows+=("run-tests||Test with shellcheck.")
rows+=("serve-11ty||Serve the 11ty output folder.")
rows+=("update-install-script||Upate install script.")
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 <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="mintsystem"
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-container() {
container_name="taskfile-build"
image_tag="mintsystem/taskfile.build:latest"
docker pull "$image_tag"
docker rm -f "$container_name"
docker volume rm "$container_name"
echo "Open http://localhost:8080 in your browser"
docker run -p 8080:80 -d \
--name "$container_name" \
-e GIT_URL="https://git.taskfile.build" \
-e TASK_COMMANDS="install,build-11ty" \
-e CADDY_ROOT="_site" \
-v "$container_name":/app \
"$image_tag"
}
install() {
CI=true 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)"
}
update-install-script() {
echo "Updating install script with bin commands."
local scripts_list
scripts_list=$(ls -1 bin | sed '/install/d' | sort)
local install_temp_file
install_temp_file=$(mktemp)
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
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