Introduce SetUpJsModuleAction

Refactor PathUtil
This commit is contained in:
pTalanov
2012-05-22 13:51:48 +04:00
parent 67491a224d
commit 18ce0ac722
5 changed files with 152 additions and 12 deletions
@@ -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
+4
View File
@@ -36,6 +36,10 @@
<keyboard-shortcut keymap="$default" first-keystroke="control shift Q"/>
<add-to-group group-id="CodeMenu" anchor="last"/>
</action>
<action id="SetUpJsModuleAction" class="org.jetbrains.jet.plugin.actions.SetUpJsModuleAction"
text="Set up Kotlin to JavaScript module">
<add-to-group group-id="CodeMenu" anchor="last"/>
</action>
<action id="CopyAsDiagnosticTest" class="org.jetbrains.jet.plugin.actions.CopyAsDiagnosticTestAction"
text="Copy Current File As Diagnostic Test">
<keyboard-shortcut keymap="$default" first-keystroke="control alt shift T"/>
@@ -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));
}
}
@@ -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()));
}
}
@@ -72,7 +72,7 @@ public class ZippedLibrarySourcesConfig extends Config {
Enumeration<? extends ZipEntry> 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());