convert IdeaProjectLoaderUtil to kotlin

GitOrigin-RevId: 6cadbb9680e3aa4c62798cf9da348b4a9bdbaf4c
This commit is contained in:
Leonid Shalupov
2025-05-07 20:36:33 +02:00
committed by intellij-monorepo-bot
parent d25bbd1184
commit eeff0201ce
2 changed files with 60 additions and 66 deletions

View File

@@ -38,14 +38,12 @@ class BuildPaths(
companion object {
@JvmStatic
val ULTIMATE_HOME: Path by lazy {
IdeaProjectLoaderUtil.guessUltimateHome(this::class.java) ?: error(
"Could not detect ultimate home folder from class: ${BuildPaths::class.java.name}")
IdeaProjectLoaderUtil.guessUltimateHome(this::class.java)
}
@JvmStatic
val COMMUNITY_ROOT: BuildDependenciesCommunityRoot by lazy {
IdeaProjectLoaderUtil.guessCommunityHome(this::class.java) ?: error(
"Could not detect community home folder from class: ${BuildPaths::class.java.name}")
IdeaProjectLoaderUtil.guessCommunityHome(this::class.java)
}
}

View File

@@ -1,110 +1,106 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.intellij.build;
package org.jetbrains.intellij.build
import com.intellij.util.lang.UrlClassLoader;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.intellij.build.dependencies.BuildDependenciesCommunityRoot;
import com.intellij.util.lang.UrlClassLoader
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.intellij.build.dependencies.BuildDependenciesCommunityRoot
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public final class IdeaProjectLoaderUtil {
private static final String JPS_BOOTSTRAP_COMMUNITY_HOME_ENV_NAME = "JPS_BOOTSTRAP_COMMUNITY_HOME";
private static final String ULTIMATE_REPO_MARKER_FILE = ".ultimate.root.marker";
private static final String COMMUNITY_REPO_MARKER_FILE = "intellij.idea.community.main.iml";
private static final String INTELLIJ_BUILD_COMMUNITY_HOME_PATH = "intellij.build.community.home.path";
private static final String INTELLIJ_BUILD_ULTIMATE_HOME_PATH = "intellij.build.ultimate.home.path";
object IdeaProjectLoaderUtil {
private const val JPS_BOOTSTRAP_COMMUNITY_HOME_ENV_NAME = "JPS_BOOTSTRAP_COMMUNITY_HOME"
private const val ULTIMATE_REPO_MARKER_FILE = ".ultimate.root.marker"
private const val COMMUNITY_REPO_MARKER_FILE = "intellij.idea.community.main.iml"
private const val INTELLIJ_BUILD_COMMUNITY_HOME_PATH = "intellij.build.community.home.path"
private const val INTELLIJ_BUILD_ULTIMATE_HOME_PATH = "intellij.build.ultimate.home.path"
/**
* This method only for internal usage. Instead of use: org.jetbrains.intellij.build.BuildPaths.Companion#getULTIMATE_HOME().
* @param klass must be a class inside idea home directory, the one with <code>file</code> protocol. Jar files in maven directory aren't accepted.
* This method only for internal usage. Use [BuildPaths.ULTIMATE_HOME] instead.
* @param klass must be a class inside idea home directory, the one with `file` protocol. Jar files in the maven directory aren't accepted.
*/
@ApiStatus.Internal
public static Path guessUltimateHome(Class<?> klass) {
String ultimateHomePathOverride = System.getProperty(INTELLIJ_BUILD_ULTIMATE_HOME_PATH);
fun guessUltimateHome(klass: Class<*>): Path {
val ultimateHomePathOverride = System.getProperty(INTELLIJ_BUILD_ULTIMATE_HOME_PATH)
if (ultimateHomePathOverride != null) {
Path path = Paths.get(ultimateHomePathOverride);
if (!path.toFile().exists()) {
throw new IllegalArgumentException("Ultimate home path: '" + path
+ "' passed via system property: '" + INTELLIJ_BUILD_ULTIMATE_HOME_PATH + " not exists");
val path = Paths.get(ultimateHomePathOverride)
require(path.toFile().exists()) {
("Ultimate home path: '" + path
+ "' passed via system property: '" + INTELLIJ_BUILD_ULTIMATE_HOME_PATH + " not exists")
}
return path;
return path
}
final Path start = getSomeRoot(klass);
Path home = start;
val start = getSomeRoot(klass)
var home: Path? = start
while (home != null) {
if (Files.exists(home.resolve(ULTIMATE_REPO_MARKER_FILE))) {
return home;
return home
}
home = home.getParent();
home = home.parent
}
throw new IllegalArgumentException("Cannot guess ultimate project home from root '" + start + "'" +
", marker file '" + ULTIMATE_REPO_MARKER_FILE + "'");
throw IllegalArgumentException("Cannot guess ultimate project home from root '" + start + "'" +
", marker file '" + ULTIMATE_REPO_MARKER_FILE + "'")
}
/**
* This method only for internal usage. Instead of use: org.jetbrains.intellij.build.BuildPaths.Companion#getCOMMUNITY_ROOT().
* @param klass must be a class inside idea home directory, the one with <code>file</code> protocol. Jar files in maven directory aren't accepted.
* This method only for internal usage. Use [BuildPaths.COMMUNITY_ROOT] instead.
* @param klass must be a class inside idea home directory, the one with `file` protocol. Jar files in the maven directory aren't accepted.
*/
@ApiStatus.Internal
public static BuildDependenciesCommunityRoot guessCommunityHome(Class<?> klass) {
String communityHomePathOverride = System.getProperty(INTELLIJ_BUILD_COMMUNITY_HOME_PATH);
fun guessCommunityHome(klass: Class<*>): BuildDependenciesCommunityRoot {
val communityHomePathOverride = System.getProperty(INTELLIJ_BUILD_COMMUNITY_HOME_PATH)
if (communityHomePathOverride != null) {
Path path = Paths.get(communityHomePathOverride);
if (!path.toFile().exists()) {
throw new IllegalArgumentException("Community home path: '" + path
+ "' passed via system property: '" + INTELLIJ_BUILD_COMMUNITY_HOME_PATH + " not exists");
val path = Paths.get(communityHomePathOverride)
require(path.toFile().exists()) {
("Community home path: '" + path
+ "' passed via system property: '" + INTELLIJ_BUILD_COMMUNITY_HOME_PATH + " not exists")
}
return new BuildDependenciesCommunityRoot(path);
return BuildDependenciesCommunityRoot(path)
}
final Path start = getSomeRoot(klass);
Path home = start;
val start = getSomeRoot(klass)
var home: Path? = start
while (home != null) {
if (Files.exists(home.resolve(COMMUNITY_REPO_MARKER_FILE))) {
return new BuildDependenciesCommunityRoot(home);
return BuildDependenciesCommunityRoot(home)
}
if (Files.exists(home.resolve("community").resolve(COMMUNITY_REPO_MARKER_FILE))) {
return new BuildDependenciesCommunityRoot(home.resolve("community"));
return BuildDependenciesCommunityRoot(home.resolve("community"))
}
home = home.getParent();
home = home.parent
}
throw new IllegalArgumentException("Cannot guess community project home from root '" + start + "'" +
", marker file '" + COMMUNITY_REPO_MARKER_FILE + "'");
throw IllegalArgumentException("Cannot guess community project home from root '" + start + "'" +
", marker file '" + COMMUNITY_REPO_MARKER_FILE + "'")
}
private static Path getSomeRoot(Class<?> klass) {
private fun getSomeRoot(klass: Class<*>): Path {
// Under jps-bootstrap home is already known, reuse it
String communityHome = System.getenv(JPS_BOOTSTRAP_COMMUNITY_HOME_ENV_NAME);
val communityHome = System.getenv(JPS_BOOTSTRAP_COMMUNITY_HOME_ENV_NAME)
if (communityHome != null) {
return Path.of(communityHome).normalize();
return Path.of(communityHome).normalize()
}
var path = getPathFromClass(klass);
val path = getPathFromClass(klass)
if (!path.toString().endsWith("class")) {
String ideaHomePath = System.getProperty("idea.home.path");
val ideaHomePath = System.getProperty("idea.home.path")
if (ideaHomePath != null) {
return Path.of(ideaHomePath);
return Path.of(ideaHomePath)
}
throw new IllegalArgumentException(
throw IllegalArgumentException(
String.format("To guess idea home, you must provide class that resides in .class file inside of idea home dir. " +
"But provided %s resides in %s", klass, path));
"But provided %s resides in %s", klass, path))
}
return path;
return path
}
private static Path getPathFromClass(Class<?> klass) {
String klassFileName = klass.getName().replace(klass.getPackageName() + ".", "");
final URL classFileURL = klass.getResource(klassFileName + ".class");
if (classFileURL == null) {
throw new IllegalStateException("Could not get .class file location from class " + klass.getName());
}
return Path.of(UrlClassLoader.urlToFilePath(classFileURL.getPath()));
private fun getPathFromClass(klass: Class<*>): Path {
val klassFileName = klass.getName().replace(klass.getPackageName() + ".", "")
val classFileURL = klass.getResource("$klassFileName.class")
checkNotNull(classFileURL) { "Could not get .class file location from class " + klass.getName() }
return Path.of(UrlClassLoader.urlToFilePath(classFileURL.path))
}
}