|
| 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 | +} |
0 commit comments