1515 */
1616package com.google.firebase.dataconnect.gradle.plugin
1717
18+ import io.github.z4kn4fein.semver.Version
19+ import io.github.z4kn4fein.semver.toVersion
1820import java.io.File
1921import java.net.HttpURLConnection
2022import java.net.URL
@@ -46,6 +48,8 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
4648
4749 @get:Input @get:Optional abstract val operatingSystem: Property <OperatingSystem >
4850
51+ @get:Input @get:Optional abstract val cpuArchitecture: Property <CpuArchitecture >
52+
4953 @get:Internal abstract val buildDirectory: DirectoryProperty
5054
5155 @get:OutputFile abstract val outputFile: RegularFileProperty
@@ -59,12 +63,14 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
5963 val inputFile: File ? = inputFile.orNull?.asFile
6064 val version: String? = version.orNull
6165 val operatingSystem: OperatingSystem = operatingSystem.get()
66+ val cpuArchitecture: CpuArchitecture = cpuArchitecture.get()
6267 val buildDirectory: File = buildDirectory.get().asFile
6368 val outputFile: File = outputFile.get().asFile
6469
6570 logger.info(" inputFile: {}" , inputFile)
6671 logger.info(" version: {}" , version)
6772 logger.info(" operatingSystem: {}" , operatingSystem)
73+ logger.info(" cpuArchitecture: {}" , cpuArchitecture)
6874 logger.info(" buildDirectory: {}" , buildDirectory)
6975 logger.info(" outputFile: {}" , outputFile)
7076
@@ -81,8 +87,15 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
8187 } else if (inputFile != = null ) {
8288 runWithFile(inputFile = inputFile, outputFile = outputFile)
8389 } else if (version != = null ) {
84- downloadDataConnectExecutable(version, operatingSystem, outputFile, execOperations)
85- verifyOutputFile(outputFile, operatingSystem, version)
90+ val parsedVersion = version.toVersion()
91+ downloadDataConnectExecutable(
92+ parsedVersion,
93+ operatingSystem,
94+ cpuArchitecture,
95+ outputFile,
96+ execOperations,
97+ )
98+ verifyOutputFile(outputFile, operatingSystem, cpuArchitecture, parsedVersion)
8699 } else {
87100 throw DataConnectGradleException (
88101 " chc94cq7vx" ,
@@ -95,35 +108,34 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
95108 private fun verifyOutputFile (
96109 outputFile : File ,
97110 operatingSystem : OperatingSystem ,
98- version : String
111+ cpuArchitecture : CpuArchitecture ,
112+ version : Version
99113 ) {
100114 logger.info(" Verifying file size and SHA512 digest of file: {}" , outputFile)
101115 val fileInfo = FileInfo .forFile(outputFile)
102116
103- val allVersions = DataConnectExecutableVersionsRegistry .load().versions
104- val allVersionNames =
105- allVersions
106- .asSequence()
107- .filter { it.os == operatingSystem }
108- .map { it.version }
109- .distinct()
110- .sorted()
111- .joinToString(" , " )
112- val applicableVersions =
113- allVersions.filter { it.version.toString() == version && it.os == operatingSystem }
117+ val versionsForOsArch: List <DataConnectExecutableVersionsRegistry .VersionInfo > =
118+ DataConnectExecutableVersionsRegistry .load().versions.filter {
119+ it.os == operatingSystem && (it.arch == = null || it.arch == cpuArchitecture)
120+ }
121+ val applicableVersions = versionsForOsArch.filter { it.version == version }
122+ val versionNamesStringForOsArch =
123+ versionsForOsArch.map { it.version }.distinct().sorted().joinToString(" , " ) { it.toString() }
114124
125+ val osDisplayId: String =
126+ operatingSystem.downloadFileNameMoniker + " /" + cpuArchitecture.downloadFileNameMoniker
115127 if (applicableVersions.isEmpty()) {
116128 val message =
117129 " verification information for Data Connect toolkit executable" +
118- " version $version for $operatingSystem is not known;" +
119- " known versions for $operatingSystem are: $allVersionNames " +
130+ " version $version for $osDisplayId is not known;" +
131+ " known versions for $osDisplayId are: $versionNamesStringForOsArch " +
120132 " (loaded from ${DataConnectExecutableVersionsRegistry .PATH } )"
121133 logger.error(" ERROR: $message " )
122134 throw DataConnectGradleException (" ym8assbfgw" , message)
123135 } else if (applicableVersions.size > 1 ) {
124136 val message =
125137 " INTERNAL ERROR: ${applicableVersions.size} verification information records for" +
126- " Data Connect toolkit executable version $version for $operatingSystem were found in" +
138+ " Data Connect toolkit executable version $version for $osDisplayId were found in" +
127139 " ${DataConnectExecutableVersionsRegistry .PATH } , but expected exactly 1"
128140 logger.error(" ERROR: $message " )
129141 throw DataConnectGradleException (" zyw5xrky6e" , message)
@@ -152,8 +164,8 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
152164 if (verificationErrors.isNotEmpty()) {
153165 val errorMessage =
154166 " Verification of ${outputFile.absolutePath} " +
155- " (version=${versionInfo.version} os=${versionInfo.os} ) failed: " +
156- " ${verificationErrors.joinToString(" , " )} "
167+ " (version=${versionInfo.version} os=${versionInfo.os} arch= ${versionInfo.arch} ) " +
168+ " failed: ${verificationErrors.joinToString(" , " )} "
157169 logger.error(errorMessage)
158170 throw DataConnectGradleException (" x9dfwhjr9c" , errorMessage)
159171 }
@@ -199,19 +211,42 @@ abstract class DataConnectExecutableDownloadTask : DefaultTask() {
199211 }
200212
201213 companion object {
214+
215+ private val firstVersionIncludingArchInFileName = " 2.16.0" .toVersion()
216+
217+ private val OperatingSystem .downloadFileNameMoniker: String
218+ get() =
219+ when (this ) {
220+ OperatingSystem .Windows -> " windows"
221+ OperatingSystem .MacOS -> " macos"
222+ OperatingSystem .Linux -> " linux"
223+ }
224+
225+ private val CpuArchitecture .downloadFileNameMoniker: String
226+ get() =
227+ when (this ) {
228+ CpuArchitecture .AMD64 -> " amd64"
229+ CpuArchitecture .ARM64 -> " arm64"
230+ }
231+
202232 fun Task.downloadDataConnectExecutable (
203- version : String ,
233+ version : Version ,
204234 operatingSystem : OperatingSystem ,
235+ cpuArchitecture : CpuArchitecture ,
205236 outputFile : File ,
206237 execOperations : ExecOperations
207238 ) {
208- val osName =
209- when (operatingSystem) {
210- OperatingSystem .Windows -> " windows"
211- OperatingSystem .MacOS -> " macos"
212- OperatingSystem .Linux -> " linux"
239+ val downloadFileName = buildString {
240+ append(" dataconnect-emulator" )
241+ append(' -' )
242+ append(operatingSystem.downloadFileNameMoniker)
243+ if (version >= firstVersionIncludingArchInFileName) {
244+ append(' -' )
245+ append(cpuArchitecture.downloadFileNameMoniker)
213246 }
214- val downloadFileName = " dataconnect-emulator-$osName -v$version "
247+ append(" -v" )
248+ append(version)
249+ }
215250 val url =
216251 URL (" https://storage.googleapis.com/firemat-preview-drop/emulator/$downloadFileName " )
217252
0 commit comments