-
Notifications
You must be signed in to change notification settings - Fork 0
171 lines (144 loc) · 5.89 KB
/
Copy pathbootstrap.yml
File metadata and controls
171 lines (144 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# .github/workflows/bootstrap.yml
name: Bootstrap (self-destruct)
on:
create: {}
push:
branches: [ main ]
permissions:
contents: write
jobs:
run-once:
if: github.repository != 'evmckinney9/python-template'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Set up Python (for pre-commit + replacement script)
uses: actions/setup-python@v6
with:
python-version: '3.12'
cache: pip
cache-dependency-path: |
**/pyproject.toml
- name: Install format deps (optional)
run: pip install '.[format]' || true
- name: Compute replacement variables
id: vars
shell: bash
run: |
# Prefer owner.name if available; fall back to owner.login
AUTHOR_NAME='${{ github.event.repository.owner.name }}'
if [ -z "$AUTHOR_NAME" ] || [ "$AUTHOR_NAME" = "null" ]; then
AUTHOR_NAME='${{ github.event.repository.owner.login }}'
fi
PROJECT_NAME='${{ github.event.repository.name }}'
PROJECT_DESC='${{ github.event.repository.description }}'
GITHUB_USERNAME='${{ github.event.repository.owner.login }}'
echo "author_name=$AUTHOR_NAME" >> "$GITHUB_OUTPUT"
echo "project_name=$PROJECT_NAME" >> "$GITHUB_OUTPUT"
echo "project_description=$PROJECT_DESC" >> "$GITHUB_OUTPUT"
echo "github_username=$GITHUB_USERNAME" >> "$GITHUB_OUTPUT"
- name: In-place placeholder replacement (text files only)
env:
AUTHOR_NAME: ${{ steps.vars.outputs.author_name }}
PROJECT_NAME: ${{ steps.vars.outputs.project_name }}
PROJECT_DESC: ${{ steps.vars.outputs.project_description }}
GITHUB_USER: ${{ steps.vars.outputs.github_username }}
run: |
python - <<'PY'
import os, sys, subprocess, pathlib
# Placeholders and replacements (raw strings, any characters allowed)
repl = {
"{{author_name}}": os.environ.get("AUTHOR_NAME", "") or "",
"{{project_name}}": os.environ.get("PROJECT_NAME", "") or "",
"{{project_description}}": os.environ.get("PROJECT_DESC", "") or "",
"{{github_username}}": os.environ.get("GITHUB_USER", "") or "",
}
# Get tracked files
files = subprocess.check_output(["git", "ls-files", "-z"]).split(b"\x00")
root = pathlib.Path(".").resolve()
def is_text(p: pathlib.Path) -> bool:
try:
b = p.read_bytes()
# Heuristic: treat files with NUL byte as binary
return b"\x00" not in b
except Exception:
return False
# Avoid modifying this workflow as we are about to delete it anyway
skip_paths = {
".github/workflows/bootstrap.yml",
}
for bpath in files:
if not bpath:
continue
rel = bpath.decode("utf-8", "ignore")
if not rel or rel in skip_paths:
continue
p = root / rel
# Skip obviously binary-ish extensions
if p.suffix.lower() in {".png", ".jpg", ".jpeg", ".gif", ".pdf", ".svg", ".ico", ".woff", ".woff2", ".ttf"}:
continue
if not p.exists() or not p.is_file():
continue
if not is_text(p):
continue
try:
s = p.read_text(encoding="utf-8")
except UnicodeDecodeError:
# Skip files that fail utf-8 decoding
continue
original = s
for k, v in repl.items():
s = s.replace(k, v)
if s != original:
p.write_text(s, encoding="utf-8")
print(f"[renamed] {rel}")
PY
- name: Rename package directory if present
env:
PROJECT_NAME: ${{ steps.vars.outputs.project_name }}
run: |
sanitize() {
# Lowercase, replace non [a-z0-9_] with underscore, strip leading digits/underscores
echo "$1" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_]+/_/g; s/^[_0-9]+//; s/_+/_/g; s/_$//'
}
SRC_DIR="src"
OLD="project_name"
if [ -d "$SRC_DIR/$OLD" ]; then
NEW=$(sanitize "$PROJECT_NAME")
if [ -z "$NEW" ]; then
echo "Sanitized project name empty; skipping rename."
elif [ "$NEW" = "$OLD" ]; then
echo "Sanitized name equals existing; skipping rename."
else
echo "Renaming $SRC_DIR/$OLD -> $SRC_DIR/$NEW"
git mv "$SRC_DIR/$OLD" "$SRC_DIR/$NEW"
fi
else
echo "No $SRC_DIR/$OLD directory; skipping package rename."
fi
- name: Replace README template if present
run: |
if [ -f template-README.md ]; then
cp template-README.md README.md
rm -f template-README.md
fi
- name: Promote artifact template if present
run: |
if [ -f template-ARTIFACT.md ]; then
mv template-ARTIFACT.md ARTIFACT.md
fi
- name: Remove legacy template flag if present (harmless if absent)
run: rm -f .github/template_flag.yml || true
- name: Run pre-commit (one-time)
run: pre-commit run --all-files || true
- name: Self-destruct (delete this workflow)
run: rm -f .github/workflows/bootstrap.yml
- name: Commit & push
run: |
set -e
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "Bootstrap: initialize project and self-destruct" || echo "Nothing to commit"
git push