|
1 | 1 | """Astrein linter aspect for aspect_rules_lint framework""" |
2 | 2 |
|
3 | | -_MNEMONIC = "AspectRulesLintAstrein" |
4 | | - |
5 | | -# Taken from https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl |
6 | | -# to avoid imports from private modules |
7 | | -def should_visit(rule, allow_kinds, allow_filegroup_tags = []): |
8 | | - """Determine whether a rule is meant to be visited by a linter aspect |
9 | | -
|
10 | | - A target with the "no-lint" tag will not be visited. |
| 3 | +load("@aspect_rules_lint//lint:defs.bzl", "filter_srcs", "noop_lint_action", "output_files", "should_visit") |
11 | 4 |
|
12 | | - Args: |
13 | | - rule: a [rules_attributes](https://bazel.build/rules/lib/builtins/rule_attributes.html) object |
14 | | - allow_kinds (list of string): return true if the rule's kind is in the list |
15 | | - allow_filegroup_tags (list of string): return true if the rule is a filegroup and has a tag in this list |
16 | | -
|
17 | | - Returns: |
18 | | - whether to apply the aspect on this rule |
19 | | - """ |
20 | | - if "no-lint" in rule.attr.tags: |
21 | | - return False |
22 | | - |
23 | | - if rule.kind in allow_kinds: |
24 | | - return True |
25 | | - if rule.kind == "filegroup": |
26 | | - for allow_tag in allow_filegroup_tags: |
27 | | - if allow_tag in rule.attr.tags: |
28 | | - return True |
29 | | - return False |
30 | | - |
31 | | -# Taken from https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl |
32 | | -# to avoid imports from private modules |
33 | | -def filter_srcs(rule): |
34 | | - if "lint-genfiles" in rule.attr.tags: |
35 | | - return rule.files.srcs |
36 | | - else: |
37 | | - return [s for s in rule.files.srcs if s.is_source and s.owner.workspace_name == ""] |
| 5 | +_MNEMONIC = "AspectRulesLintAstrein" |
38 | 6 |
|
39 | 7 | OUTFILE_FORMAT = "{label}.{mnemonic}.{suffix}" |
40 | 8 |
|
41 | | -# Taken from https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl |
42 | | -# to avoid imports from private modules |
43 | | -def output_files(mnemonic, target, ctx): |
44 | | - """Declare linter output files. |
45 | | -
|
46 | | - Args: |
47 | | - mnemonic: used as part of the filename |
48 | | - target: the target being visited by a linter aspect |
49 | | - ctx: the aspect context |
50 | | -
|
51 | | - Returns: |
52 | | - tuple of struct() of output files, and the OutputGroupInfo provider that the rule should return |
53 | | - """ |
54 | | - human_out = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "out")) |
55 | | - |
56 | | - # NB: named ".report" as there are existing callers depending on that |
57 | | - machine_out = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "report")) |
58 | | - |
59 | | - # The exit codes should instead be provided as action outputs so the build succeeds. |
60 | | - # Downstream tooling like `aspect lint` will be responsible for reading the exit codes |
61 | | - # and interpreting them. |
62 | | - human_exit_code = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "out.exit_code")) |
63 | | - machine_exit_code = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "report.exit_code")) |
64 | | - |
65 | | - human_outputs = [f for f in [human_out, human_exit_code] if f] |
66 | | - machine_outputs = [f for f in [machine_out, machine_exit_code] if f] |
67 | | - return struct( |
68 | | - human = struct( |
69 | | - out = human_out, |
70 | | - exit_code = human_exit_code, |
71 | | - ), |
72 | | - machine = struct( |
73 | | - out = machine_out, |
74 | | - exit_code = machine_exit_code, |
75 | | - ), |
76 | | - ), OutputGroupInfo( |
77 | | - rules_lint_human = depset(human_outputs), |
78 | | - rules_lint_machine = depset(machine_outputs), |
79 | | - # Legacy name used by existing callers. |
80 | | - # TODO(2.0): remove |
81 | | - rules_lint_report = depset(machine_outputs), |
82 | | - # Always cause the action to execute, even if the output isn't requested |
83 | | - _validation = depset([human_out]), |
84 | | - ) |
85 | | - |
86 | | -# Inspired by https://github.com/aspect-build/rules_lint/blob/main/lint/private/lint_aspect.bzl |
87 | | -def noop_lint_action(ctx, outputs): |
88 | | - """Generates an action that creates empty outputs when no files need linting. |
89 | | -
|
90 | | - Args: |
91 | | - ctx: Bazel context |
92 | | - outputs: Output files struct |
93 | | - """ |
94 | | - ctx.actions.write( |
95 | | - output = outputs.human.out, |
96 | | - content = "", |
97 | | - ) |
98 | | - if outputs.human.exit_code: |
99 | | - ctx.actions.write( |
100 | | - output = outputs.human.exit_code, |
101 | | - content = "0\n", |
102 | | - ) |
103 | | - ctx.actions.write( |
104 | | - output = outputs.machine.out, |
105 | | - content = "", |
106 | | - ) |
107 | | - if outputs.machine.exit_code: |
108 | | - ctx.actions.write( |
109 | | - output = outputs.machine.exit_code, |
110 | | - content = "0\n", |
111 | | - ) |
112 | | - |
113 | 9 | def astrein_action(ctx, executable, srcs, stdout, exit_code, format, astrein_runfiles): |
114 | 10 | """Run astrein linter on source files. |
115 | 11 |
|
|
0 commit comments