IDEA-303071 do not download maven common libs if they are already downloaded

GitOrigin-RevId: c4a70dbb8080ad73a5cb6fbcd824da92d05981e8
This commit is contained in:
Dmitry Kichinsky
2022-10-04 22:31:00 +00:00
committed by intellij-monorepo-bot
parent 678101140f
commit ec66b53bf0
2 changed files with 54 additions and 13 deletions
@@ -2,8 +2,11 @@
package org.jetbrains.intellij.build.impl
import org.jetbrains.intellij.build.dependencies.*
import java.math.BigInteger
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.security.MessageDigest
object BundledMavenDownloader {
private val mavenCommonLibs: List<String> = listOf(
@@ -26,24 +29,60 @@ object BundledMavenDownloader {
println("Maven common libs at $commonLibs")
}
private fun fileChecksum(path: Path): String? {
return try {
val md5 = MessageDigest.getInstance("MD5")
md5.update(Files.readAllBytes(path))
val digest = md5.digest()
BigInteger(1, digest).toString(32)
}
catch (e: Exception) {
null
}
}
fun downloadMavenCommonLibs(communityRoot: BuildDependenciesCommunityRoot): Path {
val root = communityRoot.communityRoot.resolve("plugins/maven/maven3-server-common/lib")
BuildDependenciesUtil.cleanDirectory(root)
Files.createDirectories(root)
val targetToSourceFiles = HashMap<Path, Path>()
for (coordinates in mavenCommonLibs) {
val split = coordinates.split(':')
check(split.size == 3) {
"Expected exactly 3 coordinates: $coordinates"
}
val targetFile = root.resolve("${split[1]}-${split[2]}.jar")
val uri = BuildDependenciesDownloader.getUriForMavenArtifact(
BuildDependenciesConstants.MAVEN_CENTRAL_URL,
split[0], split[1], split[2], "jar"
)
val file = BuildDependenciesDownloader.downloadFileToCacheLocation(communityRoot, uri)
Files.copy(file, root.resolve("${split[1]}-${split[2]}.jar"))
val sourceFile = BuildDependenciesDownloader.downloadFileToCacheLocation(communityRoot, uri)
targetToSourceFiles[targetFile] = sourceFile
}
Files.list(root).use { stream ->
stream.forEach { file: Path? ->
run {
if (!targetToSourceFiles.containsKey(file)) {
BuildDependenciesUtil.deleteFileOrFolder(file)
}
}
}
}
for (targetFile in targetToSourceFiles.keys) {
val sourceFile = targetToSourceFiles[targetFile]!!
if (!Files.exists(targetFile)) {
Files.copy(sourceFile, targetFile)
} else {
val sourceCheckSum = fileChecksum(sourceFile)
val targetCheckSum = fileChecksum(targetFile)
if (!sourceCheckSum.equals(targetCheckSum)) {
Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING)
}
}
}
return root
}
@@ -440,17 +440,19 @@ public final class BuildDependenciesUtil {
return s.substring(start, end);
}
public static void deleteFileOrFolder(Path file) {
try {
MoreFiles.deleteRecursively(file, RecursiveDeleteOption.ALLOW_INSECURE);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void cleanDirectory(Path directory) throws IOException {
Files.createDirectories(directory);
try (Stream<Path> stream = Files.list(directory)) {
stream.forEach(path -> {
try {
MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE);
}
catch (IOException e) {
throw new RuntimeException(e);
}
});
stream.forEach(BuildDependenciesUtil::deleteFileOrFolder);
}
}