Skip to content

Commit

Permalink
template
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-thm committed Apr 16, 2024
1 parent 56eea0e commit 67d75af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
25 changes: 3 additions & 22 deletions multitool/cwd.bzl
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
"cwd: a rule for executing an executable in the BUILD_WORKING_DIRECTORY"
"multitool cwd execution rule"

def _cwd_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
content = "\n".join([
"#!/usr/bin/env bash",
"execdir=\"$PWD\"",
"pushd $BUILD_WORKING_DIRECTORY > /dev/null",
"\"$execdir/{tool}\" \"$@\"".format(tool = ctx.file.tool.short_path),
"popd > /dev/null",
"",
]),
output = output,
)
return [DefaultInfo(executable = output, runfiles = ctx.runfiles(files = [ctx.file.tool]))]
load("@rules_multitool//multitool/private:cwd.bzl", _cwd = "cwd")

cwd = rule(
implementation = _cwd_impl,
attrs = {
"tool": attr.label(mandatory = True, allow_single_file = True, executable = True, cfg = "exec"),
},
executable = True,
)
cwd = _cwd
4 changes: 4 additions & 0 deletions multitool/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

exports_files([
"cwd.template.sh",
])

bzl_library(
name = "multitool",
srcs = ["multitool.bzl"],
Expand Down
21 changes: 21 additions & 0 deletions multitool/private/cwd.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"cwd: a rule for executing an executable in the BUILD_WORKING_DIRECTORY"

def _cwd_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)
ctx.actions.expand_template(
template = ctx.file._template,
output = output,
substitutions = {
"{{tool}}": ctx.file.tool.short_path,
},
)
return [DefaultInfo(executable = output, runfiles = ctx.runfiles(files = [ctx.file.tool]))]

cwd = rule(
implementation = _cwd_impl,
attrs = {
"tool": attr.label(mandatory = True, allow_single_file = True, executable = True, cfg = "exec"),
"_template": attr.label(default = "//multitool/private:cwd.template.sh", allow_single_file = True),
},
executable = True,
)
8 changes: 8 additions & 0 deletions multitool/private/cwd.template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

tool="{{tool}}"
execdir="$PWD"

pushd $BUILD_WORKING_DIRECTORY > /dev/null
"$execdir/$tool" "$@"
popd > /dev/null

0 comments on commit 67d75af

Please sign in to comment.