Skip to content

Commit

Permalink
Implement lint rule and configure project xml
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkumar9t2 committed Oct 20, 2023
1 parent 63eda4c commit c63a3dc
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
11 changes: 11 additions & 0 deletions rules/android/android_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@grab_bazel_common//tools/res_value:res_value.bzl", "res_value")
load("@grab_bazel_common//tools/kotlin:android.bzl", "kt_android_library")
load("@grab_bazel_common//rules/android/databinding:databinding.bzl", "kt_db_android_library")
load(":resources.bzl", "build_resources")
load(":lint.bzl", "lint")

"""Enhanced android_library rule with support for build configs, res values, Kotlin compilation and databinding support"""

Expand Down Expand Up @@ -80,3 +81,13 @@ def android_library(
deps = android_library_deps,
plugins = attrs.get("plugins", default = None),
)

lint(
name = name + ".lint",
srcs = srcs,
resources = resource_files,
manifest = attrs.get("manifest"),
deps = android_library_deps,
android = False,
library = True,
)
78 changes: 78 additions & 0 deletions rules/android/lint.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
def _lint_test_impl(ctx):
classpath = depset()
for dep in ctx.attr.deps:
if JavaInfo in dep:
classpath = depset(transitive = [classpath, dep[JavaInfo].transitive_runtime_jars, dep[JavaInfo].transitive_compile_time_jars])

# TODO Extract dependent modules via a custom provider

project_xml_file = ctx.actions.declare_file(ctx.label.name + "_project.xml")

# Create project XML:
project_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
project_xml += "<project>\n"
project_xml += "<module name=\"{0}\" android=\"false\" library=\"true\">\n".format(ctx.label.name)
for file in ctx.files.srcs:
project_xml += " <src file=\"{0}\" ".format(file.path)
if ctx.attr.is_test_sources:
project_xml += "test=\"true\" "
project_xml += "/>\n"
for file in ctx.files.resources:
project_xml += " <resource file=\"{0}\"/>\n".format(file.path)
for file in classpath.to_list():
project_xml += " <classpath jar=\"{0}\" />\n".format(file.path)
if ctx.file.manifest != None:
project_xml += " <manifest file=\"{0}\"/>\n".format(ctx.file.manifest.path)
if ctx.file.merged_manifest != None:
project_xml += " <merged-manifest file=\"{0}\"/>\n".format(ctx.file.merged_manifest.path)

project_xml += "</module>\n"
project_xml += "</project>\n"

ctx.actions.write(output = project_xml_file, content = project_xml)

args = ctx.actions.args()
args.set_param_file_format("multiline")
args.use_param_file("--flagfile=%s", use_always = True)

args.add("--project-xml", project_xml_file.path)
args.add("--output-xml", ctx.outputs.lint_result)

mnemonic = "AndroidLint"
ctx.actions.run(
mnemonic = mnemonic,
inputs = depset(ctx.files.srcs + ctx.files.resources + [project_xml_file], transitive = [classpath]),
outputs = [ctx.outputs.lint_result],
executable = ctx.executable._lint_cli,
arguments = [args],
progress_message = "%s %s" % (mnemonic, ctx.label),
execution_requirements = {
"supports-workers": "1",
"supports-multiplex-workers": "1",
"requires-worker-protocol": "json",
},
)

return [DefaultInfo(files = depset([ctx.outputs.lint_result]))]

lint = rule(
attrs = {
"srcs": attr.label_list(allow_files = True),
"resources": attr.label_list(allow_files = True),
"deps": attr.label_list(allow_files = True),
"merged_manifest": attr.label(allow_single_file = True),
"manifest": attr.label(allow_single_file = True),
"android": attr.bool(),
"library": attr.bool(),
"is_test_sources": attr.bool(),
"_lint_cli": attr.label(
executable = True,
cfg = "target",
default = Label("//tools/lint:lint_cli"),
),
},
outputs = {
"lint_result": "%{name}_result.xml",
},
implementation = _lint_test_impl,
)
24 changes: 14 additions & 10 deletions tools/lint/src/main/java/com/grab/lint/LintCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,37 @@ class LintCommand : CliktCommand() {
private val projectXml by option(
"-p",
"--project-xml",
help = "Project XML containing lint config"
help = "Project descriptor XML"
).convert { File(it) }.required()

private val lintConfig by option(
"-l",
"--lint-config",
help = "Path to lint config "
help = "Path to lint config"
).convert { File(it) }

private val outputXml by option(
"-o",
"--output-xml",
help = "Lint output xml"
).convert { File(it) }.required()

override fun run() {
val outputDir = File(".").toPath()
val baselineFile = outputDir.resolve("baseline.xml")

// TODO: Get this from rule itself
val lintConfig = outputDir.resolve("lint.xml").writeLines(
listOf(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
"<lint checkTestSources=\"true\">",
" <issue id=\"all\" severity=\"error\" />",
" <issue id=\"MissingSuperCall\" severity=\"error\" />",
"</lint>"
)
)
val outputXml = outputDir.resolve("output.xml")
val projectXml = outputDir.resolve("project.xml").writeLines(
listOf(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<project>\n",
"</project>\n"
)
)

outputXml.createNewFile()

val lintCli = LintCli()
lintCli.run(
Expand Down
4 changes: 3 additions & 1 deletion tools/lint/src/main/java/com/grab/lint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ package com.grab.lint
import io.bazel.Worker

fun main(args: Array<String>) {
Worker.create(args) { cliArgs -> LintCommand().main(args) }.run()
Worker.create(args) { cliArgs ->
LintCommand().main(cliArgs)
}.run()
}

0 comments on commit c63a3dc

Please sign in to comment.