lading 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lading/__init__.py +12 -0
- lading/cli.py +449 -0
- lading/commands/__init__.py +7 -0
- lading/commands/_shared.py +15 -0
- lading/commands/bump.py +574 -0
- lading/commands/bump_docs.py +402 -0
- lading/commands/bump_lockfiles.py +154 -0
- lading/commands/bump_output.py +131 -0
- lading/commands/bump_readme.py +284 -0
- lading/commands/bump_toml.py +253 -0
- lading/commands/cargo_output_adapter.py +217 -0
- lading/commands/lockfile.py +275 -0
- lading/commands/publish.py +746 -0
- lading/commands/publish_diagnostics.py +84 -0
- lading/commands/publish_errors.py +79 -0
- lading/commands/publish_execution.py +52 -0
- lading/commands/publish_index_check.py +394 -0
- lading/commands/publish_manifest.py +206 -0
- lading/commands/publish_plan.py +274 -0
- lading/commands/publish_preflight.py +392 -0
- lading/config.py +441 -0
- lading/exceptions.py +20 -0
- lading/runtime/__init__.py +39 -0
- lading/runtime/runner.py +53 -0
- lading/runtime/subprocess_runner.py +367 -0
- lading/testing/__init__.py +3 -0
- lading/testing/cmd_mox_runner.py +350 -0
- lading/testing/toml_utils.py +239 -0
- lading/utils/__init__.py +8 -0
- lading/utils/commands.py +39 -0
- lading/utils/metrics.py +167 -0
- lading/utils/path.py +32 -0
- lading/utils/process.py +186 -0
- lading/workspace/__init__.py +31 -0
- lading/workspace/metadata.py +157 -0
- lading/workspace/models.py +576 -0
- lading-0.2.0.dist-info/METADATA +68 -0
- lading-0.2.0.dist-info/RECORD +42 -0
- lading-0.2.0.dist-info/WHEEL +5 -0
- lading-0.2.0.dist-info/entry_points.txt +2 -0
- lading-0.2.0.dist-info/licenses/LICENSE +13 -0
- lading-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
"""Documentation processing utilities for version bumping.
|
|
2
|
+
|
|
3
|
+
This module provides utilities for updating version references within Markdown
|
|
4
|
+
documentation files. It scans fenced TOML code blocks (e.g., Cargo.toml
|
|
5
|
+
snippets in README files) and rewrites version entries to match the target
|
|
6
|
+
workspace version.
|
|
7
|
+
|
|
8
|
+
Functions
|
|
9
|
+
---------
|
|
10
|
+
resolve_documentation_targets
|
|
11
|
+
Locate documentation files matching configured glob patterns.
|
|
12
|
+
update_documentation_files
|
|
13
|
+
Rewrite TOML fences in documentation to reflect updated versions.
|
|
14
|
+
rewrite_markdown_toml_fences
|
|
15
|
+
Parse Markdown and transform TOML code blocks.
|
|
16
|
+
update_toml_snippet_versions
|
|
17
|
+
Update version entries within a single TOML snippet.
|
|
18
|
+
|
|
19
|
+
Examples
|
|
20
|
+
--------
|
|
21
|
+
>>> from pathlib import Path
|
|
22
|
+
>>> from lading.commands import bump_docs
|
|
23
|
+
>>> paths = bump_docs.resolve_documentation_targets(
|
|
24
|
+
... Path("/workspace"), documentation_config
|
|
25
|
+
... )
|
|
26
|
+
>>> changed = bump_docs.update_documentation_files(
|
|
27
|
+
... paths, "1.2.0", {"my-crate"}, dry_run=False
|
|
28
|
+
... )
|
|
29
|
+
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from __future__ import annotations
|
|
33
|
+
|
|
34
|
+
import collections.abc as cabc
|
|
35
|
+
import logging
|
|
36
|
+
import re
|
|
37
|
+
import typing as typ
|
|
38
|
+
|
|
39
|
+
from markdown_it import MarkdownIt
|
|
40
|
+
from tomlkit import parse as parse_toml
|
|
41
|
+
from tomlkit.exceptions import TOMLKitError
|
|
42
|
+
|
|
43
|
+
from lading.commands import bump_toml
|
|
44
|
+
|
|
45
|
+
_LOGGER = logging.getLogger(__name__)
|
|
46
|
+
|
|
47
|
+
if typ.TYPE_CHECKING:
|
|
48
|
+
from pathlib import Path
|
|
49
|
+
|
|
50
|
+
from markdown_it.token import Token
|
|
51
|
+
from tomlkit.toml_document import TOMLDocument
|
|
52
|
+
|
|
53
|
+
from lading import config as config_module
|
|
54
|
+
|
|
55
|
+
else: # pragma: no cover - provide runtime placeholders for type checking imports
|
|
56
|
+
Token = TOMLDocument = typ.Any
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def resolve_documentation_targets(
|
|
60
|
+
workspace_root: Path, documentation: config_module.DocumentationConfig
|
|
61
|
+
) -> tuple[Path, ...]:
|
|
62
|
+
"""Return documentation files that should be scanned for version updates.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
workspace_root
|
|
67
|
+
The root directory of the workspace to search within.
|
|
68
|
+
documentation
|
|
69
|
+
Configuration specifying glob patterns for documentation files.
|
|
70
|
+
|
|
71
|
+
Returns
|
|
72
|
+
-------
|
|
73
|
+
tuple[Path, ...]
|
|
74
|
+
Paths to documentation files matching the configured patterns.
|
|
75
|
+
|
|
76
|
+
"""
|
|
77
|
+
patterns = documentation.globs
|
|
78
|
+
if not patterns:
|
|
79
|
+
return ()
|
|
80
|
+
|
|
81
|
+
resolved: set[Path] = set()
|
|
82
|
+
for pattern in patterns:
|
|
83
|
+
for candidate in workspace_root.glob(pattern):
|
|
84
|
+
if candidate.is_file():
|
|
85
|
+
resolved.add(candidate)
|
|
86
|
+
return tuple(resolved)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def update_documentation_files(
|
|
90
|
+
documentation_paths: cabc.Iterable[Path],
|
|
91
|
+
target_version: str,
|
|
92
|
+
updated_crates: cabc.Collection[str],
|
|
93
|
+
*,
|
|
94
|
+
dry_run: bool,
|
|
95
|
+
) -> set[Path]:
|
|
96
|
+
"""Rewrite documentation TOML fences that mention workspace crates.
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
documentation_paths
|
|
101
|
+
Paths to documentation files to process.
|
|
102
|
+
target_version
|
|
103
|
+
The version string to apply to matching entries.
|
|
104
|
+
updated_crates
|
|
105
|
+
Names of crates whose dependency entries should be updated.
|
|
106
|
+
dry_run
|
|
107
|
+
When True, report changes without modifying files.
|
|
108
|
+
|
|
109
|
+
Returns
|
|
110
|
+
-------
|
|
111
|
+
set[Path]
|
|
112
|
+
Paths to documentation files that were (or would be) modified.
|
|
113
|
+
|
|
114
|
+
"""
|
|
115
|
+
changed: set[Path] = set()
|
|
116
|
+
dependency_targets = {name for name in updated_crates if name}
|
|
117
|
+
for doc_path in documentation_paths:
|
|
118
|
+
try:
|
|
119
|
+
original_text = doc_path.read_text(encoding="utf-8")
|
|
120
|
+
except OSError as exc:
|
|
121
|
+
_LOGGER.warning("Skipping %s: %s", doc_path, exc)
|
|
122
|
+
continue
|
|
123
|
+
updated_text, snippet_changed = rewrite_markdown_toml_fences(
|
|
124
|
+
original_text, dependency_targets, target_version
|
|
125
|
+
)
|
|
126
|
+
if not snippet_changed:
|
|
127
|
+
continue
|
|
128
|
+
changed.add(doc_path)
|
|
129
|
+
if not dry_run:
|
|
130
|
+
bump_toml.write_atomic_text(doc_path, updated_text)
|
|
131
|
+
return changed
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def rewrite_markdown_toml_fences(
|
|
135
|
+
markdown_text: str,
|
|
136
|
+
dependency_targets: cabc.Collection[str],
|
|
137
|
+
target_version: str,
|
|
138
|
+
) -> tuple[str, bool]:
|
|
139
|
+
"""Rewrite TOML fences for ``dependency_targets`` within Markdown text.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
markdown_text
|
|
144
|
+
The Markdown source text containing TOML code fences.
|
|
145
|
+
dependency_targets
|
|
146
|
+
Names of crates whose dependency entries should be updated.
|
|
147
|
+
target_version
|
|
148
|
+
The version string to apply to matching entries.
|
|
149
|
+
|
|
150
|
+
Returns
|
|
151
|
+
-------
|
|
152
|
+
tuple[str, bool]
|
|
153
|
+
A tuple of (updated_text, changed) where changed indicates whether
|
|
154
|
+
any modifications were made.
|
|
155
|
+
|
|
156
|
+
"""
|
|
157
|
+
changed = False
|
|
158
|
+
|
|
159
|
+
def _apply(snippet: str) -> str:
|
|
160
|
+
"""Apply dependency version rewrites to one TOML fence."""
|
|
161
|
+
nonlocal changed
|
|
162
|
+
replacement, snippet_changed = update_toml_snippet_versions(
|
|
163
|
+
snippet, dependency_targets, target_version
|
|
164
|
+
)
|
|
165
|
+
if snippet_changed:
|
|
166
|
+
changed = True
|
|
167
|
+
return replacement
|
|
168
|
+
|
|
169
|
+
updated = replace_markdown_fences(markdown_text, "toml", _apply)
|
|
170
|
+
return updated, changed
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def replace_markdown_fences(
|
|
174
|
+
markdown_text: str,
|
|
175
|
+
language: str,
|
|
176
|
+
transform: cabc.Callable[[str], str],
|
|
177
|
+
) -> str:
|
|
178
|
+
"""Replace fenced code blocks of ``language`` with ``transform``.
|
|
179
|
+
|
|
180
|
+
Parameters
|
|
181
|
+
----------
|
|
182
|
+
markdown_text
|
|
183
|
+
The Markdown source text to process.
|
|
184
|
+
language
|
|
185
|
+
The language identifier for code fences to match (e.g., "toml").
|
|
186
|
+
transform
|
|
187
|
+
A callable that receives fence content and returns transformed content.
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
-------
|
|
191
|
+
str
|
|
192
|
+
The Markdown text with matching fences replaced by transformed content.
|
|
193
|
+
|
|
194
|
+
"""
|
|
195
|
+
parser = MarkdownIt("commonmark")
|
|
196
|
+
tokens = parser.parse(markdown_text)
|
|
197
|
+
lines = markdown_text.splitlines(keepends=True)
|
|
198
|
+
output: list[str] = []
|
|
199
|
+
last_index = 0
|
|
200
|
+
for token in tokens:
|
|
201
|
+
if not token_matches_language(token, language) or token.map is None:
|
|
202
|
+
continue
|
|
203
|
+
start, end = token.map
|
|
204
|
+
output.append("".join(lines[last_index:start]))
|
|
205
|
+
output.append(render_fence(token, lines, language, transform))
|
|
206
|
+
last_index = end
|
|
207
|
+
output.append("".join(lines[last_index:]))
|
|
208
|
+
return "".join(output)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def token_matches_language(token: Token, language: str) -> bool:
|
|
212
|
+
"""Return ``True`` when ``token`` is a fence with ``language``.
|
|
213
|
+
|
|
214
|
+
Parameters
|
|
215
|
+
----------
|
|
216
|
+
token
|
|
217
|
+
A markdown-it token to inspect.
|
|
218
|
+
language
|
|
219
|
+
The language identifier to match against.
|
|
220
|
+
|
|
221
|
+
Returns
|
|
222
|
+
-------
|
|
223
|
+
bool
|
|
224
|
+
True if the token is a fence block with the specified language.
|
|
225
|
+
|
|
226
|
+
"""
|
|
227
|
+
if token.type != "fence":
|
|
228
|
+
return False
|
|
229
|
+
info = (token.info or "").split()
|
|
230
|
+
info_lang = info[0].lower() if info else ""
|
|
231
|
+
return info_lang == language.lower()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def render_fence(
|
|
235
|
+
token: Token,
|
|
236
|
+
lines: list[str],
|
|
237
|
+
language: str,
|
|
238
|
+
transform: cabc.Callable[[str], str],
|
|
239
|
+
) -> str:
|
|
240
|
+
"""Return a rewritten fence for ``token`` using ``transform``.
|
|
241
|
+
|
|
242
|
+
Parameters
|
|
243
|
+
----------
|
|
244
|
+
token
|
|
245
|
+
A markdown-it fence token with map data.
|
|
246
|
+
lines
|
|
247
|
+
The original Markdown text split into lines with endings preserved.
|
|
248
|
+
language
|
|
249
|
+
The language identifier for the fence.
|
|
250
|
+
transform
|
|
251
|
+
A callable that receives fence content and returns transformed content.
|
|
252
|
+
|
|
253
|
+
Returns
|
|
254
|
+
-------
|
|
255
|
+
str
|
|
256
|
+
The rendered fence block with transformed content.
|
|
257
|
+
|
|
258
|
+
Raises
|
|
259
|
+
------
|
|
260
|
+
ValueError
|
|
261
|
+
If the token is missing map data.
|
|
262
|
+
|
|
263
|
+
"""
|
|
264
|
+
if token.map is None:
|
|
265
|
+
message = "Fence token missing map data"
|
|
266
|
+
raise ValueError(message)
|
|
267
|
+
start, _ = token.map
|
|
268
|
+
fence_marker = token.markup or "```"
|
|
269
|
+
indent = extract_fence_indent(lines[start], fence_marker)
|
|
270
|
+
info = token.info or language
|
|
271
|
+
original_body = token.content
|
|
272
|
+
new_body = transform(original_body)
|
|
273
|
+
suffix_match = re.search(r"(\r?\n+)$", original_body)
|
|
274
|
+
suffix = suffix_match.group(1) if suffix_match else ""
|
|
275
|
+
body_text = new_body.rstrip("\r\n") + suffix
|
|
276
|
+
indented_body = "".join(
|
|
277
|
+
f"{indent}{line}" for line in body_text.splitlines(keepends=True)
|
|
278
|
+
)
|
|
279
|
+
return f"{indent}{fence_marker}{info}\n{indented_body}{indent}{fence_marker}\n"
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def extract_fence_indent(line: str, fence_marker: str) -> str:
|
|
283
|
+
"""Return indentation preceding ``fence_marker`` in ``line``.
|
|
284
|
+
|
|
285
|
+
Parameters
|
|
286
|
+
----------
|
|
287
|
+
line
|
|
288
|
+
A line of text potentially containing a fence marker.
|
|
289
|
+
fence_marker
|
|
290
|
+
The fence marker string to search for (e.g., "```").
|
|
291
|
+
|
|
292
|
+
Returns
|
|
293
|
+
-------
|
|
294
|
+
str
|
|
295
|
+
The whitespace preceding the fence marker, or empty string if not found.
|
|
296
|
+
|
|
297
|
+
"""
|
|
298
|
+
position = line.find(fence_marker)
|
|
299
|
+
return "" if position < 0 else line[:position]
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _try_assign_version_at_path(
|
|
303
|
+
document: TOMLDocument,
|
|
304
|
+
path: tuple[str, ...],
|
|
305
|
+
target_version: str,
|
|
306
|
+
) -> bool:
|
|
307
|
+
"""Attempt to assign version at the specified table path."""
|
|
308
|
+
return bump_toml.assign_version(
|
|
309
|
+
bump_toml.select_table(document, path), target_version
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _update_single_dependency_section(
|
|
314
|
+
document: TOMLDocument,
|
|
315
|
+
section: str,
|
|
316
|
+
dependency_targets: cabc.Collection[str],
|
|
317
|
+
target_version: str,
|
|
318
|
+
) -> bool:
|
|
319
|
+
"""Update a single dependency section if it exists."""
|
|
320
|
+
table = bump_toml.select_table(document, (section,))
|
|
321
|
+
if table is None:
|
|
322
|
+
return False
|
|
323
|
+
return bump_toml.update_dependency_table(table, dependency_targets, target_version)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def update_toml_snippet_dependencies(
|
|
327
|
+
document: TOMLDocument,
|
|
328
|
+
dependency_targets: cabc.Collection[str],
|
|
329
|
+
target_version: str,
|
|
330
|
+
) -> bool:
|
|
331
|
+
"""Update dependency sections in a TOML snippet document.
|
|
332
|
+
|
|
333
|
+
Parameters
|
|
334
|
+
----------
|
|
335
|
+
document
|
|
336
|
+
A parsed TOML document to modify in place.
|
|
337
|
+
dependency_targets
|
|
338
|
+
Names of crates whose dependency entries should be updated.
|
|
339
|
+
target_version
|
|
340
|
+
The version string to apply to matching entries.
|
|
341
|
+
|
|
342
|
+
Returns
|
|
343
|
+
-------
|
|
344
|
+
bool
|
|
345
|
+
True if any dependency entries were updated.
|
|
346
|
+
|
|
347
|
+
"""
|
|
348
|
+
if not dependency_targets:
|
|
349
|
+
return False
|
|
350
|
+
|
|
351
|
+
changed = False
|
|
352
|
+
for section in bump_toml.DEPENDENCY_SECTIONS:
|
|
353
|
+
if _update_single_dependency_section(
|
|
354
|
+
document, section, dependency_targets, target_version
|
|
355
|
+
):
|
|
356
|
+
changed = True
|
|
357
|
+
return changed
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def update_toml_snippet_versions(
|
|
361
|
+
snippet: str,
|
|
362
|
+
dependency_targets: cabc.Collection[str],
|
|
363
|
+
target_version: str,
|
|
364
|
+
) -> tuple[str, bool]:
|
|
365
|
+
"""Return a TOML snippet with dependency versions rewritten.
|
|
366
|
+
|
|
367
|
+
Parameters
|
|
368
|
+
----------
|
|
369
|
+
snippet
|
|
370
|
+
A TOML snippet string to process.
|
|
371
|
+
dependency_targets
|
|
372
|
+
Names of crates whose dependency entries should be updated.
|
|
373
|
+
target_version
|
|
374
|
+
The version string to apply to matching entries.
|
|
375
|
+
|
|
376
|
+
Returns
|
|
377
|
+
-------
|
|
378
|
+
tuple[str, bool]
|
|
379
|
+
A tuple of (updated_snippet, changed) where changed indicates whether
|
|
380
|
+
any modifications were made.
|
|
381
|
+
|
|
382
|
+
"""
|
|
383
|
+
try:
|
|
384
|
+
document = parse_toml(snippet)
|
|
385
|
+
except TOMLKitError:
|
|
386
|
+
return snippet, False
|
|
387
|
+
|
|
388
|
+
changed = False
|
|
389
|
+
if _try_assign_version_at_path(document, ("package",), target_version):
|
|
390
|
+
changed = True
|
|
391
|
+
if _try_assign_version_at_path(document, ("workspace", "package"), target_version):
|
|
392
|
+
changed = True
|
|
393
|
+
if update_toml_snippet_dependencies(document, dependency_targets, target_version):
|
|
394
|
+
changed = True
|
|
395
|
+
|
|
396
|
+
if not changed:
|
|
397
|
+
return snippet, False
|
|
398
|
+
|
|
399
|
+
suffix_match = re.search(r"((?:\r?\n)*)$", snippet)
|
|
400
|
+
newline_suffix = suffix_match.group(1) if suffix_match else ""
|
|
401
|
+
rendered = document.as_string().rstrip("\r\n")
|
|
402
|
+
return f"{rendered}{newline_suffix}", True
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Lockfile regeneration helpers for ``lading bump``.
|
|
2
|
+
|
|
3
|
+
The public entry point is :func:`regenerate_lockfiles`, which rebuilds the
|
|
4
|
+
workspace root lockfile and any configured nested lockfiles after manifest
|
|
5
|
+
versions change.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import collections.abc as cabc
|
|
11
|
+
import logging
|
|
12
|
+
import time
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from lading.exceptions import LadingError
|
|
16
|
+
from lading.runtime import CommandRunner, subprocess_runner
|
|
17
|
+
from lading.utils.process import with_detail
|
|
18
|
+
|
|
19
|
+
_LOGGER = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class LockfileRegenerationError(LadingError):
|
|
23
|
+
"""Raise when lockfile regeneration cannot validate or execute."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def resolve_lockfile_paths(
|
|
27
|
+
workspace_root: Path,
|
|
28
|
+
lockfile_manifests: cabc.Sequence[str],
|
|
29
|
+
) -> tuple[Path, ...]:
|
|
30
|
+
"""Return lockfile paths implied by configured manifest paths."""
|
|
31
|
+
manifests = _resolve_manifest_paths(workspace_root, lockfile_manifests)
|
|
32
|
+
return tuple(manifest.parent / "Cargo.lock" for manifest in manifests)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def regenerate_lockfiles(
|
|
36
|
+
workspace_root: Path,
|
|
37
|
+
lockfile_manifests: cabc.Sequence[str],
|
|
38
|
+
*,
|
|
39
|
+
runner: CommandRunner | None = None,
|
|
40
|
+
) -> tuple[Path, ...]:
|
|
41
|
+
"""Regenerate Cargo lockfiles for root and configured manifests.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
workspace_root : Path
|
|
46
|
+
Absolute path to the Cargo workspace root.
|
|
47
|
+
lockfile_manifests : Sequence[str]
|
|
48
|
+
Configured manifest paths relative to *workspace_root*. The workspace
|
|
49
|
+
root ``Cargo.toml`` is always prepended and de-duplicated.
|
|
50
|
+
runner : CommandRunner or None, optional
|
|
51
|
+
Callable used to invoke ``cargo``. Defaults to
|
|
52
|
+
:func:`lading.runtime.subprocess_runner` when ``None``.
|
|
53
|
+
|
|
54
|
+
Returns
|
|
55
|
+
-------
|
|
56
|
+
tuple[Path, ...]
|
|
57
|
+
Paths to every ``Cargo.lock`` file that was regenerated, in manifest
|
|
58
|
+
execution order.
|
|
59
|
+
|
|
60
|
+
Raises
|
|
61
|
+
------
|
|
62
|
+
LockfileRegenerationError
|
|
63
|
+
If any configured manifest path is invalid (outside the workspace or
|
|
64
|
+
not named ``Cargo.toml``), or if ``cargo update --workspace`` exits
|
|
65
|
+
non-zero for any manifest.
|
|
66
|
+
|
|
67
|
+
Notes
|
|
68
|
+
-----
|
|
69
|
+
**Partial-update semantics:** regeneration is not atomic. If ``cargo``
|
|
70
|
+
fails for the *n*-th manifest, the first *n - 1* lockfiles have already
|
|
71
|
+
been updated on disk and will not be rolled back. Callers that require
|
|
72
|
+
atomic guarantees must implement their own rollback (see issue ``#84``).
|
|
73
|
+
"""
|
|
74
|
+
command_runner = subprocess_runner if runner is None else runner
|
|
75
|
+
manifests = _resolve_manifest_paths(workspace_root, lockfile_manifests)
|
|
76
|
+
started_at = time.perf_counter()
|
|
77
|
+
_LOGGER.info("Regenerating %d Cargo lockfile(s)", len(manifests))
|
|
78
|
+
lockfiles: list[Path] = []
|
|
79
|
+
for manifest in manifests:
|
|
80
|
+
manifest_started_at = time.perf_counter()
|
|
81
|
+
_LOGGER.info("Regenerating Cargo lockfile for %s", manifest)
|
|
82
|
+
_run_workspace_lockfile_update(workspace_root, manifest, command_runner)
|
|
83
|
+
_LOGGER.info(
|
|
84
|
+
"Regenerated Cargo lockfile for %s in %.3fs",
|
|
85
|
+
manifest,
|
|
86
|
+
time.perf_counter() - manifest_started_at,
|
|
87
|
+
)
|
|
88
|
+
lockfiles.append(manifest.parent / "Cargo.lock")
|
|
89
|
+
_LOGGER.info(
|
|
90
|
+
"Regenerated %d Cargo lockfile(s) in %.3fs",
|
|
91
|
+
len(lockfiles),
|
|
92
|
+
time.perf_counter() - started_at,
|
|
93
|
+
)
|
|
94
|
+
return tuple(lockfiles)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _resolve_manifest_paths(
|
|
98
|
+
workspace_root: Path,
|
|
99
|
+
lockfile_manifests: cabc.Sequence[str],
|
|
100
|
+
) -> tuple[Path, ...]:
|
|
101
|
+
"""Return validated root and configured manifest paths in execution order."""
|
|
102
|
+
resolved_root = workspace_root.resolve()
|
|
103
|
+
root_manifest = (workspace_root / "Cargo.toml").resolve()
|
|
104
|
+
seen_manifests: set[Path] = {root_manifest}
|
|
105
|
+
manifests = [root_manifest]
|
|
106
|
+
for manifest in lockfile_manifests:
|
|
107
|
+
candidate = (workspace_root / manifest).resolve()
|
|
108
|
+
try:
|
|
109
|
+
candidate.relative_to(resolved_root)
|
|
110
|
+
except ValueError as exc:
|
|
111
|
+
message = (
|
|
112
|
+
f"Lockfile manifest path must stay within the workspace: {manifest}"
|
|
113
|
+
)
|
|
114
|
+
raise LockfileRegenerationError(message) from exc
|
|
115
|
+
if candidate.name != "Cargo.toml":
|
|
116
|
+
message = (
|
|
117
|
+
f"Lockfile manifest path must point to a Cargo.toml file: {manifest}"
|
|
118
|
+
)
|
|
119
|
+
raise LockfileRegenerationError(message)
|
|
120
|
+
if candidate in seen_manifests:
|
|
121
|
+
continue
|
|
122
|
+
seen_manifests.add(candidate)
|
|
123
|
+
manifests.append(candidate)
|
|
124
|
+
return tuple(manifests)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _run_workspace_lockfile_update(
|
|
128
|
+
workspace_root: Path,
|
|
129
|
+
manifest_path: Path,
|
|
130
|
+
runner: CommandRunner,
|
|
131
|
+
) -> None:
|
|
132
|
+
"""Invoke cargo for one manifest and surface failures consistently."""
|
|
133
|
+
try:
|
|
134
|
+
exit_code, stdout, stderr = runner(
|
|
135
|
+
(
|
|
136
|
+
"cargo",
|
|
137
|
+
"update",
|
|
138
|
+
"--workspace",
|
|
139
|
+
"--manifest-path",
|
|
140
|
+
str(manifest_path),
|
|
141
|
+
),
|
|
142
|
+
cwd=workspace_root,
|
|
143
|
+
)
|
|
144
|
+
except Exception as exc:
|
|
145
|
+
message = f"Cargo lockfile regeneration failed for {manifest_path}: {exc}"
|
|
146
|
+
raise LockfileRegenerationError(message) from exc
|
|
147
|
+
if exit_code != 0:
|
|
148
|
+
message = with_detail(
|
|
149
|
+
"Cargo lockfile regeneration failed for "
|
|
150
|
+
f"{manifest_path} with exit code {exit_code}",
|
|
151
|
+
stdout,
|
|
152
|
+
stderr,
|
|
153
|
+
)
|
|
154
|
+
raise LockfileRegenerationError(message)
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""Output formatting for the ``lading bump`` command.
|
|
2
|
+
|
|
3
|
+
This module turns bump command results into human-readable CLI messages. Callers
|
|
4
|
+
provide a :class:`BumpChanges` record, the target version, dry-run state, and
|
|
5
|
+
workspace root, then receive the exact multi-line string that should be printed
|
|
6
|
+
to the terminal. The module is consumed by ``lading.commands.bump`` and is not a
|
|
7
|
+
general-purpose formatting API.
|
|
8
|
+
|
|
9
|
+
Classes
|
|
10
|
+
-------
|
|
11
|
+
BumpChanges
|
|
12
|
+
Public value object describing updated manifests, documentation files, and
|
|
13
|
+
lockfiles.
|
|
14
|
+
|
|
15
|
+
Functions
|
|
16
|
+
---------
|
|
17
|
+
_format_result_message
|
|
18
|
+
Primary entry point. Assembles the final multi-line result string from
|
|
19
|
+
a ``BumpChanges`` instance, the target version string, a dry-run flag,
|
|
20
|
+
and the workspace root used to relativise displayed paths.
|
|
21
|
+
|
|
22
|
+
Notes
|
|
23
|
+
-----
|
|
24
|
+
Internal helpers ``_build_changes_description``, ``_format_no_changes_message``,
|
|
25
|
+
``_format_header``, and ``_format_manifest_path`` are not part of the public API.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import collections.abc as cabc
|
|
31
|
+
import dataclasses as dc
|
|
32
|
+
import typing as typ
|
|
33
|
+
|
|
34
|
+
if typ.TYPE_CHECKING:
|
|
35
|
+
from pathlib import Path
|
|
36
|
+
|
|
37
|
+
_SINGLE_CHANGE_CATEGORY_COUNT = 1
|
|
38
|
+
_PAIRED_CHANGE_CATEGORY_COUNT = 2
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dc.dataclass(frozen=True, slots=True)
|
|
42
|
+
class BumpChanges:
|
|
43
|
+
"""Collection of files altered by a bump run.
|
|
44
|
+
|
|
45
|
+
The dataclass is frozen and slot-based, so callers receive an immutable,
|
|
46
|
+
compact record that can be passed safely between bump processing and output
|
|
47
|
+
formatting.
|
|
48
|
+
|
|
49
|
+
Attributes
|
|
50
|
+
----------
|
|
51
|
+
manifests : Sequence[Path]
|
|
52
|
+
Cargo manifests updated by the bump run.
|
|
53
|
+
documents : Sequence[Path]
|
|
54
|
+
Documentation files updated by TOML snippet rewriting.
|
|
55
|
+
lockfiles : Sequence[Path]
|
|
56
|
+
Cargo lockfiles regenerated after manifest updates.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
manifests: cabc.Sequence[Path] = ()
|
|
60
|
+
documents: cabc.Sequence[Path] = ()
|
|
61
|
+
lockfiles: cabc.Sequence[Path] = ()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _build_changes_description(changes: BumpChanges) -> str:
|
|
65
|
+
"""Build a human-readable description of changed files."""
|
|
66
|
+
parts: list[str] = []
|
|
67
|
+
if changes.manifests:
|
|
68
|
+
parts.append(f"{len(changes.manifests)} manifest(s)")
|
|
69
|
+
if changes.documents:
|
|
70
|
+
parts.append(f"{len(changes.documents)} documentation file(s)")
|
|
71
|
+
if changes.lockfiles:
|
|
72
|
+
parts.append(f"{len(changes.lockfiles)} lockfile(s)")
|
|
73
|
+
if len(parts) == _SINGLE_CHANGE_CATEGORY_COUNT:
|
|
74
|
+
return parts[0]
|
|
75
|
+
if len(parts) == _PAIRED_CHANGE_CATEGORY_COUNT:
|
|
76
|
+
return " and ".join(parts)
|
|
77
|
+
return f"{', '.join(parts[:-1])}, and {parts[-1]}"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _format_no_changes_message(target_version: str, *, dry_run: bool) -> str:
|
|
81
|
+
"""Format message when no changes are required."""
|
|
82
|
+
if dry_run:
|
|
83
|
+
return (
|
|
84
|
+
"Dry run; no manifest changes required; "
|
|
85
|
+
f"all versions already {target_version}."
|
|
86
|
+
)
|
|
87
|
+
return f"No manifest changes required; all versions already {target_version}."
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _format_header(description: str, target_version: str, *, dry_run: bool) -> str:
|
|
91
|
+
"""Format the summary header line."""
|
|
92
|
+
if dry_run:
|
|
93
|
+
return f"Dry run; would update version to {target_version} in {description}:"
|
|
94
|
+
return f"Updated version to {target_version} in {description}:"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _format_result_message(
|
|
98
|
+
changes: BumpChanges,
|
|
99
|
+
target_version: str,
|
|
100
|
+
*,
|
|
101
|
+
dry_run: bool,
|
|
102
|
+
workspace_root: Path,
|
|
103
|
+
) -> str:
|
|
104
|
+
"""Summarise the bump outcome for CLI presentation."""
|
|
105
|
+
if not any((changes.manifests, changes.documents, changes.lockfiles)):
|
|
106
|
+
return _format_no_changes_message(target_version, dry_run=dry_run)
|
|
107
|
+
|
|
108
|
+
description = _build_changes_description(changes)
|
|
109
|
+
header = _format_header(description, target_version, dry_run=dry_run)
|
|
110
|
+
formatted_paths = [
|
|
111
|
+
f"- {_format_manifest_path(manifest_path, workspace_root)}"
|
|
112
|
+
for manifest_path in changes.manifests
|
|
113
|
+
]
|
|
114
|
+
formatted_paths.extend(
|
|
115
|
+
f"- {_format_manifest_path(document_path, workspace_root)} (documentation)"
|
|
116
|
+
for document_path in changes.documents
|
|
117
|
+
)
|
|
118
|
+
formatted_paths.extend(
|
|
119
|
+
f"- {_format_manifest_path(lockfile_path, workspace_root)} (lockfile)"
|
|
120
|
+
for lockfile_path in changes.lockfiles
|
|
121
|
+
)
|
|
122
|
+
return "\n".join([header, *formatted_paths])
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _format_manifest_path(manifest_path: Path, workspace_root: Path) -> str:
|
|
126
|
+
"""Return ``manifest_path`` relative to ``workspace_root`` when possible."""
|
|
127
|
+
try:
|
|
128
|
+
relative = manifest_path.relative_to(workspace_root)
|
|
129
|
+
except ValueError:
|
|
130
|
+
return str(manifest_path)
|
|
131
|
+
return str(relative)
|