-
-
Notifications
You must be signed in to change notification settings - Fork 0
241 lines (200 loc) · 9.21 KB
/
Copy pathsetup-from-template.yml
File metadata and controls
241 lines (200 loc) · 9.21 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
name: Initialize from template
# This workflow runs once when a new repo is created from this template.
# It replaces $PLACEHOLDER variables with values derived from the repo,
# commits the changes, then deletes itself.
on:
push:
branches: [main]
permissions:
contents: write
jobs:
setup:
name: Replace template placeholders
runs-on: ubuntu-latest
# Only run if this is NOT the template repo itself
if: github.repository != 'DazzleTools/git-repokit-template'
steps:
- uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Derive project variables
id: vars
run: |
# Extract from repo name: DazzleTools/my-cool-tool -> my-cool-tool
REPO_NAME="${{ github.event.repository.name }}"
REPO_OWNER="${{ github.repository_owner }}"
REPO_FULL="${{ github.repository }}"
# Convert project-name to package_name (hyphens to underscores)
PACKAGE_NAME=$(echo "$REPO_NAME" | tr '-' '_')
# Default CLI command (same as package name)
CLI_CMD=$(echo "$REPO_NAME" | tr '_' '-')
# Get owner info
AUTHOR="${{ github.actor }}"
AUTHOR_EMAIL="${AUTHOR}@users.noreply.github.com"
echo "PROJECT_NAME=$REPO_NAME" >> $GITHUB_OUTPUT
echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_OUTPUT
echo "GITHUB_ORG=$REPO_OWNER" >> $GITHUB_OUTPUT
echo "GITHUB_USER=$AUTHOR" >> $GITHUB_OUTPUT
echo "AUTHOR_EMAIL=$AUTHOR_EMAIL" >> $GITHUB_OUTPUT
echo "CLI_COMMAND=$CLI_CMD" >> $GITHUB_OUTPUT
echo "REPO_URL=https://github.com/$REPO_FULL" >> $GITHUB_OUTPUT
echo "Derived variables:"
echo " PROJECT_NAME=$REPO_NAME"
echo " PACKAGE_NAME=$PACKAGE_NAME"
echo " GITHUB_ORG=$REPO_OWNER"
echo " GITHUB_USER=$AUTHOR"
echo " CLI_COMMAND=$CLI_CMD"
- name: Replace placeholders in all files
env:
PROJECT_NAME: ${{ steps.vars.outputs.PROJECT_NAME }}
PACKAGE_NAME: ${{ steps.vars.outputs.PACKAGE_NAME }}
GITHUB_ORG: ${{ steps.vars.outputs.GITHUB_ORG }}
GITHUB_USER: ${{ steps.vars.outputs.GITHUB_USER }}
AUTHOR_EMAIL: ${{ steps.vars.outputs.AUTHOR_EMAIL }}
CLI_COMMAND: ${{ steps.vars.outputs.CLI_COMMAND }}
DESCRIPTION: "A new project created from git-repokit-template"
run: |
# Find all text files (exclude .git/ and binary files)
find . -type f \
-not -path './.git/*' \
-not -path './.github/workflows/*' \
-not -name '*.png' -not -name '*.jpg' -not -name '*.gif' \
-not -name '*.ico' -not -name '*.woff*' \
| while read file; do
# Only process text files
if file "$file" | grep -q text; then
sed -i \
-e "s|\\\$PROJECT_NAME|${PROJECT_NAME}|g" \
-e "s|\\\$PACKAGE_NAME|${PACKAGE_NAME}|g" \
-e "s|\\\$GITHUB_ORG|${GITHUB_ORG}|g" \
-e "s|\\\$GITHUB_USER|${GITHUB_USER}|g" \
-e "s|\\\$AUTHOR_EMAIL|${AUTHOR_EMAIL}|g" \
-e "s|\\\$CLI_COMMAND|${CLI_COMMAND}|g" \
-e "s|\\\$DESCRIPTION|${DESCRIPTION}|g" \
"$file"
fi
done
- name: Create package directory
env:
PACKAGE_NAME: ${{ steps.vars.outputs.PACKAGE_NAME }}
run: |
# Create the Python package directory with basic files
mkdir -p "$PACKAGE_NAME"
cat > "$PACKAGE_NAME/__init__.py" << PYEOF
"""${PACKAGE_NAME} - A new project."""
from ._version import __version__, __app_name__, PIP_VERSION
PYEOF
# Trim leading whitespace from heredoc
sed -i 's/^ //' "$PACKAGE_NAME/__init__.py"
cat > "$PACKAGE_NAME/__main__.py" << PYEOF
"""Allow running as: python -m ${PACKAGE_NAME}"""
from .cli import main
if __name__ == "__main__":
main()
PYEOF
sed -i 's/^ //' "$PACKAGE_NAME/__main__.py"
- name: Create version module
env:
PACKAGE_NAME: ${{ steps.vars.outputs.PACKAGE_NAME }}
PROJECT_NAME: ${{ steps.vars.outputs.PROJECT_NAME }}
run: |
cat > "$PACKAGE_NAME/_version.py" << 'PYEOF'
"""
Version information for $PROJECT_NAME.
This file is the canonical source for version numbers.
The __version__ string is automatically updated by git hooks
with build metadata (branch, build number, date, commit hash).
Format: MAJOR.MINOR.PATCH[-PHASE]_BRANCH_BUILD-YYYYMMDD-COMMITHASH
Example: 0.1.0_main_1-20260101-a1b2c3d4
Version levels:
PROJECT_PHASE: Global project maturity (prealpha -> alpha -> beta -> stable).
Changes rarely, when the overall project hits a threshold.
PHASE: Per-MINOR feature set maturity (alpha -> beta -> "" for stable).
Drops when a MINOR's feature set is complete.
"""
# Version components - edit these for version bumps
MAJOR = 0
MINOR = 1
PATCH = 0
PHASE = "" # Per-MINOR feature set: "" (stable), "alpha", "beta", "rc1", etc.
# Project-level phase (independent of version phase)
PROJECT_PHASE = "" # "prealpha", "alpha", "beta", "stable", or ""
# Auto-updated by git hooks - do not edit manually
__version__ = "0.1.0"
__app_name__ = "$PROJECT_NAME"
def get_version():
"""Return the full version string including branch and build info."""
return __version__
def get_base_version():
"""Return the semantic version string (MAJOR.MINOR.PATCH[-PHASE])."""
if "_" in __version__:
return __version__.split("_")[0]
base = f"{MAJOR}.{MINOR}.{PATCH}"
if PHASE:
base = f"{base}-{PHASE}"
return base
def get_display_version():
"""Return a human-friendly version string with project phase.
Example: 'PREALPHA 0.1.0-alpha' or 'BETA 0.5.1' or '1.0.0'
"""
base = get_base_version()
if PROJECT_PHASE and PROJECT_PHASE != "stable":
return f"{PROJECT_PHASE.upper()} {base}"
return base
def get_pip_version():
"""
Return PEP 440 compliant version for pip/setuptools.
Converts our version format to PEP 440:
- Main branch: 0.1.0_main_3-20260404-hash -> 0.1.0
- Dev branch: 0.1.0_dev_3-20260404-hash -> 0.1.0.dev3
- Alpha: 0.1.0-alpha_main_3 -> 0.1.0a0
"""
base = f"{MAJOR}.{MINOR}.{PATCH}"
# Map phase to PEP 440 pre-release segment
phase_map = {"alpha": "a0", "beta": "b0"}
if PHASE:
base += phase_map.get(PHASE, PHASE)
if "_" not in __version__:
return base
parts = __version__.split("_")
branch = parts[1] if len(parts) > 1 else "unknown"
if branch == "main":
return base
else:
build_info = "_".join(parts[2:]) if len(parts) > 2 else ""
build_num = build_info.split("-")[0] if "-" in build_info else "0"
return f"{base}.dev{build_num}"
# For convenience in imports
VERSION = get_version()
BASE_VERSION = get_base_version()
PIP_VERSION = get_pip_version()
DISPLAY_VERSION = get_display_version()
PYEOF
# Trim leading whitespace from heredoc and replace placeholder variables
sed -i 's/^ //' "$PACKAGE_NAME/_version.py"
sed -i "s/\$PROJECT_NAME/${PROJECT_NAME}/g" "$PACKAGE_NAME/_version.py"
- name: Remove template-only files
run: |
# Remove the setup workflow (this file) -- it's a one-time action
rm -f .github/workflows/setup-from-template.yml
# Remove the archetype variant that wasn't chosen
# (Keep .pypi by default -- user can switch manually)
if [ -f pyproject.toml.pypi ]; then
mv pyproject.toml.pypi pyproject.toml
fi
rm -f pyproject.toml.comfyui
# Remove the template variables reference from README
# (the "Template Variables" section at the bottom)
sed -i '/^---$/,/^```$/d' README.md 2>/dev/null || true
- name: Commit changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "feat: initialize project from git-repokit-template
Auto-populated template placeholders with:
PROJECT_NAME=${{ steps.vars.outputs.PROJECT_NAME }}
PACKAGE_NAME=${{ steps.vars.outputs.PACKAGE_NAME }}
GITHUB_ORG=${{ steps.vars.outputs.GITHUB_ORG }}
CLI_COMMAND=${{ steps.vars.outputs.CLI_COMMAND }}"
git push