Skip to content

Commit

Permalink
extract lint configs from kotlin targets
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadkahelghi-grabtaxi committed Mar 15, 2024
1 parent 800b12a commit 5bdb76b
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "grab_bazel_common",
commit = "fe47feaee47d9f872514d29ca4ac4c462e1811eb",
commit = "3f9ff2c3351d76cf630e455430a6a7bc942e58f7",
remote = "https://github.com/grab/grab-bazel-common.git",
)

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ grazel {
rules {
bazelCommon {
gitRepository {
commit = "fe47feaee47d9f872514d29ca4ac4c462e1811eb"
commit = "3f9ff2c3351d76cf630e455430a6a7bc942e58f7"
remote = "https://github.com/grab/grab-bazel-common.git"
}
toolchains {
Expand Down
2 changes: 1 addition & 1 deletion constants.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
ext {
groupId = "com.grab.grazel"
versionName = "0.4.1-alpha.23"
versionName = "0.4.1-alpha.24"

website = "https://grab.github.io/Grazel/"
}
3 changes: 3 additions & 0 deletions flavor-libs/sample-library-flavor1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ kotlin_library(
srcs = glob([
"src/main/java/com/grab/grazel/android/flavor/ModuleName.kt",
]),
lint_options = {
"enabled": "true",
},
visibility = [
"//visibility:public",
],
Expand Down
3 changes: 3 additions & 0 deletions flavor-libs/sample-library-flavor2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ kotlin_library(
srcs = glob([
"src/main/java/com/grab/grazel/android/flavor/ModuleName.kt",
]),
lint_options = {
"enabled": "true",
},
visibility = [
"//visibility:public",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ package com.grab.grazel.bazel.rules
import com.grab.grazel.bazel.rules.Visibility.Public
import com.grab.grazel.bazel.starlark.Assignee
import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs
import com.grab.grazel.bazel.starlark.StatementsBuilder
import com.grab.grazel.bazel.starlark.add
import com.grab.grazel.bazel.starlark.array
import com.grab.grazel.bazel.starlark.asString
import com.grab.grazel.bazel.starlark.glob
import com.grab.grazel.bazel.starlark.load
import com.grab.grazel.bazel.starlark.quote
import com.grab.grazel.bazel.starlark.toObject
import com.grab.grazel.extension.JavaCOptions
import com.grab.grazel.extension.KotlinCOptions
import com.grab.grazel.extension.KotlinToolChain
Expand Down Expand Up @@ -146,7 +148,8 @@ fun StatementsBuilder.ktLibrary(
plugins: List<BazelDependency> = emptyList(),
assetsGlob: List<String> = emptyList(),
assetsDir: String? = null,
tags: List<String> = emptyList()
tags: List<String> = emptyList(),
lintConfigs: LintConfigs? = null,
) {
load("@$GRAB_BAZEL_COMMON//rules:defs.bzl", "kotlin_library")

Expand Down Expand Up @@ -184,6 +187,10 @@ fun StatementsBuilder.ktLibrary(
tags.notEmpty {
"tags" `=` array(tags.map(String::quote))
}

if (lintConfigs?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigs.merged.toObject(quoteKeys = true, quoteValues = true)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const val KOTLIN_PARCELIZE = "kotlin-parcelize"
const val KOTLIN_KAPT = "kotlin-kapt"
const val ANDROID_APPLICATION_PLUGIN = "com.android.application"
const val ANDROID_LIBRARY_PLUGIN = "com.android.library"
const val LINT_PLUGIN_ID = "com.android.lint"
const val ANDROID_DYNAMIC_FEATURE = "com.android.dynamic-feature"
const val FIREBASE_CRASHLYTICS_PLUGIN = "com.google.firebase.crashlytics"
const val GOOGLE_PLAY_SERVICES_PLUGIN = "com.google.firebase.crashlytics"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,22 @@ constructor(
}
}

private fun lintConfigs(
fun lintConfigs(
lintOptions: LintOptions,
project: Project
): LintConfigs {
// enable lint for all targets by default
val enabled = true

val configPath = if (lintOptions.lintConfig?.absolutePath != null) {
project.relativePath(lintOptions.lintConfig!!.absolutePath)
} else {
null
}
val baseLinePath = if (lintOptions.baselineFile?.absolutePath != null) {
project.relativePath(lintOptions.baselineFile!!.absolutePath)
} else {
null
}
return LintConfigs(enabled, configPath, baseLinePath)
return LintConfigs(
enabled,
lintOptions.lintConfig?.let {
project.relativePath(it)
},
lintOptions.baselineFile?.let {
project.relativePath(it)
}
)
}

internal interface AndroidBinaryDataExtractor : AndroidExtractor<AndroidBinaryData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.grab.grazel.migrate.kotlin
import com.grab.grazel.bazel.rules.Visibility
import com.grab.grazel.bazel.rules.ktLibrary
import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs
import com.grab.grazel.bazel.starlark.StatementsBuilder
import com.grab.grazel.migrate.BazelBuildTarget
import com.grab.grazel.migrate.android.ResValuesData
Expand All @@ -39,6 +40,7 @@ internal data class KotlinLibraryTarget(
val plugins: List<BazelDependency> = emptyList(),
val assetsGlob: List<String> = emptyList(),
val assetsDir: String? = null,
val lintConfigs: LintConfigs? = null
) : BazelBuildTarget {

override fun statements(builder: StatementsBuilder) = builder {
Expand All @@ -54,7 +56,8 @@ internal data class KotlinLibraryTarget(
plugins = plugins,
assetsGlob = assetsGlob,
assetsDir = assetsDir,
tags = tags
tags = tags,
lintConfigs = lintConfigs
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.grab.grazel.migrate.kotlin

import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs

data class KotlinProjectData(
val name: String,
val srcs: List<String>,
val res: List<String>,
val deps: List<BazelDependency>,
val tags: List<String>
val tags: List<String>,
val lintConfigs: LintConfigs
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.grab.grazel.migrate.kotlin

import com.android.builder.model.LintOptions
import com.grab.grazel.GrazelExtension
import com.grab.grazel.bazel.rules.KOTLIN_PARCELIZE_TARGET
import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs
import com.grab.grazel.extension.KotlinExtension
import com.grab.grazel.gradle.ConfigurationScope
import com.grab.grazel.gradle.LINT_PLUGIN_ID
import com.grab.grazel.gradle.dependencies.BuildGraphType
import com.grab.grazel.gradle.dependencies.DependenciesDataSource
import com.grab.grazel.gradle.dependencies.DependencyGraphs
Expand Down Expand Up @@ -86,10 +89,32 @@ constructor(
srcs = srcs,
res = resources,
deps = deps,
tags = tags
tags = tags,
lintConfigs = lintConfigs(project),
)
}

fun lintConfigs(project: Project): LintConfigs {
return if (project.plugins.hasPlugin(LINT_PLUGIN_ID)) {
val lint = project.the<LintOptions>()
LintConfigs(
enabled = true,
configPath = lint.lintConfig?.let {
project.relativePath(it)
},
baselinePath = lint.baselineFile?.let {
project.relativePath(it)
},
)
} else {
LintConfigs(
enabled = true, // enable Lint by default even when its not enabled in gradle
configPath = null,
baselinePath = null,
)
}
}

private fun Project.kotlinSources(
sourceSets: NamedDomainObjectContainer<KotlinSourceSet>,
sourceSetType: SourceSetType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ constructor(
res = res,
deps = deps,
tags = tags,
lintConfigs = lintConfigs
)
}

4 changes: 4 additions & 0 deletions sample-kotlin-library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ kotlin_library(
srcs = glob([
"src/main/java/com/grab/grazel/sample/HelloWorld.kt",
]),
lint_options = {
"enabled": "true",
"baseline": "lint-baseline-kotlin.xml",
},
visibility = [
"//visibility:public",
],
Expand Down
5 changes: 5 additions & 0 deletions sample-kotlin-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

apply plugin: "java-library"
apply plugin: "kotlin"
apply plugin: "com.android.lint"

lint {
baseline file("lint-baseline-kotlin.xml")
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
Expand Down

0 comments on commit 5bdb76b

Please sign in to comment.