Skip to content

Commit

Permalink
making a better composite build
Browse files Browse the repository at this point in the history
  • Loading branch information
RBusarow committed Nov 20, 2023
1 parent 12283a0 commit 4632e6f
Show file tree
Hide file tree
Showing 32 changed files with 465 additions and 614 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ ij_shell_redirect_followed_by_space = false
ij_shell_switch_cases_indented = false

[{*.gant,*.gradle,*.groovy,*.gy}]
ij_continuation_indent_size = 2
ij_groovy_align_group_field_declarations = false
ij_groovy_align_multiline_array_initializer_expression = false
ij_groovy_align_multiline_assignment = false
Expand Down
14 changes: 3 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs :
check-latest : true

- name : KtLint
run : ./gradlew ktlintCheck --no-build-cache --no-daemon --stacktrace && ./gradlew -p build-logic ktlintCheck --no-build-cache --no-daemon --stacktrace
run : ./gradlew ktlintCheck --no-build-cache --no-daemon --stacktrace

lint :
runs-on : ubuntu-latest
Expand Down Expand Up @@ -253,13 +253,6 @@ jobs :
name : Instrumentation tests
runs-on : macos-latest
timeout-minutes : 20
strategy :
# Allow tests to continue on other devices if they fail on one device.
fail-fast : false
matrix :
kotlin-version : [ 1.8.22, 1.9.10 ]
agp-version : [ 7.3.1, 8.1.0 ]
gradle-version : [ 7.6.1, 8.2.1, 8.4 ]

steps :
- uses : actions/checkout@v3
Expand All @@ -270,14 +263,13 @@ jobs :
check-latest : true

- name : Gradle integration tests
run : ./gradlew integrationTest --stacktrace -Doverride_kotlin=${{ matrix.kotlin-version }} -Doverride_gradle=${{ matrix.kotlin-version }} -Doverride_agp=${{ matrix.agp-version }}
run : ./gradlew integrationTest --stacktrace

- name : Upload Test Results
uses : actions/upload-artifact@v3
if : ${{ failure() }}
with :
# Use the Kotlin version to prevent overrides.
name : test-results-gradle-integration-${{ matrix.kotlin-version }}-${{ matrix.gradle-version }}-${{ matrix.agp-version }}
name : test-results-gradle-integration
path : ./**/build/reports/tests/

gradle-wrapper-validation :
Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ out
# Ignore the generated project.
/benchmark/

# Since :gradle-plugin is in two builds, it has two build directories
/gradle-plugin/build-anvil/
# projects in the `build-logic/delegate` build have a second build dir
build-delegate/
build-anvil/

# old build directories that've moved
/gradle-plugin/build-composite-wrapper/
46 changes: 0 additions & 46 deletions build-logic/build.gradle

This file was deleted.

48 changes: 48 additions & 0 deletions build-logic/conventions/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
plugins {
alias libs.plugins.kotlin.jvm
alias libs.plugins.ktlint
id 'java-gradle-plugin'
}

gradlePlugin {
plugins {
root {
id = "com.squareup.anvil.root"
implementationClass = "com.squareup.anvil.conventions.RootPlugin"
}
library {
id = "com.squareup.anvil.library"
implementationClass = "com.squareup.anvil.conventions.LibraryPlugin"
}
publish {
id = "com.squareup.anvil.publish"
implementationClass = "com.squareup.anvil.conventions.PublishConventionPlugin"
}
itegrationTests {
id = "com.squareup.anvil.itegration-tests"
implementationClass = "com.squareup.anvil.conventions.IntegrationTestsConventionPlugin"
}
}
}

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

ktlint {
version = libs.versions.ktlint.get()
}

dependencies {
compileOnly gradleApi()

api libs.ktlintRaw
api libs.kotlinpoet
api libs.kgx

api libs.kotlin.dokka
api libs.kotlin.gradlePlugin
api libs.mavenPublishRaw
}
14 changes: 14 additions & 0 deletions build-logic/conventions/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
}
includeBuild '../settings'
}

plugins {
id 'com.squareup.anvil.gradle-settings'
}

