A command-line argument to transform file names to *.java

This commit is contained in:
Andrey Breslav
2012-01-30 22:01:27 +04:00
parent 73100134f9
commit 5595e55ab9
10 changed files with 67 additions and 11 deletions
@@ -2,8 +2,10 @@ package org.jetbrains.jet.cli;
import com.sampullara.cli.Args;
import com.sampullara.cli.Argument;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.CompileEnvironmentException;
import org.jetbrains.jet.compiler.FileNameTransformer;
import java.io.PrintStream;
@@ -13,6 +15,14 @@ import java.io.PrintStream;
*/
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public class KotlinCompiler {
private static final FileNameTransformer ANY_EXTENSION_TO_JAVA = new FileNameTransformer() {
@NotNull
@Override
public String transformFileName(@NotNull String fileName) {
return fileName.replaceFirst("\\.\\w+$", ".java");
}
};
private KotlinCompiler() {
}
@@ -40,6 +50,9 @@ public class KotlinCompiler {
@Argument(value = "ignoreErrors", description = "Emit byte code even if there are compilation errors (not recommended)")
public boolean ignoreErrors;
@Argument(value = "transformNamesToJava", description = "Transform Kotlin file names to *.java. This option is needed for compiling kotlinized Java library headers")
public boolean transformNamesToJava;
}
private static void usage(PrintStream target) {
@@ -79,7 +92,7 @@ public class KotlinCompiler {
return 0;
}
CompileEnvironment environment = new CompileEnvironment();
CompileEnvironment environment = new CompileEnvironment(arguments.transformNamesToJava ? ANY_EXTENSION_TO_JAVA : FileNameTransformer.IDENTITY);
environment.setIgnoreErrors(arguments.ignoreErrors);
if (arguments.stdlib != null) {
@@ -35,17 +35,23 @@ public class CompileEnvironment {
private JetCoreEnvironment myEnvironment;
private final Disposable myRootDisposable;
private PrintStream myErrorStream = System.out;
private final FileNameTransformer myFileNameTransformer;
private URL myStdlib;
private boolean ignoreErrors = false;
public CompileEnvironment() {
this(FileNameTransformer.IDENTITY);
}
public CompileEnvironment(FileNameTransformer fileNameTransformer) {
myRootDisposable = new Disposable() {
@Override
public void dispose() {
}
};
myEnvironment = new JetCoreEnvironment(myRootDisposable);
myFileNameTransformer = fileNameTransformer;
}
public void setErrorStream(PrintStream errorStream) {
@@ -148,7 +154,7 @@ public class CompileEnvironment {
}
public List<Module> loadModuleScript(String moduleFile) {
CompileSession scriptCompileSession = new CompileSession(myEnvironment);
CompileSession scriptCompileSession = new CompileSession(myEnvironment, myFileNameTransformer);
scriptCompileSession.addSources(moduleFile);
ensureRuntime();
@@ -289,7 +295,7 @@ public class CompileEnvironment {
}
public boolean compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) {
CompileSession session = new CompileSession(myEnvironment);
CompileSession session = new CompileSession(myEnvironment, myFileNameTransformer);
session.addSources(sourceFileOrDir);
String mainClass = null;
@@ -29,6 +29,7 @@ public class CompileSession {
private final JetCoreEnvironment myEnvironment;
private final List<JetFile> mySourceFiles = new ArrayList<JetFile>();
private final List<JetFile> myLibrarySourceFiles = new ArrayList<JetFile>();
private final FileNameTransformer myFileNameTransformer;
private List<String> myErrors = new ArrayList<String>();
public BindingContext getMyBindingContext() {
@@ -38,7 +39,12 @@ public class CompileSession {
private BindingContext myBindingContext;
public CompileSession(JetCoreEnvironment environment) {
this(environment, FileNameTransformer.IDENTITY);
}
public CompileSession(JetCoreEnvironment environment, FileNameTransformer fileNameTransformer) {
myEnvironment = environment;
myFileNameTransformer = fileNameTransformer;
}
public void addSources(String path) {
@@ -115,13 +121,13 @@ public class CompileSession {
@NotNull
public ClassFileFactory generate() {
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.BINARIES);
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.BINARIES, myFileNameTransformer);
generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
return generationState.getFactory();
}
public String generateText() {
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.TEXT);
GenerationState generationState = new GenerationState(myEnvironment.getProject(), ClassBuilderFactory.TEXT, myFileNameTransformer);
generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
return generationState.createText();
}