Skip to content

Commit b5ad0db

Browse files
authored
dataconnect: replace missingversions.py with the :firebase-dataconnet:updateJson gradle task (#7345)
1 parent 79324e1 commit b5ad0db

File tree

9 files changed

+300
-299
lines changed

9 files changed

+300
-299
lines changed

firebase-dataconnect/connectors/connectors.gradle.kts

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import com.google.firebase.dataconnect.gradle.plugin.UpdateDataConnectExecutableVersionsTask
1817
import org.gradle.kotlin.dsl.withType
1918
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2019
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
@@ -113,98 +112,3 @@ tasks.withType<KotlinJvmCompile>().configureEach {
113112
compilerOptions.freeCompilerArgs.add("-Xexplicit-api=strict")
114113
}
115114
}
116-
117-
// Adds a Gradle task that updates the JSON file that stores the list of Data Connect
118-
// executable versions.
119-
//
120-
// Example 1: Add versions 1.4.3 and 1.4.4 to the JSON file, and set 1.4.4 as the default:
121-
// ../../gradlew -Pversions=1.4.3,1.4.4 -PdefaultVersion=1.4.4 updateJson --info
122-
//
123-
// Example 2: Add version 1.2.3 to the JSON file, but do not change the default version:
124-
// ../../gradlew -Pversion=1.2.3 updateJson --info
125-
//
126-
// The `--info` argument can be omitted; it merely controls the level of log output.
127-
tasks.register<UpdateDataConnectExecutableVersionsTask>("updateJson") {
128-
outputs.upToDateWhen { false }
129-
jsonFile.set(
130-
project.layout.projectDirectory.file(
131-
"../gradleplugin/plugin/src/main/resources/com/google/firebase/dataconnect/gradle/" +
132-
"plugin/DataConnectExecutableVersions.json"
133-
)
134-
)
135-
workDirectory.set(project.layout.buildDirectory.dir("updateJson"))
136-
137-
val propertyNames =
138-
object {
139-
val version = "version"
140-
val versions = "versions"
141-
val updateMode = "updateMode"
142-
val defaultVersion = "defaultVersion"
143-
}
144-
145-
val singleVersion: String? = project.providers.gradleProperty(propertyNames.version).orNull
146-
val multipleVersions: List<String>? =
147-
project.providers.gradleProperty(propertyNames.versions).orNull?.split(',')
148-
versions.set(
149-
buildList {
150-
singleVersion?.let { add(it) }
151-
multipleVersions?.let { addAll(it) }
152-
}
153-
)
154-
155-
doFirst {
156-
if (versions.get().isEmpty()) {
157-
logger.warn(
158-
"WARNING: no '${propertyNames.version}' or '${propertyNames.versions}' specified " +
159-
"for task '$name'; no versions will be added to ${jsonFile.get()}. " +
160-
"Try specifying something like '-P${propertyNames.version}=1.2.3' or " +
161-
"'-P${propertyNames.versions}=1.2.3,4.5.6' on the gradle command line " +
162-
"if you want to add versions (warning code bm6d5ezxzd)"
163-
)
164-
}
165-
}
166-
167-
updateMode.set(
168-
project.providers.gradleProperty(propertyNames.updateMode).map {
169-
when (it) {
170-
"overwrite" -> UpdateDataConnectExecutableVersionsTask.UpdateMode.Overwrite
171-
"update" -> UpdateDataConnectExecutableVersionsTask.UpdateMode.Update
172-
else ->
173-
throw Exception(
174-
"Invalid '${propertyNames.updateMode}' specified for task '$name': $it. " +
175-
"Valid values are 'update' and 'overwrite'. " +
176-
"Try specifying '-P${propertyNames.updateMode}=update' or " +
177-
"'-P${propertyNames.updateMode}=overwrite' on the gradle command line. " +
178-
"(error code v2e3cfqbnf)"
179-
)
180-
}
181-
}
182-
)
183-
184-
doFirst {
185-
if (!updateMode.isPresent) {
186-
logger.warn(
187-
"WARNING: no '${propertyNames.updateMode}' specified for task '$name'; " +
188-
"the default update mode of 'update' will be used when updating ${jsonFile.get()}. " +
189-
"Try specifying '-P${propertyNames.updateMode}=update' or " +
190-
"'-P${propertyNames.updateMode}=overwrite' on the gradle command line " +
191-
"if you want a different update mode, or just want to be explicit about " +
192-
"which update mode is in effect (warning code tjyscqmdne)"
193-
)
194-
}
195-
}
196-
197-
defaultVersion.set(project.providers.gradleProperty(propertyNames.defaultVersion))
198-
199-
doFirst {
200-
if (!defaultVersion.isPresent) {
201-
logger.warn(
202-
"WARNING: no '${propertyNames.defaultVersion}' specified for task '$name'; " +
203-
"the default version will not be updated in ${jsonFile.get()}. " +
204-
"Try specifying something like '-P${propertyNames.defaultVersion}=1.2.3' " +
205-
"on the gradle command line if you want to update the default version " +
206-
"(warning code vqrbrktx9f)"
207-
)
208-
}
209-
}
210-
}

firebase-dataconnect/firebase-dataconnect.gradle.kts

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

17+
import com.google.firebase.dataconnect.gradle.plugin.DataConnectExecutableVersionsRegistry
18+
import com.google.firebase.dataconnect.gradle.plugin.UpdateDataConnectExecutableVersionsTask
1719
import org.gradle.kotlin.dsl.withType
1820
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
1921
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
@@ -24,6 +26,7 @@ plugins {
2426
id("com.google.protobuf")
2527
id("copy-google-services")
2628
alias(libs.plugins.kotlinx.serialization)
29+
id("com.google.firebase.dataconnect.gradle.plugin") apply false
2730
}
2831

