minor refactor to make KotlinCompiler easier to extend (to make command line based compiler tools with configurable plugins)

This commit is contained in:
James Strachan
2012-03-08 08:05:47 +00:00
parent 44b2fec726
commit f43fd0f4f6
3 changed files with 30 additions and 26 deletions
@@ -37,9 +37,6 @@ public class KotlinCompiler {
} }
}; };
private KotlinCompiler() {
}
public static class Arguments { public static class Arguments {
@Argument(value = "output", description = "output directory") @Argument(value = "output", description = "output directory")
public String outputDir; public String outputDir;
@@ -84,7 +81,7 @@ public class KotlinCompiler {
public static void main(String... args) { public static void main(String... args) {
try { try {
int rc = exec(args); int rc = new KotlinCompiler().exec(args);
if (rc != 0) { if (rc != 0) {
System.err.println("exec() finished with " + rc + " return code"); System.err.println("exec() finished with " + rc + " return code");
System.exit(rc); System.exit(rc);
@@ -95,11 +92,11 @@ public class KotlinCompiler {
} }
} }
public static int exec(String... args) { public int exec(String... args) {
return exec(System.out, args); return exec(System.out, args);
} }
public static int exec(PrintStream errStream, String... args) { public int exec(PrintStream errStream, String... args) {
System.setProperty("java.awt.headless", "true"); System.setProperty("java.awt.headless", "true");
Arguments arguments = new Arguments(); Arguments arguments = new Arguments();
try { try {
@@ -126,22 +123,7 @@ public class KotlinCompiler {
MessageRenderer messageRenderer = arguments.tags ? MessageRenderer.TAGS : MessageRenderer.PLAIN; MessageRenderer messageRenderer = arguments.tags ? MessageRenderer.TAGS : MessageRenderer.PLAIN;
CompileEnvironment environment = new CompileEnvironment(fileNameTransformer, messageRenderer); CompileEnvironment environment = new CompileEnvironment(fileNameTransformer, messageRenderer);
try { try {
environment.setIgnoreErrors(false); configureEnvironment(environment, arguments, errStream);
environment.setErrorStream(errStream);
environment.setStubs(arguments.stubs);
if (arguments.docOutputDir != null) {
KDocLoader.install(arguments.docOutputDir, environment.getMyEnvironment());
}
if (arguments.stdlib != null) {
environment.setStdlib(arguments.stdlib);
}
if (arguments.classpath != null) {
environment.addToClasspath(arguments.classpath);
}
boolean noErrors; boolean noErrors;
if (arguments.module != null) { if (arguments.module != null) {
@@ -150,7 +132,6 @@ public class KotlinCompiler {
else { else {
noErrors = environment.compileBunchOfSources(arguments.src, arguments.jar, arguments.outputDir, arguments.includeRuntime); noErrors = environment.compileBunchOfSources(arguments.src, arguments.jar, arguments.outputDir, arguments.includeRuntime);
} }
return noErrors ? 0 : 1; return noErrors ? 0 : 1;
} }
catch (Throwable t) { catch (Throwable t) {
@@ -161,4 +142,27 @@ public class KotlinCompiler {
environment.dispose(); environment.dispose();
} }
} }
/**
* Strategy method to configure the environment, allowing compiler
* based tools to customise their own plugins
*/
protected void configureEnvironment(CompileEnvironment environment, Arguments arguments, PrintStream errStream) {
environment.setIgnoreErrors(false);
environment.setErrorStream(errStream);
environment.setStubs(arguments.stubs);
if (arguments.docOutputDir != null) {
KDocLoader.install(arguments.docOutputDir, environment.getMyEnvironment());
}
if (arguments.stdlib != null) {
environment.setStdlib(arguments.stdlib);
}
if (arguments.classpath != null) {
environment.addToClasspath(arguments.classpath);
}
}
} }
@@ -99,7 +99,7 @@ public class ForTestCompileStdlib {
private static void compileKotlinPartOfStdlib(File destdir) throws IOException { private static void compileKotlinPartOfStdlib(File destdir) throws IOException {
// lame // lame
KotlinCompiler.exec(System.err, "-output", destdir.getPath(), "-src", "./libraries/stdlib/src", "-stdlib", destdir.getAbsolutePath()); new KotlinCompiler().exec(System.err, "-output", destdir.getPath(), "-src", "./libraries/stdlib/src", "-stdlib", destdir.getAbsolutePath());
} }
private static List<File> javaFilesInDir(File dir) { private static List<File> javaFilesInDir(File dir) {
@@ -55,7 +55,7 @@ public class CompileEnvironmentTest extends TestCase {
try { try {
File stdlib = ForTestCompileStdlib.stdlibJarForTests(); File stdlib = ForTestCompileStdlib.stdlibJarForTests();
File resultJar = new File(tempDir, "result.jar"); File resultJar = new File(tempDir, "result.jar");
int rv = KotlinCompiler.exec("-module", JetParsingTest.getTestDataDir() + "/compiler/smoke/Smoke.kts", int rv = new KotlinCompiler().exec("-module", JetParsingTest.getTestDataDir() + "/compiler/smoke/Smoke.kts",
"-jar", resultJar.getAbsolutePath(), "-jar", resultJar.getAbsolutePath(),
"-stdlib", stdlib.getAbsolutePath()); "-stdlib", stdlib.getAbsolutePath());
Assert.assertEquals("compilation completed with non-zero code", 0, rv); Assert.assertEquals("compilation completed with non-zero code", 0, rv);
@@ -85,7 +85,7 @@ public class CompileEnvironmentTest extends TestCase {
try { try {
File out = new File(tempDir, "out"); File out = new File(tempDir, "out");
File stdlib = ForTestCompileStdlib.stdlibJarForTests(); File stdlib = ForTestCompileStdlib.stdlibJarForTests();
KotlinCompiler.exec("-src", JetParsingTest.getTestDataDir() + "/compiler/smoke/Smoke.kt", new KotlinCompiler().exec("-src", JetParsingTest.getTestDataDir() + "/compiler/smoke/Smoke.kt",
"-output", out.getAbsolutePath(), "-output", out.getAbsolutePath(),
"-stdlib", stdlib.getAbsolutePath()); "-stdlib", stdlib.getAbsolutePath());