Skip to content

Commit

Permalink
Merge pull request #125 from grab/add-lint-options-for-kotlin
Browse files Browse the repository at this point in the history
extract lint configs from kotlin targets
  • Loading branch information
mohammadkahelghi-grabtaxi authored Mar 15, 2024
2 parents 800b12a + 321d275 commit a84d8b5
Show file tree
Hide file tree
Showing 15 changed files with 91 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 = "092435fb286705c4dcd845c93c9d84e94a4088d9",
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 = "092435fb286705c4dcd845c93c9d84e94a4088d9"
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
20 changes: 20 additions & 0 deletions sample-kotlin-library/lint-baseline-kotlin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2024 Grabtaxi Holdings PTE LTD (GRAB)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<issues format="6" by="lint 8.4.0-alpha09" type="baseline" client="" dependencies="true" name="" variant="all" version="8.4.0-alpha09">

</issues>

0 comments on commit a84d8b5

Please sign in to comment.