|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# SPDX-FileCopyrightText: 2026 JP Hutchins |
| 3 | + |
| 4 | +"""``--github-matrix``: emit a GitHub Actions matrix as JSON. |
| 5 | +
|
| 6 | +Projects the task's matrix axes — collected from anywhere in the tree via |
| 7 | +:func:`camas.core.matrix.matrix_axes`, outermost-wins on duplicate keys — |
| 8 | +into the object-of-arrays form that GHA consumes natively:: |
| 9 | +
|
| 10 | + {"PY": ["3.10", "3.11"], "OS": ["linux", "macos"]} |
| 11 | +
|
| 12 | +Use the whole object as the matrix:: |
| 13 | +
|
| 14 | + matrix: ${{ fromJSON(needs.discover.outputs.matrix) }} |
| 15 | +
|
| 16 | +or one axis at a time, composed with other YAML-side axes:: |
| 17 | +
|
| 18 | + matrix: |
| 19 | + os: [ubuntu-latest, macos-latest] |
| 20 | + PY: ${{ fromJSON(needs.discover.outputs.matrix).PY }} |
| 21 | +
|
| 22 | +CLI overrides (``--PY 3.13``) flow through the same ``override_matrix`` |
| 23 | +pipeline used by the runner before emission, so the emitted JSON reflects |
| 24 | +exactly what the run would have executed. |
| 25 | +
|
| 26 | +Output is TTY-aware: indented for interactive preview, compact one-line |
| 27 | +for pipes — so ``camas matrix --github-matrix`` reads cleanly in a shell |
| 28 | +*and* ``$(camas matrix --github-matrix)`` works directly with |
| 29 | +``$GITHUB_OUTPUT``. |
| 30 | +""" |
| 31 | + |
| 32 | +from __future__ import annotations |
| 33 | + |
| 34 | +import json |
| 35 | +from collections.abc import Mapping |
| 36 | + |
| 37 | +from ..core.matrix import matrix_axes |
| 38 | +from ..core.task import TaskNode |
| 39 | + |
| 40 | + |
| 41 | +def to_matrix_object(task: TaskNode) -> dict[str, list[str]]: |
| 42 | + """Project a task's matrix axes into a GHA-compatible object-of-arrays. |
| 43 | +
|
| 44 | + Raises ``ValueError`` when the task has no matrix axes or any axis has no |
| 45 | + values — ``--github-matrix`` requires at least one cell to emit a workflow |
| 46 | + GHA will accept (the schema mandates ``minItems: 1`` on every axis array). |
| 47 | +
|
| 48 | + >>> from camas import Parallel, Task |
| 49 | + >>> to_matrix_object(Parallel(Task("test"), matrix={"PY": ("3.12", "3.13")})) |
| 50 | + {'PY': ['3.12', '3.13']} |
| 51 | + >>> to_matrix_object(Parallel(Task("t"), matrix={"PY": ("3.13",), "OS": ("linux",)})) |
| 52 | + {'PY': ['3.13'], 'OS': ['linux']} |
| 53 | + >>> to_matrix_object(Task("hi")) |
| 54 | + Traceback (most recent call last): |
| 55 | + ... |
| 56 | + ValueError: task has no matrix axes; --github-matrix requires at least one |
| 57 | + >>> to_matrix_object(Parallel(Task("t"), matrix={"PY": ()})) |
| 58 | + Traceback (most recent call last): |
| 59 | + ... |
| 60 | + ValueError: matrix axis 'PY' has no values |
| 61 | + """ |
| 62 | + axes: Mapping[str, tuple[str, ...]] = matrix_axes(task) |
| 63 | + if not axes: |
| 64 | + raise ValueError("task has no matrix axes; --github-matrix requires at least one") |
| 65 | + for name, values in axes.items(): |
| 66 | + if not values: |
| 67 | + raise ValueError(f"matrix axis {name!r} has no values") |
| 68 | + return {name: list(values) for name, values in axes.items()} |
| 69 | + |
| 70 | + |
| 71 | +def format_matrix_json(matrix: Mapping[str, list[str]], *, pretty: bool) -> str: |
| 72 | + """Serialize the matrix object to JSON. |
| 73 | +
|
| 74 | + Compact (no spaces, single line) when ``pretty`` is False — the canonical |
| 75 | + shape for ``echo "matrix=$(...)" >> $GITHUB_OUTPUT``. Indented two spaces |
| 76 | + when ``pretty`` is True — readable preview for interactive use. |
| 77 | +
|
| 78 | + >>> format_matrix_json({"PY": ["3.12", "3.13"]}, pretty=False) |
| 79 | + '{"PY":["3.12","3.13"]}' |
| 80 | + >>> print(format_matrix_json({"PY": ["3.12"]}, pretty=True)) |
| 81 | + { |
| 82 | + "PY": [ |
| 83 | + "3.12" |
| 84 | + ] |
| 85 | + } |
| 86 | + """ |
| 87 | + if pretty: |
| 88 | + return json.dumps(matrix, indent=2) |
| 89 | + return json.dumps(matrix, separators=(",", ":")) |
| 90 | + |
| 91 | + |
| 92 | +def emit(task: TaskNode, *, pretty: bool) -> str: |
| 93 | + """Compose :func:`to_matrix_object` and :func:`format_matrix_json`. |
| 94 | +
|
| 95 | + >>> from camas import Parallel, Task |
| 96 | + >>> emit(Parallel(Task("t"), matrix={"PY": ("3.12",)}), pretty=False) |
| 97 | + '{"PY":["3.12"]}' |
| 98 | + """ |
| 99 | + return format_matrix_json(to_matrix_object(task), pretty=pretty) |
0 commit comments