IJPL-866 refactor - extract checkAssetUniqueness

GitOrigin-RevId: 88ca874c0954b1d1f1b91e9fa343a2176fbe22ce
This commit is contained in:
Vladimir Krivosheev
2024-03-23 16:58:02 +00:00
committed by intellij-monorepo-bot
parent 13220b3f68
commit d2d23c77a1
3 changed files with 56 additions and 61 deletions
@@ -346,7 +346,7 @@ class JarPackager private constructor(
val outFile = outDir.resolve(item.relativeOutputFile)
val asset = if (packToDir) {
assets.computeIfAbsent(moduleOutDir) { file ->
AssetDescriptor(isDir = true, file = file, relativePath = "", pathInClassLog = "")
AssetDescriptor(isDir = true, file = file, relativePath = "", pathInClassLog = "", nativeFiles = null)
}
}
else {
@@ -753,7 +753,7 @@ private data class AssetDescriptor(
@JvmField val relativePath: String,
@JvmField var effectiveFile: Path = file,
@JvmField val pathInClassLog: String,
@JvmField val nativeFiles: List<String>? = null
@JvmField val nativeFiles: List<String>?,
) {
@JvmField
val sources: MutableList<Source> = mutableListOf()
@@ -851,21 +851,12 @@ private suspend fun buildJars(
dryRun: Boolean,
layout: BaseLayout?
): Map<ZipSource, List<String>> {
val uniqueFiles = HashMap<Path, List<Source>>()
for (descriptor in assets) {
val existing = uniqueFiles.putIfAbsent(descriptor.file, descriptor.sources)
check(existing == null) {
"File ${descriptor.file} is already associated." +
"\nPrevious:\n ${existing!!.joinToString(separator = "\n ")}" +
"\nCurrent:\n ${descriptor.sources.joinToString(separator = "\n ")}"
}
}
checkAssetUniqueness(assets)
if (dryRun) {
return emptyMap()
}
//val optimizeLibraryContext = OptimizeLibraryContext(tempDir = context.paths.tempDir, javaHome = context.getStableJdkHome())
val list = withContext(Dispatchers.IO) {
assets.map { asset ->
async {
@@ -929,6 +920,18 @@ private suspend fun buildJars(
return result
}
private fun checkAssetUniqueness(assets: Collection<AssetDescriptor>) {
val uniqueFiles = HashMap<Path, List<Source>>(assets.size)
for (asset in assets) {
val existing = uniqueFiles.putIfAbsent(asset.file, asset.sources)
check(existing == null) {
"File ${asset.file} is already associated." +
"\nPrevious:\n ${existing!!.joinToString(separator = "\n ")}" +
"\nCurrent:\n ${asset.sources.joinToString(separator = "\n ")}"
}
}
}
private class NativeFileHandlerImpl(private val context: BuildContext, private val descriptor: AssetDescriptor) : NativeFileHandler {
override val sourceToNativeFiles = HashMap<ZipSource, List<String>>()
@@ -150,15 +150,19 @@ suspend fun buildJar(targetFile: Path, sources: List<Source>, compress: Boolean
buildJar(targetFile = targetFile, sources = sources, compress = compress, nativeFileHandler = null)
}
internal suspend fun buildJar(targetFile: Path,
sources: List<Source>,
compress: Boolean = false,
notify: Boolean = true,
nativeFileHandler: NativeFileHandler? = null) {
internal suspend fun buildJar(
targetFile: Path,
sources: List<Source>,
compress: Boolean = false,
notify: Boolean = true,
nativeFileHandler: NativeFileHandler? = null,
) {
val packageIndexBuilder = if (compress) null else PackageIndexBuilder()
writeNewFile(targetFile) { outChannel ->
ZipFileWriter(channel = outChannel,
deflater = if (compress) Deflater(Deflater.DEFAULT_COMPRESSION, true) else null).use { zipCreator ->
ZipFileWriter(
channel = outChannel,
deflater = if (compress) Deflater(Deflater.DEFAULT_COMPRESSION, true) else null,
).use { zipCreator ->
val uniqueNames = HashMap<String, Path>()
for (source in sources) {
@@ -181,8 +185,10 @@ internal suspend fun buildJar(targetFile: Path,
is InMemoryContentSource -> {
if (uniqueNames.putIfAbsent(source.relativePath, Path.of(source.relativePath)) != null) {
throw IllegalStateException("in-memory source must always be first " +
"(targetFile=$targetFile, source=${source.relativePath}, sources=${sources.joinToString()})")
throw IllegalStateException(
"in-memory source must always be first " +
"(targetFile=$targetFile, source=${source.relativePath}, sources=${sources.joinToString()})"
)
}
packageIndexBuilder?.addFile(source.relativePath)
@@ -194,34 +200,16 @@ internal suspend fun buildJar(targetFile: Path,
is ZipSource -> {
val sourceFile = source.file
try {
//if (source.optimizeConfigId != null) {
// TraceManager.spanBuilder("optimize").setAttribute("library", source.optimizeConfigId).useWithoutActiveScope {
// val tempDir = optimizeLibraryContext!!.tempDir
// val suffix = System.nanoTime().toString(Character.MAX_RADIX)
// sourceFile = tempDir.resolve("${source.optimizeConfigId}-$suffix.jar")
// val mappingFile = tempDir.resolve("${source.optimizeConfigId}-${System.nanoTime().toString(Character.MAX_RADIX)}.jar")
// try {
// optimizeLibrary(name = source.optimizeConfigId,
// input = source.file,
// output = sourceFile,
// javaHome = optimizeLibraryContext.javaHome.toString(),
// mapping = mappingFile)
// zipCreator.file("${source.optimizeConfigId}.map.txt", mappingFile)
// }
// finally {
// Files.deleteIfExists(mappingFile)
// }
// }
//}
handleZipSource(source = source,
sourceFile = sourceFile,
nativeFileHandler = nativeFileHandler,
uniqueNames = uniqueNames,
sources = sources,
packageIndexBuilder = packageIndexBuilder,
zipCreator = zipCreator,
compress = compress)
handleZipSource(
source = source,
sourceFile = sourceFile,
nativeFileHandler = nativeFileHandler,
uniqueNames = uniqueNames,
sources = sources,
packageIndexBuilder = packageIndexBuilder,
zipCreator = zipCreator,
compress = compress
)
}
finally {
@Suppress("KotlinConstantConditions")
@@ -674,18 +674,22 @@ private fun loadCoreProductPlugin(
override fun toString() = "product classpath"
}
val raw = readModuleDescriptor(reader,
readContext = context,
pathResolver = pathResolver,
dataLoader = dataLoader,
includeBase = null,
readInto = null)
val descriptor = IdeaPluginDescriptorImpl(raw = raw,
path = Paths.get(PathManager.getLibPath()),
isBundled = true,
id = null,
moduleName = null,
useCoreClassLoader = useCoreClassLoader)
val raw = readModuleDescriptor(
reader,
readContext = context,
pathResolver = pathResolver,
dataLoader = dataLoader,
includeBase = null,
readInto = null,
)
val descriptor = IdeaPluginDescriptorImpl(
raw = raw,
path = Paths.get(PathManager.getLibPath()),
isBundled = true,
id = null,
moduleName = null,
useCoreClassLoader = useCoreClassLoader,
)
context.debugData?.recordDescriptorPath(descriptor, raw, path)
descriptor.readExternal(raw = raw, pathResolver = pathResolver, context = context, isSub = false, dataLoader = dataLoader)
return descriptor