Skip to content

Instantly share code, notes, and snippets.

@jmhodges
Last active January 24, 2019 22:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhodges/8037c7fc072979aff8345c620834387d to your computer and use it in GitHub Desktop.
Save jmhodges/8037c7fc072979aff8345c620834387d to your computer and use it in GitHub Desktop.
load("@io_bazel_rules_go//go:def.bzl", "go_path")
load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary", "GoPath")
_MOCKGEN_TOOL = "@com_github_golang_mock//mockgen"
def _gomock_sh_impl(ctx):
go_toolchain = ctx.toolchains["@io_bazel_rules_go//go:toolchain"]
gopath = "$(pwd)/" + ctx.var["BINDIR"] + "/" + ctx.attr.gopath_dep[GoPath].gopath
pkg_args = []
if ctx.attr.package != '':
pkg_args = ["-package", ctx.attr.package]
args = pkg_args + [
"-destination", "$(pwd)/"+ ctx.outputs.out.path,
ctx.attr.library[GoLibrary].importpath,
",".join(ctx.attr.interfaces),
]
ctx.actions.run_shell(
outputs = [ctx.outputs.out],
inputs = [ctx.file._mockgen] + ctx.attr.gopath_dep.files.to_list(),
command = """
export GOROOT={goroot} &&
export PATH=$GOROOT/bin:$PATH &&
export GOPATH={gopath} &&
{mockgen} {args}
""".format(
goroot=go_toolchain.paths.root.path,
gopath=gopath,
mockgen="$(pwd)/"+ctx.file._mockgen.path,
args = " ".join(args)
)
)
_gomock_sh = rule(
_gomock_sh_impl,
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set, mockgen will use its reflect code to generate the mocks. source cannot also be set when this is set.",
providers = [GoLibrary],
mandatory = True,
),
"gopath_dep": attr.label(
doc = "The go_path label to use to create the GOPATH for the given library",
providers=[GoPath],
mandatory = True
),
"out": attr.output(
doc = "The new Go file to emit the generated mocks into",
mandatory = True
),
"interfaces": attr.string_list(
allow_empty = False,
doc = "The names of the Go interfaces to generate mocks for",
mandatory = True,
),
"package": attr.string(
doc = "The name of the package the generated mocks should be in. If not specified, uses mockgen's default.",
),
"_mockgen": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)
def gomock(name, library, out, **kwargs):
gopath_name = name + "_gomock_gopath"
go_path(
name = gopath_name,
# FIXME make this _MOCKGEN_TOOL overridable like the other one. This is because of a weird runtime dep on github.com/golang/mock/mockgen/model
deps = [library, _MOCKGEN_TOOL],
)
_gomock_sh(
name = name,
library = library,
gopath_dep = gopath_name,
out = out,
**kwargs
)
@Globegitter
Copy link

With the latest rules_go and latest go I just had to remove the two GOROOT lines:

export GOROOT={goroot} &&
goroot=go_toolchain.paths.root.path,

To use it (to mock a grpc client) all I had to do was:

proto_library(...)
go_proto_library(...)

go_library(
    name = "go_default_library",
    embed = [":public_go_proto"],
    importpath = "gitlab.com/...",
)

gomock(
    name = "public_grpc_mock",
    out = "public_grpc_mock.go",
    interfaces = ["BillingClient"],
    library = ":public_go_proto",
)

go_library(
    name = "public_grpc_mock_library",
    srcs = [":public_grpc_mock"],
    importpath = "gitlab.com/...",
    deps = [
        ":go_default_library",
        "@com_github_gogo_protobuf//types:go_default_library",
        "@com_github_golang_mock//gomock:go_default_library",
        "@org_golang_google_grpc//:go_default_library",
    ],
)

The importpaths are of course proper importpaths, I just redacted them here.

@nclinger
Copy link

Any thoughts on how to get the GOROOT now that toolchain doesn't contain it? Been searching through the go_rule docs and can't find a good way to do this. The only thing I found so far is using go_context but that seems to contain the Bazel GOROOT (external/go_sdk?), not my local GOROOT (/usr/local/go). Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment