Skip to content

Converty library plugins to kotlin #4627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 17, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2023 Google LLC
//
// 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.
package com.google.firebase.gradle.plugins

import com.google.firebase.gradle.plugins.ci.Coverage
import java.io.File
import java.nio.file.Paths
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.register

abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {

protected fun kotlinModuleName(project: Project): String {
val fullyQualifiedProjectPath = project.path.replace(":".toRegex(), "-")
return project.rootProject.name + fullyQualifiedProjectPath
}

protected fun setupStaticAnalysis(project: Project, library: FirebaseLibraryExtension) {
project.afterEvaluate {
configurations.all {
if ("lintChecks" == name) {
for (checkProject in library.staticAnalysis.androidLintCheckProjects) {
project.dependencies.add("lintChecks", project.project(checkProject!!))
}
}
}
}
project.tasks.register("firebaseLint") { dependsOn("lint") }
Coverage.apply(library)
}

protected fun getApiInfo(project: Project, srcDirs: Set<File>): TaskProvider<ApiInformationTask> {
val outputFile =
project.rootProject.file(
Paths.get(
project.rootProject.buildDir.path,
"apiinfo",
project.path.substring(1).replace(":", "_")
)
)
val outputApiFile = File(outputFile.absolutePath + "_api.txt")
val apiTxt =
project.file("api.txt").takeIf { it.exists() } ?: project.rootProject.file("empty-api.txt")
val apiInfo =
project.tasks.register<ApiInformationTask>("apiInformation") {
sources.value(project.provider { srcDirs })
apiTxtFile.set(apiTxt)
baselineFile.set(project.file("baseline.txt"))
this.outputFile.set(outputFile)
this.outputApiFile.set(outputApiFile)
updateBaseline.set(project.hasProperty("updateBaseline"))
}
return apiInfo
}

protected fun getGenerateApiTxt(project: Project, srcDirs: Set<File>) =
project.tasks.register<GenerateApiTxtTask>("generateApiTxtFile") {
sources.value(project.provider { srcDirs })
apiTxtFile.set(project.file("api.txt"))
baselineFile.set(project.file("baseline.txt"))
updateBaseline.set(project.hasProperty("updateBaseline"))
}

protected fun getDocStubs(project: Project, srcDirs: Set<File>) =
project.tasks.register<GenerateStubsTask>("docStubs") {
sources.value(project.provider { srcDirs })
}

protected fun configurePublishing(project: Project, firebaseLibrary: FirebaseLibraryExtension) {
project.afterEvaluate {
project.apply<MavenPublishPlugin>()
project.extensions.configure<PublishingExtension> {
repositories.maven {
val s = project.rootProject.buildDir.toString() + "/m2repository"
url = File(s).toURI()
name = "BuildDir"
}
publications.create<MavenPublication>("mavenAar") {
from(project.components.findByName(firebaseLibrary.type.componentName))
artifactId = firebaseLibrary.artifactId.get()
groupId = firebaseLibrary.groupId.get()
firebaseLibrary.applyPomCustomization(pom)
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2023 Google LLC
//
// 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.

package com.google.firebase.gradle.plugins

import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormatExtension
import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormatPlugin
import com.google.common.collect.ImmutableList
import com.google.firebase.gradle.plugins.LibraryType.JAVA
import org.gradle.api.Project
import org.gradle.api.attributes.Attribute
import org.gradle.api.plugins.JavaLibraryPlugin
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

class FirebaseJavaLibraryPlugin : BaseFirebaseLibraryPlugin() {

override fun apply(project: Project) {
project.apply<JavaLibraryPlugin>()
project.apply<GoogleJavaFormatPlugin>()
project.extensions.getByType<GoogleJavaFormatExtension>().toolVersion = "1.10.0"

setupFirebaseLibraryExtension(project)

// reduce the likelihood of kotlin module files colliding.
project.tasks.withType<KotlinCompile> {
kotlinOptions.freeCompilerArgs = ImmutableList.of("-module-name", kotlinModuleName(project))
}
}

private fun setupFirebaseLibraryExtension(project: Project) {
val firebaseLibrary =
project.extensions.create<FirebaseLibraryExtension>("firebaseLibrary", project, JAVA)

setupStaticAnalysis(project, firebaseLibrary)
setupApiInformationAnalysis(project)
configurePublishing(project, firebaseLibrary)
}

private fun setupApiInformationAnalysis(project: Project) {
val srcDirs =
project.convention.getPlugin<JavaPluginConvention>().sourceSets.getByName("main").java.srcDirs

val apiInfo = getApiInfo(project, srcDirs)
val generateApiTxt = getGenerateApiTxt(project, srcDirs)
val docStubs = getDocStubs(project, srcDirs)

project.tasks.getByName("check").dependsOn(docStubs)
project.afterEvaluate {
val classpath =
configurations
.getByName("runtimeClasspath")
.incoming
.artifactView {
attributes { attribute(Attribute.of("artifactType", String::class.java), "jar") }
}
.artifacts
.artifactFiles
apiInfo.configure { classPath = classpath }
generateApiTxt.configure { classPath = classpath }
docStubs.configure { classPath = classpath }
}
}
}
Loading