Merge pull request #233 from nskvortsov/master

Kotlin Gradle plugin improvements
This commit is contained in:
Leonid Shalupov
2013-03-28 09:57:00 -07:00
9 changed files with 97 additions and 35 deletions
@@ -10,4 +10,9 @@
<item name='org.gradle.api.Project org.gradle.api.plugins.Convention getConvention()'> <item name='org.gradle.api.Project org.gradle.api.plugins.Convention getConvention()'>
<annotation name='org.jetbrains.annotations.NotNull'/> <annotation name='org.jetbrains.annotations.NotNull'/>
</item> </item>
<item name='org.gradle.api.Project org.gradle.api.initialization.dsl.ScriptHandler getBuildscript()'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun getBuildscript() : ScriptHandler&quot;"/>
</annotation>
</item>
</root> </root>
@@ -0,0 +1,7 @@
<root>
<item name='org.gradle.api.initialization.dsl.ScriptHandler org.gradle.api.artifacts.ConfigurationContainer getConfigurations()'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun getConfigurations() : ConfigurationContainer&quot;"/>
</annotation>
</item>
</root>
@@ -7,4 +7,9 @@
<item name='org.gradle.api.internal.AbstractTask org.gradle.api.logging.Logger getLogger()'> <item name='org.gradle.api.internal.AbstractTask org.gradle.api.logging.Logger getLogger()'>
<annotation name='org.jetbrains.annotations.NotNull'/> <annotation name='org.jetbrains.annotations.NotNull'/>
</item> </item>
<item name='org.gradle.api.internal.AbstractTask org.gradle.api.Project getProject()'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun getProject() : Project&quot;"/>
</annotation>
</item>
</root> </root>
@@ -26,6 +26,11 @@
<version>4.11</version> <version>4.11</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
@@ -47,6 +52,11 @@
<artifactId>kotlin-compiler</artifactId> <artifactId>kotlin-compiler</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-jdk-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.jetbrains.kotlin</groupId> <groupId>org.jetbrains.kotlin</groupId>
<artifactId>kdoc</artifactId> <artifactId>kdoc</artifactId>
@@ -71,6 +81,9 @@
<includes><include>kotlin-jdk-annotations.jar</include></includes> <includes><include>kotlin-jdk-annotations.jar</include></includes>
<filtering>false</filtering> <filtering>false</filtering>
</resource> </resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources> </resources>
<plugins> <plugins>
@@ -30,12 +30,21 @@ import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging import org.gradle.api.logging.Logging
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import java.io.IOException import java.io.IOException
import org.apache.commons.lang.StringUtils
import org.gradle.api.initialization.dsl.ScriptHandler
public open class KotlinCompile(): AbstractCompile() { public open class KotlinCompile(): AbstractCompile() {
val srcDirsRoots = HashSet<File>() val srcDirsRoots = HashSet<File>()
val compiler = K2JVMCompiler() val compiler = K2JVMCompiler()
val logger = getLogger() val logger = Logging.getLogger(getClass())
public val kotlinOptions: K2JVMCompilerArguments = K2JVMCompilerArguments();
{
kotlinOptions.noStdlib = true
kotlinOptions.noJdkAnnotations = true
}
// override setSource to track source directory sets // override setSource to track source directory sets
override fun setSource(source: Any?) { override fun setSource(source: Any?) {
@@ -70,7 +79,7 @@ public open class KotlinCompile(): AbstractCompile() {
override fun compile() { override fun compile() {
val args = K2JVMCompilerArguments() val args = kotlinOptions
val javaSrcRoots = HashSet<File>() val javaSrcRoots = HashSet<File>()
val sources = ArrayList<File>() val sources = ArrayList<File>()
@@ -92,31 +101,25 @@ public open class KotlinCompile(): AbstractCompile() {
return return
} }
args.setSourceDirs(sources.map { it.getAbsolutePath() }) val customSources = args.getSourceDirs();
if (customSources == null || customSources.isEmpty()) {
args.setSourceDirs(sources.map { it.getAbsolutePath() })
}
val gradleClasspath = getClasspath()
val effectiveClassPath = (javaSrcRoots + gradleClasspath).makeString(File.pathSeparator)
args.setClasspath(effectiveClassPath) if (StringUtils.isEmpty(kotlinOptions.classpath)) {
val existingClasspathEntries = getClasspath().filter(KSpec<File?>({ it != null && it.exists() }))
val effectiveClassPath = (javaSrcRoots + existingClasspathEntries).makeString(File.pathSeparator)
args.setClasspath(effectiveClassPath)
}
val embeddedAnnotations = getAnnotations() val embeddedAnnotations = getAnnotations()
args.outputDir =if (StringUtils.isEmpty(kotlinOptions.outputDir)) { getDestinationDir()?.getPath() } else { kotlinOptions.outputDir }
args.annotations = if (StringUtils.isEmpty(kotlinOptions.annotations)) { embeddedAnnotations.getPath() } else { kotlinOptions.annotations }
args.outputDir = getDestinationDir()?.getPath()
args.noJdkAnnotations = true
args.annotations = embeddedAnnotations.getPath()
args.noStdlib = true
val logger = Logging.getLogger(getClass())
val messageCollector = GradleMessageCollector(logger) val messageCollector = GradleMessageCollector(logger)
val exitCode = compiler.exec(messageCollector, args) val exitCode = compiler.exec(messageCollector, args)
val annotationsDir = embeddedAnnotations.getParentFile()
try {
FileUtils.deleteDirectory(annotationsDir);
} catch (e : IOException) {
logger.warn("Could not delete temporary annotations directory " + annotationsDir, e)
}
when (exitCode) { when (exitCode) {
ExitCode.COMPILATION_ERROR -> throw GradleException("Compilation error. See log for more details") ExitCode.COMPILATION_ERROR -> throw GradleException("Compilation error. See log for more details")
ExitCode.INTERNAL_ERROR -> throw GradleException("Internal compiler error. See log for more details") ExitCode.INTERNAL_ERROR -> throw GradleException("Internal compiler error. See log for more details")
@@ -125,20 +128,26 @@ public open class KotlinCompile(): AbstractCompile() {
} }
fun getAnnotations(): File { fun getAnnotations(): File {
val jdkAnnotations: String = "kotlin-jdk-annotations.jar" val configuration = getProject()
val jdkAnnotationsResource = Resources.getResource(jdkAnnotations) ?: .getBuildscript()
throw GradleException(jdkAnnotations + " not found in Kotlin gradle plugin classpath") .getConfigurations()
.getByName(ScriptHandler.CLASSPATH_CONFIGURATION)!!
val jdkAnnotationsTempDir = Files.createTempDir() val jdkAnnotationsFromClasspath = configuration.find { it.name.startsWith("kotlin-jdk-annotations") }
val jdkAnnotationsFile = File(jdkAnnotationsTempDir, jdkAnnotations)
Files.copy(Resources.newInputStreamSupplier(jdkAnnotationsResource), jdkAnnotationsFile) if (jdkAnnotationsFromClasspath != null) {
return jdkAnnotationsFile logger.info("using jdk annontations from [${jdkAnnotationsFromClasspath.getAbsolutePath()}]")
return jdkAnnotationsFromClasspath
} else {
throw GradleException("kotlin-jdk-annotations not found in Kotlin gradle plugin classpath")
}
} }
} }
public open class KDoc(): SourceTask() { public open class KDoc(): SourceTask() {
val logger = Logging.getLogger(getClass())
/** /**
* Returns the directory to use to output the API docs * Returns the directory to use to output the API docs
*/ */
@@ -210,7 +219,6 @@ public open class KDoc(): SourceTask() {
val compiler = KDocCompiler() val compiler = KDocCompiler()
val logger = Logging.getLogger(getClass())
val messageCollector = GradleMessageCollector(logger) val messageCollector = GradleMessageCollector(logger)
val exitCode = compiler.exec(messageCollector, args); val exitCode = compiler.exec(messageCollector, args);
@@ -231,10 +239,10 @@ class GradleMessageCollector(val logger : Logger): MessageCollector {
val text = position + message val text = position + message
when (severity) { when (severity) {
in CompilerMessageSeverity.VERBOSE -> logger.debug(text) in CompilerMessageSeverity.VERBOSE -> logger.debug(text)
in CompilerMessageSeverity.ERRORS -> logger.error(text) in CompilerMessageSeverity.ERRORS -> logger.error(text)
CompilerMessageSeverity.INFO -> logger.info(text) CompilerMessageSeverity.INFO -> logger.info(text)
else -> logger.warn(text) else -> logger.warn(text)
} }
} }
} }
@@ -0,0 +1 @@
implementation-class=org.jetbrains.kotlin.gradle.plugin.KotlinPlugin
@@ -1,4 +1,3 @@
import org.jetbrains.kotlin.gradle.plugin.*
buildscript { buildscript {
repositories { repositories {
mavenCentral() mavenCentral()
@@ -11,7 +10,7 @@ buildscript {
} }
} }
apply plugin: KotlinPlugin apply plugin: "kotlin"
sourceSets { sourceSets {
deploy deploy
@@ -32,6 +31,12 @@ task show << {
buildscript.configurations.classpath.each { println it } buildscript.configurations.classpath.each { println it }
} }
compileKotlin {
kotlinOptions.annotations = "externalAnnotations"
}
task wrapper(type: Wrapper) { task wrapper(type: Wrapper) {
gradleVersion="1.4" gradleVersion="1.4"
} }
@@ -0,0 +1,18 @@
<root>
<item name='com.google.common.base.Joiner com.google.common.base.Joiner on(java.lang.String)'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun on(separator : String?) : Joiner&quot;"/>
</annotation>
</item>
<item name='com.google.common.base.Joiner com.google.common.base.Joiner.MapJoiner withKeyValueSeparator(java.lang.String)'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value"
val="&quot;fun withKeyValueSeparator(keyValueSeparator : String?) : Joiner.MapJoiner?&quot;"/>
</annotation>
</item>
<item name='com.google.common.base.Joiner com.google.common.base.Joiner skipNulls()'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun skipNulls() : Joiner&quot;"/>
</annotation>
</item>
</root>
@@ -13,8 +13,8 @@ class KotlinGreetingJoiner(val greeter : Greeter) {
} }
fun getJoinedGreeting() : String? { fun getJoinedGreeting() : String? {
val joiner = Joiner.on(" and ")?.skipNulls(); val joiner = Joiner.on(" and ").skipNulls();
return "${greeter.getGreeting()} ${joiner?.join(names)}" return "${greeter.getGreeting()} ${joiner.join(names)}"
} }
} }