001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.utils;
018
019 import com.intellij.openapi.application.ApplicationManager;
020 import com.intellij.openapi.application.PathManager;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.jps.model.java.impl.JavaSdkUtil;
023
024 import java.io.File;
025 import java.util.List;
026
027 public class PathUtil {
028
029 public static final String JPS_KOTLIN_HOME_PROPERTY = "jps.kotlin.home";
030
031 public static final String JS_LIB_JAR_NAME = "kotlin-jslib.jar";
032 public static final String JS_LIB_SRC_JAR_NAME = "kotlin-jslib-sources.jar";
033 public static final String JDK_ANNOTATIONS_JAR = "kotlin-jdk-annotations.jar";
034 public static final String ANDROID_SDK_ANNOTATIONS_JAR = "kotlin-android-sdk-annotations.jar";
035 public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar";
036 public static final String KOTLIN_JAVA_REFLECT_JAR = "kotlin-reflect.jar";
037 public static final String KOTLIN_TEST_JAR = "kotlin-test.jar";
038 public static final String KOTLIN_JAVA_RUNTIME_SRC_JAR = "kotlin-runtime-sources.jar";
039 public static final String KOTLIN_COMPILER_JAR = "kotlin-compiler.jar";
040
041 public static final String HOME_FOLDER_NAME = "kotlinc";
042 private static final File NO_PATH = new File("<no_path>");
043
044 private PathUtil() {}
045
046 @NotNull
047 public static KotlinPaths getKotlinPathsForIdeaPlugin() {
048 return ApplicationManager.getApplication().isUnitTestMode()
049 ? getKotlinPathsForDistDirectory()
050 : new KotlinPathsFromHomeDir(getCompilerPathForIdeaPlugin());
051 }
052
053 @NotNull
054 public static KotlinPaths getKotlinPathsForJpsPlugin() {
055 // When JPS is run on TeamCity, it can not rely on Kotlin plugin layout,
056 // so the path to Kotlin is specified in a system property
057 String jpsKotlinHome = System.getProperty(JPS_KOTLIN_HOME_PROPERTY);
058 if (jpsKotlinHome != null) {
059 return new KotlinPathsFromHomeDir(new File(jpsKotlinHome));
060 }
061 return new KotlinPathsFromHomeDir(getCompilerPathForJpsPlugin());
062 }
063
064 @NotNull
065 public static KotlinPaths getKotlinPathsForJpsPluginOrJpsTests() {
066 if ("true".equalsIgnoreCase(System.getProperty("kotlin.jps.tests"))) {
067 return getKotlinPathsForDistDirectory();
068 }
069 return getKotlinPathsForJpsPlugin();
070 }
071
072 @NotNull
073 public static KotlinPaths getKotlinPathsForCompiler() {
074 if (!getPathUtilJar().isFile()) {
075 // Not running from a jar, i.e. it is it must be a unit test
076 return getKotlinPathsForDistDirectory();
077 }
078 return new KotlinPathsFromHomeDir(getCompilerPathForCompilerJar());
079 }
080
081 @NotNull
082 public static KotlinPaths getKotlinPathsForDistDirectory() {
083 return new KotlinPathsFromHomeDir(new File("dist", HOME_FOLDER_NAME));
084 }
085
086 @NotNull
087 private static File getCompilerPathForCompilerJar() {
088 File jar = getPathUtilJar();
089
090 if (!jar.exists()) return NO_PATH;
091
092 if (jar.getName().equals(KOTLIN_COMPILER_JAR)) {
093 File lib = jar.getParentFile();
094 return lib.getParentFile();
095 }
096
097 return NO_PATH;
098 }
099
100 @NotNull
101 private static File getCompilerPathForJpsPlugin() {
102 File jar = getPathUtilJar();
103
104 if (!jar.exists()) return NO_PATH;
105
106 if (jar.getName().equals("kotlin-jps-plugin.jar")) {
107 File pluginHome = jar.getParentFile().getParentFile().getParentFile();
108 return new File(pluginHome, HOME_FOLDER_NAME);
109 }
110
111 return NO_PATH;
112 }
113
114 @NotNull
115 private static File getCompilerPathForIdeaPlugin() {
116 File jar = getPathUtilJar();
117
118 if (!jar.exists()) return NO_PATH;
119
120 if (jar.getName().equals("kotlin-plugin.jar")) {
121 File lib = jar.getParentFile();
122 File pluginHome = lib.getParentFile();
123
124 return new File(pluginHome, HOME_FOLDER_NAME);
125 }
126
127 return NO_PATH;
128 }
129
130 @NotNull
131 public static File getPathUtilJar() {
132 return getResourcePathForClass(PathUtil.class);
133 }
134
135 @NotNull
136 public static File getResourcePathForClass(@NotNull Class aClass) {
137 String path = "/" + aClass.getName().replace('.', '/') + ".class";
138 String resourceRoot = PathManager.getResourceRoot(aClass, path);
139 if (resourceRoot == null) {
140 throw new IllegalStateException("Resource not found: " + path);
141 }
142 return new File(resourceRoot).getAbsoluteFile();
143 }
144
145 @NotNull
146 public static List<File> getJdkClassesRoots() {
147 return JavaSdkUtil.getJdkClassesRoots(new File(System.getProperty("java.home")), true);
148 }
149 }