Skip to content

Instantly share code, notes, and snippets.

@excavador
Last active August 27, 2017 15:47
Show Gist options
  • Save excavador/b49c782ef87605c865aa8098a5139872 to your computer and use it in GitHub Desktop.
Save excavador/b49c782ef87605c865aa8098a5139872 to your computer and use it in GitHub Desktop.
load("//bzl:fmt.bzl", "fmt")
fmt(name="fmt")
load("@io_bazel_rules_go//go/private:common.bzl", "get_go_toolchain")
_script_content = """
BASE=$(pwd)
WORKSPACE=$(dirname $(readlink WORKSPACE))
cd "$WORKSPACE"
export GOROOT="{goroot}"
gofmt {options} {target}
"""
_script_name = "gofmt.bash"
def _fmt_script_impl(ctx):
go_toolchain = get_go_toolchain(ctx)
script_file = ctx.new_file(ctx.label.name + ".bash")
ctx.file_action(output=script_file, executable=True, content=_script_content.format(
go=go_toolchain.go.path,
goroot=go_toolchain.root.path,
options=ctx.attr.options,
target=" ".join(ctx.attr.target),
))
return struct(
files = depset([script_file]),
runfiles = ctx.runfiles([script_file]),
)
_fmt_script = rule(
_fmt_script_impl,
attrs = {
"options": attr.string(),
"target": attr.string_list(),
"_go_toolchain": attr.label(default = Label("@io_bazel_rules_go_toolchain//:go_toolchain")),
}
)
def fmt(name, options="-l -w", target=["."]):
script_name = name + ".bash"
_fmt_script(
name = script_name,
options = options,
target = target
)
native.sh_binary(
name = name,
srcs = [script_name],
data = ["//:WORKSPACE"],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment