allow the JS compiler to take a list of files, verbose flag and a flag to indicate if a main call is required; plus added a test case to ensure we can compile some of the kotlin standard library using the K2JSCompiler directly
This commit is contained in:
@@ -16,12 +16,16 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.cli.js;
|
package org.jetbrains.jet.cli.js;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
import com.google.common.base.Predicates;
|
import com.google.common.base.Predicates;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
import com.intellij.openapi.Disposable;
|
import com.intellij.openapi.Disposable;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import jet.Function0;
|
import jet.Function0;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||||
import org.jetbrains.jet.cli.common.CLICompiler;
|
import org.jetbrains.jet.cli.common.CLICompiler;
|
||||||
import org.jetbrains.jet.cli.common.ExitCode;
|
import org.jetbrains.jet.cli.common.ExitCode;
|
||||||
@@ -37,6 +41,7 @@ import org.jetbrains.k2js.config.Config;
|
|||||||
import org.jetbrains.k2js.config.EcmaVersion;
|
import org.jetbrains.k2js.config.EcmaVersion;
|
||||||
import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig;
|
import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig;
|
||||||
import org.jetbrains.k2js.facade.K2JSTranslator;
|
import org.jetbrains.k2js.facade.K2JSTranslator;
|
||||||
|
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -62,7 +67,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
|
|||||||
@Override
|
@Override
|
||||||
protected ExitCode doExecute(K2JSCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
|
protected ExitCode doExecute(K2JSCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
|
||||||
|
|
||||||
if (arguments.srcdir == null) {
|
if (arguments.srcdir == null && arguments.sourceFiles == null) {
|
||||||
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -srcdir", NO_LOCATION);
|
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -srcdir", NO_LOCATION);
|
||||||
return ExitCode.INTERNAL_ERROR;
|
return ExitCode.INTERNAL_ERROR;
|
||||||
}
|
}
|
||||||
@@ -79,24 +84,40 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
|
|||||||
return ExitCode.INTERNAL_ERROR;
|
return ExitCode.INTERNAL_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return translateAndGenerateOutputFile(messageCollector, environmentForJS, config, outputFile);
|
|
||||||
|
MainCallParameters mainCallParameters = arguments.createMainCallParameters();
|
||||||
|
return translateAndGenerateOutputFile(mainCallParameters, messageCollector, environmentForJS, config, outputFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static JetCoreEnvironment getEnvironment(K2JSCompilerArguments arguments, Disposable rootDisposable) {
|
private static JetCoreEnvironment getEnvironment(K2JSCompilerArguments arguments, Disposable rootDisposable) {
|
||||||
final JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
|
final JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
|
||||||
environmentForJS.addSources(arguments.srcdir);
|
if (arguments.srcdir != null) {
|
||||||
|
environmentForJS.addSources(arguments.srcdir);
|
||||||
|
}
|
||||||
|
if (arguments.sourceFiles != null) {
|
||||||
|
for (String sourceFile : arguments.sourceFiles) {
|
||||||
|
environmentForJS.addSources(sourceFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (arguments.isVerbose()) {
|
if (arguments.isVerbose()) {
|
||||||
System.out.println("Compiling source files: " + environmentForJS.getSourceFiles());
|
Iterable<String> fileNames = Iterables.transform(environmentForJS.getSourceFiles(), new Function<JetFile, String>() {
|
||||||
|
@Override
|
||||||
|
public String apply(@Nullable JetFile file) {
|
||||||
|
return file.getName();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
System.out.println("Compiling source files: " + Joiner.on(", ").join(fileNames));
|
||||||
}
|
}
|
||||||
return environmentForJS;
|
return environmentForJS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static ExitCode translateAndGenerateOutputFile(@NotNull PrintingMessageCollector messageCollector,
|
private static ExitCode translateAndGenerateOutputFile(@NotNull MainCallParameters mainCall,
|
||||||
|
@NotNull PrintingMessageCollector messageCollector,
|
||||||
@NotNull JetCoreEnvironment environmentForJS, @NotNull Config config, @NotNull String outputFile) {
|
@NotNull JetCoreEnvironment environmentForJS, @NotNull Config config, @NotNull String outputFile) {
|
||||||
try {
|
try {
|
||||||
K2JSTranslator.translateWithCallToMainAndSaveToFile(environmentForJS.getSourceFiles(), outputFile, config);
|
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCall, environmentForJS.getSourceFiles(), outputFile, config);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
messageCollector.report(CompilerMessageSeverity.ERROR, "Exception while translating:\n" + e.getMessage(),
|
messageCollector.report(CompilerMessageSeverity.ERROR, "Exception while translating:\n" + e.getMessage(),
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ package org.jetbrains.jet.cli.js;
|
|||||||
|
|
||||||
import com.sampullara.cli.Argument;
|
import com.sampullara.cli.Argument;
|
||||||
import org.jetbrains.jet.cli.common.CompilerArguments;
|
import org.jetbrains.jet.cli.common.CompilerArguments;
|
||||||
|
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
@@ -50,9 +54,14 @@ 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'")
|
||||||
|
public String mainCall;
|
||||||
|
|
||||||
@Argument(value = "help", alias = "h", description = "Show help")
|
@Argument(value = "help", alias = "h", description = "Show help")
|
||||||
public boolean help;
|
public boolean help;
|
||||||
|
|
||||||
|
public List<String> sourceFiles;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isHelp() {
|
public boolean isHelp() {
|
||||||
return help;
|
return help;
|
||||||
@@ -75,6 +84,22 @@ public class K2JSCompilerArguments extends CompilerArguments {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSrc() {
|
public String getSrc() {
|
||||||
|
if (sourceFiles != null) {
|
||||||
|
return sourceFiles.toString();
|
||||||
|
}
|
||||||
return srcdir;
|
return srcdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MainCallParameters.noCall();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ package org.jetbrains.k2js.test.semantics;
|
|||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.cli.js.K2JSCompiler;
|
||||||
|
import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
import org.jetbrains.k2js.config.Config;
|
import org.jetbrains.k2js.config.Config;
|
||||||
import org.jetbrains.k2js.config.EcmaVersion;
|
import org.jetbrains.k2js.config.EcmaVersion;
|
||||||
@@ -58,12 +60,6 @@ public final class StdLibToJSTest extends SingleFileTranslationTest {
|
|||||||
String... stdLibFiles) throws Exception {
|
String... stdLibFiles) throws Exception {
|
||||||
List<String> files = Lists.newArrayList();
|
List<String> files = Lists.newArrayList();
|
||||||
|
|
||||||
// lets add the standard JS library files
|
|
||||||
/*
|
|
||||||
for (String libFileName : Config.LIB_FILE_NAMES) {
|
|
||||||
files.add(Config.LIBRARIES_LOCATION + libFileName);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
File stdlibDir = new File("libraries/stdlib/src");
|
File stdlibDir = new File("libraries/stdlib/src");
|
||||||
assertTrue("Cannot find stdlib source: " + stdlibDir, stdlibDir.exists());
|
assertTrue("Cannot find stdlib source: " + stdlibDir, stdlibDir.exists());
|
||||||
@@ -76,5 +72,20 @@ public final class StdLibToJSTest extends SingleFileTranslationTest {
|
|||||||
runRhinoTests(getOutputFilePaths(kotlinFilename, ecmaVersions),
|
runRhinoTests(getOutputFilePaths(kotlinFilename, ecmaVersions),
|
||||||
new RhinoFunctionNativeObjectResultChecker("test.browser", "foo", "Some Dynamically Created Content!!!"));
|
new RhinoFunctionNativeObjectResultChecker("test.browser", "foo", "Some Dynamically Created Content!!!"));
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// lets add the standard JS library files
|
||||||
|
for (String libFileName : Config.LIB_FILE_NAMES) {
|
||||||
|
files.add(Config.LIBRARIES_LOCATION + libFileName);
|
||||||
|
}
|
||||||
|
// now lets try invoke the compiler
|
||||||
|
for (EcmaVersion version : ecmaVersions) {
|
||||||
|
System.out.println("Compiling with version: " + version);
|
||||||
|
K2JSCompiler compiler = new K2JSCompiler();
|
||||||
|
K2JSCompilerArguments arguments = new K2JSCompilerArguments();
|
||||||
|
arguments.outputFile = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
|
||||||
|
arguments.sourceFiles = files;
|
||||||
|
arguments.verbose = true;
|
||||||
|
compiler.exec(System.out, arguments);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public abstract class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected static final List<String> LIB_FILE_NAMES = Arrays.asList(
|
public static final List<String> LIB_FILE_NAMES = Arrays.asList(
|
||||||
"/core/annotations.kt",
|
"/core/annotations.kt",
|
||||||
"/jquery/common.kt",
|
"/jquery/common.kt",
|
||||||
"/jquery/ui.kt",
|
"/jquery/ui.kt",
|
||||||
@@ -67,11 +67,12 @@ public abstract class Config {
|
|||||||
"/html5/image.kt",
|
"/html5/image.kt",
|
||||||
"/stdlib/JUMaps.kt",
|
"/stdlib/JUMaps.kt",
|
||||||
"/stdlib/browser.kt",
|
"/stdlib/browser.kt",
|
||||||
"/core/dom.kt",
|
"/core/dom.kt"
|
||||||
"/core/dom/core.kt"
|
// TODO
|
||||||
|
// "/core/dom/core.kt"
|
||||||
);
|
);
|
||||||
|
|
||||||
protected static final String LIBRARIES_LOCATION = "js/js.libraries/src";
|
public static final String LIBRARIES_LOCATION = "js/js.libraries/src";
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Project project;
|
private final Project project;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import org.jetbrains.k2js.generate.CodeGenerator;
|
|||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -47,8 +48,15 @@ public final class K2JSTranslator {
|
|||||||
public static void translateWithCallToMainAndSaveToFile(@NotNull List<JetFile> files,
|
public static void translateWithCallToMainAndSaveToFile(@NotNull List<JetFile> files,
|
||||||
@NotNull String outputPath,
|
@NotNull String outputPath,
|
||||||
@NotNull Config config) throws Exception {
|
@NotNull Config config) throws Exception {
|
||||||
|
translateWithMainCallParametersAndSaveToFile(MainCallParameters.mainWithoutArguments(), files, outputPath, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void translateWithMainCallParametersAndSaveToFile(MainCallParameters mainCall,
|
||||||
|
List<JetFile> files,
|
||||||
|
String outputPath,
|
||||||
|
Config config) throws TranslationException, IOException {
|
||||||
K2JSTranslator translator = new K2JSTranslator(config);
|
K2JSTranslator translator = new K2JSTranslator(config);
|
||||||
String programCode = translator.generateProgramCode(files, MainCallParameters.mainWithoutArguments()) + "\n";
|
String programCode = translator.generateProgramCode(files, mainCall) + "\n";
|
||||||
writeCodeToFile(outputPath, programCode);
|
writeCodeToFile(outputPath, programCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-2
@@ -38,6 +38,14 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
|
|||||||
*/
|
*/
|
||||||
private String outFile;
|
private String outFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether verbose logging is enabled or not.
|
||||||
|
*
|
||||||
|
* @parameter default-value="false"
|
||||||
|
* @parameter expression="${verbose}"
|
||||||
|
*/
|
||||||
|
private Boolean verbose;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
|
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
|
||||||
super.configureCompilerArguments(arguments);
|
super.configureCompilerArguments(arguments);
|
||||||
@@ -45,9 +53,11 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
|
|||||||
if (arguments instanceof K2JSCompilerArguments) {
|
if (arguments instanceof K2JSCompilerArguments) {
|
||||||
K2JSCompilerArguments k2jsArgs = (K2JSCompilerArguments)arguments;
|
K2JSCompilerArguments k2jsArgs = (K2JSCompilerArguments)arguments;
|
||||||
k2jsArgs.outputFile = outFile;
|
k2jsArgs.outputFile = outFile;
|
||||||
|
if (verbose != null) {
|
||||||
|
k2jsArgs.verbose = verbose;
|
||||||
|
}
|
||||||
if (sources.size() > 0) {
|
if (sources.size() > 0) {
|
||||||
// TODO K2JSCompilerArguments should allow more than one path/file
|
k2jsArgs.sourceFiles = sources;
|
||||||
k2jsArgs.srcdir = sources.get(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile);
|
getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile);
|
||||||
|
|||||||
Reference in New Issue
Block a user