Ant task - CompileEnvironment is used + "buildToolsTest" verifies <java> output
This commit is contained in:
+2
-2
@@ -36,8 +36,8 @@ public class KotlinBytecodeCompilerTask extends Task {
|
||||
String src = getPath( this.srcdir != null ? this.srcdir : this.file );
|
||||
String dest = getPath( this.destdir );
|
||||
|
||||
log( String.format( "Kotlin bytecode compiler: [%s] => [%s]", src, dest ));
|
||||
KotlinBytecodeCompiler.src( src, dest );
|
||||
log( String.format( "[%s] => [%s]", src, dest ));
|
||||
KotlinBytecodeCompiler.compileSources( src, dest );
|
||||
}
|
||||
else {
|
||||
throw new RuntimeException( String.format( "Operation is not supported" ));
|
||||
|
||||
+57
-16
@@ -1,5 +1,13 @@
|
||||
<project name="build-tools" default="buildToolsJar">
|
||||
|
||||
<property name="tests-dir" location="${output}/build-tools-test"/>
|
||||
|
||||
<taskdef resource="net/sf/antcontrib/antlib.xml">
|
||||
<classpath>
|
||||
<pathelement location="${basedir}/build-tools/lib/ant-contrib-1.0b3.jar"/>
|
||||
</classpath>
|
||||
</taskdef>
|
||||
|
||||
<path id="sourcepath.buildTools">
|
||||
<dirset dir="${basedir}/build-tools">
|
||||
<include name="core/src"/>
|
||||
@@ -32,22 +40,55 @@
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
<macrodef name="hello-java">
|
||||
<attribute name="root"/>
|
||||
<attribute name="args" default=""/>
|
||||
<attribute name="out"/>
|
||||
<sequential>
|
||||
<var name = "java-out"
|
||||
unset = "true"/>
|
||||
<java classpathref = "dist.jars"
|
||||
classname = "namespace"
|
||||
failonerror = "true"
|
||||
outputproperty = "java-out">
|
||||
<classpath path = "${tests-dir}"/>
|
||||
<arg line = "@{args}"/>
|
||||
</java>
|
||||
<if>
|
||||
<equals arg1="${java-out}" arg2="@{out}"/>
|
||||
<then>
|
||||
<echo>${java-out}</echo>
|
||||
</then>
|
||||
<else>
|
||||
<fail message="[@{root}] 'Hello' test failed: '${java-out}' (received) != '@{out}' (expected)"/>
|
||||
</else>
|
||||
</if>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
<macrodef name="hello-test">
|
||||
<attribute name="root"/>
|
||||
<attribute name="args" default=""/>
|
||||
<attribute name="out"/>
|
||||
<sequential>
|
||||
<taskdef resource = "org/jetbrains/jet/buildtools/ant/antlib.xml"
|
||||
classpathref = "dist.jars"/>
|
||||
|
||||
<delete dir = "${tests-dir}" verbose = "false" includes="**/*.class"/>
|
||||
<kotlinc file = "@{root}/Hello.kt" destdir = "${tests-dir}"/>
|
||||
<hello-java root = "@{root}" args = "@{args}" out = "@{out}"/>
|
||||
|
||||
<delete dir = "${tests-dir}" verbose = "false" includes="**/*.class"/>
|
||||
<kotlinc srcdir = "@{root}" destdir = "${tests-dir}"/>
|
||||
<hello-java root = "@{root}" args = "@{args}" out = "@{out}"/>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
<target name="buildToolsTest" depends="jarRT, jar, buildToolsJar">
|
||||
<taskdef resource = "org/jetbrains/jet/buildtools/ant/tasks.xml"
|
||||
classpathref = "dist.jars"/>
|
||||
|
||||
<kotlinc file = "${basedir}/build-tools/test/Hello.kt"
|
||||
destdir = "${output}/build-tools-test"/>
|
||||
<java classpathref = "dist.jars"
|
||||
classname = "namespace">
|
||||
<classpath path = "${output}/build-tools-test"/>
|
||||
</java>
|
||||
|
||||
<kotlinc srcdir = "${basedir}/build-tools/test"
|
||||
destdir = "${output}/build-tools-test"/>
|
||||
<java classpathref = "dist.jars"
|
||||
classname = "namespace">
|
||||
<classpath path = "${output}/build-tools-test"/>
|
||||
</java>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/1" out="Hello, world!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/2" args="Kotlin-Developer" out="Hello, Kotlin-Developer!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/3" args="Mickey-Mouse" out="Hello, Mickey-Mouse!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/4" args="IT" out="Ciao!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/5" args="Donald-Duck" out="Hello, Donald-Duck!"/>
|
||||
</target>
|
||||
</project>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.jetbrains.jet.buildtools.core;
|
||||
|
||||
import org.jetbrains.jet.cli.KotlinCompiler;
|
||||
import org.jetbrains.jet.compiler.CompileEnvironment;
|
||||
|
||||
|
||||
/**
|
||||
@@ -8,13 +8,29 @@ import org.jetbrains.jet.cli.KotlinCompiler;
|
||||
*/
|
||||
public class KotlinBytecodeCompiler {
|
||||
|
||||
/**
|
||||
* Invokes Kotlin compiler with "-src" and "-output" options.
|
||||
* @param source "-src" option to specify
|
||||
* @param destination "-output" option to specify
|
||||
*/
|
||||
public static void src ( String source, String destination ) {
|
||||
KotlinCompiler.main( "-src", source, "-output", destination );
|
||||
|
||||
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 ));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,7 @@
|
||||
fun main(args : Array<String>) {
|
||||
if (args.size == 0) {
|
||||
System.out?.println("Please provide a name as a command-line argument")
|
||||
return
|
||||
}
|
||||
System.out?.println("Hello, ${args[0]}!")
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun main(args : Array<String>) {
|
||||
for (name in args)
|
||||
System.out?.println("Hello, $name!")
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun main(args : Array<String>) {
|
||||
val language = if (args.size == 0) "EN" else args[0]
|
||||
System.out?.println(when (language) {
|
||||
"EN" -> "Hello!"
|
||||
"FR" -> "Salut!"
|
||||
"IT" -> "Ciao!"
|
||||
else -> "Sorry, I can't greet you in $language yet"
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Greeter(val name : String) {
|
||||
fun greet() {
|
||||
System.out?.println("Hello, ${name}!");
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
Greeter(args[0]).greet()
|
||||
}
|
||||
Reference in New Issue
Block a user