Skip to content

Commit

Permalink
Merge pull request #174 from grab/add-lint-custom-rules-inspector
Browse files Browse the repository at this point in the history
add lint custom rule inspector
  • Loading branch information
mohammadkahelghi-grabtaxi authored May 27, 2024
2 parents b3ced63 + f46361d commit ee08c21
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 5 deletions.
7 changes: 7 additions & 0 deletions rules/android/lint/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load(":providers.bzl", _LINT_ENABLED = "LINT_ENABLED")
load(":lint_test.bzl", _lint_test = "lint_test")
load(":lint_sources.bzl", _lint_sources = "lint_sources")
load(":lint_update_baseline.bzl", _lint_update_baseline = "lint_update_baseline")
load(":lint_inspect.bzl", _lint_inspector = "lint_inspector")

LINT_ENABLED = _LINT_ENABLED

Expand Down Expand Up @@ -42,3 +43,9 @@ def lint(
target = name + ".lint",
baseline = lint_baseline,
)

_lint_inspector(
name = name + ".lint_inspector",
lint_target = name + ".lint",
target = name,
)
15 changes: 11 additions & 4 deletions rules/android/lint/lint_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load(
"AarInfo",
"AarNodeInfo",
"AndroidLintInfo",
"AndroidLintInspectInfo",
"AndroidLintNodeInfo",
"AndroidLintSourcesInfo",
"LINT_ENABLED",
Expand Down Expand Up @@ -485,7 +486,7 @@ def _lint_aspect_impl(target, ctx):

aar_node_infos = _aar_node_infos(sources.aars)
aars = [info.aar for info in aar_node_infos]
aars_dir = [info.aar_dir for info in aar_node_infos]
aars_dirs = [info.aar_dir for info in aar_node_infos]

# Inputs
baseline_inputs = []
Expand Down Expand Up @@ -536,7 +537,7 @@ def _lint_aspect_impl(target, ctx):
sources.srcs +
sources.resources +
aars +
aars_dir +
aars_dirs +
sources.manifest +
sources.merged_manifest +
[sources.lint_config_xml] +
Expand Down Expand Up @@ -588,7 +589,7 @@ def _lint_aspect_impl(target, ctx):
sources.srcs +
sources.resources +
aars +
aars_dir +
aars_dirs +
sources.manifest +
sources.merged_manifest +
[sources.lint_config_xml] +
Expand All @@ -609,7 +610,7 @@ def _lint_aspect_impl(target, ctx):
lint_result_code_file,
],
)

android_lint_inspect_info = AndroidLintInspectInfo(lint_checks = lint_checks, aars_dirs = aars_dirs)
android_lint_node_info = AndroidLintNodeInfo(
name = str(target.label),
android = android,
Expand All @@ -634,8 +635,14 @@ def _lint_aspect_impl(target, ctx):
result_code = None,
updated_baseline = None,
)
android_lint_inspect_info = AndroidLintInspectInfo(
lint_checks = None,
aars_dirs = None,
)

