Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions rules/android_lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ def _impl(ctx):
android_lint_results = _process_android_lint_issues(ctx, regenerate = False)

inputs = []
inputs.append(android_lint_results.output)
inputs.append(android_lint_results.xml_output)

files_output = []
if android_lint_results.xml_output != None:
files_output.append(android_lint_results.xml_output)
if android_lint_results.html_output != None:
files_output.append(android_lint_results.html_output)
return [
DefaultInfo(
runfiles = ctx.runfiles(files = inputs),
files = depset([android_lint_results.output]),
files = depset(files_output),
),
] + android_lint_results.providers

Expand Down
12 changes: 8 additions & 4 deletions rules/android_lint_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _test_impl(ctx):
android_lint_results = _process_android_lint_issues(ctx, regenerate = False)

inputs = []
inputs.append(android_lint_results.output)
inputs.append(android_lint_results.xml_output)
inputs.extend(ctx.attr._android_lint_output_validator.default_runfiles.files.to_list())

ctx.actions.write(
Expand All @@ -33,15 +33,19 @@ def _test_impl(ctx):
{executable} --lint_baseline "{lint_baseline}"
""".format(
executable = ctx.executable._android_lint_output_validator.short_path,
lint_baseline = android_lint_results.output.short_path,
lint_baseline = android_lint_results.xml_output.short_path,
),
)

files_info = [ctx.outputs.executable]
if android_lint_results.xml_output != None:
files_info.append(android_lint_results.xml_output)
if android_lint_results.html_output != None:
files_info.append(android_lint_results.html_output)
return [
DefaultInfo(
runfiles = ctx.runfiles(files = inputs),
executable = ctx.outputs.executable,
files = depset([ctx.outputs.executable, android_lint_results.output]),
files = depset(files_info),
),
] + android_lint_results.providers

Expand Down
6 changes: 6 additions & 0 deletions rules/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,11 @@ ATTRS = dict(
default = [],
doc = "Custom lint rules to run.",
),
output_formats = attr.string_list(
mandatory = False,
allow_empty = False,
default = ["xml"],
doc = "List of output formats to produce. Supported [xml, html]",
),
_use_auto_exec_groups = attr.bool(default = True),
)
44 changes: 32 additions & 12 deletions rules/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def _run_android_lint(
ctx,
android_lint,
module_name,
output,
xml_output,
html_output,
srcs,
deps,
resource_files,
Expand All @@ -43,7 +44,8 @@ def _run_android_lint(
ctx: The target context
android_lint: The Android Lint binary to use
module_name: The name of the module
output: The output file
xml_output: The xml_output file
html_output: The html_output file
srcs: The source files
deps: Depset of aars and jars to include on the classpath
resource_files: The Android resource files
Expand All @@ -63,7 +65,7 @@ def _run_android_lint(
android_lint_skip_bytecode_verifier: Disables bytecode verification
"""
inputs = []
outputs = [output]
outputs = []

args = ctx.actions.args()
args.set_param_file_format("multiline")
Expand Down Expand Up @@ -114,9 +116,14 @@ def _run_android_lint(
if android_lint_enable_check_dependencies:
args.add("--enable-check-dependencies")

# Declare the output file
args.add("--output", output)
outputs.append(output)
if xml_output != None:
args.add("--xml-output", xml_output)
outputs.append(xml_output)
if html_output != None:
args.add("--html-output", html_output)
outputs.append(html_output)
if len(outputs) == 0:
fail("Lint cannot have no outputs!")

ctx.actions.run(
mnemonic = "AndroidLint",
Expand Down Expand Up @@ -163,7 +170,7 @@ def process_android_lint_issues(ctx, regenerate):
regenerate: Whether to regenerate the baseline files

Returns:
A struct containing the output file and the providers
A struct containing the output files and the providers
"""

# Append the Android manifest file. Lint requires that the input manifest files be named
Expand Down Expand Up @@ -197,20 +204,29 @@ def process_android_lint_issues(ctx, regenerate):
_utils.list_or_depset_to_list(_utils.get_android_lint_toolchain(ctx).android_lint_config.files),
)

output = ctx.actions.declare_file("{}.xml".format(ctx.label.name))
baseline = getattr(ctx.file, "baseline", None)
xml_output = None
html_output = None
for output_format in ctx.attr.output_formats:
if output_format == "xml":
xml_output = ctx.actions.declare_file("{}.xml".format(ctx.label.name))
if output_format == "html":
html_output = ctx.actions.declare_file("{}.html".format(ctx.label.name))

_run_android_lint(
ctx,
android_lint = _utils.only(_utils.list_or_depset_to_list(_utils.get_android_lint_toolchain(ctx).android_lint.files)),
module_name = _get_module_name(ctx),
output = output,
xml_output = xml_output,
html_output = html_output,
srcs = ctx.files.srcs,
deps = depset(transitive = deps),
resource_files = ctx.files.resource_files,
manifest = manifest,
compile_sdk_version = _utils.get_android_lint_toolchain(ctx).compile_sdk_version,
java_language_level = _utils.get_android_lint_toolchain(ctx).java_language_level,
kotlin_language_level = _utils.get_android_lint_toolchain(ctx).kotlin_language_level,
baseline = getattr(ctx.file, "baseline", None),
baseline = baseline,
config = config,
warnings_as_errors = ctx.attr.warnings_as_errors,
custom_rules = ctx.files.custom_rules,
Expand All @@ -223,10 +239,14 @@ def process_android_lint_issues(ctx, regenerate):
)

return struct(
output = output,
baseline = baseline,
xml_output = xml_output,
html_output = html_output,
providers = [
_AndroidLintResultsInfo(
output = output,
baseline = baseline,
xml_output = xml_output,
html_output = html_output,
),
],
)
4 changes: 3 additions & 1 deletion rules/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
AndroidLintResultsInfo = provider(
"Info needed to evaluate lint results",
fields = {
"output": "The Android Lint baseline output",
"baseline": "The Android Lint baseline output",
"xml_output": "The Android Lint xml output",
"html_output": "The Android Lint html output",
},
)
12 changes: 9 additions & 3 deletions src/cli/AndroidLintActionArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ internal class AndroidLintActionArgs(
transform = argsParserPathTransformer,
)

val output: Path by parser.storing(
names = arrayOf("--output"),
val xmlOutput: Path? by parser.storing(
names = arrayOf("--xml-output"),
help = "",
transform = argsParserPathTransformer,
)
).default { null }

val htmlOutput: Path? by parser.storing(
names = arrayOf("--html-output"),
help = "",
transform = argsParserPathTransformer,
).default { null }

val resources: List<Path> by parser.adding(
names = arrayOf("--resource"),
Expand Down
12 changes: 10 additions & 2 deletions src/cli/AndroidLintRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ internal class AndroidLintRunner {
val args = mutableListOf(
"--project",
projectFilePath.pathString,
"--xml",
actionArgs.output.pathString,
"--path-variables",
"PWD=$rootDirPath",
"--exitcode",
Expand All @@ -113,6 +111,16 @@ internal class AndroidLintRunner {
cacheDirectoryPath.pathString,
"--client-id", "cli",
)
if (actionArgs.xmlOutput != null) {
args.add("--xml")
args.add(actionArgs.xmlOutput!!.pathString)
}

if (actionArgs.htmlOutput != null) {
args.add("--html")
args.add(actionArgs.htmlOutput!!.pathString)
}

if (actionArgs.warningsAsErrors) {
args.add("-Werror")
} else {
Expand Down
9 changes: 6 additions & 3 deletions tests/src/cli/AndroidLintActionArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class AndroidLintActionArgsTest {
"path/to/cli.jar",
"--src",
"path/to/Foo.kt",
"--output",
"output.jar",
"--xml-output",
"xml_output.xml",
"--html-output",
"html_output.html",
"--resource",
"path/to/resource/strings.xml",
"--android-manifest",
Expand Down Expand Up @@ -54,7 +56,8 @@ class AndroidLintActionArgsTest {

assertThat(parseArgs.label).isEqualTo("test")
assertThat(parseArgs.srcs).containsExactly(Paths.get("path/to/Foo.kt"))
assertThat(parseArgs.output).isEqualTo(Paths.get("output.jar"))
assertThat(parseArgs.xmlOutput).isEqualTo(Paths.get("xml_output.xml"))
assertThat(parseArgs.htmlOutput).isEqualTo(Paths.get("html_output.html"))
assertThat(parseArgs.resources).containsExactly(Paths.get("path/to/resource/strings.xml"))
assertThat(parseArgs.baselineFile).isEqualTo(Paths.get("lib_lint_baseline.xml"))
assertThat(parseArgs.config).isEqualTo(Paths.get("lint_config.xml"))
Expand Down