Skip to content

Commit 8c237f3

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
command alias: Pull out a path parameter
Summary: So that this can be controlled Finally, this diff also makes `command_alias` public. I'm doing this because I'm potentially interested in re-using the code in two places: 1. Toolchain definitions, specifically I'm about to write a `toolchain_command_alias` rule 2. Ideally, we'd replace `cmd_script` from `prelude/utils.bzl` with this. Unfortunately, I'm still a bit worried about the remaining bugs in `command_alias`, so that's a future task Reviewed By: dtolnay Differential Revision: D74133692 fbshipit-source-id: 0425e67f15aeb7c3be457c771a0a54ab1148479d
1 parent 1e55e5f commit 8c237f3

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

prelude/command_alias.bzl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ def command_alias_impl(ctx: AnalysisContext):
1919
else:
2020
base = _get_os_base(ctx, target_os.os)
2121

22-
output = _command_alias_impl(ctx, target_os, base, cmd_args(ctx.attrs.args), ctx.attrs.env)
22+
output = command_alias(
23+
ctx,
24+
"__command_alias_trampoline",
25+
target_os,
26+
base,
27+
cmd_args(ctx.attrs.args),
28+
ctx.attrs.env,
29+
)
2330

2431
default_info = DefaultInfo(
2532
default_output = output.output.default_outputs[0],
@@ -77,16 +84,21 @@ CommandAliasOutput = record(
7784
maybe_directly_runnable = cmd_args | None,
7885
)
7986

80-
def _command_alias_impl(
87+
def command_alias(
8188
ctx: AnalysisContext,
89+
# The path at which to write the output to, without an extension - that will be added
90+
path: str,
91+
# The target where this script should be able to run (this may actually be your exec platform)
8292
target_os: OsLookup,
93+
# Either the `RunInfo` to use, or in the case of a fat platform, the choice of `RunInfo`
94+
# depending on `uname`
8395
base: RunInfo | dict[str, RunInfo],
8496
args: cmd_args,
8597
env: dict[str, ArgLike]) -> CommandAliasOutput:
8698
if target_os.script == ScriptLanguage("sh"):
87-
trampoline, hidden = _command_alias_write_trampoline_unix(ctx, base, args, env)
99+
trampoline, hidden = _command_alias_write_trampoline_unix(ctx, path + ".sh", base, args, env)
88100
elif target_os.script == ScriptLanguage("bat"):
89-
trampoline, hidden = _command_alias_write_trampoline_windows(ctx, base, args, env)
101+
trampoline, hidden = _command_alias_write_trampoline_windows(ctx, path + ".bat", base, args, env)
90102
else:
91103
fail("Unsupported script language: {}".format(target_os.script))
92104

@@ -106,8 +118,7 @@ def _command_alias_impl(
106118

107119
def _command_alias_write_trampoline_unix(
108120
ctx: AnalysisContext,
109-
# Either the `RunInfo` to use, or in the case of a fat platform, the choice of `RunInfo`
110-
# depending on `uname`
121+
path: str,
111122
base: RunInfo | dict[str, RunInfo],
112123
args: cmd_args,
113124
env: dict[str, ArgLike]) -> (Artifact, cmd_args):
@@ -149,7 +160,7 @@ done
149160

150161
trampoline_args.add('exec "${R_ARGS[@]}" "$@"')
151162

152-
trampoline = ctx.actions.declare_output("__command_alias_trampoline.sh")
163+
trampoline = ctx.actions.declare_output(path)
153164
trampoline_args = cmd_args(
154165
trampoline_args,
155166
relative_to = (trampoline, 1),
@@ -166,6 +177,7 @@ done
166177

167178
def _command_alias_write_trampoline_windows(
168179
ctx: AnalysisContext,
180+
path: str,
169181
base: RunInfo,
170182
args: cmd_args,
171183
env: dict[str, ArgLike]) -> (Artifact, cmd_args):
@@ -192,7 +204,7 @@ def _command_alias_write_trampoline_windows(
192204

193205
trampoline_args.add(cmd)
194206

195-
trampoline = ctx.actions.declare_output("__command_alias_trampoline.bat")
207+
trampoline = ctx.actions.declare_output(path)
196208
trampoline_args = cmd_args(
197209
trampoline_args,
198210
relative_to = (trampoline, 1),

0 commit comments

Comments
 (0)