diff --git a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java index 0db96a4a685..b19b2b987a5 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java @@ -18,19 +18,24 @@ package org.jetbrains.jet.cli.js; import com.google.common.base.Predicates; import com.intellij.openapi.Disposable; +import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; import jet.Function0; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.analyzer.AnalyzeExhaust; 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.messages.AnalyzerWithCompilerReport; +import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation; +import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity; +import org.jetbrains.jet.cli.common.messages.PrintingMessageCollector; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS; import org.jetbrains.k2js.config.Config; +import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig; import org.jetbrains.k2js.facade.K2JSTranslator; import java.util.List; @@ -62,11 +67,46 @@ public class K2JSCompiler extends CLICompiler sources, @NotNull final Config config) { AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(messageCollector); - final List sources = environmentForJS.getSourceFiles(); - final Config config = Config.getEmptyConfig(environmentForJS.getProject()); analyzerWithCompilerReport.analyzeAndReport(new Function0() { @Override public AnalyzeExhaust invoke() { @@ -75,23 +115,14 @@ public class K2JSCompiler extends CLICompiler() { @Override @@ -81,31 +89,70 @@ public final class K2JSCompiler implements TranslatingCompiler { sink.add(environment.getOutput().getPath(), collector.getOutputs(), collector.getSources().toArray(VirtualFile.EMPTY_ARRAY)); } + @Nullable + private static Module getModule(@NotNull CompileContext context, @NotNull Chunk moduleChunk) { + if (moduleChunk.getNodes().size() != 1) { + context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple modules.", null, -1, -1); + return null; + } + return moduleChunk.getNodes().iterator().next(); + } + @NotNull private static Integer execInProcess(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out, @NotNull Module module) { try { - VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots(); - if (roots.length != 1) { - context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple module source roots.", null, -1, - -1); - return -1; - } - String[] commandLineArgs = constructArguments(context.getProject(), context.getModuleOutputDirectory(module), roots[0]); - Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler"); - return CompilerUtils.getReturnCodeFromObject(rc); + return doExec(context, environment, out, module); } catch (Throwable e) { - context.addMessage(CompilerMessageCategory.ERROR, "Exception while executing compiler: " + e.getMessage(), null, -1, -1); + context.addMessage(CompilerMessageCategory.ERROR, "Exception while executing compiler:\n" + e.getMessage(), null, -1, -1); } return -1; } - private static String[] constructArguments(@NotNull Project project, @NotNull VirtualFile outDir, @NotNull VirtualFile srcDir) { + @NotNull + private static Integer doExec(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out, + @NotNull Module module) throws Exception { + VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots(); + if (roots.length != 1) { + context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple module source roots.", null, -1, -1); + return -1; + } + String[] commandLineArgs = constructArguments(context.getProject(), context.getModuleOutputDirectory(module), roots[0]); + Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler"); + return CompilerUtils.getReturnCodeFromObject(rc); + } + + @NotNull + private static String[] constructArguments(@NotNull Project project, @Nullable VirtualFile outDir, @NotNull VirtualFile srcDir) { + ArrayList args = Lists.newArrayList("-tags", "-verbose", "-version"); + addPathToSourcesDir(args, srcDir); + addOutputPath(project, outDir, args); + addPathToZippedLib(project, args); + return ArrayUtil.toStringArray(args); + } + + private static void addPathToSourcesDir(@NotNull ArrayList args, @NotNull VirtualFile srcDir) { String srcPath = srcDir.getPath(); - String outPath = outDir.getPath(); - String outFile = K2JSRunnerUtils.constructPathToGeneratedFile(project, outPath); - return new String[] {"-tags", "-verbose", "-version", "-srcdir", srcPath, "-output", outFile}; + args.add("-srcdir"); + args.add(srcPath); + } + + private static void addOutputPath(@NotNull Project project, @Nullable VirtualFile outDir, @NotNull ArrayList args) { + if (outDir != null) { + String outPath = outDir.getPath(); + String outFile = K2JSRunnerUtils.constructPathToGeneratedFile(project, outPath); + args.add("-output"); + args.add(outFile); + } + } + + private static void addPathToZippedLib(@NotNull Project project, @NotNull ArrayList args) { + String libLocationForProject = JsModuleDetector.getLibLocationForProject(project); + if (libLocationForProject != null) { + args.add("-libzip"); + args.add(libLocationForProject); + } } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/project/IDEAConfig.java b/idea/src/org/jetbrains/jet/plugin/project/IDEAConfig.java index 4ba93444e1f..4032da59ec2 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/IDEAConfig.java +++ b/idea/src/org/jetbrains/jet/plugin/project/IDEAConfig.java @@ -16,113 +16,18 @@ package org.jetbrains.jet.plugin.project; -import com.google.common.collect.Lists; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.k2js.config.Config; -import org.jetbrains.k2js.utils.JetFileUtils; +import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig; -import java.io.*; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; +import static org.jetbrains.jet.plugin.project.JsModuleDetector.getLibLocationForProject; /** * @author Pavel Talanov */ -public final class IDEAConfig extends Config { - - @Nullable - private final String pathToLibZip; +public final class IDEAConfig extends ZippedLibrarySourcesConfig { public IDEAConfig(@NotNull Project project) { - super(project); - this.pathToLibZip = getLibLocationForProject(project); - } - - //TODO: refactor - @Nullable - private static String getLibLocationForProject(@NotNull Project project) { - VirtualFile indicationFile = JsModuleDetector.findIndicationFileInContextRoots(project); - if (indicationFile == null) { - return null; - } - try { - InputStream stream = indicationFile.getInputStream(); - String path = FileUtil.loadTextAndClose(stream); - String pathToLibFile = getFirstLine(path); - if (pathToLibFile == null) { - return null; - } - try { - URI pathToLibFileUri = new URI(pathToLibFile); - URI pathToIndicationFileUri = new URI(indicationFile.getPath()); - return pathToIndicationFileUri.resolve(pathToLibFileUri).toString(); - } - catch (URISyntaxException e) { - return null; - } - } - catch (IOException e) { - return null; - } - } - - //TODO: util - @Nullable - private static String getFirstLine(@NotNull String path) throws IOException { - BufferedReader reader = new BufferedReader(new StringReader(path)); - try { - return reader.readLine(); - } - - finally { - reader.close(); - } - } - - @NotNull - @Override - public List generateLibFiles() { - if (pathToLibZip == null) { - return Collections.emptyList(); - } - try { - File file = new File(pathToLibZip); - ZipFile zipFile = new ZipFile(file); - try { - return traverseArchive(zipFile); - } - finally { - zipFile.close(); - } - } - catch (IOException e) { - return Collections.emptyList(); - } - } - - @NotNull - private List traverseArchive(@NotNull ZipFile file) throws IOException { - List result = Lists.newArrayList(); - Enumeration zipEntries = file.entries(); - while (zipEntries.hasMoreElements()) { - ZipEntry entry = zipEntries.nextElement(); - if (!entry.isDirectory()) { - InputStream stream = file.getInputStream(entry); - String text = FileUtil.loadTextAndClose(stream); - JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject()); - result.add(jetFile); - } - } - return result; + super(project, getLibLocationForProject(project)); } } diff --git a/idea/src/org/jetbrains/jet/plugin/project/JsModuleDetector.java b/idea/src/org/jetbrains/jet/plugin/project/JsModuleDetector.java index 9d3df9bc1e3..c8849436032 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/JsModuleDetector.java +++ b/idea/src/org/jetbrains/jet/plugin/project/JsModuleDetector.java @@ -16,17 +16,24 @@ package org.jetbrains.jet.plugin.project; -import com.intellij.openapi.fileTypes.FileTypes; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.net.URI; +import java.net.URISyntaxException; + /** * @author Pavel Talanov - * - * This class has utility functions to determine whether the project (or module) is js project. + *

+ * This class has utility functions to determine whether the project (or module) is js project. */ public final class JsModuleDetector { @@ -36,19 +43,59 @@ public final class JsModuleDetector { } public static boolean isJsProject(@NotNull Project project) { - return findIndicationFileInContextRoots(project) != null; + return findIndicationFileInContentRoots(project) != null; } @Nullable - public static VirtualFile findIndicationFileInContextRoots(@NotNull Project project) { + public static VirtualFile findIndicationFileInContentRoots(@NotNull Project project) { VirtualFile[] roots = ProjectRootManager.getInstance(project).getContentRoots(); for (VirtualFile root : roots) { for (VirtualFile child : root.getChildren()) { - if (child.getFileType().equals(FileTypes.PLAIN_TEXT) && child.getName().equals(INDICATION_FILE_NAME)) { + if (child.getName().equals(INDICATION_FILE_NAME)) { return child; } } } return null; } + + //TODO: refactor + @Nullable + public static String getLibLocationForProject(@NotNull Project project) { + VirtualFile indicationFile = findIndicationFileInContentRoots(project); + if (indicationFile == null) { + return null; + } + try { + InputStream stream = indicationFile.getInputStream(); + String path = FileUtil.loadTextAndClose(stream); + String pathToLibFile = getFirstLine(path); + if (pathToLibFile == null) { + return null; + } + try { + URI pathToLibFileUri = new URI(pathToLibFile); + URI pathToIndicationFileUri = new URI(indicationFile.getPath()); + return pathToIndicationFileUri.resolve(pathToLibFileUri).toString(); + } + catch (URISyntaxException e) { + return null; + } + } + catch (IOException e) { + return null; + } + } + + @Nullable + private static String getFirstLine(@NotNull String path) throws IOException { + BufferedReader reader = new BufferedReader(new StringReader(path)); + try { + return reader.readLine(); + } + + finally { + reader.close(); + } + } } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java b/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java new file mode 100644 index 00000000000..dec84239cac --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/config/ZippedLibrarySourcesConfig.java @@ -0,0 +1,84 @@ +/* + * 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.k2js.config; + +import com.google.common.collect.Lists; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.io.FileUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.k2js.utils.JetFileUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +/** + * @author Pavel Talanov + */ +public class ZippedLibrarySourcesConfig extends Config { + @Nullable + protected final String pathToLibZip; + + public ZippedLibrarySourcesConfig(@NotNull Project project, @Nullable String pathToZip) { + super(project); + pathToLibZip = pathToZip; + } + + @NotNull + @Override + public List generateLibFiles() { + if (pathToLibZip == null) { + return Collections.emptyList(); + } + try { + File file = new File(pathToLibZip); + ZipFile zipFile = new ZipFile(file); + try { + return traverseArchive(zipFile); + } + finally { + zipFile.close(); + } + } + catch (IOException e) { + return Collections.emptyList(); + } + } + + @NotNull + private List traverseArchive(@NotNull ZipFile file) throws IOException { + List result = Lists.newArrayList(); + Enumeration zipEntries = file.entries(); + while (zipEntries.hasMoreElements()) { + ZipEntry entry = zipEntries.nextElement(); + if (!entry.isDirectory()) { + InputStream stream = file.getInputStream(entry); + String text = FileUtil.loadTextAndClose(stream); + JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject()); + result.add(jetFile); + } + } + return result; + } +}