Skip to content

Commit 0697dd3

Browse files
authored
Modernize FirebaseLibraryExtension (#6377)
Per [b/270576405](https://b.corp.google.com/issues/270576405), This migrates `FirebaseLibraryExtension` to Kotlin, and takes advantage of more modern gradle features (such as properties/conventions) to help with build time and maintenance. This also adds documentation for all the exposed properties and options on `FirebaseLibraryExtension`. Future `TODO`s have been left denoting possible future modernization efforts. This PR also fixes the following: - [b/374137268](https://b.corp.google.com/issues/374137268) -> Migrate buildSrc to spotless
1 parent ec48742 commit 0697dd3

File tree

89 files changed

+695
-525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+695
-525
lines changed

appcheck/firebase-appcheck-debug-testing/firebase-appcheck-debug-testing.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugins {
1818
}
1919

2020
firebaseLibrary {
21-
libraryGroup "appcheck"
21+
libraryGroup = "appcheck"
2222
testLab.enabled = true
2323
publishSources = true
2424
releaseNotes {

appcheck/firebase-appcheck-debug/firebase-appcheck-debug.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ plugins {
1717
}
1818

1919
firebaseLibrary {
20-
libraryGroup "appcheck"
20+
libraryGroup = "appcheck"
2121
publishSources = true
2222
releaseNotes {
2323
name.set("{{app_check}} Debug")

appcheck/firebase-appcheck-playintegrity/firebase-appcheck-playintegrity.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ plugins {
1717
}
1818

1919
firebaseLibrary {
20-
libraryGroup "appcheck"
20+
libraryGroup = "appcheck"
2121
publishSources = true
2222
releaseNotes {
2323
name.set("{{app_check}} Play integrity")

appcheck/firebase-appcheck/firebase-appcheck.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugins {
1818
}
1919

2020
firebaseLibrary {
21-
libraryGroup "appcheck"
21+
libraryGroup = "appcheck"
2222
publishSources = true
2323
releaseNotes {
2424
name.set("{{app_check}}")

appcheck/firebase-appcheck/ktx/ktx.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugins {
1818
}
1919

2020
firebaseLibrary {
21-
libraryGroup "appcheck"
21+
libraryGroup = "appcheck"
2222
testLab.enabled = true
2323
publishJavadoc = false
2424
releaseNotes {

buildSrc/build.gradle.kts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// limitations under the License.
1414

1515
plugins {
16-
id("com.ncorti.ktfmt.gradle") version "0.11.0"
1716
alias(libs.plugins.kotlinx.serialization)
17+
alias(libs.plugins.spotless)
1818
`kotlin-dsl`
1919
}
2020

@@ -26,15 +26,23 @@ repositories {
2626
maven(url = "https://plugins.gradle.org/m2/")
2727
}
2828

29+
spotless {
30+
java {
31+
target("src/**/*.java")
32+
targetExclude("**/test/resources/**")
33+
googleJavaFormat("1.22.0").reorderImports(true).skipJavadocFormatting()
34+
}
35+
kotlin {
36+
target("src/**/*.kt")
37+
ktfmt("0.52").googleStyle()
38+
}
39+
}
40+
2941
// Refer latest "perf-plugin" released version on https://maven.google.com/web/index.html?q=perf-plugin#com.google.firebase:perf-plugin
3042
// The System property allows us to integrate with an unreleased version from https://bityl.co/3oYt.
3143
// Refer go/fireperf-plugin-test-on-head for more details.
3244
val perfPluginVersion = System.getenv("FIREBASE_PERF_PLUGIN_VERSION") ?: "1.4.1"
3345

34-
ktfmt {
35-
googleStyle()
36-
}
37-
3846
dependencies {
3947
// Firebase performance plugin, it should be added here because of how gradle dependency
4048
// resolution works, otherwise it breaks Fireperf Test Apps.
@@ -84,7 +92,7 @@ gradlePlugin {
8492
}
8593
register("firebaseLibraryPlugin") {
8694
id = "firebase-library"
87-
implementationClass = "com.google.firebase.gradle.plugins.FirebaseLibraryPlugin"
95+
implementationClass = "com.google.firebase.gradle.plugins.FirebaseAndroidLibraryPlugin"
8896
}
8997
register("firebaseJavaLibraryPlugin") {
9098
id = "firebase-java-library"

buildSrc/src/main/java/com/google/firebase/gradle/NdkBinaryFixTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ abstract class NdkBinaryFixTask : DefaultTask() {
4242
Files.copy(
4343
inputFile.get().asFile.toPath(),
4444
outputFile.toPath(),
45-
StandardCopyOption.REPLACE_EXISTING
45+
StandardCopyOption.REPLACE_EXISTING,
4646
)
4747
}
4848
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/BaseFirebaseLibraryPlugin.kt

Lines changed: 110 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import java.nio.file.Paths
2323
import org.gradle.api.Plugin
2424
import org.gradle.api.Project
2525
import org.gradle.api.artifacts.ProjectDependency
26+
import org.gradle.api.provider.Provider
2627
import org.gradle.api.publish.PublishingExtension
2728
import org.gradle.api.publish.maven.MavenPom
2829
import org.gradle.api.publish.maven.MavenPublication
2930
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
3031
import org.gradle.api.tasks.TaskProvider
3132
import org.gradle.kotlin.dsl.apply
33+
import org.gradle.kotlin.dsl.assign
3234
import org.gradle.kotlin.dsl.configure
3335
import org.gradle.kotlin.dsl.create
3436
import org.gradle.kotlin.dsl.findByType
@@ -37,7 +39,61 @@ import org.gradle.kotlin.dsl.provideDelegate
3739
import org.gradle.kotlin.dsl.register
3840
import org.w3c.dom.Element
3941

42+
/**
43+
* Plugin for Firebase Libraries.
44+
*
45+
* Implements shared functionality between Java and Android libraries.
46+
*
47+
* @see [FirebaseAndroidLibraryPlugin]
48+
* @see [FirebaseJavaLibraryPlugin]
49+
* @see [FirebaseLibraryExtension]
50+
*/
4051
abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
52+
protected fun setupDefaults(project: Project, library: FirebaseLibraryExtension) {
53+
with(library) {
54+
previewMode.convention("")
55+
publishJavadoc.convention(true)
56+
publishSources.convention(true)
57+
artifactId.convention(project.name)
58+
groupId.convention(project.provider { project.group.toString() })
59+
libraryGroup.convention(artifactId)
60+
61+
customizePom {
62+
licenses {
63+
license {
64+
name = "The Apache Software License, Version 2.0"
65+
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
66+
}
67+
}
68+
scm {
69+
connection = "scm:git:https://github.com/firebase/firebase-android-sdk.git"
70+
url = "https://github.com/firebase/firebase-android-sdk"
71+
}
72+
}
73+
74+
releaseNotes {
75+
enabled.convention(true)
76+
hasKTX.convention(true)
77+
artifactId.convention(project.name)
78+
artifactName.convention(project.name)
79+
}
80+
81+
val parent = project.parent
82+
// TODO(): remove when we get rid of ktx modules
83+
if (project.name == "ktx" && parent !== null) {
84+
artifactId.convention(parent.provider { "${parent.name}-ktx" })
85+
groupId.convention(parent.provider { "${parent.group}" })
86+
}
87+
88+
androidLintCheckProjects.convention(
89+
project
90+
.provideProperty<String>("firebase.checks.lintProjects")
91+
.map { it.split(",") }
92+
.orElse(emptyList())
93+
)
94+
}
95+
}
96+
4197
protected fun registerMakeReleaseNotesTask(project: Project) =
4298
project.tasks.register<MakeReleaseNotesTask>("makeReleaseNotes") {
4399
val changelog = project.file("CHANGELOG.md")
@@ -59,8 +115,8 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
59115
project.afterEvaluate {
60116
configurations.all {
61117
if ("lintChecks" == name) {
62-
for (checkProject in library.staticAnalysis.androidLintCheckProjects) {
63-
project.dependencies.add("lintChecks", project.project(checkProject!!))
118+
for (checkProject in library.androidLintCheckProjects.get()) {
119+
project.dependencies.add("lintChecks", project.project(checkProject))
64120
}
65121
}
66122
}
@@ -75,7 +131,7 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
75131
Paths.get(
76132
project.rootProject.buildDir.path,
77133
"apiinfo",
78-
project.path.substring(1).replace(":", "_")
134+
project.path.substring(1).replace(":", "_"),
79135
)
80136
)
81137
val outputApiFile = File(outputFile.absolutePath + "_api.txt")
@@ -122,23 +178,26 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
122178
* configures maven pom generation.
123179
*
124180
* @see [applyPomTransformations]
125-
* @see [FirebaseLibraryExtension.applyPomCustomization]
181+
* @see [FirebaseLibraryExtension.customizePomAction]
126182
*/
127183
protected fun configurePublishing(project: Project, firebaseLibrary: FirebaseLibraryExtension) {
128184
with(project) {
129185
apply<MavenPublishPlugin>()
186+
187+
// TODO(b/371625225): sources jar is missing from java libs
130188
extensions.configure<PublishingExtension> {
131189
repositories.maven {
132190
url = rootProject.fileFromBuildDir("m2repository").toURI()
133191
name = "BuildDir"
134192
}
135193
publications.create<MavenPublication>("mavenAar") {
194+
// TODO(https://github.com/gradle/gradle/issues/18619): Remove afterEvaluate when
195+
// properties are supported
136196
afterEvaluate {
137-
artifactId =
138-
firebaseLibrary.artifactId.get() // these dont get populated until afterEvaluate :(
197+
artifactId = firebaseLibrary.artifactId.get()
139198
groupId = firebaseLibrary.groupId.get()
140199

141-
firebaseLibrary.applyPomCustomization(pom)
200+
pom(firebaseLibrary.customizePomAction)
142201
firebaseLibrary.applyPomTransformations(pom)
143202
from(components.findByName(firebaseLibrary.type.componentName))
144203
}
@@ -155,7 +214,6 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
155214
* @param pom the [MavenPom] to prepare
156215
* @see [addTypeWithAARSupport]
157216
*/
158-
// TODO(b/270576405): Combine with applyPomCustomization when migrating FirebaseLibraryExtension
159217
private fun FirebaseLibraryExtension.applyPomTransformations(pom: MavenPom) {
160218
pom.withXml {
161219
val dependencies = asElement().findElementsByTag("dependency")
@@ -198,7 +256,7 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
198256
/**
199257
* A list of _all_ dependencies that publish `aar` artifacts.
200258
*
201-
* This is collected via the [runtimeClasspath][FirebaseLibraryExtension.getRuntimeClasspath], and
259+
* This is collected via the [runtimeClasspath][FirebaseLibraryExtension.runtimeClasspath], and
202260
* includes project level dependencies as well as external dependencies.
203261
*
204262
* The dependencies are mapped to their [mavenName][toMavenName].
@@ -209,15 +267,17 @@ abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
209267
// TODO(b/277607560): Remove when Gradle's MavenPublishPlugin adds functionality for aar types
210268
fun FirebaseLibraryExtension.resolveAndroidDependencies() =
211269
resolveExternalAndroidLibraries() +
212-
resolveProjectLevelDependencies().filter { it.type == LibraryType.ANDROID }.map { it.mavenName }
270+
resolveProjectLevelDependencies()
271+
.filter { it.type == LibraryType.ANDROID }
272+
.map { it.mavenName.get() }
213273

214274
/**
215275
* A list of project level dependencies.
216276
*
217-
* This is collected via the [runtimeClasspath][FirebaseLibraryExtension.getRuntimeClasspath].
277+
* This is collected via the [runtimeClasspath][FirebaseLibraryExtension.runtimeClasspath].
218278
*
219279
* @throws RuntimeException if a project level dependency is found that doesn't have
220-
* [FirebaseLibraryExtension]
280+
* [FirebaseLibraryExtension]
221281
*/
222282
// TODO(b/277607560): Remove when Gradle's MavenPublishPlugin adds functionality for aar types
223283
fun FirebaseLibraryExtension.resolveProjectLevelDependencies() =
@@ -264,16 +324,23 @@ fun FirebaseLibraryExtension.resolveExternalAndroidLibraries() =
264324
* ```
265325
* "com.google.firebase:firebase-common:16.0.5"
266326
* ```
327+
*
328+
* @see [mavenName]
267329
*/
268330
val FirebaseLibraryExtension.artifactName: String
269-
get() = "$mavenName:$version"
331+
get() = "${mavenName.get()}:$version"
270332

271333
/**
272334
* Fetches the latest version for this SDK from GMaven.
273335
*
274336
* Uses [GmavenHelper] to make the request.
337+
*
338+
* To get the latest released version per the local `gradle.properties`, use [latestReleasedVersion]
339+
* .
340+
*
341+
* @see [ModuleVersion]
275342
*/
276-
val FirebaseLibraryExtension.latestVersion: ModuleVersion
343+
val FirebaseLibraryExtension.latestGMavenVersion: ModuleVersion
277344
get() {
278345
val latestVersion = GmavenHelper(groupId.get(), artifactId.get()).getLatestReleasedVersion()
279346

@@ -283,6 +350,22 @@ val FirebaseLibraryExtension.latestVersion: ModuleVersion
283350
)
284351
}
285352

353+
/**
354+
* The latest version released, per the `gradle.properties` file.
355+
*
356+
* Checks for the property `latestReleasedVersion`, which should be in the `gradle.properties` files
357+
* of released SDKs (alongside the current `version`).
358+
*
359+
* To fetch the latest released version per GMaven, use [latestGMavenVersion].
360+
*
361+
* ```properties
362+
* version=19.2.1
363+
* latestReleasedVersion=19.2.0
364+
* ```
365+
*/
366+
val FirebaseLibraryExtension.latestReleasedVersion: Provider<String>
367+
get() = project.provideProperty<String>("latestReleasedVersion")
368+
286369
/**
287370
* Fetches the namespace for this SDK from the [LibraryExtension].
288371
*
@@ -298,3 +381,16 @@ val FirebaseLibraryExtension.namespace: String
298381
get() =
299382
project.extensions.getByType<LibraryExtension>().namespace
300383
?: throw RuntimeException("Project doesn't have a defined namespace: ${project.path}")
384+
385+
/**
386+
* The name of this library on maven, without the version.
387+
*
388+
* For example, the following could be a maven name:
389+
* ```
390+
* "com.google.firebase:firebase-common"
391+
* ```
392+
*
393+
* @see [artifactName]
394+
*/
395+
val FirebaseLibraryExtension.mavenName: Provider<String>
396+
get() = groupId.zip(artifactId) { groupId, artifactId -> "$groupId:$artifactId" }

buildSrc/src/main/java/com/google/firebase/gradle/plugins/Changelog.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ data class Changelog(val releases: List<ReleaseEntry>) {
118118
data class ReleaseEntry(
119119
val version: ModuleVersion?,
120120
val content: ReleaseContent,
121-
val ktx: ReleaseContent?
121+
val ktx: ReleaseContent?,
122122
) {
123123

124124
override fun toString(): String {
@@ -151,7 +151,7 @@ data class ReleaseEntry(
151151
* This exists to provide a means for tooling to create new sections explicitly, versus offering
152152
* default values to [ReleaseEntry]
153153
* - as this could lead to edge case scenarios where empty [ReleaseEntry] instances are
154-
* accidentally created.
154+
* accidentally created.
155155
*/
156156
val Empty = ReleaseEntry(null, ReleaseContent("", emptyList()), null)
157157

buildSrc/src/main/java/com/google/firebase/gradle/plugins/CheckHeadDependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.gradle.api.tasks.TaskAction
3030
* [FirebaseLibraryExtension.artifactId], so that the check is version agnostic.
3131
*
3232
* @throws GradleException if any project level dependencies are found that are not included in the
33-
* release
33+
* release
3434
*/
3535
abstract class CheckHeadDependencies : DefaultTask() {
3636
@get:Input abstract val projectsToPublish: ListProperty<FirebaseLibraryExtension>

0 commit comments

Comments
 (0)