|
| 1 | +name: Template Cleanup |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [main] |
| 5 | +permissions: |
| 6 | + contents: write |
| 7 | +jobs: |
| 8 | + cleanup: |
| 9 | + # Skip on the template repo itself |
| 10 | + if: github.repository != 'axiomantic/template-nim' |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - uses: actions/checkout@v4 |
| 14 | + with: { token: "${{ secrets.GITHUB_TOKEN }}" } |
| 15 | + - name: Sentinel guard |
| 16 | + id: guard |
| 17 | + run: | |
| 18 | + if [ ! -f .github/template-cleanup-marker ]; then |
| 19 | + echo "Marker missing; cleanup already ran. Exiting." |
| 20 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 21 | + fi |
| 22 | + - name: Compute new names |
| 23 | + if: steps.guard.outputs.skip != 'true' |
| 24 | + id: names |
| 25 | + run: | |
| 26 | + REPO_KEBAB="${GITHUB_REPOSITORY#*/}" |
| 27 | + REPO_SNAKE="${REPO_KEBAB//-/_}" |
| 28 | + echo "kebab=$REPO_KEBAB" >> "$GITHUB_OUTPUT" |
| 29 | + echo "snake=$REPO_SNAKE" >> "$GITHUB_OUTPUT" |
| 30 | + - name: Find/replace placeholders |
| 31 | + if: steps.guard.outputs.skip != 'true' |
| 32 | + env: |
| 33 | + KEBAB: ${{ steps.names.outputs.kebab }} |
| 34 | + SNAKE: ${{ steps.names.outputs.snake }} |
| 35 | + run: | |
| 36 | + set -euo pipefail |
| 37 | + # Rename source dir/file |
| 38 | + if [ -d src/project_name ]; then |
| 39 | + git mv src/project_name "src/${SNAKE}" |
| 40 | + fi |
| 41 | + if [ -f project_name.nimble ]; then |
| 42 | + git mv project_name.nimble "${SNAKE}.nimble" |
| 43 | + fi |
| 44 | + # Replace placeholders in tracked text files (python3 — portable across GNU/BSD sed) |
| 45 | + python3 - <<'PY' |
| 46 | + import os, pathlib, subprocess |
| 47 | + snake = os.environ["SNAKE"] |
| 48 | + kebab = os.environ["KEBAB"] |
| 49 | + tracked = subprocess.check_output( |
| 50 | + ["git", "ls-files", "-z"] |
| 51 | + ).split(b"\0") |
| 52 | + for raw in tracked: |
| 53 | + if not raw: |
| 54 | + continue |
| 55 | + p = pathlib.Path(raw.decode()) |
| 56 | + if not p.is_file(): |
| 57 | + continue |
| 58 | + try: |
| 59 | + text = p.read_text(encoding="utf-8") |
| 60 | + except (UnicodeDecodeError, OSError): |
| 61 | + continue # skip binary or unreadable files |
| 62 | + new = text.replace("project_name", snake).replace("project-name", kebab) |
| 63 | + if new != text: |
| 64 | + p.write_text(new, encoding="utf-8") |
| 65 | + PY |
| 66 | + # Remove the marker (idempotency) |
| 67 | + git rm -f .github/template-cleanup-marker |
| 68 | + - name: Commit and push |
| 69 | + if: steps.guard.outputs.skip != 'true' |
| 70 | + run: | |
| 71 | + git config user.name "github-actions[bot]" |
| 72 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 73 | + git add -A |
| 74 | + git commit -m "chore: template cleanup [skip ci]" |
| 75 | + git push |
0 commit comments