Improve build DSL
This commit is contained in:
@@ -6,6 +6,8 @@ import org.gradle.api.tasks.*
|
|||||||
import org.gradle.kotlin.dsl.*
|
import org.gradle.kotlin.dsl.*
|
||||||
import org.gradle.api.artifacts.Configuration
|
import org.gradle.api.artifacts.Configuration
|
||||||
import org.gradle.api.artifacts.ConfigurationContainer
|
import org.gradle.api.artifacts.ConfigurationContainer
|
||||||
|
import org.gradle.api.artifacts.Dependency
|
||||||
|
import org.gradle.api.file.DuplicatesStrategy
|
||||||
import org.gradle.api.plugins.JavaPluginConvention
|
import org.gradle.api.plugins.JavaPluginConvention
|
||||||
import org.gradle.api.tasks.javadoc.Javadoc
|
import org.gradle.api.tasks.javadoc.Javadoc
|
||||||
import org.gradle.jvm.tasks.Jar
|
import org.gradle.jvm.tasks.Jar
|
||||||
@@ -30,12 +32,23 @@ fun<T> Project.runtimeJarArtifactBy(task: Task, artifactRef: T, body: Configurab
|
|||||||
addArtifact("runtimeJar", task, artifactRef, body)
|
addArtifact("runtimeJar", task, artifactRef, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun<T: Jar> Project.runtimeJar(task: T, body: T.() -> Unit = {}): T =
|
fun Project.buildVersion(): Dependency {
|
||||||
task.apply {
|
val cfg = configurations.create("build-version")
|
||||||
setupPublicJar()
|
return dependencies.add(cfg.name, dependencies.project(":prepare:build.version", configuration = "buildVersion"))
|
||||||
body()
|
}
|
||||||
project.runtimeJarArtifactBy(this, this)
|
|
||||||
}
|
fun<T: Jar> Project.runtimeJar(task: T, body: T.() -> Unit = {}): T {
|
||||||
|
val buildVersionCfg = configurations.create("buildVersion")
|
||||||
|
dependencies.add(buildVersionCfg.name, dependencies.project(":prepare:build.version", configuration = "buildVersion"))
|
||||||
|
extra["runtimeJarTask"] = task
|
||||||
|
return task.apply {
|
||||||
|
setupPublicJar()
|
||||||
|
from(buildVersionCfg) { into("META-INF") }
|
||||||
|
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||||
|
body()
|
||||||
|
project.runtimeJarArtifactBy(this, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun Project.runtimeJar(taskName: String = "jar", body: Jar.() -> Unit = {}): Jar = runtimeJar(getOrCreateTask(taskName, body))
|
fun Project.runtimeJar(taskName: String = "jar", body: Jar.() -> Unit = {}): Jar = runtimeJar(getOrCreateTask(taskName, body))
|
||||||
|
|
||||||
@@ -88,10 +101,8 @@ fun Project.ideaPlugin(subdir: String = "lib", body: Copy.() -> Unit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Project.ideaPlugin() = ideaPlugin {
|
fun Project.ideaPlugin(subdir: String = "lib") = ideaPlugin(subdir) {
|
||||||
tasks.findByName("jar")?.let {
|
fromRuntimeJarIfExists(this)
|
||||||
from(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -104,8 +115,17 @@ fun Project.dist(body: Copy.() -> Unit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Project.dist() = dist {
|
fun Project.dist() = dist {
|
||||||
tasks.findByName("jar")?.let {
|
fromRuntimeJarIfExists(this)
|
||||||
from(it)
|
}
|
||||||
|
|
||||||
|
private fun<T: AbstractCopyTask> Project.fromRuntimeJarIfExists(task: T) {
|
||||||
|
if (extra.has("runtimeJarTask")) {
|
||||||
|
task.from(extra["runtimeJarTask"] as Task)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tasks.findByName("jar")?.let {
|
||||||
|
task.from(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,6 +153,3 @@ fun<T> Project.addArtifact(configuration: Configuration, task: Task, artifactRef
|
|||||||
|
|
||||||
fun<T> Project.addArtifact(configurationName: String, task: Task, artifactRef: T, body: ConfigurablePublishArtifact.() -> Unit = {}) =
|
fun<T> Project.addArtifact(configurationName: String, task: Task, artifactRef: T, body: ConfigurablePublishArtifact.() -> Unit = {}) =
|
||||||
addArtifact(configurations.getOrCreate(configurationName), task, artifactRef, body)
|
addArtifact(configurations.getOrCreate(configurationName), task, artifactRef, body)
|
||||||
|
|
||||||
inline fun<reified T: Task> Project.getOrCreateTask(taskName: String, body: T.() -> Unit): T =
|
|
||||||
(tasks.findByName(taskName)?.let { it as T } ?: task<T>(taskName)).apply{ body() }
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
@file:Suppress("unused") // usages in build scripts are not tracked properly
|
@file:Suppress("unused") // usages in build scripts are not tracked properly
|
||||||
|
|
||||||
import org.gradle.api.*
|
import org.gradle.api.*
|
||||||
@@ -17,11 +16,6 @@ val bootstrapKotlinVersion: String = System.getProperty("bootstrap.kotlin.versio
|
|||||||
fun PluginDependenciesSpec.kotlin(module: String, version: String? = null): PluginDependencySpec =
|
fun PluginDependenciesSpec.kotlin(module: String, version: String? = null): PluginDependencySpec =
|
||||||
id("org.jetbrains.kotlin.$module") version (version ?: bootstrapKotlinVersion)
|
id("org.jetbrains.kotlin.$module") version (version ?: bootstrapKotlinVersion)
|
||||||
|
|
||||||
fun Project.buildVersion(): Dependency {
|
|
||||||
val cfg = configurations.create("build-version")
|
|
||||||
return dependencies.add(cfg.name, dependencies.project(":prepare:build.version", configuration = "default"))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Project.commonDep(coord: String): String {
|
fun Project.commonDep(coord: String): String {
|
||||||
val parts = coord.split(':')
|
val parts = coord.split(':')
|
||||||
return when (parts.size) {
|
return when (parts.size) {
|
||||||
@@ -58,6 +52,8 @@ fun DependencyHandler.projectDepIntransitive(name: String): Dependency =
|
|||||||
project(name, configuration = "default").apply { isTransitive = false }
|
project(name, configuration = "default").apply { isTransitive = false }
|
||||||
|
|
||||||
fun DependencyHandler.projectTests(name: String): Dependency = project(name, configuration = "tests-jar").apply { isTransitive = false }
|
fun DependencyHandler.projectTests(name: String): Dependency = project(name, configuration = "tests-jar").apply { isTransitive = false }
|
||||||
|
fun DependencyHandler.projectRuntimeJar(name: String): Dependency = project(name, configuration = "runtimeJar")
|
||||||
|
fun DependencyHandler.projectArchives(name: String): Dependency = project(name, configuration = "archives")
|
||||||
|
|
||||||
val protobufLiteProject = ":custom-dependencies:protobuf-lite"
|
val protobufLiteProject = ":custom-dependencies:protobuf-lite"
|
||||||
fun DependencyHandler.protobufLite(): ProjectDependency =
|
fun DependencyHandler.protobufLite(): ProjectDependency =
|
||||||
@@ -68,7 +64,6 @@ fun DependencyHandler.protobufFull(): ProjectDependency =
|
|||||||
project(protobufLiteProject, configuration = "relocated").apply { isTransitive = false }
|
project(protobufLiteProject, configuration = "relocated").apply { isTransitive = false }
|
||||||
val protobufFullTask = "$protobufLiteProject:prepare-relocated-protobuf"
|
val protobufFullTask = "$protobufLiteProject:prepare-relocated-protobuf"
|
||||||
|
|
||||||
|
|
||||||
private fun File.matchMaybeVersionedArtifact(baseName: String) = name.matches(baseName.toMaybeVersionedJarRegex())
|
private fun File.matchMaybeVersionedArtifact(baseName: String) = name.matches(baseName.toMaybeVersionedJarRegex())
|
||||||
|
|
||||||
private val wildcardsRe = """[^*?]+|(\*)|(\?)""".toRegex()
|
private val wildcardsRe = """[^*?]+|(\*)|(\?)""".toRegex()
|
||||||
@@ -89,5 +84,3 @@ private fun String.toMaybeVersionedJarRegex(): Regex {
|
|||||||
return Regex(if (hasJarExtension) escaped else "$escaped(-\\d.*)?\\.jar") // TODO: consider more precise version part of the regex
|
return Regex(if (hasJarExtension) escaped else "$escaped(-\\d.*)?\\.jar") // TODO: consider more precise version part of the regex
|
||||||
}
|
}
|
||||||
|
|
||||||
val propertiesX =
|
|
||||||
java.util.Properties().apply { load(java.io.FileInputStream("gradle.properties")) }
|
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ fun SourceSet.none() {
|
|||||||
resources.srcDirs()
|
resources.srcDirs()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun SourceSet.default() {
|
fun SourceSet.projectDefault() {
|
||||||
when (name) {
|
when (name) {
|
||||||
"main" -> {
|
"main" -> {
|
||||||
java.srcDirs("src")
|
java.srcDirs("src")
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
@file:Suppress("unused") // usages in build scripts are not tracked properly
|
||||||
|
|
||||||
|
import org.gradle.api.*
|
||||||
|
import org.gradle.kotlin.dsl.*
|
||||||
|
import org.gradle.api.tasks.testing.Test
|
||||||
|
|
||||||
|
fun Project.projectTest(body: Test.() -> Unit = {}): Test = getOrCreateTask("test") {
|
||||||
|
jvmArgs("-ea", "-XX:+HeapDumpOnOutOfMemoryError", "-Xmx1200m", "-XX:+UseCodeCacheFlushing", "-XX:ReservedCodeCacheSize=128m", "-Djna.nosys=true")
|
||||||
|
maxHeapSize = "1200m"
|
||||||
|
systemProperty("idea.is.unit.test", "true")
|
||||||
|
environment("NO_FS_ROOTS_ACCESS_CHECK", "true")
|
||||||
|
environment("KOTLIN_HOME", rootProject.extra["distKotlinHomeDir"])
|
||||||
|
systemProperty("jps.kotlin.home", rootProject.extra["distKotlinHomeDir"])
|
||||||
|
ignoreFailures = System.getenv("kotlin_build_ignore_test_failures")?.let { it == "yes" } ?: false
|
||||||
|
body()
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun<reified T: Task> Project.getOrCreateTask(taskName: String, body: T.() -> Unit): T =
|
||||||
|
(tasks.findByName(taskName)?.let { it as T } ?: task<T>(taskName)).apply{ body() }
|
||||||
Reference in New Issue
Block a user