Skip to content

Commit

Permalink
Merge pull request #162 from grab/collect-custom-lint-rules-from-inte…
Browse files Browse the repository at this point in the history
…rnal-dependencies

collect custom lint rules from internal dependencies
  • Loading branch information
mohammadkahelghi-grabtaxi authored Apr 5, 2024
2 parents aa85676 + 40c936c commit e68539f
Show file tree
Hide file tree
Showing 12 changed files with 906 additions and 5 deletions.
1 change: 1 addition & 0 deletions rules/android/android_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def android_binary(
baseline = lint_baseline,
lint_config = lint_options.get("config", None),
deps = kotlin_library_deps,
lint_checks = lint_options.get("lint_checks", default = []),
)

# Build deps
Expand Down
1 change: 1 addition & 0 deletions rules/android/android_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def android_library(
baseline = lint_baseline,
lint_config = lint_options.get("config", None),
deps = android_library_deps,
lint_checks = lint_options.get("lint_checks", default = []),
)
android_library_deps = android_library_deps + [lint_sources_target]
lint(
Expand Down
23 changes: 21 additions & 2 deletions rules/android/lint/lint_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def _collect_sources(target, ctx, library):
baseline = dep[AndroidLintSourcesInfo].baseline,
lint_config_xml = dep[AndroidLintSourcesInfo].lint_config[0],
classpath = classpath,
lint_checks = dep[AndroidLintSourcesInfo].lint_checks,
)
for dep in (ctx.rule.attr.deps + getattr(ctx.rule.attr, "exports", []))
if AndroidLintSourcesInfo in dep
Expand Down Expand Up @@ -168,6 +169,7 @@ def _lint_common_args(
res_config,
aars,
aar_infos,
lint_checks,
classpath,
manifest,
merged_manifest,
Expand Down Expand Up @@ -210,6 +212,12 @@ def _lint_common_args(
join_with = ",",
map_each = _encode_aars,
)
args.add_joined(
"--lint_checks",
lint_checks,
join_with = ",",
map_each = utils.to_path,
)
args.add_joined(
"--resource-files",
resources,
Expand Down Expand Up @@ -263,6 +271,7 @@ def _lint_analyze_action(
res_config,
aars,
aar_infos,
lint_checks,
classpath,
manifest,
merged_manifest,
Expand Down Expand Up @@ -292,6 +301,7 @@ def _lint_analyze_action(
res_config = res_config,
aars = aars,
aar_infos = aar_infos,
lint_checks = lint_checks,
classpath = classpath,
manifest = manifest,
merged_manifest = merged_manifest,
Expand Down Expand Up @@ -337,6 +347,7 @@ def _lint_report_action(
res_config,
aars,
aar_infos,
lint_checks,
classpath,
manifest,
merged_manifest,
Expand Down Expand Up @@ -369,6 +380,7 @@ def _lint_report_action(
res_config = res_config,
aars = aars,
aar_infos = aar_infos,
lint_checks = lint_checks,
classpath = classpath,
manifest = manifest,
merged_manifest = merged_manifest,
Expand Down Expand Up @@ -450,6 +462,9 @@ def _lint_aspect_impl(target, ctx):
# Output - End

sources = _collect_sources(target, ctx, library)

lint_checks = [jar.files.to_list()[0] for jar in sources.lint_checks]

compile_sdk_version = _compile_sdk_version(ctx.attr._android_sdk)
sdk_versions = _sdk_versions(ctx)
res_config = _res_config(ctx)
Expand Down Expand Up @@ -495,6 +510,7 @@ def _lint_aspect_impl(target, ctx):
res_config = res_config,
aars = aars,
aar_infos = aar_node_infos,
lint_checks = lint_checks,
classpath = sources.classpath,
manifest = sources.manifest,
merged_manifest = sources.merged_manifest,
Expand All @@ -516,7 +532,8 @@ def _lint_aspect_impl(target, ctx):
[sources.lint_config_xml] +
dep_partial_results +
dep_lint_models +
baseline_inputs,
baseline_inputs +
lint_checks,
transitive = [sources.classpath, java_runtime_info.files],
),
outputs = [
Expand All @@ -539,6 +556,7 @@ def _lint_aspect_impl(target, ctx):
res_config = res_config,
aars = aars,
aar_infos = aar_node_infos,
lint_checks = lint_checks,
classpath = sources.classpath,
manifest = sources.manifest,
merged_manifest = sources.merged_manifest,
Expand Down Expand Up @@ -566,7 +584,8 @@ def _lint_aspect_impl(target, ctx):
[lint_partial_results_dir] + # Current module partial results from analyze action
[lint_models_dir] + # Current module models from analyze action
[project_xml_file] + # Reuse project xml from analyze action
baseline_inputs,
baseline_inputs +
lint_checks,
transitive = [sources.classpath, java_runtime_info.files],
),
outputs = [
Expand Down
7 changes: 7 additions & 0 deletions rules/android/lint/lint_sources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def _lint_sources_impl(ctx):
manifest = _target_outputs([ctx.attr.manifest]),
baseline = _target_outputs([ctx.attr.baseline]) if ctx.attr.baseline != None else None,
lint_config = _target_outputs([ctx.attr.lint_config]) if ctx.attr.lint_config != None else _target_outputs([ctx.attr._default_lint_config]),
lint_checks = ctx.attr.lint_checks,
),
]

Expand Down Expand Up @@ -64,6 +65,12 @@ lint_sources = rule(
default = [],
aspects = [collect_aar_aspect],
),
"lint_checks": attr.label_list(
doc = """custom lint rule targets in the project""",
allow_empty = True,
default = [],
providers = [JavaInfo],
),
# TODO(arun) add assets
},
provides = [
Expand Down
1 change: 1 addition & 0 deletions rules/android/lint/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ AndroidLintSourcesInfo = provider(
manifest = "Android manifest file",
baseline = "Lint baseline XML",
lint_config = "Lint config XML",
lint_checks = "Custom Lint Targets",
),
)

Expand Down
1 change: 1 addition & 0 deletions tests/android/binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android_binary(
lint_options = {
"enabled": True,
"baseline": "lint_baseline.xml",
"lint_checks": ["//tests/android/custom-lint-rules"],
},
manifest = "src/main/AndroidManifest.xml",
manifest_values = {
Expand Down
13 changes: 12 additions & 1 deletion tests/android/binary/lint_baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 8.4.0-alpha09" type="baseline" client="" dependencies="true" name="" variant="all" version="8.4.0-alpha09">
<issues format="6" by="lint 8.5.0-alpha02" type="baseline" client="" dependencies="true" name="" variant="all" version="8.5.0-alpha02">

<issue
id="LocaleFolder"
Expand Down Expand Up @@ -74,6 +74,17 @@
column="13"/>
</issue>

<issue
id="LogNotTimberTest"
message="Using &apos;Log&apos; instead of &apos;Timber&apos;"
errorLine1=" android.util.Log.d(&quot;SomeReallyLongTagForLintToDetectAndWarn&quot;, &quot;Log message&quot;)"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="../../../../../../../tests/android/binary/src/main/java/com/grab/test/TestActivity.kt"
line="13"
column="9"/>
</issue>

<issue
id="UnusedResources"
message="The resource `R.layout.layout_test` appears to be unused"
Expand Down
19 changes: 19 additions & 0 deletions tests/android/custom-lint-rules/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 = "custom-lint-rules",
srcs = glob([
"src/main/java/**/*.kt",
]),
lint_options = {
"enabled": "true",
},
visibility = [
"//visibility:public",
],
deps = [
"//third_party/auto-service",
"@bazel_common_maven//:com_android_tools_lint_lint_api",
"@bazel_common_maven//:com_android_tools_lint_lint_checks",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bazel.common.lint.rules

import com.android.tools.lint.client.api.IssueRegistry
import com.android.tools.lint.client.api.Vendor
import com.android.tools.lint.detector.api.CURRENT_API
import com.google.auto.service.AutoService

/*
* The list of issues that will be checked when running <code>lint</code>.
*/
@AutoService(IssueRegistry::class)
class LintIssueRegistry : IssueRegistry() {
override val issues = WrongTimberUsageDetector.issues.asList()

override val api: Int
get() = CURRENT_API

override val minApi: Int
get() = 8

override val vendor: Vendor = Vendor(
vendorName = "bazel common App",
feedbackUrl = "https://github.com/grab/grab-bazel-common/issues",
contact = "https://github.com/grab/grab-bazel-common/issues"
)
}
Loading

0 comments on commit e68539f

Please sign in to comment.