Skip to content

Commit

Permalink
Refactor dependency command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ryru committed Sep 8, 2024
1 parent 01d197d commit e044fd2
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 190 deletions.
4 changes: 2 additions & 2 deletions app/src/main/kotlin/ch/addere/dga/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.addere.dga.app
import ch.addere.dga.app.configuration.OutputOptions
import ch.addere.dga.app.domain.model.CommandConfig
import ch.addere.dga.app.domain.model.FilterConfig
import ch.addere.dga.app.domain.service.DependencyCommand
import ch.addere.dga.app.domain.service.DependencyCommandHandler
import ch.addere.dga.app.infrastructure.factory.dgaModule
import ch.addere.dga.app.infrastructure.factory.importerModule
import ch.addere.dga.app.infrastructure.factory.userInputModule
Expand Down Expand Up @@ -73,7 +73,7 @@ private class Dga : CliktCommand(help = "Analyse the module dependency graph of
optionsFilter.transitiveModules
)
val argument = CommandConfig(::echo, gradleProject, filterConfig, outputOption)
val command: DependencyCommand = get { parametersOf(argument) }
val command: DependencyCommandHandler = get { parametersOf(argument) }

command.run()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package ch.addere.dga.app.domain.model

import ch.addere.dga.app.configuration.OutputOptions
import ch.addere.dga.app.configuration.OutputOptions.OutputOptionOverviewOnly
import java.io.File

