diff --git a/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java b/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java index 415ebabbd60..887be5eb40d 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java +++ b/compiler/util/src/org/jetbrains/jet/utils/PathUtil.java @@ -31,6 +31,9 @@ import java.util.ArrayList; import java.util.List; public class PathUtil { + + public static String JS_LIB_JAR_NAME = "kotlin-jslib.jar"; + private PathUtil() {} public static File getDefaultCompilerPath() { @@ -66,20 +69,28 @@ public class PathUtil { @Nullable public static File getDefaultRuntimePath() { + return getFilePackedIntoLib("kotlin-runtime.jar"); + } + + + @Nullable + public static File getDefaultJsLibPath() { + return getFilePackedIntoLib(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/kotlin-runtime.jar"); + File answer = new File(compilerPath, "lib/" + filePathFromLib); return answer.exists() ? answer : null; } + @Nullable public static File getAltHeadersPath() { - File compilerPath = getDefaultCompilerPath(); - if (compilerPath == null) return null; - - File answer = new File(compilerPath, "lib/alt/kotlin-jdk-headers.jar"); - return answer.exists() ? answer : null; + return getFilePackedIntoLib("alt/kotlin-jdk-headers.jar"); } @NotNull diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 25197fa32c1..caa8ddcfc31 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -36,6 +36,10 @@ + + + diff --git a/idea/src/org/jetbrains/jet/plugin/actions/SetUpJsModuleAction.java b/idea/src/org/jetbrains/jet/plugin/actions/SetUpJsModuleAction.java new file mode 100644 index 00000000000..385ed417420 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/actions/SetUpJsModuleAction.java @@ -0,0 +1,123 @@ +/* + * 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.actions; + +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationType; +import com.intellij.notification.Notifications; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.k2jsrun.K2JSRunnerUtils; +import org.jetbrains.jet.plugin.project.JsModuleDetector; +import org.jetbrains.jet.utils.PathUtil; + +import java.io.File; +import java.io.IOException; + +import static org.jetbrains.jet.plugin.k2jsrun.K2JSRunnerUtils.copyFileToDir; + +/** + * @author Pavel Talanov + */ +public final class SetUpJsModuleAction extends AnAction { + @Override + public void actionPerformed(AnActionEvent event) { + Project project = event.getProject(); + if (project == null) { + notifyFailure("Internal error: Project not found."); + return; + } + + File jsLibPath = PathUtil.getDefaultJsLibPath(); + if (jsLibPath == null) { + notifyFailure("JavaScript library not found. Make sure plugin is installed properly."); + return; + } + + File rootDir = getRootDir(project); + if (!rootDir.isDirectory()) { + notifyFailure("Internal error: Broken content root."); + return; + } + + if (!copyJsLib(jsLibPath, rootDir)) return; + + File file = new File(rootDir, JsModuleDetector.INDICATION_FILE_NAME); + if (file.exists()) { + notifyInfo("File " + file.getName() + " already exists."); + return; + } + + createIndicationFile(file); + + refreshRootDir(project); + } + + private static void refreshRootDir(@NotNull Project project) { + getContentRoot(project).refresh(true, false); + } + + private static void createIndicationFile(@NotNull File file) { + try { + FileUtil.writeToFile(file, PathUtil.JS_LIB_JAR_NAME); + } + catch (IOException e) { + notifyFailure("Failed to write file " + file.getName()); + } + } + + private static boolean copyJsLib(@NotNull File jsLibPath, @NotNull File rootDir) { + try { + copyFileToDir(jsLibPath, rootDir); + } + catch (IOException e) { + notifyFailure("Failed to copy file: " + e.getMessage()); + return false; + } + return true; + } + + @NotNull + private static File getRootDir(@NotNull Project project) { + VirtualFile contentRoot = getContentRoot(project); + return new File(contentRoot.getPath()); + } + + @NotNull + private static VirtualFile getContentRoot(@NotNull Project project) { + Module module = K2JSRunnerUtils.getJsModule(project); + return ModuleRootManager.getInstance(module).getContentRoots()[0]; + } + + public static void notifyFailure(@NotNull String message) { + Notifications.Bus.notify(new Notification("Set Up Kotlin to JavaScript Module", "Fail", + message, + NotificationType.ERROR)); + } + + public static void notifyInfo(@NotNull String message) { + Notifications.Bus.notify(new Notification("Set Up Kotlin to JavaScript Module", "Information", + message, + NotificationType.INFORMATION)); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/k2jsrun/K2JSRunnerUtils.java b/idea/src/org/jetbrains/jet/plugin/k2jsrun/K2JSRunnerUtils.java index bbb782d9033..b0b73a5dfdc 100644 --- a/idea/src/org/jetbrains/jet/plugin/k2jsrun/K2JSRunnerUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/k2jsrun/K2JSRunnerUtils.java @@ -54,10 +54,7 @@ public final class K2JSRunnerUtils { } String pathToGeneratedJsFile = constructPathToGeneratedFile(project, outputDir.getPath()); try { - File fileToCopy = new File(pathToGeneratedJsFile); - File dirToCopyTo = new File(configurationSettings.getGeneratedFilePath()); - File fileToCopyTo = new File(dirToCopyTo, fileToCopy.getName()); - FileUtil.copy(fileToCopy, fileToCopyTo); + copyFileToDir(new File(pathToGeneratedJsFile), new File(configurationSettings.getGeneratedFilePath())); } catch (IOException e) { throw new RuntimeException("Output JavaScript file was not generated or missing.", e); @@ -71,7 +68,7 @@ public final class K2JSRunnerUtils { } @NotNull - private static Module getJsModule(@NotNull Project project) { + public static Module getJsModule(@NotNull Project project) { Module[] modules = ModuleManager.getInstance(project).getModules(); if (modules.length != 1) { throw new UnsupportedOperationException("Kotlin to JavaScript translator temporarily does not support multiple modules."); @@ -94,4 +91,9 @@ public final class K2JSRunnerUtils { assert profile instanceof K2JSRunConfiguration; return ((K2JSRunConfiguration) profile).settings(); } + + //TODO: this method does not really belong here, but dunno where it should be + public static void copyFileToDir(@NotNull File file, @NotNull File dir) throws IOException { + FileUtil.copy(file, new File(dir, file.getName())); + } } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java b/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java index 83ac136dc4f..723e0171ba7 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java @@ -72,7 +72,7 @@ public class ZippedLibrarySourcesConfig extends Config { Enumeration zipEntries = file.entries(); while (zipEntries.hasMoreElements()) { ZipEntry entry = zipEntries.nextElement(); - if (!entry.isDirectory()) { + if (!entry.isDirectory() && entry.getName().endsWith(".kt")) { InputStream stream = file.getInputStream(entry); String text = FileUtil.loadTextAndClose(stream); JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());