Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ repositories {
mavenCentral()
maven(url = "https://storage.googleapis.com/android-ci/mvn/")
maven(url = "https://plugins.gradle.org/m2/")
maven(url = "https://s01.oss.sonatype.org/content/repositories/releases/")
}

group = "com.google.firebase"
Expand Down Expand Up @@ -66,11 +67,18 @@ dependencies {
implementation("com.google.code.gson:gson:2.8.9")
implementation(libs.android.gradlePlugin.gradle)
implementation(libs.android.gradlePlugin.builder.test.api)
implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.3") {
exclude("org.jetbrains.kotlinx", "kotlinx-serialization-json")
exclude("org.jetbrains.kotlinx", "kotlinx-serialization-core")
}

testImplementation(gradleTestKit())
testImplementation(libs.bundles.kotest)
testImplementation(libs.mockk)
testImplementation(libs.junit)
testImplementation(libs.truth)
testImplementation("commons-io:commons-io:2.15.1")
testImplementation(kotlin("test"))
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.firebase.gradle.plugins

import com.android.build.gradle.LibraryExtension
import com.google.firebase.gradle.plugins.ci.Coverage
import com.google.firebase.gradle.plugins.services.GMavenService
import java.io.File
import java.nio.file.Paths
import org.gradle.api.Plugin
Expand Down Expand Up @@ -52,6 +53,7 @@ import org.w3c.dom.Element
abstract class BaseFirebaseLibraryPlugin : Plugin<Project> {
protected fun setupDefaults(project: Project, library: FirebaseLibraryExtension) {
with(library) {
project.gradle.sharedServices.registerIfAbsent<GMavenService, _>("gmaven")
previewMode.convention("")
publishJavadoc.convention(true)
artifactId.convention(project.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.AttributeContainer
import org.gradle.api.plugins.PluginManager
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.api.services.BuildServiceRegistry
import org.gradle.api.services.BuildServiceSpec
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkQueue
Expand Down Expand Up @@ -244,3 +247,19 @@ fun LibraryAndroidComponentsExtension.onReleaseVariants(
) {
onVariants(selector().withBuildType("release"), callback)
}

/**
* Register a build service under the specified [name], if it hasn't been registered already.
*
* ```
* project.gradle.sharedServices.registerIfAbsent<GMavenService, _>("gmaven")
* ```
*
* @param T The build service class to register
* @param P The parameters class for the build service to register
* @param name The name to register the build service under
* @param config An optional configuration block to setup the build service with
*/
inline fun <reified T : BuildService<P>, reified P : BuildServiceParameters> BuildServiceRegistry
.registerIfAbsent(name: String, noinline config: BuildServiceSpec<P>.() -> Unit = {}) =
registerIfAbsent(name, T::class.java, config)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.google.firebase.gradle.plugins

import java.io.File
import java.io.InputStream
import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.NodeList

/** Replaces all matching substrings with an empty string (nothing) */
Expand Down Expand Up @@ -124,6 +126,13 @@ fun Element.findOrCreate(tag: String): Element =
fun Element.findElementsByTag(tag: String) =
getElementsByTagName(tag).children().mapNotNull { it as? Element }

/**
* Returns the text of an attribute, if it exists.
*
* @param name The name of the attribute to get the text for
*/
fun Node.textByAttributeOrNull(name: String) = attributes?.getNamedItem(name)?.textContent

/**
* Yields the items of this [NodeList] as a [Sequence].
*
Expand Down Expand Up @@ -267,6 +276,19 @@ infix fun <T> List<T>.diff(other: List<T>): List<Pair<T?, T?>> {
*/
fun <T> List<T>.coerceToSize(targetSize: Int) = List(targetSize) { getOrNull(it) }

/**
* Writes the [InputStream] to this file.
*
* While this method _does_ close the generated output stream, it's the callers responsibility to
* close the passed [stream].
*
* @return This [File] instance for chaining.
*/
fun File.writeStream(stream: InputStream): File {
outputStream().use { stream.copyTo(it) }
return this
}

/**
* The [path][File.path] represented as a qualified unix path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum class PreReleaseVersionType {
* Where `Type` is a case insensitive string of any [PreReleaseVersionType], and `Build` is a two
* digit number (single digits should have a leading zero).
*
* Note that `build` will always be present as starting at one by defalt. That is, the following
* Note that `build` will always be present as starting at one by default. That is, the following
* transform occurs:
* ```
* "12.13.1-beta" // 12.13.1-beta01
Expand Down Expand Up @@ -92,7 +92,7 @@ data class PreReleaseVersion(val type: PreReleaseVersionType, val build: Int = 1
*/
fun fromStringsOrNull(type: String, build: String): PreReleaseVersion? =
runCatching {
val preType = PreReleaseVersionType.valueOf(type.toUpperCase())
val preType = PreReleaseVersionType.valueOf(type.uppercase())
val buildNumber = build.takeUnless { it.isBlank() }?.toInt() ?: 1

PreReleaseVersion(preType, buildNumber)
Expand All @@ -115,7 +115,7 @@ data class PreReleaseVersion(val type: PreReleaseVersionType, val build: Int = 1
* PreReleaseVersion(RC, 12).toString() // "rc12"
* ```
*/
override fun toString() = "${type.name.toLowerCase()}${build.toString().padStart(2, '0')}"
override fun toString() = "${type.name.lowercase()}${build.toString().padStart(2, '0')}"
}

/**
Expand All @@ -140,7 +140,7 @@ data class ModuleVersion(
) : Comparable<ModuleVersion> {

/** Formatted as `MAJOR.MINOR.PATCH-PRE` */
override fun toString() = "$major.$minor.$patch${pre?.let { "-${it.toString()}" } ?: ""}"
override fun toString() = "$major.$minor.$patch${pre?.let { "-$it" } ?: ""}"

override fun compareTo(other: ModuleVersion) =
compareValuesBy(
Expand All @@ -149,7 +149,7 @@ data class ModuleVersion(
{ it.major },
{ it.minor },
{ it.patch },
{ it.pre == null }, // a version with no prerelease version takes precedence
{ it.pre == null }, // a version with no pre-release version takes precedence
{ it.pre },
)

Expand All @@ -176,7 +176,7 @@ data class ModuleVersion(
* ```
*/
val VERSION_REGEX =
"(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)(?:\\-\\b)?(?<pre>\\w\\D+)?(?<build>\\B\\d+)?"
"(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)(?:-\\b)?(?<pre>\\w\\D+)?(?<build>\\B\\d+)?"
.toRegex()

/**
Expand Down Expand Up @@ -209,6 +209,29 @@ data class ModuleVersion(
}
}
.getOrNull()

/**
* Parse a [ModuleVersion] from a string.
*
* You should use [fromStringOrNull] when you don't know the `artifactId` of the corresponding
* artifact, if you don't need to throw on failure, or if you need to throw a more specific
* message.
*
* This method exists to cover the common ground of getting [ModuleVersion] representations of
* artifacts.
*
* @param artifactId The artifact that this version belongs to. Will be used in the error
* message on failure.
* @param version The version to parse into a [ModuleVersion].
* @return A [ModuleVersion] created from the string.
* @throws IllegalArgumentException If the string doesn't represent a valid semver version.
* @see fromStringOrNull
*/
fun fromString(artifactId: String, version: String): ModuleVersion =
fromStringOrNull(version)
?: throw IllegalArgumentException(
"Invalid module version found for '${artifactId}': $version"
)
}

/**
Expand Down
Loading
Loading