rootProject.name = "conventions"
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
/*
* Copyright (C) 2023 Rick Busarow
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.squareup.anvil
package com.squareup.anvil.conventions

import com.rickbusarow.kgx.applyOnce
import com.rickbusarow.kgx.dependsOn
Expand All @@ -23,6 +8,7 @@ import org.gradle.api.Project
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.testing.Test
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.plugins.ide.idea.model.IdeaModel
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension

abstract class IntegrationTestsConventionPlugin : Plugin<Project> {
Expand All @@ -40,14 +26,22 @@ abstract class IntegrationTestsConventionPlugin : Plugin<Project> {

ss.runtimeClasspath += ss.output + ss.compileClasspath
}

target.configurations.register("${INTEGRATION_TEST}Compile") {
it.extendsFrom(target.configurations.getByName("testCompileOnly"))
}
target.configurations.register("${INTEGRATION_TEST}Runtime") {
it.extendsFrom(target.configurations.getByName("testRuntimeOnly"))
}

// Make the `integrationTest` source set able to access `internal` declarations in the `main`
// source set
target.extensions.configure(KotlinJvmProjectExtension::class.java) { kotlin ->
val compilations = kotlin.target.compilations
compilations.getByName(INTEGRATION_TEST) {
it.associateWith(compilations.getByName("main"))
}
}

val integrationTestTask = target.tasks
.register(INTEGRATION_TEST, Test::class.java) { task ->

Expand All @@ -62,17 +56,20 @@ abstract class IntegrationTestsConventionPlugin : Plugin<Project> {

task.testClassesDirs = ss.output.classesDirs
task.classpath = ss.runtimeClasspath
task.inputs.files(ss.output.classesDirs)
task.inputs.files(mainSourceSet.allSource)
// task.inputs.files(ss.output.classesDirs)
task.inputs.files(ss.allSource)
// task.inputs.files(mainSourceSet.allSource)
}

// Make `check` depend upon `integrationTest`
target.tasks.named(LifecycleBasePlugin.CHECK_TASK_NAME).dependsOn(integrationTestTask)

target.extensions.configure(KotlinJvmProjectExtension::class.java) { kotlin ->
val compilations = kotlin.target.compilations

compilations.getByName(INTEGRATION_TEST) {
it.associateWith(compilations.getByName("main"))
// Make the IDE treat `src/integrationTest/[java/kotlin]` as a test source directory.
target.extensions.configure(IdeaModel::class.java) { idea ->
idea.module { module ->
module.testSources.from(
integrationTestSourceSet.map { it.allSource.srcDirs },
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.squareup.anvil.conventions

import com.rickbusarow.kgx.isRootProject
import com.rickbusarow.kgx.libsCatalog
import com.rickbusarow.kgx.version
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jlleitschuh.gradle.ktlint.KtlintExtension
import org.jlleitschuh.gradle.ktlint.KtlintPlugin

open class KtlintConventionPlugin : Plugin<Project> {

override fun apply(target: Project) {
target.plugins.apply(KtlintPlugin::class.java)

target.extensions.configure(KtlintExtension::class.java) { ktlint ->
ktlint.version.set(target.libsCatalog.version("ktlint"))
ktlint.verbose.set(true)
}

if (target.isRootProject() && target.isInMainAnvilBuild()) {
propagateToIncludedBuilds(target)
}
}

/**
* Make calls to `ktlintCheck` and `ktlintFormat` in the root project propagate to all included
* builds. These two invocations will be equivalent:
*
* ```sh
* ./gradlew ktlintCheck
* ./gradlew ktlintCheck :build-logic:conventions:ktlintCheck :build-logic:settings:ktlintCheck
* ```
*/
private fun propagateToIncludedBuilds(target: Project) {
target.gradle.includedBuilds
.filter { it.name != "delegate" }
.forEach { build ->
target.tasks.named("ktlintCheck") { task ->
task.dependsOn(build.task(":ktlintCheck"))
}
target.tasks.named("ktlintFormat") { task ->
task.dependsOn(build.task(":ktlintFormat"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.squareup.anvil.conventions

import com.rickbusarow.kgx.libsCatalog
import com.rickbusarow.kgx.pluginId
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.plugins.ide.idea.model.IdeaModel

open class LibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {

target.plugins.apply(target.libsCatalog.pluginId("kotlin-jvm"))
target.plugins.apply(KtlintConventionPlugin::class.java)

when (target.path) {
":annotations",
":annotations-optional",
":compiler",
":compiler-api",
":compiler-utils",
":gradle-plugin",
-> target.setBuildDir()
}
}

private fun Project.setBuildDir() {

if (file("build").exists()) {
file("build").deleteRecursively()
}
val anvilBuildDir = file("build-anvil")
val delegateDir = file("build-delegate")

plugins.apply("idea")
// Make both the `build-anvil` and `build-delegate` directories show
// up as build directories within the IDE.
extensions.configure(IdeaModel::class.java) { idea ->
idea.module { module ->
module.excludeDirs.addAll(listOf(delegateDir, anvilBuildDir))
}
}

if (isInDelegateBuild()) {
layout.buildDirectory.set(delegateDir)
} else {
layout.buildDirectory.set(anvilBuildDir)
}
}
}
Loading

0 comments on commit 4632e6f

Please sign in to comment.