diff --git a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java index 50c82ac3a0a..39fa4009f8f 100644 --- a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java +++ b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java @@ -21,6 +21,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.cli.common.CompilerPlugin; import org.jetbrains.jet.cli.common.messages.MessageCollector; +import org.jetbrains.jet.cli.common.util.CompilerPathUtil; +import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; import org.jetbrains.jet.cli.jvm.compiler.*; import org.jetbrains.jet.codegen.BuiltinToJavaTypesMapping; import org.jetbrains.jet.config.CommonConfigurationKeys; @@ -63,7 +65,7 @@ public class BytecodeCompiler { configuration.add(CLASSPATH_KEY, new File(stdlib)); } else { - File path = PathUtil.getDefaultRuntimePath(); + File path = CompilerPathUtil.getRuntimePath(); if (path != null) { configuration.add(CLASSPATH_KEY, path); } @@ -73,7 +75,7 @@ public class BytecodeCompiler { configuration.add(CLASSPATH_KEY, new File(path)); } } - File jdkAnnotationsPath = PathUtil.getJdkAnnotationsPath(); + File jdkAnnotationsPath = CompilerPathUtil.getJdkAnnotationsPath(); if (jdkAnnotationsPath != null) { configuration.add(ANNOTATIONS_PATH_KEY, jdkAnnotationsPath); } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/util/CompilerPathUtil.java b/compiler/cli/src/org/jetbrains/jet/cli/common/util/CompilerPathUtil.java new file mode 100644 index 00000000000..872c644b132 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/util/CompilerPathUtil.java @@ -0,0 +1,70 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.cli.common.util; + +import com.intellij.openapi.application.PathManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.utils.PathUtil; + +import java.io.File; + +/** + * @author Maxim.Manuylov + * Date: 20.05.12 + */ +public class CompilerPathUtil { + private CompilerPathUtil() {} + + @Nullable + public static File getSDKHome() { + final File compilerJar = new File(getJarPathForClass(CompilerPathUtil.class)); + if (!compilerJar.exists()) return null; + + if (compilerJar.getName().equals(PathUtil.KOTLIN_COMPILER_JAR)) { + final File lib = compilerJar.getParentFile(); + final File answer = lib.getParentFile(); + return answer.exists() ? answer : null; + } + + File current = new File("").getAbsoluteFile(); // CWD + + do { + File atDevHome = new File(current, "dist/kotlinc"); + if (atDevHome.exists()) return atDevHome; + current = current.getParentFile(); + } while (current != null); + + return null; + } + + @Nullable + public static File getRuntimePath() { + return PathUtil.getRuntimePath(getSDKHome()); + } + + @Nullable + public static File getJdkAnnotationsPath() { + return PathUtil.getJdkAnnotationsPath(getSDKHome()); + } + + @NotNull + public static String getJarPathForClass(@NotNull final Class aClass) { + final String resourceRoot = PathManager.getResourceRoot(aClass, "/" + aClass.getName().replace('.', '/') + ".class"); + return new File(resourceRoot).getAbsoluteFile().getAbsolutePath(); + } +} diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index b6d6217da3a..a3a1a8c6797 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.cli.common.CLICompiler; import org.jetbrains.jet.cli.common.ExitCode; import org.jetbrains.jet.cli.common.messages.*; +import org.jetbrains.jet.cli.common.util.CompilerPathUtil; import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.cli.jvm.compiler.K2JVMCompileEnvironmentConfiguration; @@ -180,7 +181,7 @@ public class K2JVMCompiler extends CLICompiler getAnnotationsPath(@NotNull K2JVMCompilerArguments arguments) { List annotationsPath = Lists.newArrayList(); if (!arguments.noJdkAnnotations) { - annotationsPath.add(PathUtil.getJdkAnnotationsPath()); + annotationsPath.add(CompilerPathUtil.getJdkAnnotationsPath()); } if (arguments.annotations != null) { for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.annotations)) { diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java index d6b2fdcb53f..12d36096e52 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/CompileEnvironmentUtil.java @@ -25,6 +25,7 @@ import jet.modules.Module; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.cli.common.messages.MessageCollector; +import org.jetbrains.jet.cli.common.util.CompilerPathUtil; import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; import org.jetbrains.jet.codegen.BuiltinToJavaTypesMapping; import org.jetbrains.jet.codegen.ClassFileFactory; @@ -89,12 +90,12 @@ public class CompileEnvironmentUtil { } }; CompilerConfiguration configuration = new CompilerConfiguration(); - File defaultRuntimePath = PathUtil.getDefaultRuntimePath(); + File defaultRuntimePath = CompilerPathUtil.getRuntimePath(); if (defaultRuntimePath != null) { configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, defaultRuntimePath); } configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, PathUtil.findRtJar()); - File jdkAnnotationsPath = PathUtil.getJdkAnnotationsPath(); + File jdkAnnotationsPath = CompilerPathUtil.getJdkAnnotationsPath(); if (jdkAnnotationsPath != null) { configuration.add(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, jdkAnnotationsPath); } @@ -124,7 +125,7 @@ public class CompileEnvironmentUtil { } private static List runDefineModules(String moduleFile, ClassFileFactory factory) { - File stdlibJar = PathUtil.getDefaultRuntimePath(); + File stdlibJar = CompilerPathUtil.getRuntimePath(); GeneratedClassLoader loader; if (stdlibJar != null) { try { diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java index 2ad32e41328..afb431db609 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java @@ -33,6 +33,7 @@ import org.jetbrains.jet.cli.common.CompilerPluginContext; import org.jetbrains.jet.cli.common.messages.AnalyzerWithCompilerReport; import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation; import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity; +import org.jetbrains.jet.cli.common.util.CompilerPathUtil; import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; import org.jetbrains.jet.codegen.*; import org.jetbrains.jet.config.CommonConfigurationKeys; @@ -241,7 +242,7 @@ public class KotlinToJVMBytecodeCompiler { try { GeneratedClassLoader classLoader = new GeneratedClassLoader(factory, new URLClassLoader(new URL[]{ // TODO: add all classpath - PathUtil.getDefaultRuntimePath().toURI().toURL() + CompilerPathUtil.getRuntimePath().toURI().toURL() }, AllModules.class.getClassLoader())); Class scriptClass = classLoader.loadClass(ScriptCodegen.SCRIPT_DEFAULT_CLASS_NAME.getFqName().getFqName()); diff --git a/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java b/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java index f25a7e1c58a..44e84edd7d5 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java +++ b/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java @@ -19,7 +19,6 @@ */ package org.jetbrains.jet.utils; -import com.intellij.openapi.application.PathManager; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; @@ -27,84 +26,49 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; -import java.util.ArrayList; -import java.util.List; public class PathUtil { public static final String JS_LIB_JAR_NAME = "kotlin-jslib.jar"; public static final String JS_LIB_JS_NAME = "kotlinLib.js"; public static final String JDK_ANNOTATIONS_JAR = "kotlin-jdk-annotations.jar"; + public static final String KOTLIN_COMPILER_JAR = "kotlin-compiler.jar"; + public static final String KOTLIN_RUNTIME_JAR = "kotlin-runtime.jar"; private PathUtil() {} - public static File getDefaultCompilerPath() { - File plugin_jar_path = new File(getJarPathForClass(PathUtil.class)); - - if (!plugin_jar_path.exists()) return null; - - if (plugin_jar_path.getName().equals("kotlin-plugin.jar")) { - File lib = plugin_jar_path.getParentFile(); - File pluginHome = lib.getParentFile(); - - File answer = new File(pluginHome, "kotlinc"); - - return answer.exists() ? answer : null; - } - - if (plugin_jar_path.getName().equals("kotlin-compiler.jar")) { - File lib = plugin_jar_path.getParentFile(); - File answer = lib.getParentFile(); - return answer.exists() ? answer : null; - } - - File current = new File("").getAbsoluteFile(); // CWD - - do { - File atDevHome = new File(current, "dist/kotlinc"); - if (atDevHome.exists()) return atDevHome; - current = current.getParentFile(); - } while (current != null); - - return null; + @Nullable + public static File getRuntimePath(@Nullable final File sdkHome) { + return getFilePackedIntoLib(sdkHome, KOTLIN_RUNTIME_JAR); } @Nullable - public static File getDefaultRuntimePath() { - return getFilePackedIntoLib("kotlin-runtime.jar"); + public static File getCompilerPath(@Nullable final File sdkHome) { + return getFilePackedIntoLib(sdkHome, KOTLIN_COMPILER_JAR); } @Nullable - public static File getDefaultJsLibJsPath() { - return getFilePackedIntoLib(JS_LIB_JS_NAME); + public static File getJsLibJsPath(@Nullable final File sdkHome) { + return getFilePackedIntoLib(sdkHome, JS_LIB_JS_NAME); } @Nullable - public static File getDefaultJsLibJarPath() { - return getFilePackedIntoLib(JS_LIB_JAR_NAME); + public static File getJsLibJarPath(@Nullable final File sdkHome) { + return getFilePackedIntoLib(sdkHome, JS_LIB_JAR_NAME); } @Nullable - private static File getFilePackedIntoLib(@NotNull String filePathFromLib) { - File compilerPath = getDefaultCompilerPath(); - if (compilerPath == null) return null; - - File answer = new File(compilerPath, "lib/" + filePathFromLib); + public static File getJdkAnnotationsPath(@Nullable final File sdkHome) { + return getFilePackedIntoLib(sdkHome, JDK_ANNOTATIONS_JAR); + } + @Nullable + private static File getFilePackedIntoLib(@Nullable final File sdkHome, @NotNull final String filePathFromLib) { + if (sdkHome == null) return null; + final File answer = new File(sdkHome, "lib/" + filePathFromLib); return answer.exists() ? answer : null; } - @Nullable - public static File getJdkAnnotationsPath() { - return getFilePackedIntoLib(JDK_ANNOTATIONS_JAR); - } - - @NotNull - public static String getJarPathForClass(@NotNull Class aClass) { - String resourceRoot = PathManager.getResourceRoot(aClass, "/" + aClass.getName().replace('.', '/') + ".class"); - return new File(resourceRoot).getAbsoluteFile().getAbsolutePath(); - } - @NotNull public static VirtualFile jarFileOrDirectoryToVirtualFile(@NotNull File file) { if (file.exists()) { diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 498d91b9c10..850a7293a4a 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -24,15 +24,15 @@ org.jetbrains.jet.plugin.highlighter.DeclarationHintSupport - - org.jetbrains.jet.plugin.OutdatedKotlinRuntimeNotification - org.jetbrains.jet.plugin.project.K2JSModuleComponent + + org.jetbrains.jet.plugin.OutdatedKotlinRuntimeNotification + diff --git a/idea/src/org/jetbrains/jet/plugin/OutdatedKotlinRuntimeNotification.java b/idea/src/org/jetbrains/jet/plugin/OutdatedKotlinRuntimeNotification.java index d6fedabdefc..4d609fdabc7 100644 --- a/idea/src/org/jetbrains/jet/plugin/OutdatedKotlinRuntimeNotification.java +++ b/idea/src/org/jetbrains/jet/plugin/OutdatedKotlinRuntimeNotification.java @@ -14,19 +14,16 @@ package org.jetbrains.jet.plugin;/* * limitations under the License. */ -import com.intellij.ide.plugins.IdeaPluginDescriptor; -import com.intellij.ide.plugins.PluginManager; import com.intellij.ide.util.PropertiesComponent; import com.intellij.notification.Notification; import com.intellij.notification.NotificationListener; import com.intellij.notification.NotificationType; import com.intellij.notification.Notifications; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.components.AbstractProjectComponent; -import com.intellij.openapi.extensions.PluginId; -import com.intellij.openapi.project.Project; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleComponent; +import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTable; import com.intellij.openapi.ui.Messages; @@ -38,6 +35,7 @@ import com.intellij.util.text.VersionComparatorUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.plugin.quickfix.ConfigureKotlinLibraryNotificationProvider; +import org.jetbrains.jet.plugin.util.PluginPathUtil; import org.jetbrains.jet.utils.PathUtil; import javax.swing.event.HyperlinkEvent; @@ -50,36 +48,40 @@ import java.util.jar.JarFile; * @author Evgeny Gerashchenko * @since 5/22/12 */ -public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent { +public class OutdatedKotlinRuntimeNotification implements ModuleComponent { private static final String UNKNOWN_VERSION = "UNKNOWN"; - private static final String SUPPRESSED_PROPERTY_NAME = "oudtdated.runtime.suppressed.plugin.version"; + private static final String SUPPRESSED_PROPERTY_NAME_PATTERN = "oudtdated.runtime.suppressed.plugin.version[%s]"; - public OutdatedKotlinRuntimeNotification(final Project project) { - super(project); + private final Module myModule; + private final String mySuppressedPropertyName; + + public OutdatedKotlinRuntimeNotification(final Module module) { + myModule = module; + mySuppressedPropertyName = String.format(SUPPRESSED_PROPERTY_NAME_PATTERN, module.getName()); } @Override public void projectOpened() { if (ApplicationManager.getApplication().isInternal()) return; - String runtimeVersion = getRuntimeVersion(); - final String pluginVersion = getPluginVersion(); + String runtimeVersion = getRuntimeVersion(getKotlinRuntimeJar()); if (runtimeVersion == null) return; // runtime is not present in project - if ("@snapshot@".equals(pluginVersion)) return; // plugin is run from sources, can't compare versions + final String sdkVersion = getRuntimeVersion(getRuntimeFromSdk()); + if (sdkVersion == null || "snapshot".equals(sdkVersion)) return; // plugin is run from sources, can't compare versions // user already clicked suppress - if (pluginVersion.equals(PropertiesComponent.getInstance(myProject).getValue(SUPPRESSED_PROPERTY_NAME))) return; + if (sdkVersion.equals(PropertiesComponent.getInstance(myModule.getProject()).getValue(mySuppressedPropertyName))) return; boolean isRuntimeOutdated = "snapshot".equals(runtimeVersion) || UNKNOWN_VERSION.equals(runtimeVersion) - || runtimeVersion.startsWith("internal-") != pluginVersion.startsWith("internal-") - || VersionComparatorUtil.compare(pluginVersion, runtimeVersion) > 0; + || runtimeVersion.startsWith("internal-") != sdkVersion.startsWith("internal-") + || VersionComparatorUtil.compare(sdkVersion, runtimeVersion) > 0; if (!isRuntimeOutdated) return; - String message = String.format("

Your version of Kotlin runtime library is %s, while plugin version is %s." + + String message = String.format("

Your version of Kotlin runtime library in module \"%s\" is %s, while Kotlin SDK version in this module is %s." + " Runtime library should be updated to avoid compatibility problems.

" + "

Update Runtime Ignore

", - UNKNOWN_VERSION.equals(runtimeVersion) ? "older than 0.1.2296" : runtimeVersion, pluginVersion); + myModule.getName(), UNKNOWN_VERSION.equals(runtimeVersion) ? "older than 0.1.2296" : runtimeVersion, sdkVersion); Notifications.Bus.notify(new Notification("Outdated Kotlin Runtime", "Outdated Kotlin Runtime", message, NotificationType.WARNING, new NotificationListener() { @@ -90,7 +92,7 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent updateRuntime(); } else if ("ignore".equals(event.getDescription())) { - PropertiesComponent.getInstance(myProject).setValue(SUPPRESSED_PROPERTY_NAME, pluginVersion); + PropertiesComponent.getInstance(myModule.getProject()).setValue(mySuppressedPropertyName, sdkVersion); } else { throw new AssertionError(); @@ -98,16 +100,17 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent notification.expire(); } } - }), myProject); -} + }), myModule.getProject()); + } private void updateRuntime() { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { - File runtimePath = PathUtil.getDefaultRuntimePath(); + File runtimePath = PluginPathUtil.getRuntimePath(myModule); if (runtimePath == null) { - Messages.showErrorDialog(myProject, "kotlin-runtime.jar is not found. Make sure plugin is properly installed.", + Messages.showErrorDialog(myModule.getProject(), + "kotlin-runtime.jar is not found. Make sure plugin is properly installed.", "No Runtime Found"); return; } @@ -130,8 +133,7 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent } @Nullable - private String getRuntimeVersion() { - VirtualFile kotlinRuntimeJar = getKotlinRuntimeJar(); + private static String getRuntimeVersion(@Nullable final VirtualFile kotlinRuntimeJar) { if (kotlinRuntimeJar == null) return null; VirtualFile manifestFile = kotlinRuntimeJar.findFileByRelativePath(JarFile.MANIFEST_NAME); if (manifestFile != null) { @@ -145,7 +147,7 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent @Nullable private VirtualFile getKotlinRuntimeJar() { - LibraryTable table = ProjectLibraryTable.getInstance(myProject); + LibraryTable table = ModuleRootManager.getInstance(myModule).getModifiableModel().getModuleLibraryTable(); Library kotlinRuntime = table.getLibraryByName(ConfigureKotlinLibraryNotificationProvider.LIBRARY_NAME); if (kotlinRuntime != null) { for (VirtualFile root : kotlinRuntime.getFiles(OrderRootType.CLASSES)) { @@ -157,10 +159,27 @@ public class OutdatedKotlinRuntimeNotification extends AbstractProjectComponent return null; } + @Nullable + private VirtualFile getRuntimeFromSdk() { + final File runtimePath = PluginPathUtil.getRuntimePath(myModule); + return runtimePath == null ? null : PathUtil.jarFileOrDirectoryToVirtualFile(runtimePath); + } + + @Override + public void moduleAdded() {} + + @Override + public void projectClosed() {} + + @Override + public void initComponent() {} + + @Override + public void disposeComponent() {} + @NotNull - private static String getPluginVersion() { - IdeaPluginDescriptor plugin = PluginManager.getPlugin(PluginId.getId("org.jetbrains.kotlin")); - assert plugin != null : "How can it be? Kotlin plugin is available, but its component is running. Complete nonsense."; - return plugin.getVersion(); + @Override + public String getComponentName() { + return getClass().getName(); } } diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java b/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java index a8b00bf467b..da6361c25a5 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/CompilerEnvironment.java @@ -19,12 +19,11 @@ package org.jetbrains.jet.plugin.compiler; import com.intellij.openapi.compiler.CompileContext; import com.intellij.openapi.module.Module; import com.intellij.openapi.vfs.VirtualFile; -import jet.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.utils.PathUtil; +import org.jetbrains.jet.plugin.sdk.KotlinSdkUtil; -import java.io.*; +import java.io.File; import static com.intellij.openapi.compiler.CompilerMessageCategory.ERROR; @@ -37,7 +36,7 @@ public final class CompilerEnvironment { public static CompilerEnvironment getEnvironmentFor(@NotNull CompileContext compileContext, @NotNull Module module, boolean tests) { VirtualFile mainOutput = compileContext.getModuleOutputDirectory(module); final VirtualFile outputDir = tests ? compileContext.getModuleOutputDirectoryForTests(module) : mainOutput; - File kotlinHome = PathUtil.getDefaultCompilerPath(); + File kotlinHome = KotlinSdkUtil.getSDKHomeFor(module); return new CompilerEnvironment(kotlinHome, outputDir); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ConfigureKotlinLibraryNotificationProvider.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ConfigureKotlinLibraryNotificationProvider.java index ee974944e2b..046775df8a5 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ConfigureKotlinLibraryNotificationProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ConfigureKotlinLibraryNotificationProvider.java @@ -55,7 +55,7 @@ import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.plugin.JetPluginUtil; import org.jetbrains.jet.plugin.sdk.KotlinSdkUtil; -import org.jetbrains.jet.utils.PathUtil; +import org.jetbrains.jet.plugin.util.PluginPathUtil; import javax.swing.*; import java.io.File; @@ -105,7 +105,7 @@ public class ConfigureKotlinLibraryNotificationProvider implements EditorNotific return null; } - private Library findOrCreateRuntimeLibrary() { + private Library findOrCreateRuntimeLibrary(@NotNull Module module) { LibraryTable table = ProjectLibraryTable.getInstance(myProject); Library kotlinRuntime = table.getLibraryByName(LIBRARY_NAME); if (kotlinRuntime != null) { @@ -116,7 +116,7 @@ public class ConfigureKotlinLibraryNotificationProvider implements EditorNotific } } - File runtimePath = PathUtil.getDefaultRuntimePath(); + File runtimePath = PluginPathUtil.getRuntimePath(module); if (runtimePath == null) { Messages.showErrorDialog(myProject, "kotlin-runtime.jar is not found. Make sure plugin is properly installed.", "No Runtime Found"); @@ -192,7 +192,7 @@ public class ConfigureKotlinLibraryNotificationProvider implements EditorNotific ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { - Library library = findOrCreateRuntimeLibrary(); + Library library = findOrCreateRuntimeLibrary(module); if (library != null) { ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); if (model.findLibraryOrderEntry(library) == null) { @@ -214,7 +214,7 @@ public class ConfigureKotlinLibraryNotificationProvider implements EditorNotific /* package */ static void addJdkAnnotations(Module module) { Sdk sdk = ModuleRootManager.getInstance(module).getSdk(); assert sdk != null; - File annotationsIoFile = PathUtil.getJdkAnnotationsPath(); + File annotationsIoFile = PluginPathUtil.getJdkAnnotationsPath(module); if (annotationsIoFile != null) { VirtualFile jdkAnnotationsJar = LocalFileSystem.getInstance().findFileByIoFile(annotationsIoFile); if (jdkAnnotationsJar != null) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java b/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java index e033c33cd95..12928057a8d 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/JsModuleSetUp.java @@ -36,6 +36,7 @@ import com.intellij.psi.impl.PsiModificationTrackerImpl; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.plugin.project.K2JSModuleComponent; +import org.jetbrains.jet.plugin.util.PluginPathUtil; import org.jetbrains.jet.utils.PathUtil; import java.io.File; @@ -65,7 +66,7 @@ public final class JsModuleSetUp { return; } - if (!copyJsLibFiles(rootDir)) return; + if (!copyJsLibFiles(module, rootDir)) return; setUpK2JSModuleComponent(module); setUpLibraryAsSourceLibrary(module, rootDir); @@ -108,9 +109,9 @@ public final class JsModuleSetUp { DaemonCodeAnalyzer.getInstance(module.getProject()).restart(); } - private static boolean copyJsLibFiles(@NotNull File rootDir) { - File jsLibJarPath = PathUtil.getDefaultJsLibJarPath(); - File jsLibJsPath = PathUtil.getDefaultJsLibJsPath(); + private static boolean copyJsLibFiles(@NotNull Module module, @NotNull File rootDir) { + File jsLibJarPath = PluginPathUtil.getJsLibJarPath(module); + File jsLibJsPath = PluginPathUtil.getJsLibJsPath(module); if ((jsLibJarPath == null) || (jsLibJsPath == null)) { notifyFailure("JavaScript library not found. Make sure plugin is installed properly."); return false; diff --git a/idea/src/org/jetbrains/jet/plugin/sdk/KotlinSdkUtil.java b/idea/src/org/jetbrains/jet/plugin/sdk/KotlinSdkUtil.java index 779d72e4ee6..25035d2a515 100644 --- a/idea/src/org/jetbrains/jet/plugin/sdk/KotlinSdkUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/sdk/KotlinSdkUtil.java @@ -24,6 +24,7 @@ import com.intellij.psi.search.FilenameIndex; import com.intellij.psi.search.GlobalSearchScope; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.utils.PathUtil; import java.io.File; import java.io.IOException; @@ -46,7 +47,6 @@ public class KotlinSdkUtil { return new KotlinSdkProperties(""); } }; - @NotNull private static final String KOTLIN_COMPILER_JAR = "kotlin-compiler.jar"; @NotNull private static final String[] KOTLIN_COMPILER_JAR_ENTRY_NAMES = { "org/jetbrains/jet/cli/KotlinCompiler.class", "org/jetbrains/jet/cli/jvm/K2JVMCompiler.class" @@ -59,7 +59,7 @@ public class KotlinSdkUtil { } private static boolean isSDKHome(@NotNull final File dir) { - return dir.isDirectory() && isKotlinCompilerJar(getKotlinCompilerJar(dir)); + return dir.isDirectory() && isKotlinCompilerJar(PathUtil.getCompilerPath(dir)); } @Nullable @@ -77,7 +77,8 @@ public class KotlinSdkUtil { } catch (final IOException e) { try { - return getJarImplementationVersion(getKotlinCompilerJar(new File(sdkHomePath))); + final File compilerJar = PathUtil.getCompilerPath(new File(sdkHomePath)); + return compilerJar == null ? null : getJarImplementationVersion(compilerJar); } catch (final IOException e1) { return null; @@ -97,15 +98,10 @@ public class KotlinSdkUtil { } } - @NotNull - private static File getKotlinCompilerJar(@NotNull final File sdkHome) { - return new File(new File(sdkHome, "lib"), KOTLIN_COMPILER_JAR); - } - @Nullable public static String detectSDKVersion(@NotNull final List jars) { for (final VirtualFile jar : jars) { - if (jar.getName().equals(KOTLIN_COMPILER_JAR) && isKotlinCompilerJar(new File(jar.getPath()))) { + if (jar.getName().equals(PathUtil.KOTLIN_COMPILER_JAR) && isKotlinCompilerJar(new File(jar.getPath()))) { final VirtualFile libDir = jar.getParent(); if (libDir != null) { final VirtualFile sdkHomeDir = libDir.getParent(); @@ -119,22 +115,29 @@ public class KotlinSdkUtil { } public static boolean isSDKConfiguredFor(@NotNull final Module module) { - final GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false); - return containsKotlinCompilerJar(FilenameIndex.getVirtualFilesByName(module.getProject(), KOTLIN_COMPILER_JAR, scope)); + return getSDKHomeFor(module) != null; } - private static boolean containsKotlinCompilerJar(@NotNull final Collection jars) { + @Nullable + public static File getSDKHomeFor(@NotNull final Module module) { + final GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false); + return findKotlinCompilerJar(FilenameIndex.getVirtualFilesByName(module.getProject(), PathUtil.KOTLIN_COMPILER_JAR, scope)); + } + + @Nullable + private static File findKotlinCompilerJar(@NotNull final Collection jars) { for (final VirtualFile jar : jars) { - if (isKotlinCompilerJar(new File(jar.getPath()))) { - return true; + final File file = new File(jar.getPath()); + if (isKotlinCompilerJar(file)) { + return file; } } - return false; + return null; } - private static boolean isKotlinCompilerJar(@NotNull final File jar) { + private static boolean isKotlinCompilerJar(@Nullable final File jar) { try { - return doIsKotlinCompilerJar(jar); + return jar != null && doIsKotlinCompilerJar(jar); } catch (final IOException e) { return false; diff --git a/idea/src/org/jetbrains/jet/plugin/util/PluginPathUtil.java b/idea/src/org/jetbrains/jet/plugin/util/PluginPathUtil.java new file mode 100644 index 00000000000..55d8d02be4a --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/util/PluginPathUtil.java @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.util; + +import com.intellij.openapi.module.Module; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.plugin.sdk.KotlinSdkUtil; +import org.jetbrains.jet.utils.PathUtil; + +import java.io.File; + +/** + * @author Maxim.Manuylov + * Date: 20.05.12 + */ +public class PluginPathUtil { + private PluginPathUtil() {} + + @Nullable + public static File getRuntimePath(@NotNull final Module module) { + return PathUtil.getRuntimePath(KotlinSdkUtil.getSDKHomeFor(module)); + } + + @Nullable + public static File getJsLibJsPath(@NotNull final Module module) { + return PathUtil.getJsLibJsPath(KotlinSdkUtil.getSDKHomeFor(module)); + } + + @Nullable + public static File getJsLibJarPath(@NotNull final Module module) { + return PathUtil.getJsLibJarPath(KotlinSdkUtil.getSDKHomeFor(module)); + } + + @Nullable + public static File getJdkAnnotationsPath(@NotNull final Module module) { + return PathUtil.getJdkAnnotationsPath(KotlinSdkUtil.getSDKHomeFor(module)); + } +}