diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/Main.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/Main.kt index 81b4762dd2e4..ed403bf1e731 100644 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/Main.kt +++ b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/Main.kt @@ -3,8 +3,9 @@ package org.jetbrains.tools.model.updater import org.jdom.Document import org.jetbrains.tools.model.updater.impl.* -import java.io.File +import java.nio.file.Path import java.util.* +import kotlin.io.path.* class GeneratorPreferences(properties: Properties) : Preferences(properties) { val jpsPluginVersion: String by Preference() @@ -55,14 +56,19 @@ class GeneratorPreferences(properties: Properties) : Preferences(properties) { fun main(args: Array) { copyBootstrapArtifactsToMavenRepositoryIfExists() - + val preferences = GeneratorPreferences.parse(args) - val communityRoot = generateSequence(File(".").canonicalFile) { it.parentFile }.first { - it.resolve(".idea").isDirectory && !it.resolve("community").isDirectory && it.name != "project-model-updater" - }.normalize() + val communityRoot = generateSequence(Path(".").toRealPath()) { it.parent }.first { + it.resolve(".idea").isDirectory() && + !it.resolve("community").isDirectory() && + // This file name check is needed since the project model updater might be opened as a standalone project + it.fileName.toString() != "project-model-updater" + } - val monorepoRoot = communityRoot.resolve("..").takeIf { it.resolve(".idea").isDirectory }?.normalize() + val monorepoRoot = communityRoot.parent.takeIf { + it.resolve(".idea").isDirectory() + } val resolverSettings = readJpsResolverSettings(communityRoot, monorepoRoot) @@ -75,7 +81,7 @@ fun main(args: Array) { KotlinTestsDependenciesUtil.updateChecksum(isUpToDateCheck = false) - fun processRoot(root: File, isCommunity: Boolean) { + fun processRoot(root: Path, isCommunity: Boolean) { println("Processing kotlinc libraries in root: $root") val libraries = generateKotlincLibraries(preferences, isCommunity) regenerateProjectLibraries(root.resolve(".idea"), libraries, resolverSettings) @@ -92,11 +98,11 @@ fun main(args: Array) { updateCoopRunConfiguration(monorepoRoot, communityRoot) } -private fun regenerateProjectLibraries(dotIdea: File, libraries: List, resolverSettings: JpsResolverSettings) { +private fun regenerateProjectLibraries(dotIdea: Path, libraries: List, resolverSettings: JpsResolverSettings) { val librariesDir = dotIdea.resolve("libraries") - librariesDir.listFiles { file -> file.name.startsWith("kotlinc_") }!!.forEach { + for (it in librariesDir.listDirectoryEntries("kotlinc_*")) { println("Removing $it") - it.delete() + it.deleteIfExists() } for (library in libraries) { @@ -107,7 +113,7 @@ private fun regenerateProjectLibraries(dotIdea: File, libraries: List { +private fun readModules(root: Path, document: Document): Map { val projectModuleManagerComponent = document.rootElement.getChildren("component") .first { it.getAttributeValue("name") == "ProjectModuleManager" } val result = LinkedHashMap() for (moduleEntry in projectModuleManagerComponent.getChild("modules").getChildren("module")) { - val modulePath = moduleEntry.getAttributeValue("filepath").removePrefix("\$PROJECT_DIR$/") + val modulePath = moduleEntry.getAttributeValue("filepath").removePrefix($$"$PROJECT_DIR$/") val moduleName = modulePath.substringAfterLast("/").removeSuffix(".iml") val moduleXml = root.resolve(modulePath).readXml() diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/XmlNode.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/XmlNode.kt index 1e4b71332476..71c050856f8d 100644 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/XmlNode.kt +++ b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/XmlNode.kt @@ -1,10 +1,11 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.tools.model.updater.impl import org.jdom.Document import org.jdom.Element import org.jdom.output.Format -import java.io.File +import java.nio.file.Path +import kotlin.io.path.inputStream fun xml(name: String, vararg args: Pair, block: XmlNode.() -> Unit = {}): XmlNode { return XmlNode(name, args.asList(), block) @@ -62,7 +63,7 @@ class XmlNode(private val name: String, private val args: List } } -fun File.readXml(): Document { +fun Path.readXml(): Document = inputStream().use { input -> @Suppress("DEPRECATION") - return org.jdom.input.SAXBuilder().build(this) -} \ No newline at end of file + org.jdom.input.SAXBuilder().build(input) +} diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsResolver.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsResolver.kt index cbe1ad674c1e..4bb51d0256af 100644 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsResolver.kt +++ b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/impl/jpsResolver.kt @@ -1,7 +1,8 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.tools.model.updater.impl -import java.io.File +import java.nio.file.Path +import kotlin.io.path.isRegularFile data class JpsRemoteRepository(val id: String, val url: String) { init { @@ -32,12 +33,12 @@ data class JpsResolverSettings(val sha256ChecksumsEnabled: Boolean, val bindRepo } } -fun readJpsResolverSettings(communityRoot: File, monorepoRoot: File?): JpsResolverSettings { +fun readJpsResolverSettings(communityRoot: Path, monorepoRoot: Path?): JpsResolverSettings { val isUnderTeamcity = System.getenv("TEAMCITY_VERSION") != null println("State of JpsResolverSettings.useMavenResolver == ${JpsResolverSettings.useMavenResolver}") - // Checksums and bind repository must be set locally and committed by developer: + // Checksums and bind repository must be set locally and committed by a developer: // don't update them automatically on teamcity. if ( isUnderTeamcity && @@ -50,7 +51,7 @@ fun readJpsResolverSettings(communityRoot: File, monorepoRoot: File?): JpsResolv val effectiveRoot = monorepoRoot ?: communityRoot val resolverSettingsFile = effectiveRoot.resolve(".idea").resolve("dependencyResolver.xml") - if (!resolverSettingsFile.isFile) return JpsResolverSettings(false, false) + if (!resolverSettingsFile.isRegularFile()) return JpsResolverSettings(false, false) val settingsOptions = resolverSettingsFile.readXml().rootElement.getChildren("component") .single { it.getAttributeValue("name") == "MavenDependencyResolverConfiguration" } diff --git a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/kotlinBootstrapArtifacts.kt b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/kotlinBootstrapArtifacts.kt index 52f7898f6b5d..1c79c9ec3121 100644 --- a/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/kotlinBootstrapArtifacts.kt +++ b/plugins/kotlin/util/project-model-updater/src/org/jetbrains/tools/model/updater/kotlinBootstrapArtifacts.kt @@ -1,7 +1,10 @@ // Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.tools.model.updater -import java.io.File +import kotlin.io.path.ExperimentalPathApi +import kotlin.io.path.Path +import kotlin.io.path.copyToRecursively +import kotlin.io.path.isDirectory /** * We have to do the manual copying of Kotlin bootstrap artifacts from the @@ -22,18 +25,23 @@ internal fun copyBootstrapArtifactsToMavenRepositoryIfExists() { println("Custom Kotlin bootstrap location: $bootstrapLocation; trying to copy artifacts to Maven repository") - val bootstrapDir = File(bootstrapLocation).absoluteFile + val bootstrapDir = Path(bootstrapLocation).toAbsolutePath().normalize() - if (!bootstrapDir.isDirectory) { - println("WARN: Bootstrap location does not exist or is not a directory, skipping copy: ${bootstrapDir}") + if (!bootstrapDir.isDirectory()) { + System.err.println("Bootstrap location does not exist or is not a directory, skipping copy: ${bootstrapDir}") return } - - val mavenRepository = File(System.getProperty("user.home")).resolve(".m2").resolve("repository").absoluteFile + + val mavenRepository = Path(System.getProperty("user.home")) + .resolve(".m2") + .resolve("repository") + .toAbsolutePath() + .normalize() println("Copying bootstrap artifacts from $bootstrapLocation to $mavenRepository") - bootstrapDir.copyRecursively(mavenRepository, overwrite = true) + @OptIn(ExperimentalPathApi::class) + bootstrapDir.copyToRecursively(mavenRepository, followLinks = false, overwrite = true) println("Kotlin bootstrap artifacts copy completed") } \ No newline at end of file