|
2 | 2 | # /// script
|
3 | 3 | # requires-python = ">=3.13"
|
4 | 4 | # dependencies = [
|
| 5 | +# "bumpver", |
5 | 6 | # "nox",
|
6 | 7 | # ]
|
7 | 8 | # ///
|
|
10 | 11 |
|
11 | 12 | import json
|
12 | 13 | import os
|
| 14 | +import re |
13 | 15 | from pathlib import Path
|
14 | 16 |
|
15 | 17 | import nox
|
| 18 | +from bumpver.version import to_pep440 |
16 | 19 |
|
17 | 20 | nox.options.default_venv_backend = "uv|virtualenv"
|
18 | 21 | nox.options.reuse_existing_virtualenvs = True
|
@@ -150,5 +153,137 @@ def gha_matrix(session):
|
150 | 153 | print(matrix)
|
151 | 154 |
|
152 | 155 |
|
| 156 | +@nox.session |
| 157 | +def cog(session): |
| 158 | + COG_FILES = [ |
| 159 | + "CONTRIBUTING.md", |
| 160 | + "README.md", |
| 161 | + "pyproject.toml", |
| 162 | + ] |
| 163 | + session.run( |
| 164 | + "uv", |
| 165 | + "run", |
| 166 | + "--with", |
| 167 | + "bumpver", |
| 168 | + "--with", |
| 169 | + "cogapp", |
| 170 | + "--with", |
| 171 | + "nox", |
| 172 | + "cog", |
| 173 | + "-r", |
| 174 | + *COG_FILES, |
| 175 | + ) |
| 176 | + git_status = session.run("git", "status", "--porcelain", external=True, silent=True) |
| 177 | + if not any(cog_file in git_status for cog_file in COG_FILES): |
| 178 | + session.log("No changes to documentation files, skipping commit") |
| 179 | + return |
| 180 | + session.run("git", "add", *COG_FILES, external=True) |
| 181 | + session.run( |
| 182 | + "git", |
| 183 | + "commit", |
| 184 | + "-m", |
| 185 | + "auto-regenerate docs using cog", |
| 186 | + external=True, |
| 187 | + silent=True, |
| 188 | + ) |
| 189 | + |
| 190 | + |
| 191 | +@nox.session |
| 192 | +def process_docs(session): |
| 193 | + session.run("uv", "run", "docs/processor.py") |
| 194 | + session.run("git", "add", "docs/", external=True) |
| 195 | + session.run( |
| 196 | + "git", |
| 197 | + "commit", |
| 198 | + "-m", |
| 199 | + "process docs from GHFM to mkdocs-style", |
| 200 | + external=True, |
| 201 | + silent=True, |
| 202 | + ) |
| 203 | + |
| 204 | + |
| 205 | +@nox.session |
| 206 | +def update_changelog(session): |
| 207 | + version = get_version(session) |
| 208 | + |
| 209 | + with open("CHANGELOG.md", "r") as f: |
| 210 | + changelog = f.read() |
| 211 | + |
| 212 | + changelog = changelog.replace("## [Unreleased]", f"## [{version}]", 1) |
| 213 | + changelog = changelog.replace( |
| 214 | + f"## [{version}]", f"## [Unreleased]\n\n## [{version}]" |
| 215 | + ) |
| 216 | + |
| 217 | + repo_url = session.run("git", "remote", "get-url", "origin", silent=True).strip() |
| 218 | + repo_url = repo_url.replace(".git", "") |
| 219 | + |
| 220 | + changelog += f"\n[{version}]: {repo_url}/releases/tag/v{version}" |
| 221 | + changelog = re.sub( |
| 222 | + r"\[unreleased\]: .+", |
| 223 | + f"[unreleased]: {repo_url}/compare/v{version}...HEAD", |
| 224 | + changelog, |
| 225 | + ) |
| 226 | + |
| 227 | + with open("CHANGELOG.md", "w") as f: |
| 228 | + f.write(changelog) |
| 229 | + |
| 230 | + session.run("git", "add", "CHANGELOG.md", external=True) |
| 231 | + session.run( |
| 232 | + "git", |
| 233 | + "commit", |
| 234 | + "-m", |
| 235 | + f"update CHANGELOG for version {version}", |
| 236 | + external=True, |
| 237 | + silent=True, |
| 238 | + ) |
| 239 | + |
| 240 | + |
| 241 | +@nox.session |
| 242 | +def update_uvlock(session): |
| 243 | + version = get_version(session) |
| 244 | + |
| 245 | + session.run("uv", "lock") |
| 246 | + |
| 247 | + git_status = session.run("git", "status", "--porcelain", external=True, silent=True) |
| 248 | + if "uv.lock" not in git_status: |
| 249 | + session.log("No changes to uv.lock, skipping commit") |
| 250 | + return |
| 251 | + |
| 252 | + session.run("git", "add", "uv.lock", external=True) |
| 253 | + session.run( |
| 254 | + "git", |
| 255 | + "commit", |
| 256 | + "-m", |
| 257 | + f"update uv.lock for version {version}", |
| 258 | + external=True, |
| 259 | + silent=True, |
| 260 | + ) |
| 261 | + |
| 262 | + |
| 263 | +def get_version(session): |
| 264 | + command = ["uv", "run", "bumpver", "update", "--dry", "--no-fetch"] |
| 265 | + if session.posargs: |
| 266 | + args = [] |
| 267 | + for arg in session.posargs: |
| 268 | + if arg and arg not in ["--dry", "--no-fetch"]: |
| 269 | + args.extend(arg.split(" ")) |
| 270 | + command.extend(args) |
| 271 | + output = session.run(*command, silent=True) |
| 272 | + match = re.search(r"New Version: (.+)", output) |
| 273 | + return to_pep440(match.group(1)) if match else None |
| 274 | + |
| 275 | + |
| 276 | +@nox.session(requires=["cog", "process_docs", "update_changelog", "update_uvlock"]) |
| 277 | +def release(session): |
| 278 | + command = ["uv", "run", "bumpver", "update"] |
| 279 | + if session.posargs: |
| 280 | + args = [] |
| 281 | + for arg in session.posargs: |
| 282 | + if arg: |
| 283 | + args.extend(arg.split(" ")) |
| 284 | + command.extend(args) |
| 285 | + session.run(*command) |
| 286 | + |
| 287 | + |
153 | 288 | if __name__ == "__main__":
|
154 | 289 | nox.main()
|
0 commit comments