|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build the cache directory list for this action. |
| 3 | +
|
| 4 | +We always include the rustdoc output directory, then merge any extra |
| 5 | +``cache-directories`` entries from the workflow. Keeping that logic here makes |
| 6 | +``action.yml`` smaller and easy to test. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import ntpath |
| 12 | +import os |
| 13 | +import posixpath |
| 14 | + |
| 15 | + |
| 16 | +def main() -> int: |
| 17 | + """Resolve cache directories and export them to GitHub Actions environment. |
| 18 | +
|
| 19 | + Reads configuration from environment variables, builds the cache directory list, |
| 20 | + and either writes it to the GitHub environment file or prints to stdout. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + 0 on success, non-zero on failure. |
| 24 | + """ |
| 25 | + project_path = os.environ.get("INPUT_RUST_PROJECT_PATH", ".") |
| 26 | + target_triple = os.environ.get("INPUT_TARGET", "") |
| 27 | + extra_directories = os.environ.get("INPUT_CACHE_DIRECTORIES", "") |
| 28 | + github_env = os.environ.get("GITHUB_ENV") |
| 29 | + |
| 30 | + directories = resolve_cache_directories( |
| 31 | + project_path=project_path, |
| 32 | + target_triple=target_triple, |
| 33 | + extra_directories=extra_directories, |
| 34 | + ) |
| 35 | + |
| 36 | + if github_env: |
| 37 | + write_github_env(github_env, directories) |
| 38 | + else: |
| 39 | + for directory in directories: |
| 40 | + print(directory) |
| 41 | + |
| 42 | + return 0 |
| 43 | + |
| 44 | + |
| 45 | +def resolve_cache_directories( |
| 46 | + project_path: str, target_triple: str, extra_directories: str |
| 47 | +) -> list[str]: |
| 48 | + """Build the cache directory list for a Rust project. |
| 49 | +
|
| 50 | + Always includes the rustdoc output directory, then merges any extra directories |
| 51 | + from the workflow input, deduplicating by qualified path. |
| 52 | +
|
| 53 | + Arguments: |
| 54 | + project_path: Path to the Rust project root (relative or absolute). |
| 55 | + target_triple: Target triple for cross-compilation (e.g., "x86_64-unknown-linux-gnu"). |
| 56 | + extra_directories: Newline-separated list of additional cache directories. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + List of qualified cache directory paths, with rustdoc directory first. |
| 60 | + """ |
| 61 | + default_doc_dir = "target/doc" |
| 62 | + if target_triple: |
| 63 | + default_doc_dir = f"target/{target_triple}/doc" |
| 64 | + |
| 65 | + resolved: list[str] = [_qualify_path(default_doc_dir, project_path)] |
| 66 | + seen = set(resolved) |
| 67 | + |
| 68 | + for raw_line in extra_directories.splitlines(): |
| 69 | + directory = raw_line.strip().rstrip("\r") |
| 70 | + if not directory: |
| 71 | + continue |
| 72 | + |
| 73 | + qualified = _qualify_path(directory, project_path) |
| 74 | + if qualified in seen: |
| 75 | + continue |
| 76 | + |
| 77 | + resolved.append(qualified) |
| 78 | + seen.add(qualified) |
| 79 | + |
| 80 | + return resolved |
| 81 | + |
| 82 | + |
| 83 | +def write_github_env(env_file: str, directories: list[str]) -> None: |
| 84 | + """Export the resolved cache directories to a GitHub Actions environment file. |
| 85 | +
|
| 86 | + Writes the directories as a multiline environment variable that downstream |
| 87 | + workflow steps can read from `RUST_CACHE_DIRECTORIES`. |
| 88 | +
|
| 89 | + Arguments: |
| 90 | + env_file: Path to the GitHub environment file. |
| 91 | + directories: List of cache directory paths to export. |
| 92 | + """ |
| 93 | + with open(env_file, "a", encoding="utf-8") as handle: |
| 94 | + handle.write("RUST_CACHE_DIRECTORIES<<EOF\n") |
| 95 | + for directory in directories: |
| 96 | + handle.write(f"{directory}\n") |
| 97 | + handle.write("EOF\n") |
| 98 | + |
| 99 | + |
| 100 | +def _qualify_path(path: str, project_path: str) -> str: |
| 101 | + """Resolve a path relative to the Rust project root. |
| 102 | +
|
| 103 | + Absolute paths are normalized as-is. Relative paths are joined with |
| 104 | + the project path and normalized. |
| 105 | +
|
| 106 | + Arguments: |
| 107 | + path: The path to qualify (can be absolute or relative). |
| 108 | + project_path: The Rust project root path. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + Normalized path, or empty string if path is empty. |
| 112 | + """ |
| 113 | + if not path: |
| 114 | + return "" |
| 115 | + |
| 116 | + normalized_for_check = path.replace("\\", "/") |
| 117 | + if posixpath.isabs(normalized_for_check) or ntpath.isabs(path): |
| 118 | + return _normalize_path(path) |
| 119 | + |
| 120 | + if project_path == ".": |
| 121 | + return _normalize_path(path) |
| 122 | + |
| 123 | + return _normalize_path(posixpath.join(project_path, path)) |
| 124 | + |
| 125 | + |
| 126 | +def _normalize_path(path: str) -> str: |
| 127 | + """Normalize a path using POSIX forward slashes. |
| 128 | +
|
| 129 | + Converts backslashes to forward slashes and normalizes the path, |
| 130 | + but does not make it absolute. |
| 131 | +
|
| 132 | + Arguments: |
| 133 | + path: The path to normalize. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + Normalized POSIX-style path. |
| 137 | + """ |
| 138 | + path = path.replace("\\", "/") |
| 139 | + normalized = posixpath.normpath(path) |
| 140 | + if normalized == ".": |
| 141 | + return "." |
| 142 | + return normalized |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + raise SystemExit(main()) |
0 commit comments