This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerator.bzl
235 lines (200 loc) · 7.25 KB
/
generator.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
# Changed 2020 by Zenseact AB
load("@bazel_skylib//lib:shell.bzl", "shell")
load("//:rules.bzl", "LinterInfo")
SUPPORTED_LANGUAGES = [
"python",
"golang",
"jsonnet",
"ruby",
"rust",
"cc",
"java"
]
# Aspects that accept parameters cannot be called on the command line.
# As I want to call the linter aspect on the command line I can't pass parameters.
# Thus, I can't pass a 'debug' parameter to the aspect.
# So here I make a global to allow switching DEBUG logs on an off
DEBUG=False
def debug(msg):
if DEBUG:
print(msg)
def _select_linter(ctx):
kind = ctx.rule.kind
if kind in ["py_library", "py_binary", "py_test"]:
linter = ctx.attr._python_linter
elif kind in ["go_library", "go_binary", "go_test"]:
linter = ctx.attr._golang_linter
elif kind in ["jsonnet_library", "jsonnet_to_json"]:
linter = ctx.attr._jsonnet_linter
elif kind in ["ruby_library", "ruby_binary", "ruby_test"]:
linter = ctx.attr._ruby_linter
elif kind in ["rust_library", "rust_binary", "rust_test"]:
linter = ctx.attr._rust_linter
elif kind in ["cc_library", "cc_binary", "cc_test"]:
linter = ctx.attr._cc_linter
elif kind in ["java_library", "java_binary", "java_test"]:
linter = ctx.attr._java_linter
else:
linter = None
if linter == None:
debug("No linter for rule kind: {}".format(kind))
if linter != None and str(linter.label) == "@linting_system//:no-op":
linter = None
return linter
def _gather_srcs(src_lst):
return [
file for src in src_lst
for file in src.files.to_list()
]
def _lint_workspace_aspect_impl(target, ctx):
no_source_files = (
not hasattr(ctx.rule.attr, 'hdrs') and
not hasattr(ctx.rule.attr, 'srcs') and
not hasattr(ctx.rule.attr, 'src')
)
if (
# Ignore targets in external repos
ctx.label.workspace_name or
# Ignore targets without source files
no_source_files
):
return []
linter = _select_linter(ctx)
if not linter:
return []
src_files = []
if hasattr(ctx.rule.attr, 'hdrs'):
src_files += _gather_srcs(ctx.rule.attr.hdrs)
if hasattr(ctx.rule.attr, 'srcs'):
src_files += _gather_srcs(ctx.rule.attr.srcs)
if hasattr(ctx.rule.attr, 'src'):
src_files += _gather_srcs([ctx.rule.attr.src])
# src_files may be empty at this point
if (len(src_files) == 0):
return []
# Note: Don't add ctx.label.package to prefix as it is implicitly added
prefix = "__linting_system/" + ctx.label.name
outputs = []
for f in src_files:
declared_path = "{}/{}".format(prefix, f.path)
o = ctx.actions.declare_file(declared_path)
outputs.append(o)
pairs = [
"{};{}".format(left, right) for left, right in
zip([f.path for f in src_files], [o.path for o in outputs])
]
report_out = ctx.actions.declare_file("%s.lint_report" % ctx.rule.attr.name)
linter_exe = linter[LinterInfo].executable_path or \
linter[LinterInfo].executable[DefaultInfo].files_to_run.executable.path
linter_name = linter_exe.split("/")[-1]
linter_config_opt = linter[LinterInfo].config_option
if linter[LinterInfo].config != None:
linter_config = linter[LinterInfo].config[DefaultInfo].files.to_list()
else:
linter_config = None
linter_config_str = linter[LinterInfo].config_str
if linter_config_opt and not linter_config:
fail_msg = (
"When specifying linter configuration for {}, ".format(linter_name) +
"'config' must be specified if 'config_option' is set."
)
fail(msg=fail_msg)
if linter_config_str and linter_config_opt:
fail(msg="Don't both specify a config file option and raw string config")
if linter_config_opt:
configuration = " ".join(
[
"{} {}".format(linter_config_opt, shell.quote(config.path))
for config in linter_config
],
)
elif linter_config_str:
configuration = linter_config_str
else:
configuration = ""
linter_inputs = src_files
if linter_config:
linter_inputs.extend(linter_config)
linter_template_expanded_exe = ctx.actions.declare_file(
"%s_linter_exe" % ctx.rule.attr.name
)
ctx.actions.expand_template(
template = ctx.file._template,
output = linter_template_expanded_exe,
substitutions = {
"{LINTER_EXE}": linter_exe,
"{LINTER_EXE_CONFIG}": configuration,
"{LINTER_SRCS}": " ".join([
shell.quote(o.path) for
o in outputs
]),
"{REPORT}": shell.quote(report_out.path),
},
is_executable = True,
)
# If the linter exe is configured with `executable_path`, eg. `executable_path = "/usr/local/bin/black"`,
# do not attempt to resolve `executable` as it will be `None` not a `Target` object.
tool_inputs = []
tool_input_manifests = None
if linter[LinterInfo].executable:
tool_inputs, tool_input_manifests = ctx.resolve_tools(tools = [linter[LinterInfo].executable])
ctx.actions.run(
outputs = outputs + [report_out],
inputs = linter_inputs,
executable = linter_template_expanded_exe,
tools = tool_inputs,
arguments = pairs,
mnemonic = "MirrorAndLint",
use_default_shell_env = True,
input_manifests = tool_input_manifests,
)
return [
DefaultInfo(files = depset(outputs + [report_out])),
OutputGroupInfo(
report = depset(outputs + [report_out]),
)
]
def linting_aspect_generator(
name,
linters,
):
linters_map = { lang: "@linting_system//:no-op" for lang in SUPPORTED_LANGUAGES }
for l in linters:
linter_label = Label(l)
# TODO(Jonathon): Don't allow double-writing to single language (ie. duplicate linters)
if linter_label.name not in SUPPORTED_LANGUAGES:
err_msg = "Linter label names must match exactly a support language. Supported: {}".format(SUPPORTED_LANGUAGES)
fail(err_msg)
linters_map[linter_label.name] = linter_label
return aspect(
implementation = _lint_workspace_aspect_impl,
attr_aspects = [],
attrs = {
'_template' : attr.label(
default = Label('@linting_system//:lint.sh.TEMPLATE'),
allow_single_file = True,
),
# LINTERS
'_python_linter' : attr.label(
default = linters_map["python"],
),
'_golang_linter' : attr.label(
default = linters_map["golang"],
),
'_jsonnet_linter' : attr.label(
default = linters_map["jsonnet"],
),
'_ruby_linter' : attr.label(
default = linters_map["ruby"],
),
'_rust_linter' : attr.label(
default = linters_map["rust"],
),
'_cc_linter' : attr.label(
default = linters_map["cc"],
),
'_java_linter' : attr.label(
default = linters_map["java"],
),
},
)