Skip to content

Commit

Permalink
Introduce new mermaid CLI flag and assert only one output flag is pas…
Browse files Browse the repository at this point in the history
…sed to the CLI

The new --mermaid-graph flag is equivalent to the deprecated --chart-mermaid flag.
  • Loading branch information
ryru committed Sep 8, 2024
1 parent 3c73fdb commit 01d197d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
59 changes: 30 additions & 29 deletions app/src/main/kotlin/ch/addere/dga/app/App.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -29,15 +33,36 @@ 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()
.file()
.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(
Expand All @@ -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.
Expand Down Expand Up @@ -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<String>) {
val dga = Dga()
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 01d197d

Please sign in to comment.