Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle resolved dependencies #32

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ public class CoreAnalyzer @JvmOverloads constructor(private val coreOptions: Cor
ensureActive()

val actionExecutor = projectConnection.action(
SquareBuildAction(
coreOptions.allowGradleParallel,
coreOptions.useIncludeBuild
)
SquareBuildAction(coreOptions.allowGradleParallel)
)
actionExecutor.withCancellationToken(cancellationTokenSource.token())
if (coreOptions.useBuildScan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public data class CoreOptions @JvmOverloads constructor(
/** Auto-injects the "com.squareup.tooling" plugin to all projects in the build */
val autoInjectPlugin: Boolean = true,

/** Include any "includeBuild" builds from the current build */
val useIncludeBuild: Boolean = true,

/** Gradle distribution file to use */
val gradleDistributionPath: Path? = null,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,24 @@ private class ProjectBuildAction(private val project: Model) : BuildAction<Squar
* Base build action to gather all [SquareProject] from the current build.
*/
internal class SquareBuildAction(
private val allowParallelConfiguration: Boolean,
private val useIncludeBuild: Boolean,
private val allowParallelConfiguration: Boolean
) : BuildAction<List<SquareProject>> {
override fun execute(controller: BuildController): List<SquareProject> {
// Run the ProjectBuildAction in parallel, if we can
val canRunParallel = controller.getCanQueryProjectModelInParallel(SquareProject::class.java)

val actions = buildList {
addAll(controller.buildModel.projects.asSequence().map { project -> ProjectBuildAction(project) })
// Include any builds along with the root build
if (useIncludeBuild) {
controller.buildModel.includedBuilds.forEach { build ->
addAll(
build.projects // All projects included in the "settings.gradle" file of all builds
.asSequence()
.map { project ->
return@map ProjectBuildAction(project)
}
)
}
controller.buildModel.includedBuilds.forEach { build ->
addAll(
build.projects // All projects included in the "settings.gradle" file of all builds
.asSequence()
.map { project ->
return@map ProjectBuildAction(project)
}
)
}

// The "BuildModel" is the Gradle build after evaluating the "settings.gradle" file
addAll(
controller.buildModel
.projects // All projects included in the "settings.gradle" file
.asSequence()
.map { project ->
return@map ProjectBuildAction(project)
}.toList()
)
}

if (actions.isEmpty()) return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.io.path.createDirectories
import kotlin.io.path.writeText
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.minutes

class AffectedPathsTest {
Expand Down Expand Up @@ -139,81 +140,7 @@ class AffectedPathsTest {

val result = analyzer.analyze()

assertContentEquals(listOf("build2/foobar", "app", "library"), result.projectMap.keys)
assertContentEquals(listOf("app", "app:debug:debugUnitTest", "library", "app:release:releaseUnitTest"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
}

@Test
fun `Ignores projects in included builds when useIncludeBuild is false`() = runTest(timeout = 3.minutes) {
// Prep
val build1 = root.resolve("build1").createDirectories()
val build2 = build1.resolve("build2").createDirectories()
createSettingsFile(
rootDir = build1,
contents = """
rootProject.name = 'blah'
includeBuild 'build2'
include 'app'
include 'library'
""".trimIndent()
)

createModule(
rootDir = build1,
name = "app",
contents =
"""
plugins {
id 'application'
}

dependencies {
implementation project(':library')
}
""".trimIndent()
)

createModule(
rootDir = build1,
name = "library",
contents =
"""
plugins {
id 'java'
}
""".trimIndent()
)

createSettingsFile(
rootDir = build2,
contents = """
rootProject.name = 'blah2'
include 'foobar'
""".trimIndent()
)

createModule(
rootDir = build2,
name = "foobar",
contents =
"""
plugins {
id 'java'
}
""".trimIndent()
)

val analyzer = CoreAnalyzer(
CoreOptions(
directory = build1,
changedFiles = listOf("library/build.gradle"),
useIncludeBuild = false
)
)

val result = analyzer.analyze()

assertContentEquals(listOf("app", "library"), result.projectMap.keys)
assertEquals(setOf("build2/foobar", "app", "library"), result.projectMap.keys)
assertContentEquals(listOf("app", "app:debug:debugUnitTest", "library", "app:release:releaseUnitTest"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
}

Expand Down Expand Up @@ -291,8 +218,11 @@ class AffectedPathsTest {

val result = analyzer.analyze()

assertContentEquals(listOf("build2/foobar", "app", "library"), result.projectMap.keys)
assertContentEquals(listOf("build2/foobar"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
assertEquals(setOf("build2/foobar", "app", "library"), result.projectMap.keys)
assertContentEquals(
listOf("app", "app:debug:debugUnitTest", "build2/foobar", "app:release:releaseUnitTest"),
result.affectedResults.flatMap { it.affectedProjectPaths }.distinct()
)
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ logback = "1.4.5"

koin = "3.3.0"

jgit = "6.4.0.202211300538-r"
jgit = "6.10.0.202406032230-r"

vanniktech = "0.25.3"

Expand Down
4 changes: 0 additions & 4 deletions tooling/models/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ plugins {
id 'square-lib'
id 'square-publishing'
}

dependencies {
compileOnly(gradleApi())
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.api.TestVariant
import com.android.build.gradle.api.UnitTestVariant
import com.squareup.tooling.models.SquareTestConfiguration
import com.squareup.tooling.support.core.extractors.extractSquareDependency
import com.squareup.tooling.support.core.extractors.extractResolvedProjectDependencies
import com.squareup.tooling.support.core.models.SquareTestConfiguration
import org.gradle.api.Project

Expand Down Expand Up @@ -55,6 +55,6 @@ private fun BaseVariant.extractSquareTestConfiguration(project: Project): Square
}.map { it.toRelativeString(project.projectDir) }
return SquareTestConfiguration(
srcs = dirs.toSet(),
deps = compileConfiguration.allDependencies.map { it.extractSquareDependency(project) }.toSet()
deps = compileConfiguration.extractResolvedProjectDependencies(project).toSet()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package com.squareup.tooling.support.android

import com.android.build.gradle.api.BaseVariant
import com.squareup.tooling.models.SquareDependency
import com.squareup.tooling.support.core.extractors.extractDependencies
import com.squareup.tooling.support.core.extractors.extractSquareDependency
import com.squareup.tooling.support.core.extractors.extractSquareDependencies
import org.gradle.api.Project
import java.io.File

Expand Down Expand Up @@ -77,8 +76,7 @@ internal fun BaseVariant.extractSquareVariantConfigurationParams(
addAll(compileConfiguration.extendsFrom.map { it.name })
}.toTypedArray()

val deps = project.configurations.extractDependencies(*configNames)
.map { it.extractSquareDependency(project) }
val deps = project.configurations.extractSquareDependencies(project, *configNames)

return srcs to deps.toSet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class TestExtractorsTest {
val squareUnitTestConfiguration = unitTestVariant
.extractSquareTestConfiguration(appProject)

assertEquals(1, squareUnitTestConfiguration.deps.size)
assertTrue { squareUnitTestConfiguration.deps.all { it.target == "/app" } }
assertEquals(2, squareUnitTestConfiguration.deps.size)
assertTrue { squareUnitTestConfiguration.deps.any { it.target == "/app" } }

// Test the TestVariant
val testVariant = appProject.extensions.getByType(AppExtension::class.java).testVariants.first()
Expand All @@ -66,7 +66,7 @@ class TestExtractorsTest {
squareTestConfiguration.srcs.all { it.startsWith("src/") },
"All source paths must be relative to the project dir"
)
assertEquals(1, squareTestConfiguration.deps.size)
assertTrue { squareTestConfiguration.deps.all { it.target == "/app" } }
assertEquals(2, squareTestConfiguration.deps.size)
assertTrue { squareTestConfiguration.deps.any { it.target == "/app" } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class VariantExtractorsTest {
"All source paths must be relative to the project dir"
)
assertEquals(24, srcs.size, "Sources were missing")
assertTrue("No dependencies should be listed") { deps.isEmpty() }
// Filter out the kotlin-stdlib-jdk8 dependency
val filteredDeps = deps.filterNot {
it.target.contains("kotlin-stdlib")
}
assertTrue("No dependencies should be listed") { filteredDeps.isEmpty() }
}

@Test
Expand Down
Loading
Loading