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