Skip to content

Commit

Permalink
Support defining root lint.xml that applies for all projects (#133)
Browse files Browse the repository at this point in the history
Fixes #133
  • Loading branch information
arunkumar9t2 committed May 7, 2024
1 parent ecbe062 commit 835ecb1
Show file tree
Hide file tree
Showing 27 changed files with 153 additions and 189 deletions.
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ dagger_rules()
parcelize_rules()

configure_common_toolchains()

exports_files(["lint.xml"])
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ maven_install(
"android.arch.lifecycle:livedata-core",
"android.arch.lifecycle:runtime",
"android.arch.lifecycle:viewmodel",
"androidx.fragment:fragment",
"com.android.databinding:baseLibrary",
"com.android.support:animated-vector-drawable",
"com.android.support:cardview-v7",
Expand All @@ -381,7 +382,6 @@ maven_install(
"com.android.support:support-compat",
"com.android.support:support-core-ui",
"com.android.support:support-core-utils",
"com.android.support:support-fragment",
"com.android.support:support-vector-drawable",
"com.android.support:versionedparcelable",
"com.android.support:viewpager",
Expand Down
12 changes: 9 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ allprojects {
}
}

// TODO(arun) Move these to convention plugins
plugins.withType(com.android.build.gradle.BasePlugin).configureEach { plugin ->
(project.extensions.getByName("android") as BaseExtension).compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
(project.extensions.getByName("android") as BaseExtension).with {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
lint {
lintConfig = rootProject.file("lint.xml")
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions flavors/sample-android-flavor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android_library(
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
res_values = {
Expand Down Expand Up @@ -51,6 +52,7 @@ android_library(
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
res_values = {
Expand Down Expand Up @@ -89,6 +91,7 @@ android_library(
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
res_values = {
Expand Down Expand Up @@ -129,6 +132,7 @@ android_library(
enable_data_binding = True,
lint_options = {
"enabled": True,
"config": "//:lint.xml",
},
manifest = "src/main/AndroidManifest.xml",
res_values = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.grab.grazel.bazel.rules

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.array
import com.grab.grazel.bazel.starlark.asString
Expand All @@ -27,6 +26,7 @@ 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.migrate.android.BuildConfigData
import com.grab.grazel.migrate.android.LintConfigData
import com.grab.grazel.migrate.android.ResValuesData

fun StatementsBuilder.androidSdkRepository(
Expand Down Expand Up @@ -139,7 +139,7 @@ internal fun StatementsBuilder.androidBinary(
assetsGlob: List<String> = emptyList(),
assetsDir: String? = null,
buildConfigData: BuildConfigData,
lintConfigs: LintConfigs? = null,
lintConfigData: LintConfigData? = null,
resConfigFilters: Set<String> = emptySet(),
) {
load("@$GRAB_BAZEL_COMMON//rules:defs.bzl", "android_binary")
Expand Down Expand Up @@ -187,8 +187,8 @@ internal fun StatementsBuilder.androidBinary(
"res_values" `=` resValuesData.merged.toObject(quoteKeys = true, quoteValues = true)
}

if (lintConfigs?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigs.merged.toObject()
if (lintConfigData?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigData.merged.toObject()
}
}
}
Expand All @@ -209,7 +209,7 @@ internal fun StatementsBuilder.androidLibrary(
assetsDir: String? = null,
resValuesData: ResValuesData,
buildConfigData: BuildConfigData,
lintConfigs: LintConfigs?
lintConfigData: LintConfigData?
) {
load("@$GRAB_BAZEL_COMMON//rules:defs.bzl", "android_library")
rule("android_library") {
Expand Down Expand Up @@ -250,8 +250,8 @@ internal fun StatementsBuilder.androidLibrary(
"res_values" `=` resValuesData.merged.toObject(quoteKeys = true, quoteValues = true)
}

if (lintConfigs?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigs.merged.toObject()
if (lintConfigData?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigData.merged.toObject()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ interface BazelRule : StarlarkType {

fun StatementsBuilder.rule(name: String, assignmentBuilder: AssignmentBuilder.() -> Unit = {}) {
function(name, true, assignmentBuilder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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.migrate.android.LintConfigData
import com.grab.grazel.bazel.starlark.StatementsBuilder
import com.grab.grazel.bazel.starlark.add
import com.grab.grazel.bazel.starlark.array
Expand Down Expand Up @@ -149,7 +149,7 @@ fun StatementsBuilder.ktLibrary(
assetsGlob: List<String> = emptyList(),
assetsDir: String? = null,
tags: List<String> = emptyList(),
lintConfigs: LintConfigs? = null,
lintConfigData: LintConfigData? = null,
) {
load("@$GRAB_BAZEL_COMMON//rules:defs.bzl", "kotlin_library")

Expand Down Expand Up @@ -188,8 +188,8 @@ fun StatementsBuilder.ktLibrary(
"tags" `=` array(tags.map(String::quote))
}

if (lintConfigs?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigs.merged.toObject()
if (lintConfigData?.merged?.isNotEmpty() == true) {
"lint_options" `=` lintConfigData.merged.toObject()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package com.grab.grazel.bazel.starlark

import kotlinx.serialization.Serializable
import org.gradle.api.Project
import java.io.File

// TODO(arun) Rename this to BazelLabel
sealed class BazelDependency : Comparable<BazelDependency> {

override fun compareTo(other: BazelDependency) = toString().compareTo(other.toString())
Expand All @@ -34,12 +36,13 @@ sealed class BazelDependency : Comparable<BazelDependency> {
.rootProject
.relativePath(dependencyProject.projectDir)
val buildTargetName = dependencyProject.name
val sep = File.separator
return when {
relativeRootPath.contains("/") -> {
relativeRootPath.contains(sep) -> {
val path = relativeRootPath
.split("/")
.split(sep)
.dropLast(1)
.joinToString("/")
.joinToString(sep)
"//$path/$buildTargetName:$prefix$buildTargetName$suffix"
}

Expand All @@ -48,8 +51,24 @@ sealed class BazelDependency : Comparable<BazelDependency> {
}
}

data class StringDependency(val dep: String) : BazelDependency() {
override fun toString() = dep
data class FileDependency(
val file: File,
val rootProject: Project
) : BazelDependency() {
override fun toString(): String {
val fileName = file.name
val filePath = rootProject.relativePath(file)
return if (fileName == filePath) {
"//:$fileName"
} else {
// The file is not in root directory
"//${filePath.substringBeforeLast(File.separator)}:$fileName"
}
}
}

data class StringDependency(val string: String) : BazelDependency() {
override fun toString() = string
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ fun StatementsBuilder.filegroup(
"visibility" `=` array(visibility.rule.quote)
}
}

fun StatementsBuilder.exportsFiles(
vararg files: String,
) {
function(
name = "exports_files",
quote = false,
files.joinToString(prefix = "[", postfix = "]", transform = String::quote)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ import com.grab.grazel.gradle.dependencies.DependencyGraphs
import com.grab.grazel.gradle.dependencies.model.WorkspaceDependencies
import dagger.Lazy
import org.gradle.api.Project
import org.gradle.kotlin.dsl.provideDelegate
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

/**
* Common metadata about a Gradle project.
*/
@Deprecated(message = "Consider migrating to target API")
interface GradleProjectInfo {
val rootProject: Project
val grazelExtension: GrazelExtension
val hasDagger: Boolean
val hasAndroidExtension: Boolean
val hasGooglePlayServices: Boolean
val rootLintXml: File // TODO(arun) Implementing here due to lack of better place for root project data.
}

internal class DefaultGradleProjectInfo(
Expand Down Expand Up @@ -83,4 +87,7 @@ internal class DefaultGradleProjectInfo(
.subprojects
.any { project -> project.hasCrashlytics || project.hasGooglePlayServicesPlugin }
}
override val rootLintXml: File by lazy {
rootProject.file("lint.xml")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.grab.grazel.migrate.android

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

internal interface AndroidData {
val name: String
Expand All @@ -40,7 +39,7 @@ internal interface AndroidData {
val compose: Boolean
val databinding: Boolean
val tags: List<String>
val lintConfigs: LintConfigs
val lintConfigData: LintConfigData
}

internal data class AndroidLibraryData(
Expand All @@ -59,7 +58,7 @@ internal data class AndroidLibraryData(
override val databinding: Boolean = false,
override val compose: Boolean = false,
override val tags: List<String> = emptyList(),
override val lintConfigs: LintConfigs,
override val lintConfigData: LintConfigData,
) : AndroidData

internal data class AndroidBinaryData(
Expand All @@ -78,7 +77,7 @@ internal data class AndroidBinaryData(
override val databinding: Boolean = false,
override val compose: Boolean = false,
override val tags: List<String> = emptyList(),
override val lintConfigs: LintConfigs,
override val lintConfigData: LintConfigData,
val manifestValues: Map<String, String?> = emptyMap(),
val resConfigs: Set<String> = emptySet(),
val multidex: Multidex = Multidex.Native,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package com.grab.grazel.migrate.android

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import com.android.build.gradle.internal.dsl.LintOptions
import com.grab.grazel.GrazelExtension
import com.grab.grazel.bazel.rules.Multidex
import com.grab.grazel.bazel.starlark.BazelDependency
import com.grab.grazel.bazel.starlark.LintConfigs
import com.grab.grazel.gradle.ConfigurationScope.BUILD
import com.grab.grazel.gradle.dependencies.BuildGraphType
import com.grab.grazel.gradle.dependencies.DependenciesDataSource
Expand Down Expand Up @@ -145,7 +143,7 @@ constructor(
resValuesData = extension.extractResValue(matchedVariant),
deps = deps.sorted(),
tags = tags.sorted(),
lintConfigs = lintConfigs
lintConfigData = lintConfigs
)
}

Expand Down Expand Up @@ -220,7 +218,7 @@ constructor(
hasCrashlytics = project.hasCrashlytics,
compose = project.hasCompose,
databinding = project.hasDatabinding,
lintConfigs = lintConfigs,
lintConfigData = lintConfigs,
resConfigs = resourceConfiguration
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.grab.grazel.bazel.rules.Visibility
import com.grab.grazel.bazel.rules.androidBinary
import com.grab.grazel.bazel.rules.androidLibrary
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

Expand All @@ -36,7 +35,7 @@ internal interface AndroidTarget : BazelBuildTarget {
val manifest: String?
val assetsGlob: List<String>
val assetsDir: String?
val lintConfigs: LintConfigs?
val lintConfigData: LintConfigData?
}

internal data class AndroidLibraryTarget(
Expand All @@ -56,7 +55,7 @@ internal data class AndroidLibraryTarget(
override val assetsGlob: List<String> = emptyList(),
override val assetsDir: String? = null,
override val sortKey: String = "0$name",
override val lintConfigs: LintConfigs? = null,
override val lintConfigData: LintConfigData? = null,
) : AndroidTarget {
override fun statements(builder: StatementsBuilder) = builder {
androidLibrary(
Expand All @@ -74,7 +73,7 @@ internal data class AndroidLibraryTarget(
assetsDir = assetsDir,
buildConfigData = buildConfigData,
resValuesData = resValuesData,
lintConfigs = lintConfigs,
lintConfigData = lintConfigData,
)
}
}
Expand All @@ -96,7 +95,7 @@ internal data class AndroidBinaryTarget(
override val assetsGlob: List<String> = emptyList(),
override val assetsDir: String? = null,
override val sortKey: String = "0$name",
override val lintConfigs: LintConfigs? = null,
override val lintConfigData: LintConfigData? = null,
val crunchPng: Boolean = false,
val multidex: Multidex = Multidex.Native,
val debug: Boolean = true,
Expand Down Expand Up @@ -129,7 +128,7 @@ internal data class AndroidBinaryTarget(
assetsGlob = assetsGlob,
buildConfigData = buildConfigData,
assetsDir = assetsDir,
lintConfigs = lintConfigs,
lintConfigData = lintConfigData,
)
}
}
Loading

0 comments on commit 835ecb1

Please sign in to comment.