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

Change dependency distribution #137

Merged
merged 2 commits into from
May 10, 2022
Merged
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 @@ -10,6 +10,7 @@ import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSFile
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
import tech.antibytes.kmock.processor.ProcessorContract.Aggregated
import tech.antibytes.kmock.processor.ProcessorContract.KmpCodeGenerator
Expand Down Expand Up @@ -51,6 +52,10 @@ internal class KMockProcessor(
return commonAggregated.extractedTemplates.isEmpty()
}

private fun List<KSFile>.merge(toAdd: List<KSFile>): List<KSFile> {
return this.toMutableSet().also { it.addAll(toAdd) }.toList()
}

private fun <T : Source> mergeSources(
rootSource: Aggregated<T>,
dependentSource: Aggregated<T>,
Expand All @@ -62,9 +67,7 @@ internal class KMockProcessor(
extractedTemplates = rootSource.extractedTemplates.toMutableList().also {
it.addAll(filteredTemplates)
},
dependencies = rootSource.dependencies.toMutableList().also {
it.addAll(dependentSource.dependencies)
}
totalDependencies = rootSource.totalDependencies.merge(dependentSource.totalDependencies)
)
}

Expand Down Expand Up @@ -125,14 +128,13 @@ internal class KMockProcessor(
mockGenerator.writeCommonMocks(
templateSources = singleCommonSources.extractedTemplates,
templateMultiSources = commonMultiAggregated,
dependencies = singleCommonSources.dependencies,
relaxer = relaxer,
)

if (interfaceBinderIsApplicable(multiCommonSources.extractedTemplates)) {
interfaceGenerator.bind(
templateSources = multiCommonSources.extractedTemplates,
dependencies = multiCommonSources.dependencies
dependencies = multiCommonSources.totalDependencies,
)
}

Expand All @@ -149,18 +151,18 @@ internal class KMockProcessor(
): Aggregated<TemplateSource> {
val aggregated = singleSourceAggregator.extractSharedInterfaces(resolver)
val filteredInterfaces = filter.filter(
filter.filterSharedSources(aggregated.extractedTemplates),
commonAggregated.extractedTemplates
templateSources = filter.filterSharedSources(aggregated.extractedTemplates),
filteredBy = commonAggregated.extractedTemplates
)

entryPointGenerator.generateShared(
filteredInterfaces,
templateSources = filteredInterfaces,
dependencies = aggregated.totalDependencies
)

mockGenerator.writeSharedMocks(
filteredInterfaces,
aggregated.dependencies,
relaxer
templateSources = filteredInterfaces,
relaxer = relaxer
)

return mergeSources(commonAggregated, aggregated, filteredInterfaces)
Expand All @@ -173,15 +175,18 @@ internal class KMockProcessor(
): Aggregated<TemplateSource> {
val aggregated = singleSourceAggregator.extractPlatformInterfaces(resolver)
val filteredInterfaces = filter.filter(
aggregated.extractedTemplates,
sharedAggregated.extractedTemplates
templateSources = aggregated.extractedTemplates,
filteredBy = sharedAggregated.extractedTemplates
)
val totalAggregated = mergeSources(
rootSource = sharedAggregated,
dependentSource = aggregated,
filteredTemplates = filteredInterfaces,
)
val totalAggregated = mergeSources(sharedAggregated, aggregated, filteredInterfaces)

mockGenerator.writePlatformMocks(
filteredInterfaces,
aggregated.dependencies,
relaxer
templateSources = filteredInterfaces,
relaxer = relaxer,
)

return totalAggregated
Expand All @@ -201,10 +206,7 @@ internal class KMockProcessor(

Pair(commonAggregated, sharedAggregated)
} else {
Pair(
Aggregated(emptyList(), emptyList(), emptyList()),
Aggregated(emptyList(), emptyList(), emptyList())
)
Pair(EMPTY_AGGREGATED_SINGLE, EMPTY_AGGREGATED_SINGLE)
}
}

Expand All @@ -214,18 +216,21 @@ internal class KMockProcessor(
relaxer: Relaxer?
) {
if (isFirstRound) {
val totalDependencies = totalAggregated.totalDependencies.merge(commonMultiAggregated.totalDependencies)

factoryGenerator.writeFactories(
templateSources = totalAggregated.extractedTemplates,
templateMultiSources = commonMultiAggregated.extractedTemplates,
dependencies = totalAggregated.dependencies,
relaxer = relaxer
relaxer = relaxer,
dependencies = totalDependencies
)

entryPointGenerator.generateCommon(
templateSources = commonAggregated.extractedTemplates,
templateMultiSources = commonMultiAggregated.extractedTemplates,
totalTemplates = totalAggregated.extractedTemplates,
totalMultiSources = commonMultiAggregated.extractedTemplates,
dependencies = totalDependencies,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ internal interface ProcessorContract {
val indicator: String
val templateName: String
val packageName: String
val dependencies: List<KSFile>
}

data class TemplateSource(
override val indicator: String,
override val templateName: String,
override val packageName: String,
override val dependencies: List<KSFile>,
val template: KSClassDeclaration,
val generics: Map<String, List<KSTypeReference>>?
) : Source
Expand All @@ -95,14 +97,15 @@ internal interface ProcessorContract {
override val indicator: String,
override val templateName: String,
override val packageName: String,
override val dependencies: List<KSFile>,
val templates: List<KSClassDeclaration>,
val generics: List<Map<String, List<KSTypeReference>>?>
) : Source

data class Aggregated<out T : Source>(
val illFormed: List<KSAnnotated>,
val extractedTemplates: List<T>,
val dependencies: List<KSFile>
val totalDependencies: List<KSFile>,
)

interface SourceSetValidator {
Expand Down Expand Up @@ -335,20 +338,17 @@ internal interface ProcessorContract {
interface MockGenerator {
fun writePlatformMocks(
templateSources: List<TemplateSource>,
dependencies: List<KSFile>,
relaxer: Relaxer?
)

fun writeSharedMocks(
templateSources: List<TemplateSource>,
dependencies: List<KSFile>,
relaxer: Relaxer?
)

fun writeCommonMocks(
templateSources: List<TemplateSource>,
templateMultiSources: Aggregated<TemplateMultiSource>,
dependencies: List<KSFile>,
relaxer: Relaxer?
)
}
Expand Down Expand Up @@ -435,21 +435,23 @@ internal interface ProcessorContract {
fun writeFactories(
templateSources: List<TemplateSource>,
templateMultiSources: List<TemplateMultiSource>,
dependencies: List<KSFile>,
relaxer: Relaxer?,
dependencies: List<KSFile>,
)
}

interface MockFactoryEntryPointGenerator {
fun generateCommon(
templateSources: List<TemplateSource>,
templateMultiSources: List<TemplateMultiSource>,
totalMultiSources: List<TemplateMultiSource>,
totalTemplates: List<TemplateSource>,
totalMultiSources: List<TemplateMultiSource>,
dependencies: List<KSFile>,
)

fun generateShared(
templateSources: List<TemplateSource>,
dependencies: List<KSFile>,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,12 @@ internal class KMockMultiSourceAggregator(
private val generics: GenericResolver,
private val customAnnotations: Map<String, String>,
) : MultiSourceAggregator, BaseSourceAggregator(logger, customAnnotations, generics) {
private fun isPackageIndicator(char: Char) = char == '.'

private fun resolvePackageName(
currentName: String?,
nameCandidate: String
): String {
val currentDepth = currentName?.count(::isPackageIndicator) ?: Int.MAX_VALUE
val candidateDepth = nameCandidate.count(::isPackageIndicator)

return if (currentDepth > candidateDepth) {
nameCandidate
} else {
currentName!!
}
}

private fun resolveInterfaces(
interfaceName: String,
interfaces: List<KSDeclaration>,
sourceIndicator: String,
templateCollector: MutableMap<String, TemplateMultiSource>
dependencies: List<KSFile>,
templateCollector: MutableMap<String, TemplateMultiSource>,
) {
val interfazes: MutableList<KSClassDeclaration> = mutableListOf()
val generics: MutableList<Map<String, List<KSTypeReference>>?> = mutableListOf()
Expand All @@ -72,24 +57,26 @@ internal class KMockMultiSourceAggregator(
indicator = sourceIndicator,
templateName = interfaceName,
packageName = rootPackage,
dependencies = dependencies,
templates = interfazes,
generics = generics
generics = generics,
)
}

private fun List<KSType>.extractDeclarations(): List<KSDeclaration> = this.map { type -> type.declaration }

private fun resolveInterfaces(
raw: Map<String, List<Pair<String, List<KSType>>>>,
templateCollector: MutableMap<String, TemplateMultiSource>
raw: Map<String, List<Triple<String, List<KSFile>, List<KSType>>>>,
templateCollector: MutableMap<String, TemplateMultiSource>,
) {
raw.forEach { (sourceIndicator, interfaces) ->
interfaces.forEach { interfacesBundle ->
resolveInterfaces(
interfaceName = interfacesBundle.first,
interfaces = interfacesBundle.second.extractDeclarations(),
interfaces = interfacesBundle.third.extractDeclarations(),
sourceIndicator = sourceIndicator,
templateCollector = templateCollector
dependencies = interfacesBundle.second,
templateCollector = templateCollector,
)
}
}
Expand Down Expand Up @@ -117,7 +104,7 @@ internal class KMockMultiSourceAggregator(
condition: (String, KSAnnotation) -> Boolean,
): Aggregated<TemplateMultiSource> {
val illAnnotated = mutableListOf<KSAnnotated>()
val typeContainer = mutableMapOf<String, MutableList<Pair<String, List<KSType>>>>()
val typeContainer = mutableMapOf<String, MutableList<Triple<String, List<KSFile>, List<KSType>>>>()
val templateCollector: MutableMap<String, TemplateMultiSource> = mutableMapOf()
val fileCollector: MutableList<KSFile> = mutableListOf()

Expand All @@ -131,22 +118,26 @@ internal class KMockMultiSourceAggregator(
val interfaces = typeContainer.getOrElse(sourceIndicator) { mutableListOf() }

interfaces.add(
Pair(
Triple(
determineMockName(annotation),
annotation.arguments.last().value as List<KSType>
listOf(annotatedSymbol.containingFile!!),
annotation.arguments.last().value as List<KSType>,
)
)
typeContainer[sourceIndicator] = interfaces
fileCollector.add(annotatedSymbol.containingFile!!)
}
}

resolveInterfaces(typeContainer, templateCollector)
resolveInterfaces(
typeContainer,
templateCollector
)

return Aggregated(
illAnnotated,
templateCollector.values.toList(),
fileCollector
illFormed = illAnnotated,
extractedTemplates = templateCollector.values.toList(),
totalDependencies = fileCollector
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ internal class KMockSingleSourceAggregator(
private fun resolveInterface(
declaration: KSDeclaration,
sourceIndicator: String,
templateCollector: MutableMap<String, TemplateSource>
dependencies: List<KSFile>,
templateCollector: MutableMap<String, TemplateSource>,
) {
val interfaze = safeCastInterface(declaration)
val qualifiedName = ensureNotNullClassName(interfaze.qualifiedName?.asString())
Expand All @@ -48,18 +49,24 @@ internal class KMockSingleSourceAggregator(
indicator = sourceIndicator,
templateName = aliases[qualifiedName] ?: interfaze.deriveSimpleName(packageName),
packageName = packageName,
dependencies = dependencies,
template = interfaze,
generics = resolveGenerics(interfaze)
)
}

private fun resolveInterfaces(
raw: Map<String, MutableList<KSType>>,
raw: Map<String, Pair<List<KSFile>, MutableList<KSType>>>,
templateCollector: MutableMap<String, TemplateSource>
) {
raw.forEach { (sourceIndicator, interfaces) ->
interfaces.forEach { interfaze ->
resolveInterface(interfaze.declaration, sourceIndicator, templateCollector)
interfaces.second.forEach { interfaze ->
resolveInterface(
declaration = interfaze.declaration,
sourceIndicator = sourceIndicator,
dependencies = interfaces.first,
templateCollector = templateCollector
)
}
}
}
Expand All @@ -78,7 +85,7 @@ internal class KMockSingleSourceAggregator(
condition: (String, KSAnnotation) -> Boolean,
): Aggregated<TemplateSource> {
val illAnnotated = mutableListOf<KSAnnotated>()
val typeContainer = mutableMapOf<String, MutableList<KSType>>()
val typeContainer = mutableMapOf<String, Pair<List<KSFile>, MutableList<KSType>>>()
val templateCollector: MutableMap<String, TemplateSource> = mutableMapOf()
val fileCollector: MutableList<KSFile> = mutableListOf()

Expand All @@ -89,20 +96,20 @@ internal class KMockSingleSourceAggregator(
illAnnotated.add(annotatedSymbol)
} else {
val sourceIndicator = determineSourceCategory(annotation)
val interfaces = typeContainer.getOrElse(sourceIndicator) { mutableListOf() }
val (_, interfaces) = typeContainer.getOrElse(sourceIndicator) { Pair(null, mutableListOf()) }

interfaces.addAll(annotation.arguments.last().value as List<KSType>)
typeContainer[sourceIndicator] = interfaces
typeContainer[sourceIndicator] = Pair(listOf(annotatedSymbol.containingFile!!), interfaces)
fileCollector.add(annotatedSymbol.containingFile!!)
}
}

resolveInterfaces(typeContainer, templateCollector)

return Aggregated(
illAnnotated,
templateCollector.values.toList(),
fileCollector
illFormed = illAnnotated,
extractedTemplates = templateCollector.values.toList(),
totalDependencies = fileCollector
)
}

Expand Down
Loading