Permalink
Browse files

Migrate main build script to Kotlin

See #169
  • Loading branch information...
1 parent db1eabc commit b02916c7160ea45ad1bf7600d5a80c3bdaf6ceb9 @bamboo bamboo committed Oct 31, 2016
Showing with 195 additions and 164 deletions.
  1. +0 −155 build.gradle
  2. +193 −8 build.gradle.kts
  3. +2 −1 settings.gradle
View
@@ -1,155 +0,0 @@
-buildscript {
- ext.kotlinVersion = file('kotlin-version.txt').text.trim()
- ext.kotlinRepo = 'https://repo.gradle.org/gradle/repo'
- repositories {
- maven { url kotlinRepo }
- }
- dependencies {
- classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
- classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.1.1'
- }
-}
-
-
-apply from: 'build.gradle.kts'
-
-
-// --- Enable automatic generation of API extensions -------------------
-File apiExtensionsOutputDir = file('src/generated/kotlin')
-task generateConfigurationExtensions(type: codegen.GenerateConfigurationExtensions) {
- outputFile = new File(apiExtensionsOutputDir, 'org/gradle/script/lang/kotlin/ConfigurationsExtensions.kt')
-}
-task generateKotlinDependencyExtensions(type: codegen.GenerateKotlinDependencyExtensions) {
- outputFile = new File(apiExtensionsOutputDir, 'org/gradle/script/lang/kotlin/KotlinDependencyExtensions.kt')
- embeddedKotlinVersion = kotlinVersion
- gradleScriptKotlinRepository = kotlinRepo
-}
-task generateExtensions {
- dependsOn generateConfigurationExtensions
- dependsOn generateKotlinDependencyExtensions
-}
-sourceSets {
- main.kotlin.srcDirs += apiExtensionsOutputDir
-}
-compileKotlin.dependsOn generateExtensions
-
-
-// -- Performance testing ----------------------------------------------
-//
-// 1. Creates a custom Gradle installation with latest gradle-script-kotlin jar
-//
-// 2. Benchmarks latest installation against configured wrapper
-//
-def customInstallationDir = file("$buildDir/custom/gradle-${gradle.gradleVersion}")
-
-task copyCurrentDistro(type: Copy) {
- description "Copies the current Gradle distro into '${customInstallationDir}'."
-
- from gradle.gradleHomeDir
- into customInstallationDir
- exclude "**/*kotlin*"
-
- // preserve last modified date on each file to make it easier
- // to check which files were patched by next step
- def copyDetails = []
- eachFile { copyDetails << it }
- doLast {
- copyDetails.each { FileCopyDetails details ->
- def target = new File(customInstallationDir, details.path)
- target.setLastModified(details.lastModified)
- }
- }
-
- // don't bother recreating it
- onlyIf { !customInstallationDir.exists() }
-}
-
-task customInstallation(type: Copy, dependsOn: copyCurrentDistro) {
- description 'Copies latest gradle-script-kotlin snapshot over the custom installation.'
-
- from configurations.compile
- from tasks.jar
- into "$customInstallationDir/lib"
-}
-
-test.dependsOn customInstallation
-
-task benchmark(type: integration.Benchmark, dependsOn: customInstallation) {
- latestInstallation = customInstallationDir
-}
-
-
-// -- Integration testing ---------------------------------------------
-
-// Checks a single sample, for instance:
-//
-// check-hello-kotlin
-//
-tasks.addRule('Pattern: check-<SAMPLE>') { taskName ->
- if (taskName.startsWith ('check-')) {
- def checkSample = task ("$taskName-task", type: integration.CheckSample) {
- dependsOn customInstallation
- installation = customInstallationDir
- sampleDir = file("samples/${taskName - 'check-'}")
- }
- task(taskName).dependsOn(checkSample)
- }
-}
-
-task checkSamples {
- description 'Checks all samples'
- file('samples').eachDir {
- if (!it.name.contains('android')) {
- dependsOn "check-${it.name}"
- }
- }
-}
-check.dependsOn checkSamples
-
-task prepareIntegrationTestFixtures(type: GradleBuild) {
- dir = file('fixtures')
-}
-test.dependsOn prepareIntegrationTestFixtures
-
-
-// --- classpath.properties --------------------------------------------
-File generatedResourcesDir = file("$buildDir/generate-resources/main")
-task generateClasspathManifest(type: codegen.GenerateClasspathManifest) {
- outputDirectory = generatedResourcesDir
-}
-sourceSets {
- main.output.dir generatedResourcesDir, builtBy: generateClasspathManifest
-}
-
-
-// --- Configure publications ------------------------------------------
-def buildTagFor(String version) {
- switch (version.substring(version.lastIndexOf('-') + 1)) {
- case 'SNAPSHOT':
- return 'snapshot'
- case ~/M\d+[a-z]*$/:
- return 'milestone'
- default:
- return 'release'
- }
-}
-
-def targetRepoKey = "libs-${buildTagFor(project.version)}s-local"
-
-artifactory {
- contextUrl = 'https://repo.gradle.org/gradle'
- publish {
- repository {
- repoKey = targetRepoKey
- username = project.findProperty('artifactory_user') ?: 'nouser'
- password = project.findProperty('artifactory_password') ?: 'nopass'
- maven = true
- }
- defaults {
- publications('mavenJava')
- }
- }
- resolve {
- repoKey = 'repo'
- }
-}
View
@@ -1,7 +1,41 @@
+import codegen.GenerateClasspathManifest
+import codegen.GenerateConfigurationExtensions
+import codegen.GenerateKotlinDependencyExtensions
+
+import groovy.lang.GroovyObject
+
+import org.gradle.api.file.FileCopyDetails
+import org.gradle.api.internal.HasConvention
import org.gradle.api.publish.*
import org.gradle.api.publish.maven.*
+import org.gradle.api.tasks.Copy
+import org.gradle.api.tasks.GradleBuild
import org.gradle.jvm.tasks.Jar
+import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
+
+import org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention
+import org.jfrog.gradle.plugin.artifactory.dsl.PublisherConfig
+import org.jfrog.gradle.plugin.artifactory.dsl.ResolverConfig
+
+import java.io.File
+
+buildscript {
+ val kotlinVersion = file("kotlin-version.txt").readText().trim()
+ val kotlinRepo = "https://repo.gradle.org/gradle/repo"
+ extra["kotlinVersion"] = kotlinVersion
+ extra["kotlinRepo"] = kotlinRepo
+
+ repositories {
+ maven { setUrl(kotlinRepo) }
+ }
+
+ dependencies {
+ classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
+ classpath("org.jfrog.buildinfo:build-info-extractor-gradle:4.1.1")
+ }
+}
+
apply {
plugin("kotlin")
plugin("maven-publish")
@@ -12,13 +46,8 @@ group = "org.gradle"
version = "0.5.0-SNAPSHOT"
-val kotlinVersion by extra.properties
-
dependencies {
compileOnly(gradleApi())
- // compileOnly(gradle("core"))
- // compileOnly(gradle("process-services"))
- // compileOnly(gradle("tooling-api"))
compile("org.codehaus.groovy:groovy-all:2.4.7")
compile("org.slf4j:slf4j-api:1.7.10")
@@ -35,8 +64,11 @@ dependencies {
testCompile("com.fasterxml.jackson.module:jackson-module-kotlin:2.7.5")
}
+val sourceSets = the<JavaPluginConvention>().sourceSets
+val mainSourceSet = sourceSets.getByName("main")!!
+
tasks.withType<Jar> {
- from(the<JavaPluginConvention>().sourceSets.getByName("main").allSource)
+ from(mainSourceSet.allSource)
manifest.attributes.apply {
put("Implementation-Title", "Gradle Script Kotlin")
put("Implementation-Version", version)
@@ -51,6 +83,159 @@ configure<PublishingExtension> {
}
}
-fun gradle(module: String) = "org.gradle:gradle-$module:3.1-20160818000032+0000"
+// --- Enable automatic generation of API extensions -------------------
+val apiExtensionsOutputDir = file("src/generated/kotlin")
+
+val generateConfigurationExtensions = task<GenerateConfigurationExtensions>("generateConfigurationExtensions") {
+ outputFile = File(apiExtensionsOutputDir, "org/gradle/script/lang/kotlin/ConfigurationsExtensions.kt")
+}
+
+val kotlinVersion = extra["kotlinVersion"] as String
+val kotlinRepo = extra["kotlinRepo"] as String
+
+val generateKotlinDependencyExtensions = task<GenerateKotlinDependencyExtensions>("generateKotlinDependencyExtensions") {
+ outputFile = File(apiExtensionsOutputDir, "org/gradle/script/lang/kotlin/KotlinDependencyExtensions.kt")
+ embeddedKotlinVersion = kotlinVersion
+ gradleScriptKotlinRepository = kotlinRepo
+}
+
+val generateExtensions = task("generateExtensions") {
+ dependsOn(generateConfigurationExtensions)
+ dependsOn(generateKotlinDependencyExtensions)
+}
+
+(mainSourceSet as HasConvention).convention.getPlugin<KotlinSourceSet>().apply {
+ kotlin.srcDir(apiExtensionsOutputDir)
+}
+val compileKotlin = tasks.getByName("compileKotlin")
+compileKotlin.dependsOn(generateExtensions)
+
+
+// -- Performance testing ----------------------------------------------
+//
+// 1. Creates a custom Gradle installation with latest gradle-script-kotlin jar
+//
+// 2. Benchmarks latest installation against configured wrapper
+//
+val customInstallationDir = file("$buildDir/custom/gradle-${gradle.gradleVersion}")
+
+val copyCurrentDistro = task<Copy>("copyCurrentDistro") {
+ description = "Copies the current Gradle distro into '$customInstallationDir'."
+
+ from(gradle.gradleHomeDir)
+ into(customInstallationDir)
+ exclude("**/*kotlin*")
+
+ // preserve last modified date on each file to make it easier
+ // to check which files were patched by next step
+ val copyDetails = mutableListOf<FileCopyDetails>()
+ eachFile { copyDetails.add(this) }
+ doLast {
+ copyDetails.forEach { details ->
+ val target = File(customInstallationDir, details.path)
+ target.setLastModified(details.lastModified)
+ }
+ }
+
+ // don't bother recreating it
+ onlyIf { !customInstallationDir.exists() }
+}
+
+
+val customInstallation = task<Copy>("customInstallation") {
+ description = "Copies latest gradle-script-kotlin snapshot over the custom installation."
+ dependsOn(copyCurrentDistro)
+ from(configurations.compile)
+ from(tasks.getByName("jar"))
+ into("$customInstallationDir/lib")
+}
+val test = tasks.getByName("test")
+test.dependsOn(customInstallation)
+
+task<integration.Benchmark>("benchmark") {
+ dependsOn(customInstallation)
+ latestInstallation = customInstallationDir
+}
+
+
+// -- Integration testing ---------------------------------------------
+//
+// Checks a single sample, for instance:
+//
+// check-hello-kotlin
+//
+tasks.addRule("Pattern: check-<SAMPLE>", closureOf<String> {
+ val taskName = this
+ if (taskName.startsWith("check-")) {
+ val checkSample = task<integration.CheckSample>("$taskName-task") {
+ dependsOn(customInstallation)
+ installation = customInstallationDir
+ sampleDir = file("samples/${taskName.removePrefix("check-")}")
+ }
+ task(taskName).dependsOn(checkSample)
+ }
+})
+
+val checkSamples = task("checkSamples") {
+ description = "Checks all samples"
+ file("samples").listFiles().forEach {
+ if (it.isDirectory && !it.name.contains("android")) {
+ dependsOn("check-${it.name}")
+ }
+ }
+}
+val check = tasks.getByName("check")!!
+check.dependsOn(checkSamples)
+
+val prepareIntegrationTestFixtures = task<GradleBuild>("prepareIntegrationTestFixtures") {
+ setDir(file("fixtures"))
+}
+test.dependsOn(prepareIntegrationTestFixtures)
+
+
+// --- classpath.properties --------------------------------------------
+val generatedResourcesDir = file("$buildDir/generate-resources/main")
+task<GenerateClasspathManifest>("generateClasspathManifest") {
+ outputDirectory = generatedResourcesDir
+}
+mainSourceSet.output.dir(mapOf("builtBy" to "generateClasspathManifest"), generatedResourcesDir)
+
+
+// --- Configure publications ------------------------------------------
+fun buildTagFor(version: String): String =
+ when (version.substringAfterLast('-')) {
+ "SNAPSHOT" -> "snapshot"
+ in Regex("""M\d+[a-z]*$""") -> "milestone"
+ else -> "release"
+ }
+
+configure<ArtifactoryPluginConvention> {
+ setContextUrl("https://repo.gradle.org/gradle")
+ publish(delegateClosureOf<PublisherConfig> {
+ repository(delegateClosureOf<GroovyObject> {
+ val targetRepoKey = "libs-${buildTagFor(project.version as String)}s-local"
+ setProperty("repoKey", targetRepoKey)
+ setProperty("username", project.findProperty("artifactory_user") ?: "nouser")
+ setProperty("password", project.findProperty("artifactory_password") ?: "nopass")
+ setProperty("maven", true)
+ })
+ defaults(delegateClosureOf<GroovyObject> {
+ invokeMethod("publications", "mavenJava")
+ })
+ })
+ resolve(delegateClosureOf<ResolverConfig> {
+ setProperty("repoKey", "repo")
+ })
+}
+
+
+// --- Utility functions -----------------------------------------------
+fun kotlin(module: String) = "org.jetbrains.kotlin:kotlin-$module:${extra["kotlinVersion"]}"
+
+operator fun Regex.contains(s: String) = matches(s)
+
+fun <T> Any.delegateClosureOf(action: T.() -> Unit) =
+ object : groovy.lang.Closure<Unit>(this, null) {
+ fun doCall() = (delegate as T).action()
+ }
-fun kotlin(module: String) = "org.jetbrains.kotlin:kotlin-$module:$kotlinVersion"
View
@@ -1 +1,2 @@
-rootProject.name='gradle-script-kotlin'
+rootProject.name = 'gradle-script-kotlin'
+rootProject.buildFileName = 'build.gradle.kts'

0 comments on commit b02916c

Please sign in to comment.