return AndroidLintInfo(
info = android_lint_node_info,
inspect_info = android_lint_inspect_info,
transitive_nodes = depset(
[android_lint_node_info],
transitive = [depset(transitive = [transitive_lint_node_infos])],
Expand Down
78 changes: 78 additions & 0 deletions rules/android/lint/lint_inspect.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
load("@grab_bazel_common//rules/android/lint:providers.bzl", "AndroidLintInfo")
load("@grab_bazel_common//rules/android:utils.bzl", "utils")

def _lint_inspector(ctx):
lint_target = ctx.attr.lint_target
target = ctx.attr.target

lint_checks = ctx.attr.lint_target[AndroidLintInfo].inspect_info.lint_checks
aars_dirs = ctx.attr.lint_target[AndroidLintInfo].inspect_info.aars_dirs

executable = ctx.actions.declare_file("lint/%s_inspect.sh" % target.label.name)
lint_rules = ctx.actions.declare_file("lint/%s_lint_rules.json" % target.label.name)

ctx.actions.write(
output = executable,
content = """
#!/bin/bash
echo "\nlint_rules: $(realpath {lint_rules})"
""".format(
lint_rules = lint_rules.short_path,
),
)

mnemonic = "AndroidLintInspector"
args = ctx.actions.args()

args.add_joined(
"--aars-dirs",
aars_dirs,
join_with = ",",
map_each = utils.to_path,
)
args.add_joined(
"--lint-checks",
lint_checks,
join_with = ",",
map_each = utils.to_path,
)
args.add("--output", lint_rules.path)

ctx.actions.run(
mnemonic = mnemonic,
inputs = depset(
lint_checks + aars_dirs,
),
outputs = [lint_rules],
executable = ctx.executable._lint_inspector_cli,
arguments = [args],
progress_message = "%s %s" % (mnemonic, str(ctx.label).lstrip("@")),
)

return [
DefaultInfo(
executable = executable,
runfiles = ctx.runfiles(files = [lint_rules]),
files = depset([lint_rules]),
),
]

lint_inspector = rule(
implementation = _lint_inspector,
executable = True,
attrs = {
"_lint_inspector_cli": attr.label(
executable = True,
cfg = "exec",
default = Label("//tools/lint:lint_inspector"),
),
"target": attr.label(
doc = "name of the android/kotlin/java target",
),
"lint_target": attr.label(
doc = "The lint target to inspect",
providers = [AndroidLintInfo],
mandatory = True,
),
},
)
9 changes: 9 additions & 0 deletions rules/android/lint/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ AndroidLintNodeInfo = provider(
AndroidLintInfo = provider(
doc = "Provider containing info about lint on this target and it's dependencies",
fields = dict(
inspect_info = "AndroidLintInspectInfo containing lint checks jars and aars directories that have custom lint checks",
info = "AndroidLintNodeInfo containing data about Android Lint",
transitive_nodes = "Depset of AndroidLintNodeInfo containing data about dependencies' lint",
),
Expand Down Expand Up @@ -46,6 +47,14 @@ AarNodeInfo = provider(
},
)

AndroidLintInspectInfo = provider(
doc = "AndroidLintInspectInfo containing lint checks jars and aars directories that have custom lint checks",
fields = dict(
lint_checks = "custom lint checks jar files",
aars_dirs = "path to extracted aar direcotries containing lint rules",
),
)

AarInfo = provider(
"A provider to collect all aars from transitive dependencies",
fields = {
Expand Down
4 changes: 4 additions & 0 deletions rules/android/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def _collect_providers(provider_type, *all_deps):
providers.append(dep[provider_type])
return providers

def _to_depset(list):
return depset(list)

utils = struct(
to_path = _to_path,
inspect = _inspect,
collect_providers = _collect_providers,
to_depset = _to_depset,
)
13 changes: 13 additions & 0 deletions tools/lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ java_binary(
"//tools/lint/src/main/java/com/grab/lint",
],
)

java_binary(
name = "lint_inspector",
# jvm_flags = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"],
main_class = "com.grab.lint.inspector.MainKt",
visibility = [
"//visibility:public",
],
runtime_deps = [
"//tools/lint/src/main/java/com/grab/lint/inspector:lint-inspector-lib",
],
#
)
1 change: 0 additions & 1 deletion tools/lint/src/main/java/com/grab/lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ kotlin_library(
"@bazel_common_maven//:com_android_tools_lint_lint_api",
"@bazel_common_maven//:com_android_tools_lint_lint_checks",
"@bazel_common_maven//:com_github_ajalt_clikt",
"@bazel_common_maven//:org_jetbrains_kotlinx_kotlinx_coroutines_core",
],
)
33 changes: 33 additions & 0 deletions tools/lint/src/main/java/com/grab/lint/inspector/ArgumentParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.grab.lint.inspector

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import java.io.File

class ArgumentParser : CliktCommand() {

private val outPut by option(
"-o",
"--output",
help = "a json file result that contains custom lint rules infos"
).convert { File(it) }.required()

private val lintChecks by option(
"-lc",
"--lint-checks",
help = "jar files that contains custom lint checks"
).convert { it.split(",").map { File(it) } }.default(emptyList())

private val aarDeps by option(
"-ad",
"--aars-dirs",
help = "aars extract directories that contains custom lint checks"
).convert { it.split(",").map { File(it) }.filter { it.name == "lint.jar" } }.default(emptyList())

override fun run() {
Inspector(outPut, lintChecks, aarDeps).run()
}
}
19 changes: 19 additions & 0 deletions tools/lint/src/main/java/com/grab/lint/inspector/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@grab_bazel_common//rules:defs.bzl", "kotlin_library")

kotlin_library(
name = "lint-inspector-lib",
srcs = glob([
"*.kt",
]),
visibility = [
"//visibility:public",
],
deps = [
"@bazel_common_maven//:com_android_tools_lint_lint_api",
"@bazel_common_maven//:com_android_tools_lint_lint_checks",
"@bazel_common_maven//:com_github_ajalt_clikt",
"@bazel_common_maven//:com_squareup_moshi_moshi_kotlin",
"@bazel_common_maven//:net_sf_kxml_kxml2",
"@bazel_common_maven//:xmlpull_xmlpull",
],
)
Loading

0 comments on commit ee08c21

Please sign in to comment.