forked from jmhodges/bazel_gomock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomock.bzl
249 lines (232 loc) · 9.31 KB
/
gomock.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_rule")
load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary")
_MOCKGEN_TOOL = "@com_github_golang_mock//mockgen"
_MOCKGEN_MODEL_LIB = "@com_github_golang_mock//mockgen/model:go_default_library"
def _gomock_source_impl(ctx):
args = ["-source", ctx.file.source.path]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
args += [",".join(ctx.attr.interfaces)]
out = ctx.outputs.out
cmd = ctx.file.mockgen_tool
go_ctx = go_context(ctx)
inputs = go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools + [ctx.file.source]
# We can use the go binary from the stdlib for most of the environment
# variables, but our GOPATH is specific to the library target we were given.
ctx.actions.run_shell(
outputs = [out],
inputs = inputs,
tools = [
cmd,
go_ctx.go,
],
command = """
source <($PWD/{godir}/go env) &&
export PATH=$GOROOT/bin:$PWD/{godir}:$PATH &&
{cmd} {args} > {out}
""".format(
godir = go_ctx.go.path[:-1 - len(go_ctx.go.basename)],
cmd = "$(pwd)/" + cmd.path,
args = " ".join(args),
out = out.path,
),
)
_gomock_source = go_rule(
_gomock_source_impl,
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
providers = [GoLibrary],
mandatory = True,
),
"source": attr.label(
doc = "A Go source file to find all the interfaces to generate mocks for. See also the docs for library.",
mandatory = False,
allow_single_file = 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. If not set, all of the interfaces in the library or source file will have mocks generated for them.",
mandatory = True,
),
"package": attr.string(
doc = "The name of the package the generated mocks should be in. If not specified, uses mockgen's default.",
),
"self_package": attr.string(
doc = "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.",
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
mandatory = False,
),
},
)
def gomock(name, library, out, **kwargs):
mockgen_tool = _MOCKGEN_TOOL
if kwargs.get("mockgen_tool", None):
mockgen_tool = kwargs["mockgen_tool"]
if kwargs.get("source", None):
_gomock_source(
name = name,
library = library,
out = out,
**kwargs
)
else:
_gomock_reflect(
name = name,
library = library,
out = out,
mockgen_tool = mockgen_tool,
**kwargs
)
def _gomock_reflect(name, library, out, mockgen_tool, **kwargs):
interfaces = kwargs.get("interfaces", None)
mockgen_model_lib = _MOCKGEN_MODEL_LIB
if kwargs.get("mockgen_model_library", None):
mockgen_model_lib = kwargs["mockgen_model_library"]
prog_src = name + "_gomock_prog"
prog_src_out = prog_src + ".go"
_gomock_prog_gen(
name = prog_src,
interfaces = interfaces,
library = library,
package = kwargs.get("package", None),
out = prog_src_out,
mockgen_tool = mockgen_tool,
)
prog_bin = name + "_gomock_prog_bin"
go_binary(
name = prog_bin,
srcs = [prog_src_out],
deps = [library, mockgen_model_lib],
)
_gomock_prog_exec(
name = name,
interfaces = interfaces,
library = library,
package = kwargs.get("package", None),
out = out,
prog_bin = prog_bin,
mockgen_tool = mockgen_tool,
self_package = kwargs.get("self_package", None),
)
def _gomock_prog_gen_impl(ctx):
args = ["-prog_only"]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
args += [ctx.attr.library[GoLibrary].importpath]
args += [",".join(ctx.attr.interfaces)]
cmd = ctx.file.mockgen_tool
out = ctx.outputs.out
ctx.actions.run_shell(
outputs = [out],
tools = [cmd],
command = """
{cmd} {args} > {out}
""".format(
cmd = "$(pwd)/" + cmd.path,
args = " ".join(args),
out = out.path,
),
)
_gomock_prog_gen = go_rule(
_gomock_prog_gen_impl,
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks.",
providers = [GoLibrary],
mandatory = True,
),
"out": attr.output(
doc = "The new Go source file put the mock generator code",
mandatory = True,
),
"interfaces": attr.string_list(
allow_empty = False,
doc = "The names of the Go interfaces to generate mocks for. If not set, all of the interfaces in the library or source file will have mocks generated for them.",
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_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
mandatory = False,
),
},
)
def _gomock_prog_exec_impl(ctx):
args = ["-exec_only", ctx.file.prog_bin.path]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
if ctx.attr.self_package != "":
args += ["-self_package", ctx.attr.self_package]
args += [ctx.attr.library[GoLibrary].importpath]
args += [",".join(ctx.attr.interfaces)]
ctx.actions.run_shell(
outputs = [ctx.outputs.out],
inputs = [ctx.file.prog_bin],
tools = [ctx.file.mockgen_tool],
command = """{cmd} {args} > {out}""".format(
cmd = "$(pwd)/" + ctx.file.mockgen_tool.path,
args = " ".join(args),
out = ctx.outputs.out.path,
),
env = {
# GOCACHE is required starting in Go 1.12
"GOCACHE": "./.gocache",
},
)
_gomock_prog_exec = go_rule(
_gomock_prog_exec_impl,
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
providers = [GoLibrary],
mandatory = True,
),
"out": attr.output(
doc = "The new Go source file to put the generated mock code",
mandatory = True,
),
"interfaces": attr.string_list(
allow_empty = False,
doc = "The names of the Go interfaces to generate mocks for. If not set, all of the interfaces in the library or source file will have mocks generated for them.",
mandatory = True,
),
"package": attr.string(
doc = "The name of the package the generated mocks should be in. If not specified, uses mockgen's default.",
),
"self_package": attr.string(
doc = "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.",
),
"prog_bin": attr.label(
doc = "The program binary generated by mockgen's -prog_only and compiled by bazel.",
allow_single_file = True,
executable = True,
cfg = "host",
mandatory = True,
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
mandatory = False,
),
},
)