|
| 1 | +package org.jetbrains.kotlin.gradle.targets.js |
| 2 | + |
| 3 | +import org.gradle.api.DefaultTask |
| 4 | +import org.gradle.api.artifacts.Configuration |
| 5 | +import org.gradle.api.file.ArchiveOperations |
| 6 | +import org.gradle.api.file.FileSystemOperations |
| 7 | +import org.gradle.api.model.ObjectFactory |
| 8 | +import org.gradle.api.provider.Provider |
| 9 | +import org.gradle.api.tasks.* |
| 10 | +import org.gradle.internal.hash.FileHasher |
| 11 | +import org.gradle.work.DisableCachingByDefault |
| 12 | +import org.jetbrains.kotlin.gradle.logging.kotlinInfo |
| 13 | +import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatsService |
| 14 | +import org.jetbrains.kotlin.statistics.metrics.NumericalMetrics |
| 15 | +import java.io.File |
| 16 | +import java.net.URI |
| 17 | +import javax.inject.Inject |
| 18 | + |
| 19 | +@DisableCachingByDefault |
| 20 | +abstract class AbstractSetupTask<Env : AbstractEnv, Settings : AbstractSettings<Env>> : DefaultTask() { |
| 21 | + @get:Internal |
| 22 | + protected abstract val settings: Settings |
| 23 | + |
| 24 | + @get:Internal |
| 25 | + protected abstract val artifactPattern: String |
| 26 | + |
| 27 | + @get:Internal |
| 28 | + protected abstract val artifactModule: String |
| 29 | + |
| 30 | + @get:Internal |
| 31 | + protected abstract val artifactName: String |
| 32 | + |
| 33 | + @get:Internal |
| 34 | + protected val env: Env by lazy { settings.requireConfigured() } |
| 35 | + |
| 36 | + private val shouldDownload by lazy { |
| 37 | + env.download |
| 38 | + } |
| 39 | + |
| 40 | + @get:Inject |
| 41 | + internal abstract val archiveOperations: ArchiveOperations |
| 42 | + |
| 43 | + @get:Inject |
| 44 | + internal abstract val fileHasher: FileHasher |
| 45 | + |
| 46 | + @get:Inject |
| 47 | + internal abstract val objects: ObjectFactory |
| 48 | + |
| 49 | + @get:Inject |
| 50 | + internal abstract val fs: FileSystemOperations |
| 51 | + |
| 52 | + val ivyDependency: String |
| 53 | + @Input get() = env.ivyDependency |
| 54 | + |
| 55 | + val downloadBaseUrl: String? |
| 56 | + @Input |
| 57 | + @Optional |
| 58 | + get() = env.downloadBaseUrl |
| 59 | + |
| 60 | + val destination: File |
| 61 | + @OutputDirectory get() = env.dir |
| 62 | + |
| 63 | + val destinationHashFile: File |
| 64 | + @OutputFile get() = destination.parentFile.resolve("${destination.name}.hash") |
| 65 | + |
| 66 | + @Transient |
| 67 | + @get:Internal |
| 68 | + internal var configuration: Provider<Configuration>? = null |
| 69 | + |
| 70 | + @get:Classpath |
| 71 | + @get:Optional |
| 72 | + val dist: File? by lazy { |
| 73 | + if (!shouldDownload) return@lazy null |
| 74 | + |
| 75 | + withUrlRepo { |
| 76 | + val startDownloadTime = System.currentTimeMillis() |
| 77 | + configuration!!.get().files.single().also { |
| 78 | + val downloadDuration = System.currentTimeMillis() - startDownloadTime |
| 79 | + if (downloadDuration > 0) { |
| 80 | + KotlinBuildStatsService.getInstance() |
| 81 | + ?.report(NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED, it.length() * 1000 / downloadDuration) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + init { |
| 88 | + onlyIf { |
| 89 | + shouldDownload |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Suppress("unused") |
| 94 | + @TaskAction |
| 95 | + fun exec() { |
| 96 | + if (!shouldDownload) return |
| 97 | + |
| 98 | + logger.kotlinInfo("Using distribution from '$dist'") |
| 99 | + |
| 100 | + extractWithUpToDate( |
| 101 | + destination, |
| 102 | + destinationHashFile, |
| 103 | + dist!!, |
| 104 | + fileHasher, |
| 105 | + ::extract |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + private fun <T> withUrlRepo(action: () -> T): T { |
| 110 | + val repo = downloadBaseUrl?.let { |
| 111 | + project.repositories.ivy { repo -> |
| 112 | + repo.name = "Distributions at ${it}" |
| 113 | + repo.url = URI(it) |
| 114 | + |
| 115 | + repo.patternLayout { |
| 116 | + it.artifact(artifactPattern) |
| 117 | + } |
| 118 | + repo.metadataSources { it.artifact() } |
| 119 | + repo.content { it.includeModule(artifactModule, artifactName) } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return action().also { |
| 124 | + repo?.let { project.repositories.remove(it) } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private fun extractWithUpToDate( |
| 129 | + destination: File, |
| 130 | + destinationHashFile: File, |
| 131 | + dist: File, |
| 132 | + fileHasher: FileHasher, |
| 133 | + extract: (File, File) -> Unit, |
| 134 | + ) { |
| 135 | + var distHash: String? = null |
| 136 | + val upToDate = destinationHashFile.let { file -> |
| 137 | + if (file.exists()) { |
| 138 | + file.useLines { seq -> |
| 139 | + val list = seq.first().split(" ") |
| 140 | + list.size == 2 && |
| 141 | + list[0] == fileHasher.calculateDirHash(destination) && |
| 142 | + list[1] == fileHasher.hash(dist).toByteArray().toHex().also { distHash = it } |
| 143 | + } |
| 144 | + } else false |
| 145 | + } |
| 146 | + |
| 147 | + if (upToDate) { |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + if (destination.isDirectory) { |
| 152 | + destination.deleteRecursively() |
| 153 | + } |
| 154 | + |
| 155 | + extract(dist, destination.parentFile) |
| 156 | + |
| 157 | + destinationHashFile.writeText( |
| 158 | + fileHasher.calculateDirHash(destination)!! + |
| 159 | + " " + |
| 160 | + (distHash ?: fileHasher.hash(dist).toByteArray().toHex()) |
| 161 | + ) |
| 162 | + } |
| 163 | + |
| 164 | + abstract fun extract(archive: File, destination: File) |
| 165 | +} |
0 commit comments