Skip to content

Commit f7c08ec

Browse files
committed
Make PMD binary configurable
To follow in the lead of spotbugs and checkstyle.
1 parent da93854 commit f7c08ec

File tree

6 files changed

+110
-14
lines changed

6 files changed

+110
-14
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Use checkstyle to lint the `srcs`.
110110
## pmd_ruleset
111111

112112
<pre>
113-
pmd_ruleset(<a href="#pmd_ruleset-name">name</a>, <a href="#pmd_ruleset-format">format</a>, <a href="#pmd_ruleset-rulesets">rulesets</a>, <a href="#pmd_ruleset-shallow">shallow</a>)
113+
pmd_ruleset(<a href="#pmd_ruleset-name">name</a>, <a href="#pmd_ruleset-format">format</a>, <a href="#pmd_ruleset-pmd_binary">pmd_binary</a>, <a href="#pmd_ruleset-rulesets">rulesets</a>, <a href="#pmd_ruleset-shallow">shallow</a>)
114114
</pre>
115115

116116
Select a rule set for PMD tests.
@@ -122,6 +122,7 @@ Select a rule set for PMD tests.
122122
| :------------- | :------------- | :------------- | :------------- | :------------- |
123123
| <a id="pmd_ruleset-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
124124
| <a id="pmd_ruleset-format"></a>format | Generate report in the given format. One of html, text, or xml (default is xml) | String | optional | "xml" |
125+
| <a id="pmd_ruleset-pmd_binary"></a>pmd_binary | PMD binary to use. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //java:pmd |
125126
| <a id="pmd_ruleset-rulesets"></a>rulesets | Use these rulesets. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
126127
| <a id="pmd_ruleset-shallow"></a>shallow | Use the targetted output to increase PMD's depth of processing | Boolean | optional | True |
127128

@@ -394,6 +395,48 @@ attribute to allow all the tests to be run in one go.
394395
| <a id="java_test_suite-kwargs"></a>kwargs | <p align="center"> - </p> | none |
395396

396397

398+
<a id="#pmd_binary"></a>
399+
400+
## pmd_binary
401+
402+
<pre>
403+
pmd_binary(<a href="#pmd_binary-name">name</a>, <a href="#pmd_binary-main_class">main_class</a>, <a href="#pmd_binary-deps">deps</a>, <a href="#pmd_binary-runtime_deps">runtime_deps</a>, <a href="#pmd_binary-srcs">srcs</a>, <a href="#pmd_binary-visibility">visibility</a>, <a href="#pmd_binary-kwargs">kwargs</a>)
404+
</pre>
405+
406+
Macro for quickly generating a `java_binary` target for use with `pmd_ruleset`.
407+
408+
By default, this will set the `main_class` to point to the default one used by PMD
409+
but it's ultimately a drop-replacement for a regular `java_binary` target.
410+
411+
At least one of `runtime_deps`, `deps`, and `srcs` must be specified so that the
412+
`java_binary` target will be valid.
413+
414+
An example would be:
415+
416+
```starlark
417+
pmd_binary(
418+
name = "pmd",
419+
runtime_deps = [
420+
artifact("net.sourceforge.pmd:pmd-dist"),
421+
],
422+
)
423+
```
424+
425+
426+
**PARAMETERS**
427+
428+
429+
| Name | Description | Default Value |
430+
| :------------- | :------------- | :------------- |
431+
| <a id="pmd_binary-name"></a>name | The name of the target | none |
432+
| <a id="pmd_binary-main_class"></a>main_class | The main class to use for PMD. | <code>"net.sourceforge.pmd.PMD"</code> |
433+
| <a id="pmd_binary-deps"></a>deps | The deps required for compiling this binary. May be omitted. | <code>None</code> |
434+
| <a id="pmd_binary-runtime_deps"></a>runtime_deps | The deps required by PMD at runtime. May be omitted. | <code>None</code> |
435+
| <a id="pmd_binary-srcs"></a>srcs | If you're compiling your own PMD binary, the sources to use. | <code>None</code> |
436+
| <a id="pmd_binary-visibility"></a>visibility | <p align="center"> - </p> | <code>["//visibility:public"]</code> |
437+
| <a id="pmd_binary-kwargs"></a>kwargs | <p align="center"> - </p> | none |
438+
439+
397440
<a id="#spotbugs_binary"></a>
398441

399442
## spotbugs_binary

docs/stardoc-input.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load(
99
_java_library = "java_library",
1010
_java_test = "java_test",
1111
_java_test_suite = "java_test_suite",
12+
_pmd_binary = "pmd_binary",
1213
_pmd_ruleset = "pmd_ruleset",
1314
_pmd_test = "pmd_test",
1415
_spotbugs_binary = "spotbugs_binary",
@@ -25,6 +26,7 @@ java_library = _java_library
2526
java_junit5_test = _java_junit5_test
2627
java_test = _java_test
2728
java_test_suite = _java_test_suite
29+
pmd_binary = _pmd_binary
2830
pmd_ruleset = _pmd_ruleset
2931
pmd_test = _pmd_test
3032
spotbugs_binary = _spotbugs_binary

java/BUILD.bazel

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load("//java/private:artifact.bzl", "artifact")
33
load("//java/private:checkstyle_config.bzl", "checkstyle_binary", "checkstyle_config")
4+
load("//java/private:pmd_ruleset.bzl", "pmd_binary", "pmd_ruleset")
45
load("//java/private:spotbugs_config.bzl", "spotbugs_binary", "spotbugs_config")
5-
load("//java/private:pmd_ruleset.bzl", "pmd_ruleset")
66

77
package(default_visibility = ["//visibility:public"])
88

@@ -33,9 +33,8 @@ checkstyle_binary(
3333
]
3434
)
3535

