Skip to content

Commit

Permalink
flattening the build structure
Browse files Browse the repository at this point in the history
This makes the IDE happier about debugging unit tests.
  • Loading branch information
RBusarow committed Jan 23, 2025
1 parent 12f5ed8 commit 1721c42
Show file tree
Hide file tree
Showing 26 changed files with 173 additions and 334 deletions.
41 changes: 37 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ jobs:
java-version: '17'
check-latest: true

- name: Android Lint
run: ./gradlew -p build-logic/delegate lint --no-build-cache --no-daemon --stacktrace
- name: Android Lint in integration-tests
working-directory: ./integration-tests
run: ./gradlew lint

- name: Android Lint in sample
working-directory: ./sample
run: ./gradlew lint

- name: Upload Lint Results
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
Expand Down Expand Up @@ -205,10 +210,12 @@ jobs:
check-latest: true

- name: Run integration tests
run: ./gradlew -p build-logic/delegate test --no-build-cache --no-daemon --stacktrace -Doverride_kotlin=${{ matrix.kotlin-version }} -Doverride_config-generateDaggerFactoriesWithAnvil=false
working-directory: ./integration-tests
run: ./gradlew -p test --no-build-cache --no-daemon --stacktrace -Doverride_kotlin=${{ matrix.kotlin-version }} -Doverride_config-generateDaggerFactoriesWithAnvil=false

- name: Build the sample
run: ./gradlew :delegate:sample:app:assembleDebug --no-build-cache --no-daemon --stacktrace -Doverride_kotlin=${{ matrix.kotlin-version }} -Doverride_config-generateDaggerFactoriesWithAnvil=false
working-directory: ./sample
run: ./gradlew assembleDebug --no-build-cache --no-daemon --stacktrace -Doverride_kotlin=${{ matrix.kotlin-version }} -Doverride_config-generateDaggerFactoriesWithAnvil=false

- name: Upload Test Results
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
Expand Down Expand Up @@ -278,6 +285,7 @@ jobs:
profile : Galaxy Nexus
ram-size : 4096M
target: default
working-directory: ./sample
script: ./gradlew connectedCheck --no-build-cache --no-daemon --stacktrace

- name: Upload results
Expand All @@ -286,6 +294,30 @@ jobs:
name: instrumentation-test-results
path: ./**/build/reports/androidTests/connected/**

integration-tests:
name: Integration tests
runs-on: macos-latest
timeout-minutes: 25

steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b # v4
with:
distribution: 'zulu'
java-version: '17'
check-latest: true

- name: Integration tests
working-directory: ./integration-tests
run: ./gradlew test jvmTest android --stacktrace

- name: Upload Test Results
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
if: ${{ failure() }}
with:
name: test-results-gradle-integration
path: ./integration-tests/**/build/reports/tests/

gradle-integration-tests:
name: Gradle integration tests
runs-on: macos-latest
Expand Down Expand Up @@ -349,6 +381,7 @@ jobs:
- test-gradle-plugin
- kapt-for-dagger-factories
- instrumentation-tests
- integration-tests
- gradle-integration-tests
- gradle-wrapper-validation
- build-benchmark-project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.squareup.anvil.conventions

import com.rickbusarow.kgx.buildDir
import com.rickbusarow.kgx.extras
import com.rickbusarow.kgx.fromInt
import com.rickbusarow.kgx.getValue
import com.rickbusarow.kgx.javaExtension
import com.rickbusarow.kgx.provideDelegate
import com.squareup.anvil.conventions.utils.isInAnvilBuild
import com.squareup.anvil.conventions.utils.isInAnvilIncludedBuild
import com.squareup.anvil.conventions.utils.isInAnvilRootBuild
import com.squareup.anvil.conventions.utils.libs
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
Expand All @@ -18,9 +13,9 @@ import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Properties

