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
@@ -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
*/