|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +"""CLI-level aspect exporting dependable_element (rules_score SEooC) artifacts. |
| 15 | +
|
| 16 | +The aspect exposes every dependable_element (rules_score SEooC)'s own doc/report |
| 17 | +artifacts as a dedicated output group. It no-ops for any target that isn't a |
| 18 | +dependable_element, so it is safe to apply unconditionally on //... |
| 19 | +Every `dependable_element`'s own doc/report artifacts become an explicitly requestable output group of that *same* build invocation - without changing what is built for any other target. |
| 20 | +
|
| 21 | +Rather than merely re-exposing each dependable_element's existing output files as-is, this |
| 22 | +aspect *relocates/copies* them under a synthetic `_dependable_element_export/` path segment to simplify downloading it from remote-execution or uploading it in CI. |
| 23 | +
|
| 24 | +Usage: |
| 25 | +
|
| 26 | +bazel build //<your_target(s)> \ |
| 27 | + --aspects=@score_tooling//bazel/aspects/rules_score:dependable_element_export.bzl%dependable_element_export_aspect \ |
| 28 | + --output_groups=+dependable_element_export \ |
| 29 | + --remote_download_regex=".*/_dependable_element_export/.*" |
| 30 | +
|
| 31 | +See README.md next to this file for details. |
| 32 | +""" |
| 33 | + |
| 34 | +load("@score_tooling//bazel/rules/rules_score:providers.bzl", "DependableElementInfo") |
| 35 | + |
| 36 | +_EXPORT_DIR = "_dependable_element_export" |
| 37 | + |
| 38 | +def _dependable_element_export_aspect_impl(target, ctx): |
| 39 | + """Re-export a dependable_element's own default outputs under a uniform path segment. |
| 40 | +
|
| 41 | + No-ops for any target that isn't a dependable_element (identified via the |
| 42 | + `DependableElementInfo` provider that `dependable_element()` forwards for exactly this kind |
| 43 | + of cross-target lookup - see rules_score's own use of it for integrity-level checks). |
| 44 | + """ |
| 45 | + if DependableElementInfo not in target: |
| 46 | + return [] |
| 47 | + |
| 48 | + # Own package prefix of the file's short_path, if present, is stripped so the relocated |
| 49 | + # path doesn't redundantly nest the package path inside itself a second time. |
| 50 | + own_package_prefix = ctx.label.package + "/" |
| 51 | + |
| 52 | + exported_files = [] |
| 53 | + for f in target[DefaultInfo].files.to_list(): |
| 54 | + short_path = f.short_path |
| 55 | + if short_path.startswith("../"): |
| 56 | + # Drop external repo prefix. |
| 57 | + relative_path = short_path.split("/", 2)[2] |
| 58 | + elif short_path.startswith(own_package_prefix): |
| 59 | + relative_path = short_path[len(own_package_prefix):] |
| 60 | + else: |
| 61 | + relative_path = short_path |
| 62 | + |
| 63 | + # Some dependable_element outputs are tree artifacts (e.g. a Sphinx HTML directory), |
| 64 | + # not plain files - handled with a directory copy instead of a single-file one. |
| 65 | + if f.is_directory: |
| 66 | + exported = ctx.actions.declare_directory(_EXPORT_DIR + "/" + relative_path) |
| 67 | + ctx.actions.run_shell( |
| 68 | + inputs = [f], |
| 69 | + outputs = [exported], |
| 70 | + command = "mkdir -p \"$2\" && cp -r \"$1\"/. \"$2\"/", |
| 71 | + arguments = [f.path, exported.path], |
| 72 | + mnemonic = "DependableElementExportCopyDir", |
| 73 | + progress_message = "Exporting dependable_element directory " + f.short_path, |
| 74 | + ) |
| 75 | + else: |
| 76 | + exported = ctx.actions.declare_file(_EXPORT_DIR + "/" + relative_path) |
| 77 | + ctx.actions.run_shell( |
| 78 | + inputs = [f], |
| 79 | + outputs = [exported], |
| 80 | + command = "mkdir -p \"$(dirname \"$2\")\" && cp \"$1\" \"$2\"", |
| 81 | + arguments = [f.path, exported.path], |
| 82 | + mnemonic = "DependableElementExportCopyFile", |
| 83 | + progress_message = "Exporting dependable_element file " + f.short_path, |
| 84 | + ) |
| 85 | + exported_files.append(exported) |
| 86 | + |
| 87 | + return [ |
| 88 | + OutputGroupInfo( |
| 89 | + dependable_element_export = depset(exported_files), |
| 90 | + ), |
| 91 | + ] |
| 92 | + |
| 93 | +dependable_element_export_aspect = aspect( |
| 94 | + implementation = _dependable_element_export_aspect_impl, |
| 95 | + doc = ( |
| 96 | + "Exports dependable_element (rules_score SEooC) default outputs (doc/report " + |
| 97 | + "artifacts), relocated (via copy actions) under a uniform " + |
| 98 | + "'_dependable_element_export/' path segment, as a dedicated " + |
| 99 | + "'dependable_element_export' output group - for use as an additive `--aspects=` flag " + |
| 100 | + "on an otherwise unrelated build." |
| 101 | + ), |
| 102 | +) |
0 commit comments