36-
java_binary(
36+
pmd_binary(
3737
name = "pmd",
38-
main_class = "net.sourceforge.pmd.PMD",
3938
runtime_deps = [
4039
artifact("net.sourceforge.pmd:pmd-dist"),
4140
],

java/defs.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ load(
1818
_java_test = "java_test",
1919
)
2020
load("//java/private:pmd.bzl", _pmd_test = "pmd_test")
21-
load("//java/private:pmd_ruleset.bzl", _pmd_ruleset = "pmd_ruleset")
21+
load("//java/private:pmd_ruleset.bzl", _pmd_binary = "pmd_binary", _pmd_ruleset = "pmd_ruleset")
2222
load("//java/private:spotbugs.bzl", _spotbugs_test = "spotbugs_test")
2323
load(
2424
"//java/private:spotbugs_config.bzl",
@@ -36,6 +36,7 @@ java_test = _java_test
3636
java_test_suite = _java_test_suite
3737
JUNIT5_DEPS = _JUNIT5_DEPS
3838
JUNIT5_VINTAGE_DEPS = _JUNIT5_VINTAGE_DEPS
39+
pmd_binary = _pmd_binary
3940
pmd_ruleset = _pmd_ruleset
4041
pmd_test = _pmd_test
4142
spotbugs_binary = _spotbugs_binary

java/private/pmd.bzl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
load(":pmd_ruleset.bzl", "PmdInfo")
22

33
def _pmd_test_impl(ctx):
4+
pmd_info = ctx.attr.ruleset[PmdInfo]
5+
46
cmd = [
5-
ctx.executable._pmd.short_path,
7+
pmd_info.binary.short_path,
68
]
79

810
# We want to disable the suggestion to use the analysis cache
@@ -12,8 +14,6 @@ def _pmd_test_impl(ctx):
1214
inputs = []
1315
transitive_inputs = depset()
1416

15-
pmd_info = ctx.attr.ruleset[PmdInfo]
16-
1717
file_list = ctx.actions.declare_file("%s-pmd-srcs" % ctx.label.name)
1818
ctx.actions.write(
1919
file_list,
@@ -50,7 +50,7 @@ def _pmd_test_impl(ctx):
5050

5151
return DefaultInfo(
5252
executable = out,
53-
runfiles = runfiles.merge(ctx.attr._pmd[DefaultInfo].default_runfiles),
53+
runfiles = runfiles.merge(ctx.attr.ruleset[DefaultInfo].default_runfiles),
5454
)
5555

5656
pmd_test = rule(
@@ -71,11 +71,6 @@ pmd_test = rule(
7171
[PmdInfo],
7272
],
7373
),
74-
"_pmd": attr.label(
75-
cfg = "exec",
76-
executable = True,
77-
default = "//java:pmd",
78-
),
7974
},
8075
executable = True,
8176
test = True,

java/private/pmd_ruleset.bzl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,70 @@
1+
2+
def pmd_binary(
3+
name,
4+
main_class = "net.sourceforge.pmd.PMD",
5+
deps = None,
6+
runtime_deps = None,
7+
srcs = None,
8+
visibility = ["//visibility:public"],
9+
**kwargs):
10+
"""Macro for quickly generating a `java_binary` target for use with `pmd_ruleset`.
11+
12+
By default, this will set the `main_class` to point to the default one used by PMD
13+
but it's ultimately a drop-replacement for a regular `java_binary` target.
14+
15+
At least one of `runtime_deps`, `deps`, and `srcs` must be specified so that the
16+
`java_binary` target will be valid.
17+
18+
An example would be:
19+
20+
```starlark
21+
pmd_binary(
22+
name = "pmd",
23+
runtime_deps = [
24+
artifact("net.sourceforge.pmd:pmd-dist"),
25+
],
26+
)
27+
```
28+
29+
Args:
30+
name: The name of the target
31+
main_class: The main class to use for PMD.
32+
deps: The deps required for compiling this binary. May be omitted.
33+
runtime_deps: The deps required by PMD at runtime. May be omitted.
34+
srcs: If you're compiling your own PMD binary, the sources to use.
35+
"""
36+
37+
native.java_binary(
38+
name = name,
39+
main_class = main_class,
40+
srcs = srcs,
41+
deps = deps,
42+
runtime_deps = runtime_deps,
43+
visibility = visibility,
44+
**kwargs
45+
)
46+
47+
148
PmdInfo = provider(
249
fields = {
350
"format": "The format to generate reports in",
451
"rulesets": "Depset of files containing rulesets",
552
"shallow": "Whether to use target outputs as part of processing",
53+
"binary": "The PMD binary to use",
654
},
755
)
856

957
def _pmd_ruleset_impl(ctx):
1058
return [
1159
DefaultInfo(
1260
files = depset(ctx.files.rulesets),
61+
runfiles = ctx.attr.pmd_binary[DefaultInfo].default_runfiles,
1362
),
1463
PmdInfo(
1564
rulesets = depset(ctx.files.rulesets),
1665
format = ctx.attr.format,
1766
shallow = ctx.attr.shallow,
67+
binary = ctx.executable.pmd_binary,
1868
),
1969
]
2070

@@ -35,6 +85,12 @@ pmd_ruleset = rule(
3585
doc = "Use the targetted output to increase PMD's depth of processing",
3686
default = True,
3787
),
88+
"pmd_binary": attr.label(
89+
doc = "PMD binary to use.",
90+
default = "//java:pmd",
91+
executable = True,
92+
cfg = "exec",
93+
),
3894
},
3995
provides = [
4096
PmdInfo,

0 commit comments

Comments
 (0)