Skip to content

Commit

Permalink
Avoid unnecessary task configuration in build (#3533)
Browse files Browse the repository at this point in the history
* Use configureEach on task collections

* Use tasks.named where possible

* Drop redundant configure() calls

* Register tasks instead of creating them
  • Loading branch information
3flex committed Sep 26, 2022
1 parent 45ba524 commit 6eb51f7
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 86 deletions.
12 changes: 5 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,21 @@ allprojects {
}
}

tasks.withType(JavaCompile) {
tasks.withType(JavaCompile).configureEach {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType(org.jetbrains.grammarkit.tasks.GenerateParserTask) {
tasks.withType(org.jetbrains.grammarkit.tasks.GenerateParserTask).configureEach {
doFirst {
// https://github.com/JetBrains/gradle-grammar-kit-plugin/pull/78.
logging.captureStandardOutput(LogLevel.INFO)
}
}

// https://youtrack.jetbrains.com/issue/KT-46466
tasks.withType(org.gradle.plugins.signing.Sign) { signingTask ->
tasks.withType(org.gradle.api.publish.maven.tasks.AbstractPublishToMaven) { publishTask ->
publishTask.dependsOn(signingTask)
}
tasks.withType(org.gradle.api.publish.maven.tasks.AbstractPublishToMaven).configureEach {
dependsOn(tasks.withType(Sign))
}

configurations.all {
Expand All @@ -81,7 +79,7 @@ allprojects {
version = VERSION_NAME
}

tasks.named("dokkaHtmlMultiModule").configure {
tasks.named("dokkaHtmlMultiModule") {
outputDirectory = file("$rootDir/docs/2.x")

moduleName.set("SQLDelight")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ class MultiplatformConventions : Plugin<Project> {

// linking fails for the linux test build if not built on a linux host
// ensure the tests and linking for them is only done on linux hosts
if (!HostManager.hostIsLinux) {
project.tasks.findByName("linuxX64Test")?.enabled = false
project.tasks.findByName("linkDebugTestLinuxX64")?.enabled = false
}
project.tasks.named("linuxX64Test") { it.enabled = HostManager.hostIsLinux }
project.tasks.named("linkDebugTestLinuxX64") { it.enabled = HostManager.hostIsLinux }

if (!HostManager.hostIsMingw) {
project.tasks.findByName("mingwX64Test")?.enabled = false
project.tasks.findByName("linkDebugTestMingwX64")?.enabled = false
}
project.tasks.named("mingwX64Test") { it.enabled = HostManager.hostIsMingw }
project.tasks.named("linkDebugTestMingwX64") { it.enabled = HostManager.hostIsMingw }
}
}
}
10 changes: 5 additions & 5 deletions dialects/mysql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ dependencies {

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include "com/mysql/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}
10 changes: 5 additions & 5 deletions dialects/postgresql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ dependencies {

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include "org/postgresql/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}
10 changes: 5 additions & 5 deletions dialects/sqlite-3-24/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ dependencies {
testImplementation libs.sqlPsiTestFixtures
}

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
12 changes: 6 additions & 6 deletions dialects/sqlite-3-25/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ dependencies {
testImplementation libs.sqlPsiTestFixtures
}

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

tasks.getByName("compileKotlin").configure {
tasks.named("compileKotlin") {
dependsOn(":dialects:sqlite-3-24:shadowJar")
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
12 changes: 6 additions & 6 deletions dialects/sqlite-3-30/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ dependencies {
testImplementation libs.sqlPsiTestFixtures
}

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

tasks.getByName("compileKotlin").configure {
tasks.named("compileKotlin") {
dependsOn(":dialects:sqlite-3-25:shadowJar")
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
14 changes: 7 additions & 7 deletions dialects/sqlite-3-35/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ dependencies {
testImplementation libs.sqlPsiTestFixtures
}

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

tasks.getByName("compileKotlin").configure {
tasks.named("compileKotlin") {
dependsOn(":dialects:sqlite-3-30:shadowJar")
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
12 changes: 6 additions & 6 deletions dialects/sqlite-3-38/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ dependencies {
testImplementation libs.sqlPsiTestFixtures
}

tasks.getByName("shadowJar").configure {
tasks.named("shadowJar") {
classifier = ''
include "*.jar"
include "app/cash/sqldelight/**"
include 'META-INF/services/*'
}

tasks.jar.configure {
tasks.named('jar') {
// Prevents shadowJar (with classifier = '') and this task from writing to the same path.
enabled = false
}

configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(shadowJar)
it.outgoing.artifact(tasks.named('shadowJar'))
}
}

tasks.getByName("compileKotlin").configure {
tasks.named("compileKotlin") {
dependsOn(":dialects:sqlite-3-35:shadowJar")
}

artifacts {
runtimeOnly(shadowJar)
archives(shadowJar)
runtimeOnly(tasks.named('shadowJar'))
archives(tasks.named('shadowJar'))
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
2 changes: 1 addition & 1 deletion drivers/android-driver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

// https://github.com/Kotlin/dokka/issues/2472
afterEvaluate {
tasks.getByName("javaDocReleaseGeneration").enabled(false)
tasks.named("javaDocReleaseGeneration") { enabled(false) }
}
12 changes: 4 additions & 8 deletions drivers/native-driver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,11 @@ kotlin {

//linking fails for the linux test build if not built on a linux host
//ensure the tests and linking for them is only done on linux hosts
if(!HostManager.hostIsLinux) {
tasks.findByName("linuxX64Test")?.enabled = false
tasks.findByName("linkDebugTestLinuxX64")?.enabled = false
}
tasks.named("linuxX64Test") { enabled = HostManager.hostIsLinux }
tasks.named("linkDebugTestLinuxX64") { enabled = HostManager.hostIsLinux }

if(!HostManager.hostIsMingw) {
tasks.findByName("mingwX64Test")?.enabled = false
tasks.findByName("linkDebugTestMingwX64")?.enabled = false
}
tasks.named("mingwX64Test") { enabled = HostManager.hostIsMingw }
tasks.named("linkDebugTestMingwX64") { enabled = HostManager.hostIsMingw }
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"
2 changes: 1 addition & 1 deletion sample/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ kotlin {
}

afterEvaluate {
tasks.named("compileCommonMainKotlinMetadata").configure {
tasks.named("compileCommonMainKotlinMetadata") {
// https://youtrack.jetbrains.com/issue/KT-51293
enabled = false
}
Expand Down
10 changes: 5 additions & 5 deletions sqldelight-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
alias(libs.plugins.dokka)
}

test {
tasks.named('test') {
testLogging {
events "passed", "skipped", "failed"
exceptionFormat "full"
Expand Down Expand Up @@ -58,7 +58,7 @@ dependencies {
testImplementation project(path: ':dialects:sqlite-3-38', configuration: 'shadowRuntimeElements')
}

task pluginVersion {
tasks.register('pluginVersion') {
def getGitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
Expand Down Expand Up @@ -88,7 +88,7 @@ val GIT_SHA = "${getGitHash}"
apply from: "$rootDir/gradle/gradle-mvn-push.gradle"

afterEvaluate {
tasks.named('compileKotlin').configure { dependsOn('pluginVersion') }
tasks.named('dokkaHtml').configure { dependsOn('pluginVersion') }
tasks.named('javaSourcesJar').configure { dependsOn('pluginVersion') }
tasks.named('compileKotlin') { dependsOn('pluginVersion') }
tasks.named('dokkaHtml') { dependsOn('pluginVersion') }
tasks.named('javaSourcesJar') { dependsOn('pluginVersion') }
}

0 comments on commit 6eb51f7

Please sign in to comment.