feat: add to_output_relative_path
This is useful in rules_js ecosystem where the working directory is bazel-out/arch/bin so many paths need to be relative to there.
This commit is contained in:
parent
fafdd8610c
commit
562c4b231d
|
@ -0,0 +1 @@
|
|||
e2e/
|
|
@ -25,12 +25,6 @@ local_repository(
|
|||
path = "./lib/tests/external_test_repo",
|
||||
)
|
||||
|
||||
# Avoid descending into the e2e workspaces
|
||||
local_repository(
|
||||
name = "e2e_bzlmod",
|
||||
path = "./e2e/bzlmod",
|
||||
)
|
||||
|
||||
############################################
|
||||
# Gazelle, for generating bzl_library targets
|
||||
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
|
||||
|
|
|
@ -56,6 +56,24 @@ in order to locate entries by their key.
|
|||
The runfiles manifest entry path for a file
|
||||
|
||||
|
||||
<a id="to_output_relative_path"></a>
|
||||
|
||||
## to_output_relative_path
|
||||
|
||||
<pre>
|
||||
to_output_relative_path(<a href="#to_output_relative_path-f">f</a>)
|
||||
</pre>
|
||||
|
||||
The relative path from bazel-out/[arch]/bin to the given File object
|
||||
|
||||
**PARAMETERS**
|
||||
|
||||
|
||||
| Name | Description | Default Value |
|
||||
| :------------- | :------------- | :------------- |
|
||||
| <a id="to_output_relative_path-f"></a>f | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
<a id="to_workspace_path"></a>
|
||||
|
||||
## to_workspace_path
|
||||
|
|
|
@ -5,6 +5,7 @@ load("//lib/private:paths.bzl", "paths")
|
|||
relative_file = paths.relative_file
|
||||
to_manifest_path = paths.to_manifest_path
|
||||
to_workspace_path = paths.to_workspace_path
|
||||
to_output_relative_path = paths.to_output_relative_path
|
||||
|
||||
# Bash helper function for looking up runfiles.
|
||||
# See windows_utils.bzl for the cmd.exe equivalent.
|
||||
|
|
|
@ -42,6 +42,18 @@ def _relative_file(to_file, frm_file):
|
|||
)
|
||||
)
|
||||
|
||||
def _to_output_relative_path(f):
|
||||
"The relative path from bazel-out/[arch]/bin to the given File object"
|
||||
if f.is_source:
|
||||
execroot = "../../../"
|
||||
else:
|
||||
execroot = ""
|
||||
if f.short_path.startswith("../"):
|
||||
path = "external/" + f.short_path[3:]
|
||||
else:
|
||||
path = f.short_path
|
||||
return execroot + path
|
||||
|
||||
def _to_manifest_path(ctx, file):
|
||||
"""The runfiles manifest entry path for a file
|
||||
|
||||
|
@ -87,5 +99,6 @@ def _to_workspace_path(file):
|
|||
paths = struct(
|
||||
relative_file = _relative_file,
|
||||
to_manifest_path = _to_manifest_path,
|
||||
to_output_relative_path = _to_output_relative_path,
|
||||
to_workspace_path = _to_workspace_path,
|
||||
)
|
||||
|
|
|
@ -128,13 +128,28 @@ def _workspace_path_test_impl(ctx):
|
|||
asserts.equals(env, "lib/paths.bzl", paths.to_workspace_path(ctx.file.f2))
|
||||
return unittest.end(env)
|
||||
|
||||
def _output_relative_path_test_impl(ctx):
|
||||
env = unittest.begin(ctx)
|
||||
asserts.equals(env, "../../../external/bazel_skylib/LICENSE", paths.to_output_relative_path(ctx.file.f1))
|
||||
asserts.equals(env, "../../../lib/paths.bzl", paths.to_output_relative_path(ctx.file.f2))
|
||||
asserts.equals(env, "external/external_test_repo/test_a", paths.to_output_relative_path(ctx.file.f3))
|
||||
asserts.equals(env, "lib/tests/template.txt", paths.to_output_relative_path(ctx.file.f4))
|
||||
return unittest.end(env)
|
||||
|
||||
_ATTRS = {
|
||||
# source file in external repo
|
||||
"f1": attr.label(allow_single_file = True, default = "@bazel_skylib//:LICENSE"),
|
||||
# source file in current repo
|
||||
"f2": attr.label(allow_single_file = True, default = "//lib:paths.bzl"),
|
||||
# output file in external repo
|
||||
"f3": attr.label(allow_single_file = True, default = "@external_test_repo//:test_a"),
|
||||
# output file in current repo
|
||||
"f4": attr.label(allow_single_file = True, default = "//lib/tests:gen_template"),
|
||||
}
|
||||
|
||||
relative_file_test = unittest.make(_relative_file_test_impl)
|
||||
manifest_path_test = unittest.make(_manifest_path_test_impl, attrs = _ATTRS)
|
||||
output_relative_path_test = unittest.make(_output_relative_path_test_impl, attrs = _ATTRS)
|
||||
workspace_path_test = unittest.make(_workspace_path_test_impl, attrs = _ATTRS)
|
||||
|
||||
def paths_test_suite():
|
||||
|
@ -142,5 +157,6 @@ def paths_test_suite():
|
|||
"paths_tests",
|
||||
relative_file_test,
|
||||
manifest_path_test,
|
||||
output_relative_path_test,
|
||||
workspace_path_test,
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue