|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.dataconnect.gradle.plugin |
| 18 | + |
| 19 | +import com.google.firebase.dataconnect.gradle.plugin.DataConnectExecutableDownloadTask.Companion.downloadDataConnectExecutable |
| 20 | +import com.google.firebase.dataconnect.gradle.plugin.DataConnectExecutableDownloadTask.FileInfo |
| 21 | +import java.io.File |
| 22 | +import kotlin.random.Random |
| 23 | +import org.gradle.api.DefaultTask |
| 24 | +import org.gradle.api.file.DirectoryProperty |
| 25 | +import org.gradle.api.file.RegularFileProperty |
| 26 | +import org.gradle.api.provider.ListProperty |
| 27 | +import org.gradle.api.provider.Property |
| 28 | +import org.gradle.api.tasks.Input |
| 29 | +import org.gradle.api.tasks.InputFile |
| 30 | +import org.gradle.api.tasks.Internal |
| 31 | +import org.gradle.api.tasks.Optional |
| 32 | +import org.gradle.api.tasks.TaskAction |
| 33 | + |
| 34 | +@Suppress("unused") |
| 35 | +abstract class UpdateDataConnectExecutableVersionsTask : DefaultTask() { |
| 36 | + |
| 37 | + @get:InputFile abstract val jsonFile: RegularFileProperty |
| 38 | + |
| 39 | + @get:Input abstract val versions: ListProperty<String> |
| 40 | + |
| 41 | + @get:Input @get:Optional abstract val defaultVersion: Property<String> |
| 42 | + |
| 43 | + @get:Input @get:Optional abstract val updateMode: Property<UpdateMode> |
| 44 | + |
| 45 | + @get:Internal abstract val workDirectory: DirectoryProperty |
| 46 | + |
| 47 | + @TaskAction |
| 48 | + fun run() { |
| 49 | + val jsonFile: File = jsonFile.get().asFile |
| 50 | + val versions: List<String> = versions.get() |
| 51 | + val defaultVersion: String? = defaultVersion.orNull |
| 52 | + val updateMode: UpdateMode? = updateMode.orNull |
| 53 | + val workDirectory: File = workDirectory.get().asFile |
| 54 | + |
| 55 | + logger.info("jsonFile={}", jsonFile.absolutePath) |
| 56 | + logger.info("versions={}", versions) |
| 57 | + logger.info("defaultVersion={}", defaultVersion) |
| 58 | + logger.info("updateMode={}", updateMode) |
| 59 | + logger.info("workDirectory={}", workDirectory) |
| 60 | + |
| 61 | + var json: DataConnectExecutableVersionsRegistry.Root = |
| 62 | + if (updateMode == UpdateMode.Overwrite) { |
| 63 | + DataConnectExecutableVersionsRegistry.Root( |
| 64 | + defaultVersion = "<unspecified>", |
| 65 | + versions = emptyList() |
| 66 | + ) |
| 67 | + } else { |
| 68 | + logger.info("Loading JSON file {}", jsonFile.absolutePath) |
| 69 | + DataConnectExecutableVersionsRegistry.load(jsonFile) |
| 70 | + } |
| 71 | + |
| 72 | + if (defaultVersion !== null) { |
| 73 | + json = json.copy(defaultVersion = defaultVersion) |
| 74 | + } |
| 75 | + |
| 76 | + for (version in versions) { |
| 77 | + val windowsExecutable = download(version, OperatingSystem.Windows, workDirectory) |
| 78 | + val macosExecutable = download(version, OperatingSystem.MacOS, workDirectory) |
| 79 | + val linuxExecutable = download(version, OperatingSystem.Linux, workDirectory) |
| 80 | + json = json.withVersions(version, windowsExecutable, macosExecutable, linuxExecutable) |
| 81 | + } |
| 82 | + |
| 83 | + logger.info( |
| 84 | + "Writing information about versions {} to file with updateMode={}: {}", |
| 85 | + versions.joinToString(", "), |
| 86 | + updateMode, |
| 87 | + jsonFile.absolutePath |
| 88 | + ) |
| 89 | + DataConnectExecutableVersionsRegistry.save(json, jsonFile) |
| 90 | + } |
| 91 | + |
| 92 | + private fun DataConnectExecutableVersionsRegistry.Root.withVersions( |
| 93 | + version: String, |
| 94 | + windows: DownloadedFile, |
| 95 | + macos: DownloadedFile, |
| 96 | + linux: DownloadedFile |
| 97 | + ): DataConnectExecutableVersionsRegistry.Root { |
| 98 | + data class UpdatedVersion( |
| 99 | + val operatingSystem: OperatingSystem, |
| 100 | + val sizeInBytes: Long, |
| 101 | + val sha512DigestHex: String, |
| 102 | + ) { |
| 103 | + constructor( |
| 104 | + operatingSystem: OperatingSystem, |
| 105 | + downloadedFile: DownloadedFile |
| 106 | + ) : this(operatingSystem, downloadedFile.sizeInBytes, downloadedFile.sha512DigestHex) |
| 107 | + } |
| 108 | + val updatedVersions = |
| 109 | + listOf( |
| 110 | + UpdatedVersion(OperatingSystem.Windows, windows), |
| 111 | + UpdatedVersion(OperatingSystem.MacOS, macos), |
| 112 | + UpdatedVersion(OperatingSystem.Linux, linux), |
| 113 | + ) |
| 114 | + |
| 115 | + val newVersions = versions.toMutableList() |
| 116 | + for (updatedVersion in updatedVersions) { |
| 117 | + val index = |
| 118 | + newVersions.indexOfFirst { |
| 119 | + it.version == version && it.os == updatedVersion.operatingSystem |
| 120 | + } |
| 121 | + if (index >= 0) { |
| 122 | + val newVersion = |
| 123 | + newVersions[index].copy( |
| 124 | + size = updatedVersion.sizeInBytes, |
| 125 | + sha512DigestHex = updatedVersion.sha512DigestHex, |
| 126 | + ) |
| 127 | + newVersions[index] = newVersion |
| 128 | + } else { |
| 129 | + val newVersion = |
| 130 | + DataConnectExecutableVersionsRegistry.VersionInfo( |
| 131 | + version = version, |
| 132 | + os = updatedVersion.operatingSystem, |
| 133 | + size = updatedVersion.sizeInBytes, |
| 134 | + sha512DigestHex = updatedVersion.sha512DigestHex, |
| 135 | + ) |
| 136 | + newVersions.add(newVersion) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return this.copy(versions = newVersions.toList()) |
| 141 | + } |
| 142 | + |
| 143 | + private fun download( |
| 144 | + version: String, |
| 145 | + operatingSystem: OperatingSystem, |
| 146 | + outputDirectory: File |
| 147 | + ): DownloadedFile { |
| 148 | + val randomId = Random.nextAlphanumericString(length = 20) |
| 149 | + val outputFile = |
| 150 | + File(outputDirectory, "DataConnectToolkit_${version}_${operatingSystem}_$randomId") |
| 151 | + |
| 152 | + downloadDataConnectExecutable(version, operatingSystem, outputFile) |
| 153 | + |
| 154 | + logger.info("Calculating SHA512 hash of file: {}", outputFile.absolutePath) |
| 155 | + val fileInfo = FileInfo.forFile(outputFile) |
| 156 | + |
| 157 | + return DownloadedFile( |
| 158 | + file = outputFile, |
| 159 | + sizeInBytes = fileInfo.sizeInBytes, |
| 160 | + sha512DigestHex = fileInfo.sha512DigestHex, |
| 161 | + ) |
| 162 | + } |
| 163 | + |
| 164 | + private data class DownloadedFile( |
| 165 | + val file: File, |
| 166 | + val sizeInBytes: Long, |
| 167 | + val sha512DigestHex: String, |
| 168 | + ) |
| 169 | + |
| 170 | + enum class UpdateMode { |
| 171 | + Overwrite, |
| 172 | + Update |
| 173 | + } |
| 174 | +} |
0 commit comments