| 1269 | } |
| 1270 | |
| 1271 | fun assetsDownload( |
| 1272 | assets: List<Asset>, |
| 1273 | projectDir: File, |
| 1274 | ) { |
| 1275 | val checksumDir = File(projectDir, ".checksum") |
| 1276 | checksumDir.mkdirs() |
| 1277 | |
| 1278 | assets.forEach { asset -> |
| 1279 | val target = File(projectDir, asset.localPath) |
| 1280 | target.parentFile.mkdirs() |
| 1281 | |
| 1282 | // Path for checksum file |
| 1283 | val canonicalName = asset.localPath.replace("/", "_") |
| 1284 | val checksumFile = File(checksumDir, "${asset.variant}-$canonicalName.md5") |
| 1285 | |
| 1286 | // Load previous checksum (empty string if missing) |
| 1287 | val previousChecksum: String = |
| 1288 | if (checksumFile.exists()) { |
| 1289 | checksumFile.readText() |
| 1290 | } else { |
| 1291 | "" |
| 1292 | } |
| 1293 | |
| 1294 | val remoteChecksum = assetsFileChecksum(asset) |
| 1295 | |
| 1296 | val needsDownload = (previousChecksum != remoteChecksum) || previousChecksum.isEmpty() |
| 1297 | |
| 1298 | if (needsDownload) { |
| 1299 | project.logger.lifecycle("Downloading ${asset.url} → ${asset.localPath}") |
| 1300 | |
| 1301 | assetsFileDownload(asset, File(rootProject.projectDir, asset.localPath)) |
| 1302 | // Recompute checksum after download |
| 1303 | val digest = MessageDigest.getInstance("MD5") |
| 1304 | target.inputStream().use { input -> |
| 1305 | val buffer = ByteArray(8192) |
| 1306 | var read: Int |
| 1307 | while (input.read(buffer).also { read = it } > 0) { |
| 1308 | digest.update(buffer, 0, read) |
| 1309 | } |
| 1310 | } |
| 1311 | val newChecksum = digest.digest().joinToString("") { "%02x".format(it) } |
| 1312 | if (!remoteChecksum.isNullOrEmpty() && newChecksum != remoteChecksum) { |
| 1313 | throw GradleException("Check sum mismatch for ${asset.localPath} (expected $remoteChecksum, got $newChecksum)") |
| 1314 | } |
| 1315 | |
| 1316 | checksumFile.writeText(newChecksum) |
| 1317 | project.logger.lifecycle("Updated checksum stored: ${checksumFile.absolutePath}") |
| 1318 | } else { |
| 1319 | project.logger.lifecycle("File ${asset.localPath} is up-to-date (checksum matches).") |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | tasks.register("assetsDownloadDebug") { |
| 1325 | group = "setup" |
no test coverage detected