Skip to content

Commit 7e2bec1

Browse files
emilypgoogleyifanyangdaymxn
authored
Convert firebase library plugins to kotlin (#4627)
* Converty library plugins to kotlin * Refactor shared logic out * Add copyright notices * debug api info * Added util method for more idiomatic Plugin application * Resolve comments * Simplify publishing * Refactor plugin base class * Add copyright notice * Adjust leftover plugin apply * Use takeIf --------- Co-authored-by: Yifan Yang <[email protected]> Co-authored-by: Daymon <[email protected]>
1 parent 0c1240e commit 7e2bec1

File tree

6 files changed

+317
-497
lines changed

6 files changed

+317
-497
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.firebase.gradle.plugins
15+
16+
import com.google.firebase.gradle.plugins.ci.Coverage
17+
import java.io.File
18+
import java.nio.file.Paths
19+
import org.gradle.api.Plugin
20+
import org.gradle.api.Project
21+
import org.gradle.api.publish.PublishingExtension
22+
import org.gradle.api.publish.maven.MavenPublication
23+
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
24+
import org.gradle.api.tasks.TaskProvider
25+
import org.gradle.kotlin.dsl.apply
26+
import org.gradle.kotlin.dsl.configure
27+
import org.gradle.kotlin.dsl.create
28+
import org.gradle.kotlin.dsl.register
29+
30+
abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
31+
32+
protected fun kotlinModuleName(project: Project): String {
33+
val fullyQualifiedProjectPath = project.path.replace(":".toRegex(), "-")
34+
return project.rootProject.name + fullyQualifiedProjectPath
35+
}
36+
37+
protected fun setupStaticAnalysis(project: Project, library: FirebaseLibraryExtension) {
38+
project.afterEvaluate {
39+
configurations.all {
40+
if ("lintChecks" == name) {
41+
for (checkProject in library.staticAnalysis.androidLintCheckProjects) {
42+
project.dependencies.add("lintChecks", project.project(checkProject!!))
43+
}
44+
}
45+
}
46+
}
47+
project.tasks.register("firebaseLint") { dependsOn("lint") }
48+
Coverage.apply(library)
49+
}
50+
51+
protected fun getApiInfo(project: Project, srcDirs: Set<File>): TaskProvider<ApiInformationTask> {
52+
val outputFile =
53+
project.rootProject.file(
54+
Paths.get(
55+
project.rootProject.buildDir.path,
56+
"apiinfo",
57+
project.path.substring(1).replace(":", "_")
58+
)
59+
)
60+
val outputApiFile = File(outputFile.absolutePath + "_api.txt")
61+
val apiTxt =
62+
project.file("api.txt").takeIf { it.exists() } ?: project.rootProject.file("empty-api.txt")
63+
val apiInfo =
64+
project.tasks.register<ApiInformationTask>("apiInformation") {
65+
sources.value(project.provider { srcDirs })
66+
apiTxtFile.set(apiTxt)
67+
baselineFile.set(project.file("baseline.txt"))
68+
this.outputFile.set(outputFile)
69+
this.outputApiFile.set(outputApiFile)
70+
updateBaseline.set(project.hasProperty("updateBaseline"))
71+
}
72+
return apiInfo
73+
}
74+
75+
protected fun getGenerateApiTxt(project: Project, srcDirs: Set<File>) =
76+
project.tasks.register<GenerateApiTxtTask>("generateApiTxtFile") {
77+
sources.value(project.provider { srcDirs })
78+
apiTxtFile.set(project.file("api.txt"))
79+
baselineFile.set(project.file("baseline.txt"))
80+
updateBaseline.set(project.hasProperty("updateBaseline"))
81+
}
82+
83+
protected fun getDocStubs(project: Project, srcDirs: Set<File>) =
84+
project.tasks.register<GenerateStubsTask>("docStubs") {
85+
sources.value(project.provider { srcDirs })
86+
}
87+
88+
protected fun configurePublishing(project: Project, firebaseLibrary: FirebaseLibraryExtension) {
89+
project.afterEvaluate {
90+
project.apply<MavenPublishPlugin>()
91+
project.extensions.configure<PublishingExtension> {
92+
repositories.maven {
93+
val s = project.rootProject.buildDir.toString() + "/m2repository"
94+
url = File(s).toURI()
95+
name = "BuildDir"
96+
}
97+
publications.create<MavenPublication>("mavenAar") {
98+
from(project.components.findByName(firebaseLibrary.type.componentName))
99+
artifactId = firebaseLibrary.artifactId.get()
100+
groupId = firebaseLibrary.groupId.get()
101+
firebaseLibrary.applyPomCustomization(pom)
102+
}
103+
}
104+
}
105+
}
106+
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/FirebaseJavaLibraryPlugin.java

Lines changed: 0 additions & 207 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.gradle.plugins
16+
17+
import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormatExtension
18+
import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormatPlugin
19+
import com.google.common.collect.ImmutableList
20+
import com.google.firebase.gradle.plugins.LibraryType.JAVA
21+
import org.gradle.api.Project
22+
import org.gradle.api.attributes.Attribute
23+
import org.gradle.api.plugins.JavaLibraryPlugin
24+
import org.gradle.api.plugins.JavaPluginConvention
25+
import org.gradle.kotlin.dsl.*
26+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
27+
28+
class FirebaseJavaLibraryPlugin : BaseFirebaseLibraryPlugin() {
29+
30+
override fun apply(project: Project) {
31+
project.apply<JavaLibraryPlugin>()
32+
project.apply<GoogleJavaFormatPlugin>()
33+
project.extensions.getByType<GoogleJavaFormatExtension>().toolVersion = "1.10.0"
34+
35+
setupFirebaseLibraryExtension(project)
36+
37+
// reduce the likelihood of kotlin module files colliding.
38+
project.tasks.withType<KotlinCompile> {
39+
kotlinOptions.freeCompilerArgs = ImmutableList.of("-module-name", kotlinModuleName(project))
40+
}
41+
}
42+
43+
private fun setupFirebaseLibraryExtension(project: Project) {
44+
val firebaseLibrary =
45+
project.extensions.create<FirebaseLibraryExtension>("firebaseLibrary", project, JAVA)
46+
47+
setupStaticAnalysis(project, firebaseLibrary)
48+
setupApiInformationAnalysis(project)
49+
configurePublishing(project, firebaseLibrary)
50+
}
51+
52+
private fun setupApiInformationAnalysis(project: Project) {
53+
val srcDirs =
54+
project.convention.getPlugin<JavaPluginConvention>().sourceSets.getByName("main").java.srcDirs
55+
56+
val apiInfo = getApiInfo(project, srcDirs)
57+
val generateApiTxt = getGenerateApiTxt(project, srcDirs)
58+
val docStubs = getDocStubs(project, srcDirs)
59+
60+
project.tasks.getByName("check").dependsOn(docStubs)
61+
project.afterEvaluate {
62+
val classpath =
63+
configurations
64+
.getByName("runtimeClasspath")
65+
.incoming
66+
.artifactView {
67+
attributes { attribute(Attribute.of("artifactType", String::class.java), "jar") }
68+
}
69+
.artifacts
70+
.artifactFiles
71+
apiInfo.configure { classPath = classpath }
72+
generateApiTxt.configure { classPath = classpath }
73+
docStubs.configure { classPath = classpath }
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)