Files
Tools/scripts/generate_readme.sh
HRiggs 5545ccaf1f
All checks were successful
Generate README / build (push) Successful in 8s
fugma
2025-12-02 23:22:41 -05:00

102 lines
2.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# Generate README.md with download & run commands for .sh and .bat scripts
# This file is intended to be run in CI and locally.
determine_repo_web_base() {
# Use hardcoded base URL
printf '%s' "https://git.hudsonriggs.systems/HRiggs/Tools"
}
determine_branch() {
# Prefer BRANCH env, fall back to current branch, else main
if [[ -n "${BRANCH:-}" ]]; then
printf '%s' "${BRANCH}"
return
fi
local branch
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
if [[ -z "${branch}" || "${branch}" == "HEAD" ]]; then
branch="main"
fi
printf '%s' "${branch}"
}
list_files() {
# List tracked files matching a glob, excluding scripts/generate_readme.sh
local pattern="$1"
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
# Use git to respect .gitignore and include only tracked files
git ls-files "${pattern}" 2>/dev/null | grep -v '^scripts/generate_readme.sh$' || true
else
find . -type f -name "${pattern}" -not -path './scripts/generate_readme.sh' -print | sed 's|^\./||' || true
fi
}
main() {
local web_base branch raw_base
web_base="$(determine_repo_web_base)"
branch="$(determine_branch)"
raw_base="${web_base}/raw/branch/${branch}"
# Collect files
mapfile -t sh_files < <(list_files '*.sh' | sort)
mapfile -t bat_files < <(list_files '*.bat' | sort)
# Begin README content
{
echo "# tools"
echo
echo "Auto-generated README. Do not edit by hand."
echo
echo "This file is regenerated by \`scripts/generate_readme.sh\` on every push."
echo
if ((${#sh_files[@]} > 0)); then
echo "## Shell (.sh)"
echo
echo "Run on Linux/macOS using curl:"
echo
for f in "${sh_files[@]}"; do
local name url
name="$(basename "${f}")"
url="${raw_base}/${f}"
echo "### ${name}"
echo
echo '```bash'
echo "curl -fsSL -o ${name} \"${url}\" && chmod +x ${name} && ./\"${name}\""
echo '```'
echo
done
fi
if ((${#bat_files[@]} > 0)); then
echo "## Windows (.bat)"
echo
echo "Run from PowerShell:"
echo
for f in "${bat_files[@]}"; do
local name url
name="$(basename "${f}")"
url="${raw_base}/${f}"
echo "### ${name}"
echo
echo '```powershell'
echo '$dest = Join-Path $env:TEMP "'"${name}"'";'
echo 'Invoke-WebRequest -Uri '"\"${url}\""' -OutFile $dest;'
echo '& $dest'
echo '```'
echo
done
fi
echo "---"
echo
echo "Generated from repo: \`${web_base}\` on branch \`${branch}\`."
} > README.md
}
main "$@"