From 01d197dbd122e9182bdd13728c651c5fe169decf Mon Sep 17 00:00:00 2001 From: Pascal Knecht Date: Sat, 7 Sep 2024 12:16:15 +0200 Subject: [PATCH] Introduce new mermaid CLI flag and assert only one output flag is passed to the CLI The new --mermaid-graph flag is equivalent to the deprecated --chart-mermaid flag. --- app/build.gradle.kts | 2 +- app/src/main/kotlin/ch/addere/dga/app/App.kt | 59 ++++++++++--------- .../dga/app/configuration/OutputOptions.kt | 8 +++ .../dga/app/domain/model/CommandConfig.kt | 3 +- .../dga/app/domain/model/OutputConfig.kt | 7 --- .../app/domain/service/DependencyCommand.kt | 22 ++++--- 6 files changed, 54 insertions(+), 47 deletions(-) create mode 100644 app/src/main/kotlin/ch/addere/dga/app/configuration/OutputOptions.kt delete mode 100644 app/src/main/kotlin/ch/addere/dga/app/domain/model/OutputConfig.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ce11b4d..f7715e0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -8,7 +8,7 @@ dependencies { implementation(project(":core")) implementation(project(":importer")) - implementation("com.github.ajalt.clikt:clikt:4.0.0") + implementation("com.github.ajalt.clikt:clikt:4.4.0") implementation("io.insert-koin:koin-core:3.4.2") } diff --git a/app/src/main/kotlin/ch/addere/dga/app/App.kt b/app/src/main/kotlin/ch/addere/dga/app/App.kt index d9aa564..c346cd7 100644 --- a/app/src/main/kotlin/ch/addere/dga/app/App.kt +++ b/app/src/main/kotlin/ch/addere/dga/app/App.kt @@ -1,8 +1,8 @@ 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.model.OutputConfig import ch.addere.dga.app.domain.service.DependencyCommand import ch.addere.dga.app.infrastructure.factory.dgaModule import ch.addere.dga.app.infrastructure.factory.importerModule @@ -14,9 +14,13 @@ import com.github.ajalt.clikt.core.CliktError import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.arguments.help import com.github.ajalt.clikt.parameters.groups.OptionGroup +import com.github.ajalt.clikt.parameters.groups.default +import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions import com.github.ajalt.clikt.parameters.groups.provideDelegate +import com.github.ajalt.clikt.parameters.groups.single import com.github.ajalt.clikt.parameters.options.convert import com.github.ajalt.clikt.parameters.options.default +import com.github.ajalt.clikt.parameters.options.deprecated import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.help import com.github.ajalt.clikt.parameters.options.option @@ -29,7 +33,7 @@ import org.koin.core.parameter.parametersOf import java.io.File import kotlin.system.exitProcess -class Dga : CliktCommand(help = "Analyse the module dependency graph of a Gradle project."), +private class Dga : CliktCommand(help = "Analyse the module dependency graph of a Gradle project."), KoinComponent { private val gradleProject: File by argument() @@ -37,7 +41,28 @@ class Dga : CliktCommand(help = "Analyse the module dependency graph of a Gradle .help("Path of the Gradle project directory") private val optionsFilter by OptionsFilter() - private val optionsOutput by OptionsOutput() + private val outputOption: OutputOptions by mutuallyExclusiveOptions( + option("--modules") + .flag() + .convert { OutputOptions.OutputOptionModules } + .help("Shows all modules of the project applying to the specified filters."), + option("--configurations") + .flag() + .convert { OutputOptions.OutputOptionConfigurations } + .help("Displays all configurations applying to the specified filters and sorted by frequency of occurrence."), + option("--mermaid-graph") + .flag() + .convert { OutputOptions.OutputOptionMermaid } + .help("Generate the Mermaid graph chart source for the dependencies fulfilling the filter criteria."), + option("--chart-mermaid") + .flag() + .convert { OutputOptions.OutputOptionMermaid } + .help("Generate the Mermaid graph chart source for the dependencies fulfilling the filter criteria.") + .deprecated("Use --mermaid-graph instead"), + help = "Options controlling how to output the analysed data. Display options can not be combined.", + name = "Display Options", + ).single().default(OutputOptions.OutputOptionOverviewOnly) + override fun run() { val filterConfig = FilterConfig( @@ -47,19 +72,14 @@ class Dga : CliktCommand(help = "Analyse the module dependency graph of a Gradle optionsFilter.configurations, optionsFilter.transitiveModules ) - val outputConfig = OutputConfig( - optionsOutput.isAllModules, - optionsOutput.isAllConfigurations, - optionsOutput.isChartMermaid - ) - val argument = CommandConfig(::echo, gradleProject, filterConfig, outputConfig) + val argument = CommandConfig(::echo, gradleProject, filterConfig, outputOption) val command: DependencyCommand = get { parametersOf(argument) } command.run() } } -class OptionsFilter : OptionGroup( +private class OptionsFilter : OptionGroup( name = "Filter Options", help = """ Filter control what to analyse. If several filters are set, dependencies must fulfill all of them. @@ -94,25 +114,6 @@ class OptionsFilter : OptionGroup( .help("Also include transitive modules.") } -class OptionsOutput : OptionGroup( - name = "Display Options", - help = """ - Options controlling how to output the analysed data. Display options can not be combined. - """.trimIndent() -) { - val isAllModules: Boolean by option("--modules") - .flag() - .help("Shows all modules of the project applying to the specified filters.") - - val isAllConfigurations: Boolean by option("--configurations") - .flag() - .help("Displays all configurations applying to the specified filters and sorted by frequency of occurrence.") - - val isChartMermaid: Boolean by option("--chart-mermaid") - .flag() - .help("Generate the Mermaid graph chart source for the dependencies fulfilling the filter criteria.") -} - fun main(args: Array) { val dga = Dga() try { diff --git a/app/src/main/kotlin/ch/addere/dga/app/configuration/OutputOptions.kt b/app/src/main/kotlin/ch/addere/dga/app/configuration/OutputOptions.kt new file mode 100644 index 0000000..bb5cfa0 --- /dev/null +++ b/app/src/main/kotlin/ch/addere/dga/app/configuration/OutputOptions.kt @@ -0,0 +1,8 @@ +package ch.addere.dga.app.configuration + +sealed class OutputOptions { + data object OutputOptionOverviewOnly : OutputOptions() + data object OutputOptionModules : OutputOptions() + data object OutputOptionConfigurations : OutputOptions() + data object OutputOptionMermaid : OutputOptions() +} diff --git a/app/src/main/kotlin/ch/addere/dga/app/domain/model/CommandConfig.kt b/app/src/main/kotlin/ch/addere/dga/app/domain/model/CommandConfig.kt index 3bf41c4..68ffc61 100644 --- a/app/src/main/kotlin/ch/addere/dga/app/domain/model/CommandConfig.kt +++ b/app/src/main/kotlin/ch/addere/dga/app/domain/model/CommandConfig.kt @@ -1,10 +1,11 @@ package ch.addere.dga.app.domain.model +import ch.addere.dga.app.configuration.OutputOptions import java.io.File data class CommandConfig( val printer: (String) -> Unit, val gradleProjectPath: File, val filterConfig: FilterConfig, - val outputConfig: OutputConfig + val outputConfig: OutputOptions ) diff --git a/app/src/main/kotlin/ch/addere/dga/app/domain/model/OutputConfig.kt b/app/src/main/kotlin/ch/addere/dga/app/domain/model/OutputConfig.kt deleted file mode 100644 index 44974ad..0000000 --- a/app/src/main/kotlin/ch/addere/dga/app/domain/model/OutputConfig.kt +++ /dev/null @@ -1,7 +0,0 @@ -package ch.addere.dga.app.domain.model - -data class OutputConfig( - val isAllModule: Boolean, - val isAllConfigurations: Boolean, - val isChartMermaid: Boolean -) diff --git a/app/src/main/kotlin/ch/addere/dga/app/domain/service/DependencyCommand.kt b/app/src/main/kotlin/ch/addere/dga/app/domain/service/DependencyCommand.kt index df180f7..9c087f7 100644 --- a/app/src/main/kotlin/ch/addere/dga/app/domain/service/DependencyCommand.kt +++ b/app/src/main/kotlin/ch/addere/dga/app/domain/service/DependencyCommand.kt @@ -1,5 +1,9 @@ package ch.addere.dga.app.domain.service +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.model.CommandConfig import ch.addere.dga.app.domain.service.printer.ConsolePrinter import ch.addere.dga.app.domain.service.printer.DependencyPrinter @@ -72,16 +76,16 @@ class DependencyCommand( } printer.println() - overview.printToConsole(overviewDataForOutput) + when (config.outputConfig) { + OutputOptionOverviewOnly -> overview.printToConsole(overviewDataForOutput) + OutputOptionModules -> modulePrinter.printToConsole(extractModules(filteredDependencies)) + OutputOptionConfigurations -> dependencies.printToConsole( + configurationAndCount( + filteredDependencies + ) + ) - if (config.outputConfig.isAllModule) { - val uniqueModules = extractModules(filteredDependencies) - modulePrinter.printToConsole(uniqueModules) - } else if (config.outputConfig.isAllConfigurations) { - val countedConfigurations = configurationAndCount(filteredDependencies) - dependencies.printToConsole(countedConfigurations) - } else if (config.outputConfig.isChartMermaid) { - mermaid.printToConsole(filteredDependencies) + OutputOptionMermaid -> mermaid.printToConsole(filteredDependencies) } printer.println() }