Skip to content

Commit

Permalink
Make PMD binary configurable
Browse files Browse the repository at this point in the history
To follow in the lead of spotbugs and checkstyle.
  • Loading branch information
shs96c committed Jan 20, 2022
1 parent da93854 commit f7c08ec
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 14 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Use checkstyle to lint the `srcs`.
## pmd_ruleset

<pre>
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>)
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>)
</pre>

Select a rule set for PMD tests.
Expand All @@ -122,6 +122,7 @@ Select a rule set for PMD tests.
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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 | |
| <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" |
| <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 |
| <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 | [] |
| <a id="pmd_ruleset-shallow"></a>shallow | Use the targetted output to increase PMD's depth of processing | Boolean | optional | True |

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


<a id="#pmd_binary"></a>

## pmd_binary

<pre>
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>)
</pre>

Macro for quickly generating a `java_binary` target for use with `pmd_ruleset`.

By default, this will set the `main_class` to point to the default one used by PMD
but it's ultimately a drop-replacement for a regular `java_binary` target.

At least one of `runtime_deps`, `deps`, and `srcs` must be specified so that the
`java_binary` target will be valid.

An example would be:

```starlark
pmd_binary(
name = "pmd",
runtime_deps = [
artifact("net.sourceforge.pmd:pmd-dist"),
],
)
```


**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="pmd_binary-name"></a>name | The name of the target | none |
| <a id="pmd_binary-main_class"></a>main_class | The main class to use for PMD. | <code>"net.sourceforge.pmd.PMD"</code> |
| <a id="pmd_binary-deps"></a>deps | The deps required for compiling this binary. May be omitted. | <code>None</code> |
| <a id="pmd_binary-runtime_deps"></a>runtime_deps | The deps required by PMD at runtime. May be omitted. | <code>None</code> |
| <a id="pmd_binary-srcs"></a>srcs | If you're compiling your own PMD binary, the sources to use. | <code>None</code> |
| <a id="pmd_binary-visibility"></a>visibility | <p align="center"> - </p> | <code>["//visibility:public"]</code> |
| <a id="pmd_binary-kwargs"></a>kwargs | <p align="center"> - </p> | none |


<a id="#spotbugs_binary"></a>

## spotbugs_binary
Expand Down
2 changes: 2 additions & 0 deletions docs/stardoc-input.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load(
_java_library = "java_library",
_java_test = "java_test",
_java_test_suite = "java_test_suite",
_pmd_binary = "pmd_binary",
_pmd_ruleset = "pmd_ruleset",
_pmd_test = "pmd_test",
_spotbugs_binary = "spotbugs_binary",
Expand All @@ -25,6 +26,7 @@ java_library = _java_library
java_junit5_test = _java_junit5_test
java_test = _java_test
java_test_suite = _java_test_suite
pmd_binary = _pmd_binary
pmd_ruleset = _pmd_ruleset
pmd_test = _pmd_test
spotbugs_binary = _spotbugs_binary
Expand Down
5 changes: 2 additions & 3 deletions java/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//java/private:artifact.bzl", "artifact")
load("//java/private:checkstyle_config.bzl", "checkstyle_binary", "checkstyle_config")
load("//java/private:pmd_ruleset.bzl", "pmd_binary", "pmd_ruleset")
load("//java/private:spotbugs_config.bzl", "spotbugs_binary", "spotbugs_config")
load("//java/private:pmd_ruleset.bzl", "pmd_ruleset")

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

Expand Down Expand Up @@ -33,9 +33,8 @@ checkstyle_binary(
]
)

