Files
Tools/scripts/generate_readme.sh
HRiggs e0d1a9193f
Some checks failed
Generate README / build (push) Failing after 21s
New tools and deploy
2025-09-28 17:09:17 -04:00

121 lines
3.1 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() {
local origin_url
origin_url="$(git config --get remote.origin.url || true)"
if [[ -z "${origin_url}" ]]; then
echo "Error: could not determine git remote origin URL" >&2
exit 1
fi
local web_base
if [[ "${origin_url}" =~ ^https?:// ]]; then
web_base="${origin_url%.git}"
elif [[ "${origin_url}" =~ ^git@([^:]+):(.+)\.git$ ]]; then
local host path
host="${BASH_REMATCH[1]}"
path="${BASH_REMATCH[2]}"
web_base="https://${host}/${path}"
else
# Fallback: strip trailing .git if present
web_base="${origin_url%.git}"
fi
printf '%s' "${web_base}"
}
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 "$@"