diff --git a/build.xml b/build.xml index cdee4e237a9..99b8c4a2b8b 100644 --- a/build.xml +++ b/build.xml @@ -19,6 +19,7 @@ + @@ -36,6 +37,9 @@ + + + diff --git a/compiler/cli/cli.iml b/compiler/cli/cli.iml index 7a376081b61..03add988345 100644 --- a/compiler/cli/cli.iml +++ b/compiler/cli/cli.iml @@ -14,6 +14,7 @@ + diff --git a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompileEnvironmentConfiguration.java b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompileEnvironmentConfiguration.java new file mode 100644 index 00000000000..055cd0f4bd0 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompileEnvironmentConfiguration.java @@ -0,0 +1,36 @@ +/* + * 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.js; + +import com.intellij.openapi.util.Disposer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.cli.common.CompileEnvironmentConfig; +import org.jetbrains.jet.cli.common.messages.MessageCollector; + +/** + * @author Pavel Talanov + */ +public class K2JSCompileEnvironmentConfiguration extends CompileEnvironmentConfig { + /** + * NOTE: It's very important to call dispose for every object of this class or there will be memory leaks. + * + * @see Disposer + */ + public K2JSCompileEnvironmentConfiguration(@NotNull MessageCollector messageCollector) { + super(messageCollector); + } +} 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 63d8e8fb778..85ffe896d88 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/js/K2JSCompiler.java @@ -16,12 +16,83 @@ package org.jetbrains.jet.cli.js; +import com.google.common.base.Predicates; +import com.intellij.openapi.Disposable; +import com.intellij.psi.PsiFile; +import jet.Function0; +import jet.modules.Module; +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.jvm.compiler.CompileEnvironmentUtil; +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 java.io.File; +import java.io.PrintStream; +import java.util.Collections; +import java.util.List; + +import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION; + /** * @author Pavel Talanov */ -public final class K2JSCompiler { +public class K2JSCompiler extends CLICompiler { public static void main(String... args) { - //TODO + doMain(new K2JSCompiler(), args); + } + + @NotNull + @Override + protected K2JSCompilerArguments createArguments() { + return new K2JSCompilerArguments(); + } + + + @NotNull + @Override + protected ExitCode doExecute(PrintStream stream, K2JSCompilerArguments arguments, MessageRenderer renderer) { + PrintingMessageCollector messageCollector = new PrintingMessageCollector(stream, renderer, true); + if (arguments.module == null) { + stream.print(renderer.render(CompilerMessageSeverity.ERROR, "Module should be specified", NO_LOCATION)); + return ExitCode.INTERNAL_ERROR; + } + + File directory = new File(arguments.module).getParentFile(); + List modules = CompileEnvironmentUtil + .loadModuleScript(arguments.module, MessageCollector.PLAIN_TEXT_TO_SYSTEM_ERR); + for (Module module : modules) { + Disposable rootDisposable = CompileEnvironmentUtil.createMockDisposable(); + final JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable); + CompileEnvironmentUtil.addSourcesFromModuleToEnvironment(environmentForJS, module, directory); + AnalyzerWithCompilerReport analyzerWithCompilerReport = + new AnalyzerWithCompilerReport(messageCollector); + final List sources = environmentForJS.getSourceFiles(); + analyzerWithCompilerReport.analyzeAndReport(new Function0() { + @Override + public AnalyzeExhaust invoke() { + BindingContext context = AnalyzerFacadeForJS + .analyzeFiles(sources, Predicates.alwaysTrue(), new Config(environmentForJS.getProject()) { + @NotNull + @Override + protected List generateLibFiles() { + return Collections.emptyList(); + } + }); + return AnalyzeExhaust.success(context, JetStandardLibrary.getInstance()); + } + }, sources); + } + + stream.print(renderer.render(CompilerMessageSeverity.ERROR, "Greeting", NO_LOCATION)); + return ExitCode.OK; } } diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java index ca8f5e0bb58..31e3888b344 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java @@ -24,10 +24,16 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Chunk; +import jet.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.project.JsModuleDetector; +import java.io.PrintStream; + +import static org.jetbrains.jet.plugin.compiler.CompilerUtils.invokeExecMethod; +import static org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMessagesAndHandleExitCode; + /** * @author Pavel Talanov */ @@ -47,8 +53,45 @@ public final class K2JSCompiler implements TranslatingCompiler { @Override public void compile(final CompileContext context, Chunk moduleChunk, final VirtualFile[] files, OutputSink sink) { - context.addMessage(CompilerMessageCategory.INFORMATION, "Some useful code will be there", null, -1, -1); - //TODO: + if (files.length == 0) { + return; + } + + if (moduleChunk.getNodes().size() != 1) { + context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple modules.", null, -1, -1); + return; + } + + Module module = moduleChunk.getNodes().iterator().next(); + final CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(context, module, /*tests = */ false); + if (!environment.success()) { + environment.reportErrorsTo(context); + return; + } + + CompilerUtils.OutputItemsCollectorImpl collector = new CompilerUtils.OutputItemsCollectorImpl(environment.getOutput().getPath()); + outputCompilerMessagesAndHandleExitCode(context, collector, new Function1() { + @Override + public Integer invoke(PrintStream stream) { + return execInProcess(context, environment, stream); + } + }); + sink.add(environment.getOutput().getPath(), collector.getOutputs(), collector.getSources().toArray(VirtualFile.EMPTY_ARRAY)); + } + + @NotNull + private static Integer execInProcess(@NotNull CompileContext context, + @NotNull CompilerEnvironment environment, @NotNull PrintStream out) { + try { + String[] commandLineArgs = {"-tags", "-verbose", "-version"}; + Object rc = invokeExecMethod(environment, out, context, commandLineArgs, + "org.jetbrains.jet.cli.js.K2JSCompiler"); + return CompilerUtils.getReturnCodeFromObject(rc); + } + catch (Throwable e) { + context.addMessage(CompilerMessageCategory.ERROR, "Fail!", null, -1, -1); + } + return -1; } @NotNull