Ant task - class names made shorter; destjar/includeRuntime/excludeStdlib supported; no static references;

This commit is contained in:
Evgeny Goldin
2012-01-17 23:39:42 +02:00
parent 157353e2cd
commit f34c285fef
7 changed files with 139 additions and 87 deletions
@@ -0,0 +1,63 @@
package org.jetbrains.jet.buildtools.ant;
import static org.jetbrains.jet.buildtools.core.Util.*;
import org.apache.tools.ant.Task;
import org.jetbrains.jet.buildtools.core.BytecodeCompiler;
import java.io.File;
/**
* Kotlin bytecode compiler Ant task.
*
* See
* http://evgeny-goldin.org/javadoc/ant/tutorial-writing-tasks.html
* http://evgeny-goldin.org/javadoc/ant/Tasks/javac.html - attribute names should be similar to {@code <javac>}.
*/
public class BytecodeCompilerTask extends Task {
private final BytecodeCompiler compiler = new BytecodeCompiler();
private File srcdir;
private File file;
private File destdir;
private File destjar;
private boolean includeRuntime = true;
private boolean excludeStdlib = false;
public void setSrcdir ( File srcdir ) { this.srcdir = srcdir; }
public void setFile ( File file ) { this.file = file; }
public void setDestdir ( File destdir ) { this.destdir = destdir; }
public void setDestjar ( File destjar ) { this.destjar = destjar; }
public void setIncludeRuntime ( boolean includeRuntime ) { this.includeRuntime = includeRuntime; }
public void setExcludeStdlib ( boolean excludeStdlib ) { this.excludeStdlib = excludeStdlib; }
@Override
public void execute() {
if (( this.srcdir != null ) || ( this.file != null )) {
if (( this.destdir == null ) && ( this.destjar == null )) {
throw new RuntimeException( "\"destdir\" or \"destjar\" should be specified" );
}
String src = getPath( this.srcdir != null ? this.srcdir : this.file );
String dest = getPath( this.destdir != null ? this.destdir : this.destjar );
log( String.format( "[%s] => [%s]", src, dest ));
if ( this.destdir != null ) {
compiler.sourcesToDir( src, dest, this.includeRuntime, this.excludeStdlib );
}
else {
compiler.sourcesToJar( src, dest, this.includeRuntime, this.excludeStdlib );
}
}
else {
throw new RuntimeException( "Operation is not supported yet" );
}
}
}
@@ -7,10 +7,10 @@ import org.apache.tools.ant.Task;
* Kotlin JavaScript compiler Ant task.
* http://evgeny-goldin.org/javadoc/ant/tutorial-writing-tasks.html
*/
public class KotlinJavaScriptCompilerTask extends Task {
public class JavaScriptCompilerTask extends Task {
@Override
public void execute() {
log( "KotlinJavaScriptCompilerTask" );
log( "JavaScriptCompilerTask" );
}
}
@@ -1,46 +0,0 @@
package org.jetbrains.jet.buildtools.ant;
import static org.jetbrains.jet.buildtools.core.Util.*;
import org.apache.tools.ant.Task;
import org.jetbrains.jet.buildtools.core.KotlinBytecodeCompiler;
import java.io.File;
/**
* Kotlin bytecode compiler Ant task.
*
* See
* http://evgeny-goldin.org/javadoc/ant/tutorial-writing-tasks.html
* http://evgeny-goldin.org/javadoc/ant/Tasks/javac.html - attribute names should be similar to {@code <javac>}.
*/
public class KotlinBytecodeCompilerTask extends Task {
private File srcdir;
private File file;
private File destdir;
public void setSrcdir ( File srcdir ) { this.srcdir = srcdir; }
public void setFile ( File file ) { this.file = file; }
public void setDestdir ( File destdir ) { this.destdir = destdir; }
@Override
public void execute() {
if ( this.destdir == null ) {
throw new RuntimeException( "\"destdir\" attribute is not specified" );
}
if (( this.srcdir != null ) || ( this.file != null )) {
String src = getPath( this.srcdir != null ? this.srcdir : this.file );
String dest = getPath( this.destdir );
log( String.format( "[%s] => [%s]", src, dest ));
KotlinBytecodeCompiler.compileSources( src, dest );
}
else {
throw new RuntimeException( String.format( "Operation is not supported" ));
}
}
}
@@ -5,8 +5,8 @@
<antlib>
<taskdef
name = "kotlinc"
classname = "org.jetbrains.jet.buildtools.ant.KotlinBytecodeCompilerTask"/>
classname = "org.jetbrains.jet.buildtools.ant.BytecodeCompilerTask"/>
<taskdef
name = "kotlinJSc"
classname = "org.jetbrains.jet.buildtools.ant.KotlinJavaScriptCompilerTask"/>
classname = "org.jetbrains.jet.buildtools.ant.JavaScriptCompilerTask"/>
</antlib>
@@ -0,0 +1,71 @@
package org.jetbrains.jet.buildtools.core;
import org.jetbrains.jet.compiler.CompileEnvironment;
/**
* Wrapper class for Kotlin bytecode compiler.
*/
public class BytecodeCompiler {
private final CompileEnvironment ENV = createCompileEnvironment();
public BytecodeCompiler () {
}
/**
* Creates and initializes new {@link CompileEnvironment} instance.
* @return new {@link CompileEnvironment} instance
*/
private CompileEnvironment createCompileEnvironment () {
CompileEnvironment environment = new CompileEnvironment();
environment.setJavaRuntime( CompileEnvironment.findRtJar( true ));
if ( ! environment.initializeKotlinRuntime()) {
throw new RuntimeException( "No Kotlin runtime library found" );
}
return environment;
}
/**
* Retrieves compilation error message.
* @param source compilation source
* @return compilation error message
*/
private String compilationError ( String source ) {
return String.format( "[%s] compilation failed, see \"ERROR:\" messages above for more details.", source );
}
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source (directory or file)
* @param destination compilation destination
*/
public void sourcesToDir ( String source, String destination, boolean includeRuntime, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, null, destination, includeRuntime, ! excludeStdlib );
if ( ! success ) {
throw new RuntimeException( compilationError( source ));
}
}
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source (directory or file)
* @param jar compilation destination jar
*/
public void sourcesToJar ( String source, String jar, boolean includeRuntime, boolean excludeStdlib ) {
boolean success = ENV.compileBunchOfSources( source, jar, null, includeRuntime, ! excludeStdlib );
if ( ! success ) {
throw new RuntimeException( compilationError( source ));
}
}
}
@@ -4,5 +4,5 @@ package org.jetbrains.jet.buildtools.core;
/**
* Wrapper class for Kotlin JavaScript compiler.
*/
public class KotlinJavaScriptCompiler {
public class JavaScriptCompiler {
}
@@ -1,36 +0,0 @@
package org.jetbrains.jet.buildtools.core;
import org.jetbrains.jet.compiler.CompileEnvironment;
/**
* Wrapper class for Kotlin bytecode compiler.
*/
public class KotlinBytecodeCompiler {
private static CompileEnvironment environment() {
CompileEnvironment environment = new CompileEnvironment();
environment.setJavaRuntime(CompileEnvironment.findRtJar( true ));
if ( ! environment.initializeKotlinRuntime()) {
throw new RuntimeException( "No Kotlin runtime library found" );
}
return environment;
}
/**
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
*
* @param source compilation source
* @param destination compilation destination
*/
public static void compileSources ( String source, String destination ) {
boolean success = environment().compileBunchOfSources( source, null, destination, true, false );
if ( ! success ) {
throw new RuntimeException( String.format( "[%s] compilation failed, see \"ERROR:\" messages above for more details.", source ));
}
}
}