data class CommandConfig(
val printer: (String) -> Unit,
val gradleProjectPath: File,
val filterConfig: FilterConfig,
val outputConfig: OutputOptions
)
) {
val hasActiveFilter: Boolean =
filterConfig.modules.isNotEmpty() ||
filterConfig.originModules.isNotEmpty() ||
filterConfig.destinationModules.isNotEmpty() ||
filterConfig.configurations.isNotEmpty() ||
filterConfig.includeTransitiveDependencies
val hasActiveOutput: Boolean = outputConfig != OutputOptionOverviewOnly
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ data class FilterConfig(
val originModules: List<Module>,
val destinationModules: List<Module>,
val configurations: List<Configuration>,
val isTransitiveModules: Boolean
val includeTransitiveDependencies: Boolean
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ch.addere.dga.app.domain.service

import ch.addere.dga.app.domain.model.CommandConfig
import ch.addere.dga.app.domain.service.printer.ConsolePrinter
import ch.addere.dga.core.domain.model.Dependency
import ch.addere.dga.importer.application.service.GradleProjectLoader

class DependencyCommandHandler(
private val config: CommandConfig,
private val printer: ConsolePrinter,
private val gradleProjectLoader: GradleProjectLoader,
private val projectOverviewPrinter: ProjectOverviewPrinter,
private val filterService: FilterService,
private val projectFilterPrinter: ProjectFilterPrinter,
private val projectOutputPrinter: ProjectOutputPrinter,
) {

fun run() {
val projectName: String = gradleProjectLoader.loadGradleProject(config.gradleProjectPath)
projectOverviewPrinter.output(projectName)

val relevantDependencies: Set<Dependency> = if (config.hasActiveFilter) {
val filteredDependencies: Set<Dependency> = filterService.filter(config.filterConfig)
projectFilterPrinter.output(filteredDependencies)
filteredDependencies
} else {
filterService.allDependencies()
}

if (config.hasActiveOutput) {
projectOutputPrinter.output(config.outputConfig, relevantDependencies)
}
printer.println()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch.addere.dga.app.domain.service

import ch.addere.dga.app.domain.model.FilterConfig
import ch.addere.dga.core.domain.model.Dependency
import ch.addere.dga.core.domain.model.FilteredConfiguration
import ch.addere.dga.core.domain.model.FilteredModules
import ch.addere.dga.core.domain.service.ConfigurationService
import ch.addere.dga.core.domain.service.DependencyRelationService
import ch.addere.dga.core.domain.service.DependencyService
import ch.addere.dga.core.domain.service.ModuleService

class FilterService(
private val dependencyService: DependencyService,
private val moduleService: ModuleService,
private val configurationService: ConfigurationService,
private val dependencyRelationService: DependencyRelationService,
) {


fun allDependencies(): Set<Dependency> {
return dependencyService.filteredDependencies(
FilteredModules(false, emptyList()),
FilteredModules(false, emptyList()),
FilteredModules(false, emptyList()),
FilteredConfiguration(false, emptyList())
)
}


fun filter(filterConfig: FilterConfig): Set<Dependency> {
val inputModules = filterConfig.modules
val inputOrigin = filterConfig.originModules
val inputDestination = filterConfig.destinationModules
val inputConfigurations = filterConfig.configurations

val filteredModules = inputModules.flatMap(moduleService::resolvePartialModuleName).toList()
val filteredOrigin = inputOrigin.flatMap(moduleService::resolvePartialModuleName).toList()
val filteredDestination =
inputDestination.flatMap(moduleService::resolvePartialModuleName).toList()
val filteredConfigurations =
inputConfigurations.flatMap(configurationService::resolvePartialConfigurationName)
.toList()

var filteredDependencies: Set<Dependency> =
dependencyService.filteredDependencies(
FilteredModules(inputModules.isNotEmpty(), filteredModules),
FilteredModules(inputOrigin.isNotEmpty(), filteredOrigin),
FilteredModules(inputDestination.isNotEmpty(), filteredDestination),
FilteredConfiguration(inputConfigurations.isNotEmpty(), filteredConfigurations)
)

if (filterConfig.includeTransitiveDependencies) {
val configurationSet = filteredDependencies.map { it.configuration }.toSet()
filteredDependencies = filteredDependencies.flatMap { dependency ->
dependencyRelationService.allDependenciesOf(
dependency.origin,
configurationSet
) union
dependencyRelationService.allDependenciesOf(
dependency.destination,
configurationSet
)
}.toSet()
}

return filteredDependencies
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ch.addere.dga.app.domain.service

import ch.addere.dga.app.domain.service.printer.ConsolePrinter
import ch.addere.dga.core.domain.model.Dependency

class ProjectFilterPrinter(private val printer: ConsolePrinter) {

/**
* Print filtered module and configuration information.
*/
fun output(dependencies: Set<Dependency>) {
val overviewData = filteredData(dependencies)
printer.println()
printer.println("Applying filter on data results in:")
printer.println(String.format("%6d modules", overviewData.nofModules))
printer.println(
String.format(
"%6d dependency configurations (%d unique dependency configurations)",
overviewData.nofDependencies,
overviewData.nofUniqueDependencies
)
)
}

private fun filteredData(dependencies: Set<Dependency>): FilterData {
val nofModules = dependencies.flatMap { setOf(it.origin, it.destination) }.toSet().size
val nofDependencies = dependencies.map { it.configuration }.count()
val nofUniqueDependencies = dependencies.map { it.configuration }.toSet().size
return FilterData(nofModules, nofDependencies, nofUniqueDependencies)
}

private data class FilterData(
val nofModules: Int,
val nofDependencies: Int,
val nofUniqueDependencies: Int
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ch.addere.dga.app.domain.service

import ch.addere.dga.app.configuration.OutputOptions
import ch.addere.dga.app.configuration.OutputOptions.OutputOptionConfigurations
import ch.addere.dga.app.configuration.OutputOptions.OutputOptionMermaid
import ch.addere.dga.app.configuration.OutputOptions.OutputOptionModules
import ch.addere.dga.app.configuration.OutputOptions.OutputOptionOverviewOnly
import ch.addere.dga.app.domain.service.printer.DependencyPrinter
import ch.addere.dga.app.domain.service.printer.MermaidPrinter
import ch.addere.dga.app.domain.service.printer.ModulePrinter
import ch.addere.dga.core.domain.model.Dependency

class ProjectOutputPrinter(
private val dependencies: DependencyPrinter,
private val mermaid: MermaidPrinter,
private val modulePrinter: ModulePrinter,
) {

/**
* Print analysed and filtered Gradle project according to the specified output option.
*/
fun output(outputConfig: OutputOptions, relevantDependencies: Set<Dependency>) {
when (outputConfig) {
OutputOptionOverviewOnly -> return
OutputOptionModules -> modulePrinter.printToConsole(extractModules(relevantDependencies))
OutputOptionConfigurations -> dependencies.printToConsole(
configurationAndCount(
relevantDependencies
)
)

OutputOptionMermaid -> mermaid.printToConsole(relevantDependencies)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ch.addere.dga.app.domain.service

import ch.addere.dga.app.domain.service.printer.ConsolePrinter
import ch.addere.dga.core.domain.service.DependencyService
import ch.addere.dga.core.domain.service.ModuleService

class ProjectOverviewPrinter(
private val printer: ConsolePrinter,
private val moduleService: ModuleService,
private val dependencyService: DependencyService,
) {

/**
* Print common information about the whole analysed Gradle project.
*/
fun output(projectName: String) {
val overviewData = overviewData(projectName)
printer.println()
printer.println("Analyse project \"${overviewData.projectName}\"")
printer.println(String.format("%6d modules", overviewData.nofModules))
printer.println(
String.format(
"%6d dependency configurations (%d unique dependency configurations)",
overviewData.nofDependencies,
overviewData.nofUniqueDependencies
)
)
}

private fun overviewData(projectName: String): OverviewData {
val nofModules = moduleService.nofModules()
val nofDependencies = dependencyService.nofProjectDependencies()
val nofUniqueDependencies = dependencyService.nofUniqueConfigurations()
return OverviewData(projectName, nofModules, nofDependencies, nofUniqueDependencies)
}

private data class OverviewData(
val projectName: String,
val nofModules: Int,
val nofDependencies: Int,
val nofUniqueDependencies: Int
)
}
Loading

0 comments on commit e044fd2

Please sign in to comment.