K2JS: add ed outputPrefix and outputPostfix compiler arguments to K2JSCompiler.

This commit is contained in:
Zalim Bashorov
2013-10-18 19:21:59 +04:00
parent d02e53c812
commit 0d322fe8bc
20 changed files with 352 additions and 14 deletions
@@ -45,4 +45,10 @@ public class K2JSCompilerArguments extends CommonCompilerArguments {
@Argument(value = "main", description = "Whether a main function should be called; either '" + CALL +
"' or '" + NO_CALL + "', default '" + CALL + "' (main function will be auto detected)")
public String main;
@Argument(value = "outputPrefix", description = "Path to file which will be added to the begin of output file")
public String outputPrefix;
@Argument(value = "outputPostfix", description = "Path to file which will be added to the end of output file")
public String outputPostfix;
}
@@ -104,8 +104,30 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
return COMPILATION_ERROR;
}
File outputPrefixFile = null;
if (arguments.outputPrefix != null) {
outputPrefixFile = new File(arguments.outputPrefix);
if (!outputPrefixFile.exists()) {
messageCollector.report(CompilerMessageSeverity.ERROR,
"Output prefix file '" + arguments.outputPrefix + "' not found",
CompilerMessageLocation.NO_LOCATION);
return ExitCode.COMPILATION_ERROR;
}
}
File outputPostfixFile = null;
if (arguments.outputPostfix != null) {
outputPostfixFile = new File(arguments.outputPostfix);
if (!outputPostfixFile.exists()) {
messageCollector.report(CompilerMessageSeverity.ERROR,
"Output postfix file '" + arguments.outputPostfix + "' not found",
CompilerMessageLocation.NO_LOCATION);
return ExitCode.COMPILATION_ERROR;
}
}
MainCallParameters mainCallParameters = createMainCallParameters(arguments.main);
return translateAndGenerateOutputFile(mainCallParameters, environmentForJS, config, outputFile);
return translateAndGenerateOutputFile(mainCallParameters, environmentForJS, config, outputFile, outputPrefixFile, outputPostfixFile);
}
private static void reportCompiledSourcesList(@NotNull MessageCollector messageCollector,
@@ -131,10 +153,13 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
@NotNull MainCallParameters mainCall,
@NotNull JetCoreEnvironment environmentForJS,
@NotNull Config config,
@NotNull String outputFile
@NotNull String outputFile,
@Nullable File outputPrefix,
@Nullable File outputPostfix
) {
try {
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCall, environmentForJS.getSourceFiles(), outputFile, config);
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCall, environmentForJS.getSourceFiles(),
outputFile, outputPrefix, outputPostfix, config);
}
catch (Exception e) {
throw new RuntimeException(e);
@@ -0,0 +1,2 @@
ERROR: Output postfix file 'not/existing/path' not found
COMPILATION_ERROR
@@ -0,0 +1,2 @@
ERROR: Output prefix file 'not/existing/path' not found
COMPILATION_ERROR
@@ -31,6 +31,7 @@ import java.io.File;
import java.io.PrintStream;
public class CliBaseTest {
protected static final String NOT_EXISTING_PATH = "not/existing/path";
@Rule
public final Tmpdir tmpdir = new Tmpdir();
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2013 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.jvm;
import com.google.common.collect.Lists;
import com.intellij.util.ArrayUtil;
import junit.framework.Assert;
import org.junit.Test;
import java.io.File;
import java.util.Collections;
import java.util.List;
public class K2JsCliTest extends CliBaseTest {
@Test
public void simple() {
doSimpleTest(/*expectedOutFile =*/ true);
}
@Test
public void outputPrefixFileNotFound() {
doSimpleTest(/*expectedOutFile =*/ false,
"-outputPrefix", NOT_EXISTING_PATH);
}
@Test
public void outputPostfixFileNotFound() {
doSimpleTest(/*expectedOutFile =*/ false,
"-outputPostfix", NOT_EXISTING_PATH);
}
private void doSimpleTest(boolean expectedOutFile, String... additionalArgs) {
File outputFile = new File(tmpdir.getTmpDir(), "out.js");
List<String> args = Lists.newArrayList(
"-sourceFiles", "compiler/testData/cli/simple2js.kt",
"-output", outputFile.getPath());
Collections.addAll(args, additionalArgs);
executeCompilerCompareOutputJS(ArrayUtil.toStringArray(args));
Assert.assertEquals(expectedOutFile, outputFile.isFile());
}
}
@@ -24,6 +24,9 @@ import org.junit.Test;
import java.io.File;
public class K2JvmCliTest extends CliBaseTest {
protected static final String ANOTHER_NOT_EXISTING_PATH = "yet/another/not/existing/path";
@Test
public void wrongKotlinSignature() throws Exception {
String[] args = {
@@ -46,8 +49,8 @@ public class K2JvmCliTest extends CliBaseTest {
public void nonExistingClassPathAndAnnotationsPath() {
String[] args = {
"-src", "compiler/testData/cli/simple.kt",
"-classpath", "not/existing/path",
"-annotations", "yet/another/not/existing/path",
"-classpath", NOT_EXISTING_PATH,
"-annotations", ANOTHER_NOT_EXISTING_PATH,
"-output", tmpdir.getTmpDir().getPath()};
executeCompilerCompareOutputJVM(args);
@@ -57,7 +60,7 @@ public class K2JvmCliTest extends CliBaseTest {
@Test
public void nonExistingSourcePath() {
String[] args = {
"-src", "not/existing/path",
"-src", NOT_EXISTING_PATH,
"-output", tmpdir.getTmpDir().getPath()};
executeCompilerCompareOutputJVM(args);
}
@@ -0,0 +1,118 @@
/*
* Copyright 2010-2013 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.test;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import junit.framework.Test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.config.TestConfigFactory;
import org.jetbrains.k2js.test.semantics.TranslatorTestCaseBuilder;
import java.io.File;
import java.util.List;
import static org.jetbrains.k2js.test.utils.TranslationUtils.createJetFileList;
import static org.jetbrains.k2js.test.utils.TranslationUtils.getConfig;
@SuppressWarnings("JUnitTestCaseWithNoTests")
public final class OutputPrefixPostfixTest extends SingleFileTranslationTest {
private static final String PREFIX_EXT = ".prefix";
private static final String POSTFIX_EXT = ".postfix";
@NotNull
private final String filename;
private final File outputPrefixFile;
private final File outputPostfixFile;
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public OutputPrefixPostfixTest(@NotNull String filename) {
super("outputPrefixPostfix/");
this.filename = filename;
String inputFilePath = getInputFilePath(filename);
this.outputPrefixFile = newFileIfExists(inputFilePath + PREFIX_EXT);
this.outputPostfixFile = newFileIfExists(inputFilePath + POSTFIX_EXT);
}
@Override
protected void translateFiles(
@NotNull Project project,
@NotNull List<String> files,
@NotNull String outputFile,
@NotNull MainCallParameters mainCallParameters,
@NotNull EcmaVersion version,
@NotNull TestConfigFactory configFactory
) throws Exception {
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCallParameters, createJetFileList(project, files, null),
outputFile, outputPrefixFile, outputPostfixFile,
getConfig(project, version, configFactory));
}
@Override
public void runTest() throws Exception {
checkFooBoxIsOk(filename);
}
@Override
protected void runFunctionOutputTest(
@NotNull Iterable<EcmaVersion> ecmaVersions,
@NotNull String kotlinFilename,
@NotNull String namespaceName,
@NotNull String functionName,
@NotNull Object expectedResult
) throws Exception {
super.runFunctionOutputTest(ecmaVersions, kotlinFilename, namespaceName, functionName, expectedResult);
for (EcmaVersion ecmaVersion : ecmaVersions) {
String output = FileUtil.loadFile(new File(getOutputFilePath(filename, ecmaVersion)));
if (outputPrefixFile != null) {
assertTrue(output.startsWith(FileUtil.loadFile(outputPrefixFile)));
}
if (outputPostfixFile != null) {
assertTrue(output.endsWith(FileUtil.loadFile(outputPostfixFile)));
}
}
}
public static Test suite() throws Exception {
return TranslatorTestCaseBuilder
.suiteForDirectory(BasicTest.pathToTestFilesRoot() + "outputPrefixPostfix/cases/", new TranslatorTestCaseBuilder.NamedTestFactory() {
@NotNull
@Override
public Test createTest(@NotNull String filename) {
OutputPrefixPostfixTest examplesTest = new OutputPrefixPostfixTest(filename);
examplesTest.setName(filename);
return examplesTest;
}
});
}
@Nullable
private static File newFileIfExists(@NotNull String path) {
File file = new File(path);
if (!file.exists()) {
file = null;
}
return file;
}
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.jetbrains.k2js.test.semantics;
package org.jetbrains.k2js.test;
import com.intellij.openapi.project.Project;
import junit.framework.Test;
@@ -22,10 +22,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.BasicTest;
import org.jetbrains.k2js.test.SingleFileTranslationTest;
import org.jetbrains.k2js.test.config.TestConfig;
import org.jetbrains.k2js.test.config.TestConfigFactory;
import org.jetbrains.k2js.test.semantics.TranslatorTestCaseBuilder;
import java.util.List;
@@ -53,7 +52,8 @@ public final class SourcemapTest extends SingleFileTranslationTest {
@NotNull EcmaVersion version,
@NotNull TestConfigFactory configFactory
) throws Exception {
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCallParameters, createJetFileList(project, files, null), outputFile,
K2JSTranslator.translateWithMainCallParametersAndSaveToFile(mainCallParameters, createJetFileList(project, files, null),
outputFile, null, null,
getConfig(project, version, configFactory));
}
@@ -2,6 +2,7 @@ package org.jetbrains.js.compiler.sourcemap;
import com.google.dart.compiler.common.SourceInfo;
import com.google.dart.compiler.util.TextOutput;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.PairConsumer;
import gnu.trove.TObjectIntHashMap;
@@ -77,6 +78,11 @@ public class SourceMap3Builder implements SourceMapBuilder {
previousGeneratedColumn = -1;
}
@Override
public void skipLinesInBegin(int count) {
out.insert(0, StringUtil.repeatSymbol(';', count));
}
@Override
public void processSourceInfo(Object sourceInfo) {
if (sourceInfo instanceof SourceInfo) {
@@ -132,7 +138,7 @@ public class SourceMap3Builder implements SourceMapBuilder {
public void addLink() {
textOutput.print("\n//@ sourceMappingURL=");
textOutput.print(generatedFile.getName());
textOutput.print(".map");
textOutput.print(".map\n");
}
private static final class Base64VLQ {
@@ -21,6 +21,8 @@ import java.io.File;
public interface SourceMapBuilder {
void newLine();
void skipLinesInBegin(int count);
void addMapping(String source, int sourceLine, int sourceColumn);
void processSourceInfo(Object info);
@@ -20,6 +20,7 @@ import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.google.dart.compiler.util.TextOutputImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -48,16 +49,35 @@ public final class K2JSTranslator {
public static final String FLUSH_SYSTEM_OUT = "Kotlin.System.flush();\n";
public static final String GET_SYSTEM_OUT = "Kotlin.System.output();\n";
public static void translateWithMainCallParametersAndSaveToFile(@NotNull MainCallParameters mainCall,
public static void translateWithMainCallParametersAndSaveToFile(
@NotNull MainCallParameters mainCall,
@NotNull List<JetFile> files,
@NotNull String outputPath,
@NotNull Config config) throws TranslationException, IOException {
@Nullable File outputPrefixFile,
@Nullable File outputPostfixFile,
@NotNull Config config
) throws TranslationException, IOException {
K2JSTranslator translator = new K2JSTranslator(config);
File outFile = new File(outputPath);
TextOutputImpl output = new TextOutputImpl();
SourceMapBuilder sourceMapBuilder = config.isSourcemap() ? new SourceMap3Builder(outFile, output, new SourceMapBuilderConsumer()) : null;
String programCode = translator.generateProgramCode(files, mainCall, output, sourceMapBuilder);
FileUtil.writeToFile(outFile, programCode);
if (outputPrefixFile != null) {
String prefix = FileUtil.loadFile(outputPrefixFile);
FileUtil.writeToFile(outFile, prefix);
if (sourceMapBuilder != null) {
sourceMapBuilder.skipLinesInBegin(StringUtil.getLineBreakCount(prefix));
}
}
FileUtil.writeToFile(outFile, programCode.getBytes(), outputPrefixFile != null);
if (outputPostfixFile != null) {
byte[] postfix = FileUtil.loadFileBytes(outputPostfixFile);
FileUtil.writeToFile(outFile, postfix, true);
}
if (sourceMapBuilder != null) {
FileUtil.writeToFile(sourceMapBuilder.getOutFile(), sourceMapBuilder.build());
}
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2013 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 foo
fun box(): String {
return "OK"
}
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2013 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 foo
fun box(): String {
return "OK"
}
@@ -0,0 +1,3 @@
/*
SOME POSTFIX
*/
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2013 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 foo
fun box(): String {
return "OK"
}
@@ -0,0 +1,3 @@
/*
SOME PREFIX
*/
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2013 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 foo
fun box(): String {
return "OK"
}
@@ -0,0 +1,3 @@
/*
SOME POSTFIX
*/
@@ -0,0 +1,3 @@
/*
SOME PREFIX
*/