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

Refactor GradleProjectInfo to use data from workspace dependencies instead of Configuration API #117

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ internal interface GrazelComponent {
fun migrationChecker(): Lazy<MigrationChecker>
fun projectBazelFileBuilderFactory(): Lazy<ProjectBazelFileBuilder.Factory>
fun workspaceBuilderFactory(): Lazy<WorkspaceBuilder.Factory>
fun rootBazelFileBuilder(): Lazy<RootBazelFileBuilder>
fun rootBazelFileFactory(): Lazy<RootBazelFileBuilder.Factory>
fun gradleProjectInfoFactory(): Lazy<DefaultGradleProjectInfo.Factory>
fun artifactPinner(): Lazy<ArtifactPinner>
fun dependenciesDataSource(): Lazy<DependenciesDataSource>
fun mavenInstallArtifactsCalculator(): Lazy<MavenInstallArtifactsCalculator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.grab.grazel.gradle

import com.grab.grazel.GrazelExtension
import com.grab.grazel.bazel.rules.DAGGER_GROUP
import com.grab.grazel.di.qualifiers.RootProject
import com.grab.grazel.gradle.dependencies.DependenciesDataSource
import com.grab.grazel.gradle.dependencies.DependencyGraphs
import com.grab.grazel.gradle.dependencies.model.WorkspaceDependencies
import dagger.Lazy
import org.gradle.api.Project
import javax.inject.Inject
Expand All @@ -33,35 +32,44 @@ interface GradleProjectInfo {
val rootProject: Project
val grazelExtension: GrazelExtension
val hasDagger: Boolean
val hasDatabinding: Boolean
val hasAndroidExtension: Boolean
val hasGooglePlayServices: Boolean
}

@Singleton
@Suppress("UnstableApiUsage")
internal class DefaultGradleProjectInfo @Inject constructor(
@param:RootProject
internal class DefaultGradleProjectInfo(
override val rootProject: Project,
override val grazelExtension: GrazelExtension,
private val dependencyGraphsProvider: Lazy<DependencyGraphs>,
internal val dependenciesDataSource: DependenciesDataSource
private val workspaceDependencies: WorkspaceDependencies
) : GradleProjectInfo {

@Singleton
class Factory
@Inject constructor(
@param:RootProject
private val rootProject: Project,
private val grazelExtension: GrazelExtension,
private val dependencyGraphsProvider: Lazy<DependencyGraphs>,
) {
fun create(
workspaceDependencies: WorkspaceDependencies
): GradleProjectInfo = DefaultGradleProjectInfo(
rootProject,
grazelExtension,
dependencyGraphsProvider,
workspaceDependencies
)
}

private val projectGraph: DependencyGraphs get() = dependencyGraphsProvider.get()

override val hasDagger: Boolean by lazy {
projectGraph.nodes().any { project ->
dependenciesDataSource
.mavenDependencies(project)
.any { dependency -> dependency.group == DAGGER_GROUP }
}
}

override val hasDatabinding: Boolean by lazy {
projectGraph
.nodes()
.any { it.hasDatabinding }
workspaceDependencies
.result
.values
.parallelStream()
.flatMap { it.stream() }
.anyMatch { it.shortId.contains("com.google.dagger") }
}

override val hasAndroidExtension: Boolean by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.internal.artifacts.DefaultResolvedDependency
import java.io.File
import java.util.TreeSet
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -77,14 +77,6 @@ internal data class ArtifactsConfig(
)

internal interface DependenciesDataSource {
/**
* Return the project's maven dependencies before the resolution strategy and any other custom substitution by Gradle
*/
fun mavenDependencies(
project: Project,
vararg buildGraphTypes: BuildGraphType
): Sequence<Dependency>

/**
* Return the project's project (module) dependencies before the resolution strategy and any other custom
* substitutions by Gradle
Expand Down Expand Up @@ -171,31 +163,6 @@ internal class DefaultDependenciesDataSource @Inject constructor(
.any { MavenArtifact(it.moduleGroup, it.moduleName).isIgnored }
}

override fun mavenDependencies(
project: Project,
vararg buildGraphTypes: BuildGraphType
): Sequence<Dependency> {
return declaredDependencies(
project,
*buildGraphTypes.map { it.configurationScope }.toTypedArray()
).filter { (configuration, _) ->
if (buildGraphTypes.isEmpty()) {
true
} else {
configurationDataSource.isThisConfigurationBelongsToThisVariants(
project,
*buildGraphTypes.map { it.variant }.toTypedArray(),
configuration = configuration
)
}
}.map { it.second }
.filter { it.group != null && it.group !in IGNORED_ARTIFACT_GROUPS }
.filter {
val artifact = MavenArtifact(it.group, it.name)
!artifact.isExcluded && !artifact.isIgnored
}.filter { it !is ProjectDependency }
}

override fun projectDependencies(
project: Project, vararg scopes: ConfigurationScope
) = declaredDependencies(project, *scopes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ import com.grab.grazel.bazel.starlark.statements
import com.grab.grazel.gradle.GradleProjectInfo
import com.grab.grazel.migrate.BazelFileBuilder
import javax.inject.Inject
import javax.inject.Singleton

internal class RootBazelFileBuilder @Inject constructor(
internal class RootBazelFileBuilder(
private val gradleProjectInfo: GradleProjectInfo,
private val grazelExtension: GrazelExtension
private val grazelExtension: GrazelExtension,
) : BazelFileBuilder {

@Singleton
class Factory
@Inject
constructor(private val grazelExtension: GrazelExtension) {
fun create(gradleProjectInfo: GradleProjectInfo) = RootBazelFileBuilder(
gradleProjectInfo,
grazelExtension
)
}

override fun build(): List<Statement> = statements {
setupKotlin()
if (gradleProjectInfo.hasDagger) daggerBuildRules()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.grab.grazel.bazel.starlark.StatementsBuilder
import com.grab.grazel.bazel.starlark.add
import com.grab.grazel.bazel.starlark.statements
import com.grab.grazel.di.qualifiers.RootProject
import com.grab.grazel.gradle.DefaultGradleProjectInfo
import com.grab.grazel.gradle.GradleProjectInfo
import com.grab.grazel.gradle.dependencies.model.WorkspaceDependencies
import com.grab.grazel.gradle.isAndroidApplication
Expand All @@ -59,20 +60,23 @@ internal class WorkspaceBuilder(
private val mavenInstallArtifactsCalculator: MavenInstallArtifactsCalculator
) : BazelFileBuilder {
@Singleton
class Factory @Inject constructor(
class Factory
@Inject
constructor(
@param:RootProject private val rootProject: Project,
private val grazelExtension: GrazelExtension,
private val gradleProjectInfo: GradleProjectInfo,
private val mavenInstallArtifactsCalculator: MavenInstallArtifactsCalculator
private val gradleProjectInfoFactory: DefaultGradleProjectInfo.Factory,
private val mavenInstallArtifactsCalculator: MavenInstallArtifactsCalculator,
) {
fun create(
projectsToMigrate: List<Project>,
gradleProjectInfo: GradleProjectInfo,
workspaceDependencies: WorkspaceDependencies = WorkspaceDependencies(emptyMap()),
) = WorkspaceBuilder(
rootProject,
projectsToMigrate,
grazelExtension,
gradleProjectInfo,
gradleProjectInfoFactory.create(workspaceDependencies),
workspaceDependencies,
mavenInstallArtifactsCalculator
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.grab.grazel.tasks.internal
import com.grab.grazel.bazel.starlark.writeToFile
import com.grab.grazel.di.GrazelComponent
import com.grab.grazel.di.qualifiers.RootProject
import com.grab.grazel.gradle.DefaultGradleProjectInfo
import com.grab.grazel.gradle.GradleProjectInfo
import com.grab.grazel.gradle.MigrationChecker
import com.grab.grazel.gradle.dependencies.DefaultDependencyResolutionService
import com.grab.grazel.migrate.internal.RootBazelFileBuilder
Expand Down Expand Up @@ -49,7 +51,8 @@ internal open class GenerateRootBazelScriptsTask
constructor(
private val migrationChecker: Lazy<MigrationChecker>,
private val workspaceBuilderFactory: Lazy<WorkspaceBuilder.Factory>,
private val rootBazelBuilder: Lazy<RootBazelFileBuilder>,
private val rootBazelBuilderFactory: Lazy<RootBazelFileBuilder.Factory>,
private val gradleProjectInfoFactory: Lazy<DefaultGradleProjectInfo.Factory>,
objectFactory: ObjectFactory,
layout: ProjectLayout
) : DefaultTask() {
Expand Down Expand Up @@ -77,18 +80,23 @@ constructor(
val projectsToMigrate = rootProject
.subprojects
.filter { migrationChecker.get().canMigrate(it) }
val workspaceDependencies = dependencyResolutionService
.get()
.get(workspaceDependencies.get().asFile)

workspaceBuilderFactory.get()
.create(
projectsToMigrate = projectsToMigrate,
workspaceDependencies = dependencyResolutionService
.get()
.get(workspaceDependencies.get().asFile)
).build()
.writeToFile(workspaceFile.get().asFile)
val gradleProjectInfo: GradleProjectInfo = gradleProjectInfoFactory.get()
.create(workspaceDependencies)

workspaceBuilderFactory.get().create(
projectsToMigrate = projectsToMigrate,
gradleProjectInfo = gradleProjectInfo,
workspaceDependencies = workspaceDependencies
).build().writeToFile(workspaceFile.get().asFile)
logger.quiet("Generated WORKSPACE".ansiGreen)

val rootBuildBazelContents = rootBazelBuilder.get().build()
val rootBuildBazelContents = rootBazelBuilderFactory.get()
.create(gradleProjectInfo)
.build()
if (rootBuildBazelContents.isNotEmpty()) {
rootBuildBazelContents.writeToFile(buildBazel.get().asFile)
logger.quiet("Generated $BUILD_BAZEL".ansiGreen)
Expand All @@ -106,7 +114,8 @@ constructor(
TASK_NAME,
grazelComponent.migrationChecker(),
grazelComponent.workspaceBuilderFactory(),
grazelComponent.rootBazelFileBuilder(),
grazelComponent.rootBazelFileFactory(),
grazelComponent.gradleProjectInfoFactory(),
rootProject.objects,
rootProject.layout
).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.grab.grazel.bazel.starlark.asString
import com.grab.grazel.bazel.starlark.statements
import com.grab.grazel.buildProject
import com.grab.grazel.gradle.ANDROID_APPLICATION_PLUGIN
import com.grab.grazel.gradle.dependencies.model.WorkspaceDependencies
import com.grab.grazel.util.createGrazelComponent
import com.grab.grazel.util.doEvaluate
import org.gradle.api.Project
Expand All @@ -36,11 +37,15 @@ class AndroidWorkspaceRepositoriesTest : GrazelPluginTest() {
@Test
fun `assert android sdk repository is generated based on values from android binary target`() {
val buildRootProject = buildRootProject()
val workspaceBuilder = buildRootProject
.createGrazelComponent()
val grazelComponent = buildRootProject.createGrazelComponent()
val gradleProjectInfo = grazelComponent.gradleProjectInfoFactory().get()
val workspaceBuilder = grazelComponent
.workspaceBuilderFactory()
.get()
.create(listOf(buildRootProject))
.create(
listOf(buildRootProject),
gradleProjectInfo.create(WorkspaceDependencies(emptyMap())),
)

val generatedCode = statements {
workspaceBuilder.addAndroidSdkRepositories(this)
Expand All @@ -56,11 +61,15 @@ class AndroidWorkspaceRepositoriesTest : GrazelPluginTest() {
@Test
fun `assert android ndk repository is generated with empty path values`() {
val buildRootProject = buildRootProject()
val workspaceBuilder = buildRootProject
.createGrazelComponent()
val grazelComponent = buildRootProject.createGrazelComponent()
val gradleProjectInfo = grazelComponent.gradleProjectInfoFactory().get()
val workspaceBuilder = grazelComponent
.workspaceBuilderFactory()
.get()
.create(listOf(buildRootProject))
.create(
listOf(buildRootProject),
gradleProjectInfo.create(WorkspaceDependencies(emptyMap())),
)
val generatedCode = statements {
workspaceBuilder.addAndroidSdkRepositories(this)
}.asString()
Expand All @@ -77,11 +86,15 @@ class AndroidWorkspaceRepositoriesTest : GrazelPluginTest() {
val buildRootProject = buildRootProject {
android.ndkApiLevel = ndkApiLevel
}
val workspaceBuilder = buildRootProject
.createGrazelComponent()
val grazelComponent = buildRootProject.createGrazelComponent()
val gradleProjectInfo = grazelComponent.gradleProjectInfoFactory().get()
val workspaceBuilder = grazelComponent
.workspaceBuilderFactory()
.get()
.create(listOf(buildRootProject))
.create(
listOf(buildRootProject),
gradleProjectInfo.create(WorkspaceDependencies(emptyMap())),
)
val generatedCode = statements {
workspaceBuilder.addAndroidSdkRepositories(this)
}.asString()
Expand Down
Loading
Loading