java_binary(
pmd_binary(
name = "pmd",
main_class = "net.sourceforge.pmd.PMD",
runtime_deps = [
artifact("net.sourceforge.pmd:pmd-dist"),
],
Expand Down
3 changes: 2 additions & 1 deletion java/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load(
_java_test = "java_test",
)
load("//java/private:pmd.bzl", _pmd_test = "pmd_test")
load("//java/private:pmd_ruleset.bzl", _pmd_ruleset = "pmd_ruleset")
load("//java/private:pmd_ruleset.bzl", _pmd_binary = "pmd_binary", _pmd_ruleset = "pmd_ruleset")
load("//java/private:spotbugs.bzl", _spotbugs_test = "spotbugs_test")
load(
"//java/private:spotbugs_config.bzl",
Expand All @@ -36,6 +36,7 @@ java_test = _java_test
java_test_suite = _java_test_suite
JUNIT5_DEPS = _JUNIT5_DEPS
JUNIT5_VINTAGE_DEPS = _JUNIT5_VINTAGE_DEPS
pmd_binary = _pmd_binary
pmd_ruleset = _pmd_ruleset
pmd_test = _pmd_test
spotbugs_binary = _spotbugs_binary
Expand Down
13 changes: 4 additions & 9 deletions java/private/pmd.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
load(":pmd_ruleset.bzl", "PmdInfo")

def _pmd_test_impl(ctx):
pmd_info = ctx.attr.ruleset[PmdInfo]

cmd = [
ctx.executable._pmd.short_path,
pmd_info.binary.short_path,
]

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

pmd_info = ctx.attr.ruleset[PmdInfo]

file_list = ctx.actions.declare_file("%s-pmd-srcs" % ctx.label.name)
ctx.actions.write(
file_list,
Expand Down Expand Up @@ -50,7 +50,7 @@ def _pmd_test_impl(ctx):

return DefaultInfo(
executable = out,
runfiles = runfiles.merge(ctx.attr._pmd[DefaultInfo].default_runfiles),
runfiles = runfiles.merge(ctx.attr.ruleset[DefaultInfo].default_runfiles),
)

pmd_test = rule(
Expand All @@ -71,11 +71,6 @@ pmd_test = rule(
[PmdInfo],
],
),
"_pmd": attr.label(
cfg = "exec",
executable = True,
default = "//java:pmd",
),
},
executable = True,
test = True,
Expand Down
56 changes: 56 additions & 0 deletions java/private/pmd_ruleset.bzl
Original file line number Diff line number Diff line change
@@ -1,20 +1,70 @@

def pmd_binary(
name,
main_class = "net.sourceforge.pmd.PMD",
deps = None,
runtime_deps = None,
srcs = None,
visibility = ["//visibility:public"],
**kwargs):
"""Macro for quickly generating a `java_binary` target for use with `pmd_ruleset`.
By default, this will set the `main_class` to point to the default one used by PMD
but it's ultimately a drop-replacement for a regular `java_binary` target.
At least one of `runtime_deps`, `deps`, and `srcs` must be specified so that the
`java_binary` target will be valid.
An example would be:
```starlark
pmd_binary(
name = "pmd",
runtime_deps = [
artifact("net.sourceforge.pmd:pmd-dist"),
],
)
```
Args:
name: The name of the target
main_class: The main class to use for PMD.
deps: The deps required for compiling this binary. May be omitted.
runtime_deps: The deps required by PMD at runtime. May be omitted.
srcs: If you're compiling your own PMD binary, the sources to use.
"""

native.java_binary(
name = name,
main_class = main_class,
srcs = srcs,
deps = deps,
runtime_deps = runtime_deps,
visibility = visibility,
**kwargs
)


PmdInfo = provider(
fields = {
"format": "The format to generate reports in",
"rulesets": "Depset of files containing rulesets",
"shallow": "Whether to use target outputs as part of processing",
"binary": "The PMD binary to use",
},
)

def _pmd_ruleset_impl(ctx):
return [
DefaultInfo(
files = depset(ctx.files.rulesets),
runfiles = ctx.attr.pmd_binary[DefaultInfo].default_runfiles,
),
PmdInfo(
rulesets = depset(ctx.files.rulesets),
format = ctx.attr.format,
shallow = ctx.attr.shallow,
binary = ctx.executable.pmd_binary,
),
]

Expand All @@ -35,6 +85,12 @@ pmd_ruleset = rule(
doc = "Use the targetted output to increase PMD's depth of processing",
default = True,
),
"pmd_binary": attr.label(
doc = "PMD binary to use.",
default = "//java:pmd",
executable = True,
cfg = "exec",
),
},
provides = [
PmdInfo,
Expand Down

0 comments on commit f7c08ec

Please sign in to comment.