|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -package org.jetbrains.kotlin.utils; |
18 | | - |
19 | | -import com.intellij.openapi.application.ApplicationManager; |
20 | | -import com.intellij.openapi.application.PathManager; |
21 | | -import org.jetbrains.annotations.NotNull; |
22 | | -import org.jetbrains.jps.model.java.impl.JavaSdkUtil; |
23 | | - |
24 | | -import java.io.File; |
25 | | -import java.util.List; |
26 | | -import java.util.regex.Pattern; |
27 | | - |
28 | | -public class PathUtil { |
29 | | - |
30 | | - public static final String JPS_KOTLIN_HOME_PROPERTY = "jps.kotlin.home"; |
31 | | - |
32 | | - public static final String JS_LIB_JAR_NAME = "kotlin-stdlib-js.jar"; |
33 | | - public static final String JS_LIB_10_JAR_NAME = "kotlin-jslib.jar"; |
34 | | - public static final String ALLOPEN_PLUGIN_JAR_NAME = "allopen-compiler-plugin.jar"; |
35 | | - public static final String NOARG_PLUGIN_JAR_NAME = "noarg-compiler-plugin.jar"; |
36 | | - public static final String SAM_WITH_RECEIVER_PLUGIN_JAR_NAME = "sam-with-receiver-compiler-plugin.jar"; |
37 | | - public static final String JS_LIB_SRC_JAR_NAME = "kotlin-stdlib-js-sources.jar"; |
38 | | - public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar"; |
39 | | - public static final String KOTLIN_JAVA_RUNTIME_JRE7_JAR = "kotlin-stdlib-jre7.jar"; |
40 | | - public static final String KOTLIN_JAVA_RUNTIME_JRE8_JAR = "kotlin-stdlib-jre8.jar"; |
41 | | - public static final String KOTLIN_JAVA_RUNTIME_JRE7_SRC_JAR = "kotlin-stdlib-jre7-sources.jar"; |
42 | | - public static final String KOTLIN_JAVA_RUNTIME_JRE8_SRC_JAR = "kotlin-stdlib-jre8-sources.jar"; |
43 | | - public static final String KOTLIN_JAVA_STDLIB_JAR = "kotlin-stdlib.jar"; |
44 | | - public static final String KOTLIN_JAVA_REFLECT_JAR = "kotlin-reflect.jar"; |
45 | | - public static final String KOTLIN_JAVA_SCRIPT_RUNTIME_JAR = "kotlin-script-runtime.jar"; |
46 | | - public static final String KOTLIN_TEST_JAR = "kotlin-test.jar"; |
47 | | - public static final String KOTLIN_TEST_JS_JAR = "kotlin-test-js.jar"; |
48 | | - public static final String KOTLIN_JAVA_STDLIB_SRC_JAR = "kotlin-stdlib-sources.jar"; |
49 | | - public static final String KOTLIN_JAVA_STDLIB_SRC_JAR_OLD = "kotlin-runtime-sources.jar"; |
50 | | - public static final String KOTLIN_REFLECT_SRC_JAR = "kotlin-reflect-sources.jar"; |
51 | | - public static final String KOTLIN_TEST_SRC_JAR = "kotlin-test-sources.jar"; |
52 | | - public static final String KOTLIN_COMPILER_JAR = "kotlin-compiler.jar"; |
53 | | - |
54 | | - public static final Pattern KOTLIN_RUNTIME_JAR_PATTERN = Pattern.compile("kotlin-(stdlib|runtime)(-\\d[\\d.]+(-.+)?)?\\.jar"); |
55 | | - public static final Pattern KOTLIN_STDLIB_JS_JAR_PATTERN = Pattern.compile("kotlin-stdlib-js.*\\.jar"); |
56 | | - public static final Pattern KOTLIN_JS_LIBRARY_JAR_PATTERN = Pattern.compile("kotlin-js-library.*\\.jar"); |
57 | | - |
58 | | - public static final String HOME_FOLDER_NAME = "kotlinc"; |
59 | | - private static final File NO_PATH = new File("<no_path>"); |
60 | | - |
61 | | - private PathUtil() {} |
62 | | - |
63 | | - @NotNull |
64 | | - public static KotlinPaths getKotlinPathsForIdeaPlugin() { |
65 | | - return ApplicationManager.getApplication().isUnitTestMode() |
66 | | - ? getKotlinPathsForDistDirectory() |
67 | | - : new KotlinPathsFromHomeDir(getCompilerPathForIdeaPlugin()); |
68 | | - } |
69 | | - |
70 | | - @NotNull |
71 | | - public static KotlinPaths getKotlinPathsForJpsPlugin() { |
72 | | - // When JPS is run on TeamCity, it can not rely on Kotlin plugin layout, |
73 | | - // so the path to Kotlin is specified in a system property |
74 | | - String jpsKotlinHome = System.getProperty(JPS_KOTLIN_HOME_PROPERTY); |
75 | | - if (jpsKotlinHome != null) { |
76 | | - return new KotlinPathsFromHomeDir(new File(jpsKotlinHome)); |
| 17 | +package org.jetbrains.kotlin.utils |
| 18 | + |
| 19 | +import com.intellij.openapi.application.ApplicationManager |
| 20 | +import com.intellij.openapi.application.PathManager |
| 21 | +import org.jetbrains.jps.model.java.impl.JavaSdkUtil |
| 22 | + |
| 23 | +import java.io.File |
| 24 | +import java.util.regex.Pattern |
| 25 | + |
| 26 | +object PathUtil { |
| 27 | + const val JPS_KOTLIN_HOME_PROPERTY = "jps.kotlin.home" |
| 28 | + |
| 29 | + const val JS_LIB_JAR_NAME = "kotlin-stdlib-js.jar" |
| 30 | + const val JS_LIB_10_JAR_NAME = "kotlin-jslib.jar" |
| 31 | + const val ALLOPEN_PLUGIN_JAR_NAME = "allopen-compiler-plugin.jar" |
| 32 | + const val NOARG_PLUGIN_JAR_NAME = "noarg-compiler-plugin.jar" |
| 33 | + const val SAM_WITH_RECEIVER_PLUGIN_JAR_NAME = "sam-with-receiver-compiler-plugin.jar" |
| 34 | + const val JS_LIB_SRC_JAR_NAME = "kotlin-stdlib-js-sources.jar" |
| 35 | + const val KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar" |
| 36 | + const val KOTLIN_JAVA_RUNTIME_JRE7_JAR = "kotlin-stdlib-jre7.jar" |
| 37 | + const val KOTLIN_JAVA_RUNTIME_JRE8_JAR = "kotlin-stdlib-jre8.jar" |
| 38 | + const val KOTLIN_JAVA_RUNTIME_JRE7_SRC_JAR = "kotlin-stdlib-jre7-sources.jar" |
| 39 | + const val KOTLIN_JAVA_RUNTIME_JRE8_SRC_JAR = "kotlin-stdlib-jre8-sources.jar" |
| 40 | + const val KOTLIN_JAVA_STDLIB_JAR = "kotlin-stdlib.jar" |
| 41 | + const val KOTLIN_JAVA_REFLECT_JAR = "kotlin-reflect.jar" |
| 42 | + const val KOTLIN_JAVA_SCRIPT_RUNTIME_JAR = "kotlin-script-runtime.jar" |
| 43 | + const val KOTLIN_TEST_JAR = "kotlin-test.jar" |
| 44 | + const val KOTLIN_TEST_JS_JAR = "kotlin-test-js.jar" |
| 45 | + const val KOTLIN_JAVA_STDLIB_SRC_JAR = "kotlin-stdlib-sources.jar" |
| 46 | + const val KOTLIN_JAVA_STDLIB_SRC_JAR_OLD = "kotlin-runtime-sources.jar" |
| 47 | + const val KOTLIN_REFLECT_SRC_JAR = "kotlin-reflect-sources.jar" |
| 48 | + const val KOTLIN_TEST_SRC_JAR = "kotlin-test-sources.jar" |
| 49 | + const val KOTLIN_COMPILER_JAR = "kotlin-compiler.jar" |
| 50 | + |
| 51 | + @JvmField |
| 52 | + val KOTLIN_RUNTIME_JAR_PATTERN: Pattern = Pattern.compile("kotlin-(stdlib|runtime)(-\\d[\\d.]+(-.+)?)?\\.jar") |
| 53 | + val KOTLIN_STDLIB_JS_JAR_PATTERN: Pattern = Pattern.compile("kotlin-stdlib-js.*\\.jar") |
| 54 | + val KOTLIN_JS_LIBRARY_JAR_PATTERN: Pattern = Pattern.compile("kotlin-js-library.*\\.jar") |
| 55 | + |
| 56 | + private const val HOME_FOLDER_NAME = "kotlinc" |
| 57 | + private val NO_PATH = File("<no_path>") |
| 58 | + |
| 59 | + @JvmStatic |
| 60 | + val kotlinPathsForIdeaPlugin: KotlinPaths |
| 61 | + get() = if (ApplicationManager.getApplication().isUnitTestMode) |
| 62 | + kotlinPathsForDistDirectory |
| 63 | + else |
| 64 | + KotlinPathsFromHomeDir(compilerPathForIdeaPlugin) |
| 65 | + |
| 66 | + // When JPS is run on TeamCity, it can not rely on Kotlin plugin layout, |
| 67 | + // so the path to Kotlin is specified in a system property |
| 68 | + private val kotlinPathsForJpsPlugin: KotlinPaths |
| 69 | + get() { |
| 70 | + val jpsKotlinHome = System.getProperty(JPS_KOTLIN_HOME_PROPERTY) |
| 71 | + return if (jpsKotlinHome != null) { |
| 72 | + KotlinPathsFromHomeDir(File(jpsKotlinHome)) |
| 73 | + } |
| 74 | + else KotlinPathsFromHomeDir(compilerPathForJpsPlugin) |
77 | 75 | } |
78 | | - return new KotlinPathsFromHomeDir(getCompilerPathForJpsPlugin()); |
79 | | - } |
80 | 76 |
|
81 | | - @NotNull |
82 | | - public static KotlinPaths getKotlinPathsForJpsPluginOrJpsTests() { |
83 | | - if ("true".equalsIgnoreCase(System.getProperty("kotlin.jps.tests"))) { |
84 | | - return getKotlinPathsForDistDirectory(); |
| 77 | + val kotlinPathsForJpsPluginOrJpsTests: KotlinPaths |
| 78 | + get() = if ("true".equals(System.getProperty("kotlin.jps.tests"), ignoreCase = true)) { |
| 79 | + kotlinPathsForDistDirectory |
85 | 80 | } |
86 | | - return getKotlinPathsForJpsPlugin(); |
87 | | - } |
| 81 | + else kotlinPathsForJpsPlugin |
88 | 82 |
|
89 | | - @NotNull |
90 | | - public static KotlinPaths getKotlinPathsForCompiler() { |
91 | | - if (!getPathUtilJar().isFile()) { |
| 83 | + @JvmStatic |
| 84 | + val kotlinPathsForCompiler: KotlinPaths |
| 85 | + get() = if (!pathUtilJar.isFile) { |
92 | 86 | // Not running from a jar, i.e. it is it must be a unit test |
93 | | - return getKotlinPathsForDistDirectory(); |
| 87 | + kotlinPathsForDistDirectory |
94 | 88 | } |
95 | | - return new KotlinPathsFromHomeDir(getCompilerPathForCompilerJar()); |
96 | | - } |
| 89 | + else KotlinPathsFromHomeDir(compilerPathForCompilerJar) |
97 | 90 |
|
98 | | - @NotNull |
99 | | - public static KotlinPaths getKotlinPathsForDistDirectory() { |
100 | | - return new KotlinPathsFromHomeDir(new File("dist", HOME_FOLDER_NAME)); |
101 | | - } |
| 91 | + @JvmStatic |
| 92 | + val kotlinPathsForDistDirectory: KotlinPaths |
| 93 | + get() = KotlinPathsFromHomeDir(File("dist", HOME_FOLDER_NAME)) |
102 | 94 |
|
103 | | - @NotNull |
104 | | - private static File getCompilerPathForCompilerJar() { |
105 | | - File jar = getPathUtilJar(); |
| 95 | + private val compilerPathForCompilerJar: File |
| 96 | + get() { |
| 97 | + val jar = pathUtilJar |
| 98 | + if (!jar.exists()) return NO_PATH |
106 | 99 |
|
107 | | - if (!jar.exists()) return NO_PATH; |
| 100 | + if (jar.name == KOTLIN_COMPILER_JAR) { |
| 101 | + val lib = jar.parentFile |
| 102 | + return lib.parentFile |
| 103 | + } |
108 | 104 |
|
109 | | - if (jar.getName().equals(KOTLIN_COMPILER_JAR)) { |
110 | | - File lib = jar.getParentFile(); |
111 | | - return lib.getParentFile(); |
| 105 | + return NO_PATH |
112 | 106 | } |
113 | 107 |
|
114 | | - return NO_PATH; |
115 | | - } |
116 | | - |
117 | | - @NotNull |
118 | | - private static File getCompilerPathForJpsPlugin() { |
119 | | - File jar = getPathUtilJar(); |
| 108 | + private val compilerPathForJpsPlugin: File |
| 109 | + get() { |
| 110 | + val jar = pathUtilJar |
| 111 | + if (!jar.exists()) return NO_PATH |
120 | 112 |
|
121 | | - if (!jar.exists()) return NO_PATH; |
| 113 | + if (jar.name == "kotlin-jps-plugin.jar") { |
| 114 | + val pluginHome = jar.parentFile.parentFile.parentFile |
| 115 | + return File(pluginHome, HOME_FOLDER_NAME) |
| 116 | + } |
122 | 117 |
|
123 | | - if (jar.getName().equals("kotlin-jps-plugin.jar")) { |
124 | | - File pluginHome = jar.getParentFile().getParentFile().getParentFile(); |
125 | | - return new File(pluginHome, HOME_FOLDER_NAME); |
| 118 | + return NO_PATH |
126 | 119 | } |
127 | 120 |
|
128 | | - return NO_PATH; |
129 | | - } |
| 121 | + private val compilerPathForIdeaPlugin: File |
| 122 | + get() { |
| 123 | + val jar = pathUtilJar |
| 124 | + if (!jar.exists()) return NO_PATH |
130 | 125 |
|
131 | | - @NotNull |
132 | | - private static File getCompilerPathForIdeaPlugin() { |
133 | | - File jar = getPathUtilJar(); |
| 126 | + if (jar.name == "kotlin-plugin.jar") { |
| 127 | + val lib = jar.parentFile |
| 128 | + val pluginHome = lib.parentFile |
134 | 129 |
|
135 | | - if (!jar.exists()) return NO_PATH; |
| 130 | + return File(pluginHome, HOME_FOLDER_NAME) |
| 131 | + } |
136 | 132 |
|
137 | | - if (jar.getName().equals("kotlin-plugin.jar")) { |
138 | | - File lib = jar.getParentFile(); |
139 | | - File pluginHome = lib.getParentFile(); |
140 | | - |
141 | | - return new File(pluginHome, HOME_FOLDER_NAME); |
| 133 | + return NO_PATH |
142 | 134 | } |
143 | 135 |
|
144 | | - return NO_PATH; |
145 | | - } |
146 | | - |
147 | | - @NotNull |
148 | | - public static File getPathUtilJar() { |
149 | | - return getResourcePathForClass(PathUtil.class); |
150 | | - } |
| 136 | + val pathUtilJar: File |
| 137 | + get() = getResourcePathForClass(PathUtil::class.java) |
151 | 138 |
|
152 | | - @NotNull |
153 | | - public static File getResourcePathForClass(@NotNull Class aClass) { |
154 | | - String path = "/" + aClass.getName().replace('.', '/') + ".class"; |
155 | | - String resourceRoot = PathManager.getResourceRoot(aClass, path); |
156 | | - if (resourceRoot == null) { |
157 | | - throw new IllegalStateException("Resource not found: " + path); |
158 | | - } |
159 | | - return new File(resourceRoot).getAbsoluteFile(); |
| 139 | + @JvmStatic |
| 140 | + fun getResourcePathForClass(aClass: Class<*>): File { |
| 141 | + val path = "/" + aClass.name.replace('.', '/') + ".class" |
| 142 | + val resourceRoot = PathManager.getResourceRoot(aClass, path) ?: throw IllegalStateException("Resource not found: $path") |
| 143 | + return File(resourceRoot).absoluteFile |
160 | 144 | } |
161 | 145 |
|
162 | | - @NotNull |
163 | | - public static List<File> getJdkClassesRootsFromCurrentJre() { |
164 | | - return getJdkClassesRootsFromJre(System.getProperty("java.home")); |
165 | | - } |
| 146 | + @JvmStatic |
| 147 | + fun getJdkClassesRootsFromCurrentJre(): List<File> = |
| 148 | + getJdkClassesRootsFromJre(System.getProperty("java.home")) |
166 | 149 |
|
167 | | - @NotNull |
168 | | - public static List<File> getJdkClassesRootsFromJre(@NotNull String javaHome) { |
169 | | - return JavaSdkUtil.getJdkClassesRoots(new File(javaHome), true); |
170 | | - } |
| 150 | + @JvmStatic |
| 151 | + fun getJdkClassesRootsFromJre(javaHome: String): List<File> = |
| 152 | + JavaSdkUtil.getJdkClassesRoots(File(javaHome), true) |
171 | 153 |
|
172 | | - @NotNull |
173 | | - public static List<File> getJdkClassesRoots(@NotNull File jdkHome) { |
174 | | - return JavaSdkUtil.getJdkClassesRoots(jdkHome, false); |
175 | | - } |
| 154 | + @JvmStatic |
| 155 | + fun getJdkClassesRoots(jdkHome: File): List<File> = |
| 156 | + JavaSdkUtil.getJdkClassesRoots(jdkHome, false) |
176 | 157 | } |
0 commit comments