2932
firebaseLibrary {
@@ -163,3 +166,13 @@ tasks.withType<KotlinJvmCompile>().configureEach {
163166
compilerOptions.freeCompilerArgs.add("-Xexplicit-api=strict")
164167
}
165168
}
169+
170+
// Registers a Gradle task that updates the JSON file that stores the list of Data Connect
171+
// executable versions. The task gets the list of all versions from the Internet and then
172+
// updates the JSON file with their sizes and hashes.
173+
tasks.register<UpdateDataConnectExecutableVersionsTask>("updateJson") {
174+
jsonFile.set(
175+
file("gradleplugin/plugin/src/main/resources/${DataConnectExecutableVersionsRegistry.PATH}")
176+
)
177+
workDirectory.set(project.layout.buildDirectory.dir("updateJson"))
178+
}

firebase-dataconnect/gradleplugin/plugin/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ dependencies {
2828
implementation(gradleKotlinDsl())
2929
implementation(firebaseLibs.kotlinx.serialization.core)
3030
implementation(firebaseLibs.kotlinx.serialization.json)
31+
implementation("com.google.cloud:google-cloud-storage:2.56.0")
32+
implementation("io.github.z4kn4fein:semver:3.0.0")
3133
}
3234

3335
gradlePlugin {

firebase-dataconnect/gradleplugin/plugin/src/main/kotlin/com/google/firebase/dataconnect/gradle/plugin/DataConnectExecutableDownloadTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
110110
.sorted()
111111
.joinToString(", ")
112112
val applicableVersions =
113-
allVersions.filter { it.version == version && it.os == operatingSystem }
113+
allVersions.filter { it.version.toString() == version && it.os == operatingSystem }
114114

115115
if (applicableVersions.isEmpty()) {
116116
val message =

firebase-dataconnect/gradleplugin/plugin/src/main/kotlin/com/google/firebase/dataconnect/gradle/plugin/DataConnectExecutableVersionRegistry.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.google.firebase.dataconnect.gradle.plugin
1717

18+
import io.github.z4kn4fein.semver.LooseVersionSerializer
19+
import io.github.z4kn4fein.semver.Version
1820
import java.io.InputStream
1921
import java.io.OutputStream
2022
import kotlinx.serialization.ExperimentalSerializationApi
@@ -38,6 +40,7 @@ object DataConnectExecutableVersionsRegistry {
3840
Json {
3941
prettyPrint = true
4042
prettyPrintIndent = " "
43+
allowTrailingComma = true
4144
}
4245
}
4346

@@ -59,13 +62,13 @@ object DataConnectExecutableVersionsRegistry {
5962

6063
@Serializable
6164
data class Root(
62-
val defaultVersion: String,
65+
@Serializable(with = LooseVersionSerializer::class) val defaultVersion: Version,
6366
val versions: List<VersionInfo>,
6467
)
6568

6669
@Serializable
6770
data class VersionInfo(
68-
val version: String,
71+
@Serializable(with = LooseVersionSerializer::class) val version: Version,
6972
@Serializable(with = OperatingSystemSerializer::class) val os: OperatingSystem,
7073
val size: Long,
7174
val sha512DigestHex: String,
@@ -79,24 +82,24 @@ object DataConnectExecutableVersionsRegistry {
7982
)
8083

8184
override fun deserialize(decoder: Decoder): OperatingSystem =
82-
when (val name = decoder.decodeString()) {
83-
"windows" -> OperatingSystem.Windows
84-
"macos" -> OperatingSystem.MacOS
85-
"linux" -> OperatingSystem.Linux
86-
else ->
87-
throw DataConnectGradleException(
85+
decoder.decodeString().let { serializedValue ->
86+
OperatingSystem.entries.singleOrNull { it.serializedValue == serializedValue }
87+
?: throw DataConnectGradleException(
8888
"nd5z2jk4hr",
89-
"Unknown operating system: $name (must be windows, macos, or linux)"
89+
"Unknown operating system: $serializedValue " +
90+
"(must be one of ${OperatingSystem.entries.joinToString { it.serializedValue }})"
9091
)
9192
}
9293

9394
override fun serialize(encoder: Encoder, value: OperatingSystem) =
94-
encoder.encodeString(
95-
when (value) {
96-
OperatingSystem.Windows -> "windows"
97-
OperatingSystem.MacOS -> "macos"
98-
OperatingSystem.Linux -> "linux"
99-
}
100-
)
95+
encoder.encodeString(value.serializedValue)
10196
}
97+
98+
val OperatingSystem.serializedValue: String
99+
get() =
100+
when (this) {
101+
OperatingSystem.Windows -> "windows"
102+
OperatingSystem.MacOS -> "macos"
103+
OperatingSystem.Linux -> "linux"
104+
}
102105
}

firebase-dataconnect/gradleplugin/plugin/src/main/kotlin/com/google/firebase/dataconnect/gradle/plugin/DataConnectProviders.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DataConnectProviders(
4949
val defaultVersion: Provider<DataConnectExecutable> =
5050
project.provider {
5151
val root = DataConnectExecutableVersionsRegistry.load()
52-
DataConnectExecutable.Version(root.defaultVersion)
52+
DataConnectExecutable.Version(root.defaultVersion.toString())
5353
}
5454

5555
valueFromLocalSettings

0 commit comments

Comments
 (0)