|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Expand parasol-publish-workspaces.txt for publish CI and catalog-index. |
| 3 | +
|
| 4 | +Adding a workspace: put its directory name in parasol-publish-workspaces.txt |
| 5 | +and retarget that workspace's metadata dynamicArtifact to this fork's GHCR. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import json |
| 12 | +import os |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +ALLOW_LIST_NAME = "parasol-publish-workspaces.txt" |
| 17 | + |
| 18 | + |
| 19 | +def _listed_names(root: Path) -> list[str]: |
| 20 | + path = root / ALLOW_LIST_NAME |
| 21 | + if not path.is_file(): |
| 22 | + return [] |
| 23 | + names: list[str] = [] |
| 24 | + for raw in path.read_text(encoding="utf-8").splitlines(): |
| 25 | + line = raw.split("#", 1)[0].strip() |
| 26 | + if not line: |
| 27 | + continue |
| 28 | + names.append(line.removeprefix("workspaces/")) |
| 29 | + return names |
| 30 | + |
| 31 | + |
| 32 | +def workspace_paths(root: Path) -> list[str]: |
| 33 | + return [f"workspaces/{name}" for name in _listed_names(root)] |
| 34 | + |
| 35 | + |
| 36 | +def package_lines(root: Path) -> list[str]: |
| 37 | + lines: list[str] = [] |
| 38 | + for name in _listed_names(root): |
| 39 | + plugins_list = root / "workspaces" / name / "plugins-list.yaml" |
| 40 | + if not plugins_list.is_file(): |
| 41 | + print( |
| 42 | + f"warning: missing {plugins_list}, skipping", |
| 43 | + file=sys.stderr, |
| 44 | + ) |
| 45 | + continue |
| 46 | + for raw in plugins_list.read_text(encoding="utf-8").splitlines(): |
| 47 | + line = raw.split("#", 1)[0].strip() |
| 48 | + if not line or ":" not in line: |
| 49 | + continue |
| 50 | + plugin_path = line.split(":", 1)[0].strip().strip("'\"") |
| 51 | + if plugin_path: |
| 52 | + lines.append(f"{name}/{plugin_path}") |
| 53 | + return lines |
| 54 | + |
| 55 | + |
| 56 | +def main() -> int: |
| 57 | + parser = argparse.ArgumentParser(description=__doc__) |
| 58 | + parser.add_argument( |
| 59 | + "--root", |
| 60 | + type=Path, |
| 61 | + default=Path("."), |
| 62 | + help="Overlay repo root", |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + "--workspace-path", |
| 66 | + default="", |
| 67 | + help="If set, publish only this workspace (overrides the allow-list)", |
| 68 | + ) |
| 69 | + group = parser.add_mutually_exclusive_group(required=True) |
| 70 | + group.add_argument( |
| 71 | + "--workspaces-json", |
| 72 | + action="store_true", |
| 73 | + help="Print GitHub output workspaces=<json array of workspaces/name>", |
| 74 | + ) |
| 75 | + group.add_argument( |
| 76 | + "--packages-file", |
| 77 | + type=Path, |
| 78 | + help="Write catalog --packages-file lines (workspace/plugins/...)", |
| 79 | + ) |
| 80 | + args = parser.parse_args() |
| 81 | + root = args.root.resolve() |
| 82 | + |
| 83 | + if args.workspaces_json: |
| 84 | + explicit = (args.workspace_path or os.environ.get("WORKSPACE_PATH", "")).strip() |
| 85 | + if explicit: |
| 86 | + if not explicit.startswith("workspaces/"): |
| 87 | + explicit = f"workspaces/{explicit}" |
| 88 | + paths = [explicit] |
| 89 | + else: |
| 90 | + paths = workspace_paths(root) |
| 91 | + print(f"Workspaces to publish: {json.dumps(paths)}", file=sys.stderr) |
| 92 | + gh_output = os.environ.get("GITHUB_OUTPUT") |
| 93 | + if gh_output: |
| 94 | + with open(gh_output, "a", encoding="utf-8") as fh: |
| 95 | + fh.write("workspaces=" + json.dumps(paths) + "\n") |
| 96 | + else: |
| 97 | + print(json.dumps(paths)) |
| 98 | + return 0 |
| 99 | + |
| 100 | + lines = package_lines(root) |
| 101 | + args.packages_file.write_text( |
| 102 | + "\n".join(lines) + ("\n" if lines else ""), |
| 103 | + encoding="utf-8", |
| 104 | + ) |
| 105 | + print(f"Wrote {len(lines)} package path(s) to {args.packages_file}", file=sys.stderr) |
| 106 | + return 0 |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + raise SystemExit(main()) |
0 commit comments