This commit is contained in:
develar
2012-06-22 13:22:04 +04:00
parent 9443007dc3
commit 8d764af432
4 changed files with 23 additions and 20 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.jet.cli.js; package org.jetbrains.jet.cli.js;
import com.sampullara.cli.Argument; import com.sampullara.cli.Argument;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.CompilerArguments; import org.jetbrains.jet.cli.common.CompilerArguments;
import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.facade.MainCallParameters;
@@ -53,8 +54,9 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "version", description = "Display compiler version") @Argument(value = "version", description = "Display compiler version")
public boolean version; public boolean version;
@Argument(value = "mainCall", description = "Whether a main function should be invoked; either 'main' or 'mainWithArgs'") @Nullable
public String mainCall; @Argument(value = "main", description = "Whether a main function should be called; either 'call' or 'noCall', default 'call' (main function will be auto detected)")
public String main;
@Argument(value = "help", alias = "h", description = "Show help") @Argument(value = "help", alias = "h", description = "Show help")
public boolean help; public boolean help;
@@ -85,15 +87,12 @@ public class K2JSCompilerArguments extends CompilerArguments {
} }
public MainCallParameters createMainCallParameters() { public MainCallParameters createMainCallParameters() {
if (mainCall != null) { if ("noCall".equals(main)) {
if (mainCall.equals("main")) { return MainCallParameters.noCall();
return MainCallParameters.mainWithoutArguments(); }
} else {
if (mainCall.equals("mainWithArgs")) { // TODO should we pass the arguments to the compiler?
// TODO should we pass the arguments to the compiler? return MainCallParameters.mainWithoutArguments();
return MainCallParameters.mainWithArguments(new ArrayList<String>());
}
} }
return MainCallParameters.noCall();
} }
} }
@@ -48,7 +48,6 @@ import static org.jetbrains.jet.plugin.compiler.CompilerUtils.outputCompilerMess
* @author Pavel Talanov * @author Pavel Talanov
*/ */
public final class K2JSCompiler implements TranslatingCompiler { public final class K2JSCompiler implements TranslatingCompiler {
@Override @Override
public boolean isCompilableFile(VirtualFile file, CompileContext context) { public boolean isCompilableFile(VirtualFile file, CompileContext context) {
if (!(file.getFileType() instanceof JetFileType)) { if (!(file.getFileType() instanceof JetFileType)) {
@@ -130,8 +129,10 @@ public final class K2JSCompiler implements TranslatingCompiler {
@NotNull @NotNull
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile) { private static String[] constructArguments(@NotNull Module module, @Nullable String outFile) {
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version", "-mainCall", "mainWithArgs"); VirtualFile[] sourceFiles = getSourceFiles(module);
addPathToSourcesDir(module, args);
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version");
addPathToSourcesDir(sourceFiles, args);
addOutputPath(outFile, args); addOutputPath(outFile, args);
addLibLocationAndTarget(module, args); addLibLocationAndTarget(module, args);
return ArrayUtil.toStringArray(args); return ArrayUtil.toStringArray(args);
@@ -204,12 +205,12 @@ public final class K2JSCompiler implements TranslatingCompiler {
} }
} }
private static void addPathToSourcesDir(@NotNull Module module, @NotNull ArrayList<String> args) { private static void addPathToSourcesDir(@NotNull VirtualFile[] sourceFiles, @NotNull ArrayList<String> args) {
args.add("-sourceFiles"); args.add("-sourceFiles");
StringBuilder sb = StringBuilderSpinAllocator.alloc(); StringBuilder sb = StringBuilderSpinAllocator.alloc();
try { try {
for (VirtualFile file : getSourceFiles(module)) { for (VirtualFile file : sourceFiles) {
sb.append(file.getPath()).append(','); sb.append(file.getPath()).append(',');
} }
args.add(sb.substring(0, sb.length() - 1)); args.add(sb.substring(0, sb.length() - 1));
@@ -25,7 +25,6 @@ import java.util.List;
* @author Pavel Talanov * @author Pavel Talanov
*/ */
public abstract class MainCallParameters { public abstract class MainCallParameters {
@NotNull @NotNull
public static MainCallParameters noCall() { public static MainCallParameters noCall() {
return new MainCallParameters() { return new MainCallParameters() {
@@ -22,6 +22,7 @@ import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil; import com.google.dart.compiler.util.AstUtil;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
@@ -181,7 +182,10 @@ public final class Translation {
defineModule(statements, context, config); defineModule(statements, context, config);
if (mainCallParameters.shouldBeGenerated()) { if (mainCallParameters.shouldBeGenerated()) {
statements.add(generateCallToMain(context, files, mainCallParameters.arguments())); JsStatement statement = generateCallToMain(context, files, mainCallParameters.arguments());
if (statement != null) {
statements.add(statement);
}
} }
generateTestCalls(context, files, block, rawStatements); generateTestCalls(context, files, block, rawStatements);
JsNamer namer = new JsPrettyNamer(); JsNamer namer = new JsPrettyNamer();
@@ -197,12 +201,12 @@ public final class Translation {
context.jsScope().declareName("_").makeRef()).makeStmt()); context.jsScope().declareName("_").makeRef()).makeStmt());
} }
@NotNull @Nullable
private static JsStatement generateCallToMain(@NotNull TranslationContext context, @NotNull List<JetFile> files, private static JsStatement generateCallToMain(@NotNull TranslationContext context, @NotNull List<JetFile> files,
@NotNull List<String> arguments) throws MainFunctionNotFoundException { @NotNull List<String> arguments) throws MainFunctionNotFoundException {
JetNamedFunction mainFunction = getMainFunction(files); JetNamedFunction mainFunction = getMainFunction(files);
if (mainFunction == null) { if (mainFunction == null) {
throw new MainFunctionNotFoundException("Main function was not found. Please check compiler arguments"); return null;
} }
JsInvocation translatedCall = generateInvocation(context, mainFunction); JsInvocation translatedCall = generateInvocation(context, mainFunction);
setArguments(context, arguments, translatedCall); setArguments(context, arguments, translatedCall);