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_JAVA_RUNTIME_SRC_JAR = "kotlin-runtime-sources.jar"; 038 public static final String KOTLIN_COMPILER_JAR = "kotlin-compiler.jar"; 039 040 public static final String HOME_FOLDER_NAME = "kotlinc"; 041 private static final File NO_PATH = new File("<no_path>"); 042 043 private PathUtil() {} 044 045 @NotNull 046 public static KotlinPaths getKotlinPathsForIdeaPlugin() { 047 return ApplicationManager.getApplication().isUnitTestMode() 048 ? getKotlinPathsForDistDirectory() 049 : new KotlinPathsFromHomeDir(getCompilerPathForIdeaPlugin()); 050 } 051 052 @NotNull 053 public static KotlinPaths getKotlinPathsForJpsPlugin() { 054 // When JPS is run on TeamCity, it can not rely on Kotlin plugin layout, 055 // so the path to Kotlin is specified in a system property 056 String jpsKotlinHome = System.getProperty(JPS_KOTLIN_HOME_PROPERTY); 057 if (jpsKotlinHome != null) { 058 return new KotlinPathsFromHomeDir(new File(jpsKotlinHome)); 059 } 060 return new KotlinPathsFromHomeDir(getCompilerPathForJpsPlugin()); 061 } 062 063 @NotNull 064 public static KotlinPaths getKotlinPathsForJpsPluginOrJpsTests() { 065 if ("true".equalsIgnoreCase(System.getProperty("kotlin.jps.tests"))) { 066 return getKotlinPathsForDistDirectory(); 067 } 068 return getKotlinPathsForJpsPlugin(); 069 } 070 071 @NotNull 072 public static KotlinPaths getKotlinPathsForCompiler() { 073 if (!getPathUtilJar().isFile()) { 074 // Not running from a jar, i.e. it is it must be a unit test 075 return getKotlinPathsForDistDirectory(); 076 } 077 return new KotlinPathsFromHomeDir(getCompilerPathForCompilerJar()); 078 } 079 080 @NotNull 081 public static KotlinPaths getKotlinPathsForDistDirectory() { 082 return new KotlinPathsFromHomeDir(new File("dist", HOME_FOLDER_NAME)); 083 } 084 085 @NotNull 086 private static File getCompilerPathForCompilerJar() { 087 File jar = getPathUtilJar(); 088 089 if (!jar.exists()) return NO_PATH; 090 091 if (jar.getName().equals(KOTLIN_COMPILER_JAR)) { 092 File lib = jar.getParentFile(); 093 return lib.getParentFile(); 094 } 095 096 return NO_PATH; 097 } 098 099 @NotNull 100 private static File getCompilerPathForJpsPlugin() { 101 File jar = getPathUtilJar(); 102 103 if (!jar.exists()) return NO_PATH; 104 105 if (jar.getName().equals("kotlin-jps-plugin.jar")) { 106 File pluginHome = jar.getParentFile().getParentFile().getParentFile(); 107 return new File(pluginHome, HOME_FOLDER_NAME); 108 } 109 110 return NO_PATH; 111 } 112 113 @NotNull 114 private static File getCompilerPathForIdeaPlugin() { 115 File jar = getPathUtilJar(); 116 117 if (!jar.exists()) return NO_PATH; 118 119 if (jar.getName().equals("kotlin-plugin.jar")) { 120 File lib = jar.getParentFile(); 121 File pluginHome = lib.getParentFile(); 122 123 return new File(pluginHome, HOME_FOLDER_NAME); 124 } 125 126 return NO_PATH; 127 } 128 129 @NotNull 130 public static File getPathUtilJar() { 131 return getResourcePathForClass(PathUtil.class); 132 } 133 134 @NotNull 135 public static File getResourcePathForClass(@NotNull Class aClass) { 136 String path = "/" + aClass.getName().replace('.', '/') + ".class"; 137 String resourceRoot = PathManager.getResourceRoot(aClass, path); 138 if (resourceRoot == null) { 139 throw new IllegalStateException("Resource not found: " + path); 140 } 141 return new File(resourceRoot).getAbsoluteFile(); 142 } 143 144 @NotNull 145 public static List<File> getJdkClassesRoots() { 146 return JavaSdkUtil.getJdkClassesRoots(new File(System.getProperty("java.home")), true); 147 } 148 }