Skip to content

Commit 04adc45

Browse files
authored
Merge pull request #12 from compas-dev/prune
Prune
2 parents 3718dad + 8984ca2 commit 04adc45

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* Added support for `Mkdocs` documentation generation, using `mkdocs.docs` task. (`Mkdocs` is an optional dependency at the moment, but may become a required dependency in the future).
13+
* Added support for pruning old documentation versions with `mkdocs.prune` task.
1314

1415
### Changed
1516

requirements-dev.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ ruff
77
sphinx_compas2_theme
88
twine
99
wheel
10-
tomlkit
1110
build

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tomlkit
2+
semver

src/compas_invocations2/mkdocs.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import json
2+
13
import invoke
4+
import semver
25

36
from compas_invocations2.console import chdir
47

@@ -16,3 +19,50 @@ def docs(ctx, clean=False, verbose=False):
1619

1720
with chdir(ctx.base_folder):
1821
ctx.run("mkdocs build {} {} -d dist/docs".format(clean_flag, verbose_flag))
22+
23+
24+
@invoke.task(
25+
help={
26+
"push": "True to push changes to the remote after pruning, otherwise False.",
27+
"dry": "True to print which versions would be deleted without actually deleting them.",
28+
}
29+
)
30+
def prune_docs(ctx, push=True, dry=False):
31+
"""Prunes deployed doc versions, keeping only the latest patch per minor version.
32+
33+
Fetches the list of deployed versions via mike, groups them by major.minor,
34+
and deletes any patch version that is not the highest in its group.
35+
36+
"""
37+
result = ctx.run("mike list --json", hide=True)
38+
entries = json.loads(result.stdout)
39+
40+
# latest[(major, minor)] = semver.Version — overwritten whenever a higher patch is seen
41+
latest = {}
42+
all_semver = []
43+
for entry in entries:
44+
ver_str = entry["version"]
45+
try:
46+
v = semver.Version.parse(ver_str)
47+
except ValueError:
48+
continue
49+
all_semver.append(ver_str)
50+
key = (v.major, v.minor)
51+
if key not in latest or v.compare(latest[key]) > 0:
52+
latest[key] = v
53+
54+
to_keep = [str(v) for v in latest.values()]
55+
to_delete = [v for v in all_semver if v not in to_keep]
56+
57+
if dry:
58+
print("Keep: {}".format(", ".join(sorted(to_keep)) or "(none)"))
59+
print("Delete: {}".format(", ".join(sorted(to_delete)) or "(none)"))
60+
return
61+
62+
if not to_delete:
63+
print("No old patch versions to prune.")
64+
return
65+
66+
push_flag = "-p" if push else ""
67+
ctx.run(f"mike delete {push_flag} {' '.join(to_delete)}".strip())
68+
print(f"Deleted: {', '.join(to_delete)}")

0 commit comments

Comments
 (0)