Skip to content

Instantly share code, notes, and snippets.

@excavador
Last active October 27, 2017 15:34
Show Gist options
  • Save excavador/a6bfb227fcfd1cc7e7e5a07355ab6669 to your computer and use it in GitHub Desktop.
Save excavador/a6bfb227fcfd1cc7e7e5a07355ab6669 to your computer and use it in GitHub Desktop.
load("//skylark:gofmt.bzl", "gofmt")
gofmt(name="gofmt")
def _gofmt_impl(ctx):
result = ctx.new_file(ctx.label.name)
workspace = ctx.file._workspace
gofmt = ctx.executable._gofmt
options = ctx.attr.options
target = " ".join(ctx.attr.target)
ctx.file_action(output=result, executable=True, content="""#!/bin/bash
WORKSPACE=$(dirname $(readlink {workspace}))
cd "$WORKSPACE"
file {gofmt}
echo {gofmt} {options} {target}
""".format(
gofmt=gofmt.path,
workspace=workspace.short_path,
options=options,
target=target,
))
return [DefaultInfo(runfiles=ctx.runfiles([workspace, gofmt]))]
gofmt = rule(
implementation = _gofmt_impl,
executable=True,
attrs = {
"options": attr.string(default = "-l -w"),
"target": attr.string_list(default=["exe", "lib"]),
"_gofmt": attr.label(cfg="host", executable=True, default=Label("@go_sdk//:bin/gofmt")),
"_workspace": attr.label(allow_single_file = True, default = Label("//:WORKSPACE")),
},
)
➜ bazel run :gofmt
ERROR: XXX/BUILD:18:1: in (an implicit dependency) attribute of gofmt rule //:gofmt: file '@go_sdk//:bin/gofmt' is misplaced here (expected no files).
ERROR: Analysis of target '//:gofmt' failed; build aborted: Analysis of target '//:gofmt' failed; build aborted.
INFO: Elapsed time: 0.135s
ERROR: Build failed. Not running target.
git_repository(
name = "io_bazel_rules_go",
remote = "https://github.com/bazelbuild/rules_go.git",
commit = "3fd320621566c44aad0f7df5d0f5858efd2bbcf6",
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains(go_version="1.9.2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment