Skip to content

Commit

Permalink
use type-safe accessor "path" when adding a dependency with type-safe…
Browse files Browse the repository at this point in the history
… syntax

fixes #569
progress toward #52
  • Loading branch information
RBusarow committed May 12, 2022
1 parent 1f080d6 commit 0c6bf92
Show file tree
Hide file tree
Showing 65 changed files with 867 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import modulecheck.parsing.source.AndroidResourceDeclaredName
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.isAndroid
import modulecheck.project.project
import modulecheck.utils.LazySet
import modulecheck.utils.SafeCache
import modulecheck.utils.dataSource
Expand Down Expand Up @@ -75,7 +76,7 @@ data class AndroidResourceDeclaredNames(

val transitiveSourceSetName = tpd.source.declaringSourceSetName()

tpd.contributed.project
tpd.contributed.project(project.projectCache)
.androidUnqualifiedDeclarationNamesForSourceSetName(transitiveSourceSetName)
} else flowOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.mapNotNull
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.parsing.source.AnvilScopeName
import modulecheck.parsing.source.AnvilScopeNameEntry
Expand All @@ -28,9 +29,9 @@ import modulecheck.parsing.source.JvmFile
import modulecheck.parsing.source.KotlinFile
import modulecheck.parsing.source.RawAnvilAnnotatedType
import modulecheck.parsing.source.asExplicitKotlinReference
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.project
import modulecheck.utils.SafeCache
import org.jetbrains.kotlin.name.FqName

Expand Down Expand Up @@ -163,7 +164,7 @@ data class AnvilGraph(
.orEmpty()
.asFlow()
.mapNotNull { cpd ->
cpd.project
cpd.project(project.projectCache)
.declarations()
.get(SourceSetName.MAIN, includeUpstream = true)
.filter { maybeExtraReferences.contains(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

package modulecheck.api.context

import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.project
import modulecheck.utils.SafeCache

data class AnvilScopeDependencies(
Expand All @@ -43,7 +44,7 @@ data class AnvilScopeDependencies(
.filter { cpd ->

val contributed = cpd
.project
.project(project.projectCache)
.anvilScopeContributionsForSourceSetName(cpd.configurationName.toSourceSetName())

contributed.any { (scopeName, _) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package modulecheck.api.context

import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.parsing.gradle.model.TransitiveProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.TransitiveProjectDependency
import modulecheck.utils.SafeCache
import modulecheck.utils.mapToSet

Expand Down Expand Up @@ -57,20 +57,20 @@ data class ClasspathDependencies(
)

val directDependencies = projectDependencies[sourceSetName]
.filterNot { it.project == project }
.filterNot { it.project() == project }

val directDependencyPaths = directDependencies.mapToSet { it.project.path }
val directDependencyPaths = directDependencies.mapToSet { it.path }

val inherited = directDependencies.flatMap { sourceCpd ->
sourceApiConfigs(sourceSetName, sourceCpd.isTestFixture)
.flatMap { apiConfig ->

sourceCpd.project
sourceCpd.project()
.classpathDependencies()
.get(apiConfig.toSourceSetName())
.asSequence()
.filter { it.contributed.configurationName.isApi() }
.filterNot { it.contributed.project.path in directDependencyPaths }
.filterNot { it.contributed.path in directDependencyPaths }
.map { transitiveCpd ->
TransitiveProjectDependency(sourceCpd, transitiveCpd.contributed)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import kotlinx.coroutines.flow.toList
import modulecheck.api.context.Declarations.DeclarationsKey.ALL
import modulecheck.api.context.Declarations.DeclarationsKey.WithUpstream
import modulecheck.api.context.Declarations.DeclarationsKey.WithoutUpstream
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.parsing.source.DeclaredName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectCache
import modulecheck.project.ProjectContext
import modulecheck.project.isAndroid
import modulecheck.utils.LazySet
Expand Down Expand Up @@ -113,7 +114,10 @@ data class Declarations private constructor(

suspend fun ProjectContext.declarations(): Declarations = get(Declarations)

suspend fun ConfiguredProjectDependency.declarations(): LazySet<DeclaredName> {
suspend fun ConfiguredProjectDependency.declarations(
projectCache: ProjectCache
): LazySet<DeclaredName> {
val project = projectCache.getValue(path)
if (isTestFixture) {
return project.declarations().get(SourceSetName.TEST_FIXTURES, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package modulecheck.api.context

import modulecheck.api.context.DependencySources.SourceResult.Found
import modulecheck.api.context.DependencySources.SourceResult.NOT_PRESENT
import modulecheck.parsing.gradle.model.ProjectPath.StringProjectPath
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.ProjectPath
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.utils.SafeCache
Expand All @@ -31,7 +31,7 @@ data class DependencySources(

data class SourceKey(
val sourceSetName: SourceSetName,
val dependencyProjectPath: StringProjectPath,
val dependencyProjectPath: ProjectPath,
val isTestFixture: Boolean
)

Expand All @@ -44,7 +44,7 @@ data class DependencySources(
get() = DependencySources

suspend fun sourceOfOrNull(
dependencyProjectPath: StringProjectPath,
dependencyProjectPath: ProjectPath,
sourceSetName: SourceSetName,
isTestFixture: Boolean
): ConfiguredProjectDependency? {
Expand All @@ -60,7 +60,7 @@ data class DependencySources(
val sourceOrNull = project.classpathDependencies()
.get(sourceSetName)
.firstOrNull { transitive ->
transitive.contributed.project.path == dependencyProjectPath &&
transitive.contributed.path == dependencyProjectPath &&
transitive.contributed.isTestFixture == isTestFixture
}
?.source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import modulecheck.parsing.gradle.model.ProjectPath.StringProjectPath
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.project
import modulecheck.utils.SafeCache

data class Depths(
Expand All @@ -43,7 +44,7 @@ data class Depths(

private suspend fun fetchForSourceSet(sourceSetName: SourceSetName): ProjectDepth {
val (childDepth, children) = project.projectDependencies[sourceSetName]
.map { it.project }
.map { it.project(project.projectCache) }
.distinct()
.map { it.depthForSourceSetName(SourceSetName.MAIN) }
.groupBy { it.depth }
Expand Down Expand Up @@ -99,7 +100,8 @@ data class ProjectDepth(
val children = dependentProject
.projectDependencies[sourceSetName]
.flatMap {
it.project.depthForSourceSetName(SourceSetName.MAIN)
it.project(dependentProject.projectCache)
.depthForSourceSetName(SourceSetName.MAIN)
.fullTree(SourceSetName.MAIN)
}
children.toSet() + this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

package modulecheck.api.context

import modulecheck.project.DownstreamDependency
import modulecheck.parsing.gradle.model.DownstreamDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.project
import modulecheck.utils.flatMapToSet

data class DownstreamProjects(
Expand All @@ -37,10 +38,10 @@ data class DownstreamProjects(
otherProject
.classpathDependencies()
.all()
.filter { it.contributed.project == project }
.filter { it.contributed.project(project.projectCache) == project }
.map { transitive ->
DownstreamDependency(
dependentProject = otherProject,
dependentProjectPath = otherProject.path,
configuredProjectDependency = transitive.withContributedConfiguration().contributed
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package modulecheck.api.context

import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.parsing.gradle.model.ConfiguredDependency
import modulecheck.parsing.gradle.model.all
import modulecheck.project.ConfiguredDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.ProjectContext.Element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package modulecheck.api.context

import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.all
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext

Expand Down Expand Up @@ -51,7 +51,7 @@ data class PublicDependencies(
val inherited = combined
.flatMap { cpd ->
cpd
.project
.project()
.allPublicClassPathDependencyDeclarations(false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.parsing.source.DeclaredName
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.project
import modulecheck.utils.SafeCache

data class ResolvedDeclaredNames internal constructor(
Expand Down Expand Up @@ -72,15 +73,16 @@ data class ResolvedDeclaredNames internal constructor(
.get(sourceSetName)
.asSequence()
.map { it.contributed }
.distinctBy { it.project to it.isTestFixture }
.distinctBy { it.project(project.projectCache) to it.isTestFixture }
.firstNotNullOfOrNull { sourceCpd ->

listOfNotNull(
SourceSetName.MAIN,
SourceSetName.TEST_FIXTURES.takeIf { sourceCpd.isTestFixture }
)
.firstNotNullOfOrNull { dependencySourceSetName ->
sourceCpd.project.resolvedDeclaredNames()
sourceCpd.project(project.projectCache)
.resolvedDeclaredNames()
.getSource(declaredName, dependencySourceSetName)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package modulecheck.api.context

import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.parsing.gradle.model.TransitiveProjectDependency
import modulecheck.project.McProject
import modulecheck.project.ProjectContext
import modulecheck.project.TransitiveProjectDependency
import modulecheck.utils.SafeCache

data class SourceSetDependencies(
Expand All @@ -45,19 +45,19 @@ data class SourceSetDependencies(
)

val directDependencies = projectDependencies[sourceSetName]
.filterNot { it.project == project }
.filterNot { it.path == project.path }
.toSet()

val directDependencyPaths = directDependencies.map { it.project.path }.toSet()
val directDependencyPaths = directDependencies.map { it.path }.toSet()

val inherited = directDependencies.flatMap { sourceCpd ->
sourceConfigs(sourceCpd.isTestFixture)
.flatMap { dependencySourceSetName ->

sourceCpd.project
sourceCpd.project()
.sourceSetDependencies()
.get(dependencySourceSetName)
.filterNot { it.contributed.project.path in directDependencyPaths }
.filterNot { it.contributed.path in directDependencyPaths }
.map { transitiveCpd ->
TransitiveProjectDependency(sourceCpd, transitiveCpd.contributed)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import modulecheck.finding.FindingName
import modulecheck.finding.internal.positionIn
import modulecheck.finding.internal.statementOrNullIn
import modulecheck.parsing.gradle.dsl.BuildFileStatement
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.utils.LazyDeferred
import modulecheck.utils.lazyDeferred
Expand Down Expand Up @@ -51,10 +51,10 @@ data class InheritedDependencyFinding(
}

override fun fromStringOrEmpty(): String {
return if (dependency.path == source.project.path) {
return if (dependency.path == source.path) {
""
} else {
source.project.path.value
source.path.value
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import modulecheck.finding.RemovesDependency
import modulecheck.finding.internal.statementOrNullIn
import modulecheck.parsing.gradle.dsl.BuildFileStatement
import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.utils.LazyDeferred
import modulecheck.utils.lazyDeferred
Expand Down Expand Up @@ -53,10 +53,10 @@ data class MustBeApiFinding(
}

override fun fromStringOrEmpty(): String {
return if (dependency.path == source?.project?.path) {
return if (dependency.path == source?.path) {
""
} else {
"${source?.project?.path?.value}"
"${source?.path?.value}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import modulecheck.finding.internal.positionOfStatement
import modulecheck.finding.internal.statementOrNullIn
import modulecheck.parsing.gradle.dsl.BuildFileStatement
import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.project.McProject
import modulecheck.utils.LazyDeferred
import modulecheck.utils.lazyDeferred
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import modulecheck.finding.Deletable
import modulecheck.finding.FindingName
import modulecheck.finding.RemovesDependency
import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.project.McProject

data class RedundantDependency(
Expand Down Expand Up @@ -54,14 +54,14 @@ data class RedundantDependencyFinding(
"declared in the current module. This is technically unnecessary if a \"minimalist\" build " +
"file is desired."

override val dependencyIdentifier = oldDependency.project.path.value + fromStringOrEmpty()
override val dependencyIdentifier = oldDependency.path.value + fromStringOrEmpty()

override fun fromStringOrEmpty(): String {

return if (from.all { dependency.path == it.project.path }) {
return if (from.all { dependency.path == it.path }) {
""
} else {
from.joinToString { it.project.path.value }
from.joinToString { it.path.value }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import modulecheck.finding.Deletable
import modulecheck.finding.FindingName
import modulecheck.finding.RemovesDependency
import modulecheck.parsing.gradle.model.ConfigurationName
import modulecheck.project.ConfiguredProjectDependency
import modulecheck.parsing.gradle.model.ConfiguredProjectDependency
import modulecheck.project.McProject

data class UnusedDependency(
Expand Down
Loading

0 comments on commit 0c6bf92

Please sign in to comment.