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 com.intellij.openapi.util.io.FileUtil;
022    import com.intellij.openapi.vfs.VirtualFile;
023    import com.intellij.openapi.vfs.VirtualFileManager;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.jps.model.java.impl.JavaSdkUtil;
026    
027    import java.io.File;
028    import java.util.List;
029    
030    public class PathUtil {
031    
032        public static final String JPS_KOTLIN_HOME_PROPERTY = "jps.kotlin.home";
033    
034        public static final String JS_LIB_JAR_NAME = "kotlin-jslib.jar";
035        public static final String JS_LIB_SRC_JAR_NAME = "kotlin-jslib-sources.jar";
036        public static final String JDK_ANNOTATIONS_JAR = "kotlin-jdk-annotations.jar";
037        public static final String ANDROID_SDK_ANNOTATIONS_JAR = "kotlin-android-sdk-annotations.jar";
038        public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar";
039        public static final String KOTLIN_JAVA_RUNTIME_SRC_JAR = "kotlin-runtime-sources.jar";
040        public static final String HOME_FOLDER_NAME = "kotlinc";
041    
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 resourceRoot = PathManager.getResourceRoot(aClass, "/" + aClass.getName().replace('.', '/') + ".class");
138            return new File(resourceRoot).getAbsoluteFile();
139        }
140    
141        @NotNull
142        public static VirtualFile jarFileOrDirectoryToVirtualFile(@NotNull File file) {
143            if (file.exists()) {
144                if (file.isDirectory()) {
145                    return VirtualFileManager.getInstance()
146                            .findFileByUrl("file://" + FileUtil.toSystemIndependentName(file.getAbsolutePath()));
147                }
148                else {
149                    return VirtualFileManager.getInstance().findFileByUrl("jar://" + FileUtil.toSystemIndependentName(file.getAbsolutePath()) + "!/");
150                }
151            }
152            else {
153                throw new IllegalStateException("Path " + file + " does not exist.");
154            }
155        }
156    
157        @NotNull
158        public static List<File> getJdkClassesRoots() {
159            return JavaSdkUtil.getJdkClassesRoots(new File(System.getProperty("java.home")), true);
160        }
161    }