abstract class BasePlugin : Plugin<Project> {

Expand All @@ -33,10 +28,6 @@ abstract class BasePlugin : Plugin<Project> {

val extension = target.extensions.create("conventions", ConventionsExtension::class.java)

if (!target.isInAnvilBuild()) {
target.copyRootProjectGradleProperties()
}

beforeApply(target)

target.plugins.apply("base")
Expand All @@ -45,8 +36,6 @@ abstract class BasePlugin : Plugin<Project> {

configureGradleProperties(target)

configureBuildDirs(target)

configureJava(target)

target.plugins.withType(KotlinBasePluginWrapper::class.java) {
Expand All @@ -55,29 +44,9 @@ abstract class BasePlugin : Plugin<Project> {

configureTests(target)

target.configurations.configureEach { config ->
config.resolutionStrategy {
it.force(target.libs.kotlin.metadata)
}
}

afterApply(target)
}

/**
* Included builds need the `GROUP` and `VERSION_NAME` values from the main build's
* `gradle.properties`. We can't just use a symlink because Windows exists.
* See https://github.com/square/anvil/pull/763#discussion_r1379563691
*/
private fun Project.copyRootProjectGradleProperties() {
rootProject.file("../../gradle.properties")
.inputStream()
.use { Properties().apply { load(it) } }
.forEach { key, value ->
extras.set(key.toString(), value.toString())
}
}

private fun configureGradleProperties(target: Project) {
target.version = target.property("VERSION_NAME") as String
target.group = target.property("GROUP") as String
Expand Down Expand Up @@ -123,38 +92,11 @@ abstract class BasePlugin : Plugin<Project> {
freeCompilerArgs.add("-Xexplicit-api=strict")
}

jvmTarget.set(JvmTarget.fromInt(target.jvmTargetInt()))
}
}
}

/**
* The "anvil" projects exist in two different builds, they need two different build directories
* so that there is no shared mutable state.
*/
private fun configureBuildDirs(target: Project) {

when {
!target.isInAnvilBuild() -> return
languageVersion.set(KotlinVersion.KOTLIN_2_0)

target.isInAnvilRootBuild() -> {
target.layout.buildDirectory.set(target.file("build/root-build"))
}

target.isInAnvilIncludedBuild() -> {
target.layout.buildDirectory.set(target.file("build/included-build"))
jvmTarget.set(JvmTarget.fromInt(target.jvmTargetInt()))
}
}

// Set the kase working directory ('<build directory>/kase/<test|gradleTest>') as a System property,
// so that it's in the right place for projects with relocated directories.
// https://github.com/rickbusarow/kase/blob/255db67f40d5ec83e31755bc9ce81b1a2b08cf11/kase/src/main/kotlin/com/rickbusarow/kase/files/HasWorkingDir.kt#L93-L96
target.tasks.withType(Test::class.java).configureEach { task ->
task.systemProperty(
"kase.baseWorkingDir",
target.buildDir().resolve("kase/${task.name}"),
)
}
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.squareup.anvil.conventions

import com.rickbusarow.kgx.checkProjectIsRoot
import com.squareup.anvil.benchmark.BenchmarkPlugin
import com.squareup.anvil.conventions.utils.isInAnvilRootBuild
import com.squareup.anvil.conventions.utils.isInAnvilBuild
import com.squareup.anvil.conventions.utils.libs
import org.gradle.api.Project

Expand All @@ -12,15 +12,11 @@ open class RootPlugin : BasePlugin() {

target.checkProjectIsRoot { "RootPlugin must only be applied to the root project" }

if (target.isInAnvilRootBuild()) {
if (target.isInAnvilBuild()) {
target.plugins.apply(BenchmarkPlugin::class.java)
}

target.plugins.apply("java-base")

if (target.gradle.includedBuilds.isNotEmpty()) {
target.plugins.apply(CompositePlugin::class.java)
}
}

override fun afterApply(target: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,16 @@ import org.gradle.plugin.devel.GradlePluginDevelopmentExtension
* - This build is included by the `:delegate` build in order to consume the gradle plugin.
*
* @see isInAnvilRootBuild to check if this project is part of the true root build
* @see isInAnvilIncludedBuild to check if this project is part of the plugin included build
*/
internal fun Project.isInAnvilBuild() = rootProject.name == "anvil"

/**
* Returns true if this project is in the root 'anvil' build, and it is the true root of the build.
*
* @see isInAnvilBuild
* @see isInAnvilIncludedBuild
*/
internal fun Project.isInAnvilRootBuild() = isInAnvilBuild() && gradle.parent == null

/**
* Returns true if this project is in the root 'anvil' build,
* but it is not the true root of the build.
* Unless something changes, that means it is included by the `:delegate` build.
*/
internal fun Project.isInAnvilIncludedBuild() = isInAnvilBuild() && gradle.parent != null

/**
* Returns true if this project is included in the 'delegate' build.
*/
internal fun Project.isInDelegateBuild() = rootProject.name == "delegate"

internal val Project.gradlePublishingExtension: PublishingExtension
get() = extensions.getByType(PublishingExtension::class.java)

Expand Down
1 change: 0 additions & 1 deletion build-logic/delegate/gradle.properties

This file was deleted.

Loading

0 comments on commit 1721c42

Please sign in to comment.