Skip to content

Commit

Permalink
all html code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EdbertChan committed Apr 24, 2024
1 parent 89fb6c9 commit 97ab9b8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 41 deletions.
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.

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),
)
49 changes: 29 additions & 20 deletions rules/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _run_android_lint(
ctx,
android_lint,
module_name,
output,
xml_output,
html_output,
srcs,
deps,
Expand All @@ -44,7 +44,7 @@ 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
srcs: The source files
deps: Depset of aars and jars to include on the classpath
resource_files: The Android resource files
Expand All @@ -64,7 +64,7 @@ def _run_android_lint(
android_lint_skip_bytecode_verifier: Disables bytecode verification
"""
inputs = []
outputs = [output, html_output]
outputs = []

args = ctx.actions.args()
args.set_param_file_format("multiline")
Expand Down Expand Up @@ -115,16 +115,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)

args.add("--html-output", html_output)
outputs.append(html_output)

label = ctx.attr.android_home.label
if ctx.attr.android_home:
args.add("--android_home", label.workspace_root)
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 @@ -171,7 +169,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 @@ -205,13 +203,20 @@ 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))
html_output = ctx.actions.declare_file("{}.html".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),
Expand All @@ -220,7 +225,7 @@ def process_android_lint_issues(ctx, regenerate):
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 @@ -233,10 +238,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",
},
)
10 changes: 5 additions & 5 deletions src/cli/AndroidLintActionArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +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(
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
14 changes: 10 additions & 4 deletions src/cli/AndroidLintRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ internal class AndroidLintRunner {
projectFilePath.pathString,
"--path-variables",
"PWD=$rootDirPath",
"--xml",
actionArgs.output.pathString,
"--html",
actionArgs.htmlOutput.pathString,
"--exitcode",
"--compile-sdk-version",
actionArgs.compileSdkVersion,
Expand All @@ -115,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

0 comments on commit 97ab9b8

Please sign in to comment.