Skip to content
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 @@ -57,7 +57,9 @@ class AppArgumentProcessor(private val argumentParser: ArgumentParser = Argument
) { secondaries ->
FeatureRequest(
featureName = secondaries[SecondaryFlagConstants.NAME].orEmpty(),
packageName = secondaries[SecondaryFlagConstants.PACKAGE]
packageName = secondaries[SecondaryFlagConstants.PACKAGE],
enableKtlint = secondaries.containsKey(SecondaryFlagConstants.KTLINT),
enableDetekt = secondaries.containsKey(SecondaryFlagConstants.DETEKT)
)
}

Expand Down
11 changes: 9 additions & 2 deletions cli/src/main/kotlin/com/mitteloupe/cag/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import kotlin.system.exitProcess

private const val USAGE_SYNTAX =
"usage: cag [--new-project --name=ProjectName --package=PackageName [--no-compose] [--ktlint] [--detekt] [--ktor] [--retrofit]]... " +
"[--new-architecture [--no-compose] [--ktlint] [--detekt]]... [--new-feature --name=FeatureName [--package=PackageName]]... " +
"[--new-architecture [--no-compose] [--ktlint] [--detekt]]... " +
"[--new-feature --name=FeatureName [--package=PackageName] [--ktlint] [--detekt]]... " +
"[--new-datasource --name=DataSourceName [--with=ktor|retrofit|ktor,retrofit]]... " +
"[--new-use-case --name=UseCaseName [--path=TargetPath]]... [--new-view-model --name=ViewModelName [--path=TargetPath]]..."

Expand Down Expand Up @@ -122,7 +123,7 @@ fun main(arguments: Array<String>) {

featureRequests.forEach { requestFeature ->
val packageName =
requestFeature.packageName ?: basePackage?.let { "$it${requestFeature.featureName.lowercase()}" }
requestFeature.packageName ?: basePackage?.let { "${'$'}it${'$'}{requestFeature.featureName.lowercase()}" }

val request =
GenerateFeatureRequestBuilder(
Expand All @@ -131,6 +132,8 @@ fun main(arguments: Array<String>) {
featureName = requestFeature.featureName
).featurePackageName(packageName)
.enableCompose(true)
.enableKtlint(requestFeature.enableKtlint)
.enableDetekt(requestFeature.enableDetekt)
.build()
executeAndReport {
generator.generateFeature(request)
Expand Down Expand Up @@ -246,6 +249,10 @@ private fun printHelpMessage() {
Specify the feature name (required)
--package=PackageName | --package PackageName | -p=PackageName | -p PackageName | -pPackageName
Override the feature package for the preceding feature
--ktlint | -kl
Enable ktlint for the preceding feature (adds plugin and .editorconfig if missing)
--detekt | -d
Enable detekt for the preceding feature (adds plugin and detekt.yml if missing)
--new-datasource | -nds
Generate a new data source
--name=DataSourceName | -n=DataSourceName | -n DataSourceName | -nDataSourceName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ interface PrimaryFlag {
isMandatory = true,
missingErrorMessage = "Feature name is required. Use --name=FeatureName or -n=FeatureName"
),
SecondaryFlag(SecondaryFlagConstants.PACKAGE, "-p")
SecondaryFlag(SecondaryFlagConstants.PACKAGE, "-p"),
SecondaryFlag(SecondaryFlagConstants.KTLINT, "-kl", isBoolean = true),
SecondaryFlag(SecondaryFlagConstants.DETEKT, "-d", isBoolean = true)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package com.mitteloupe.cag.cli.request

data class FeatureRequest(val featureName: String, val packageName: String?)
data class FeatureRequest(
val featureName: String,
val packageName: String?,
val enableKtlint: Boolean,
val enableDetekt: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ class AppArgumentProcessorTest {
// Then
assertEquals(true, result)
}

@Test
fun `Given --new-feature with --ktlint when getNewFeatures then returns FeatureRequest with ktlint enabled`() {
// Given
val givenArguments = arrayOf("--new-feature", "--name=Quality", "--ktlint")
val expectedRequests =
listOf(
FeatureRequest(
featureName = "Quality",
packageName = null,
enableKtlint = true,
enableDetekt = false
)
)

// When
val result = classUnderTest.getNewFeatures(givenArguments)

// Then
assertEquals(expectedRequests, result)
}

@Test
fun `Given short flags -nf -kl -d when getNewFeatures then returns FeatureRequest with both enabled`() {
// Given
val givenArguments = arrayOf("-nf", "-n=Quality", "-kl", "-d")
val expectedRequests =
listOf(
FeatureRequest(
featureName = "Quality",
packageName = null,
enableKtlint = true,
enableDetekt = true
)
)

// When
val result = classUnderTest.getNewFeatures(givenArguments)

// Then
assertEquals(expectedRequests, result)
}
}

class Features {
Expand All @@ -71,24 +113,54 @@ class AppArgumentProcessorTest {
// Given
val givenArguments =
arrayOf("--new-feature", "--name=First", "--package=com.first", "--new-feature", "--name=Second")
val expectedRequests =
listOf(
FeatureRequest(
featureName = "First",
packageName = "com.first",
enableKtlint = false,
enableDetekt = false
),
FeatureRequest(
featureName = "Second",
packageName = null,
enableKtlint = false,
enableDetekt = false
)
)

// When
val result = classUnderTest.getNewFeatures(givenArguments)

// Then
assertEquals(listOf(FeatureRequest("First", "com.first"), FeatureRequest("Second", null)), result)
assertEquals(expectedRequests, result)
}

@Test
fun `Given short flags when getNewFeatures then parses correctly`() {
// Given
val givenArguments = arrayOf("-nf", "-nThird", "-p", "com.third", "-nf", "-n=Fourth", "-pcom.fourth")
val expectedRequests =
listOf(
FeatureRequest(
featureName = "Third",
packageName = "com.third",
enableKtlint = false,
enableDetekt = false
),
FeatureRequest(
featureName = "Fourth",
packageName = "com.fourth",
enableKtlint = false,
enableDetekt = false
)
)

// When
val result = classUnderTest.getNewFeatures(givenArguments)

// Then
assertEquals(listOf(FeatureRequest("Third", "com.third"), FeatureRequest("Fourth", "com.fourth")), result)
assertEquals(expectedRequests, result)
}

@Test
Expand Down
32 changes: 19 additions & 13 deletions cli/src/test/kotlin/com/mitteloupe/cag/cli/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import java.io.OutputStream
import java.io.PrintStream
import java.nio.file.Files

private const val SHORT_USAGE =
"usage: cag [--new-project --name=ProjectName --package=PackageName [--no-compose] [--ktlint] [--detekt] [--ktor] [--retrofit]]... " +
"[--new-architecture [--no-compose] [--ktlint] [--detekt]]... " +
"[--new-feature --name=FeatureName [--package=PackageName] [--ktlint] [--detekt]]... " +
"[--new-datasource --name=DataSourceName [--with=ktor|retrofit|ktor,retrofit]]... " +
"[--new-use-case --name=UseCaseName [--path=TargetPath]]... " +
"[--new-view-model --name=ViewModelName [--path=TargetPath]]..."

@RunWith(Enclosed::class)
@SuiteClasses(
MainTest.NoArguments::class,
Expand Down Expand Up @@ -60,22 +68,16 @@ class MainTest {
class NoArguments : BaseMainTest() {
@Test
fun `Given no args when main then prints updated usage`() {
// Give
val expected =
"$SHORT_USAGE\n\n" +
"Run with --help or -h for more options.\n"

// When
main(emptyArray())

// Then
assertEquals(
"usage: cag [--new-project --name=ProjectName --package=PackageName " +
"[--no-compose] [--ktlint] [--detekt] [--ktor] [--retrofit]]... " +
"[--new-architecture [--no-compose] [--ktlint] [--detekt]]... " +
"[--new-feature --name=FeatureName [--package=PackageName]]... " +
"[--new-datasource --name=DataSourceName [--with=ktor|retrofit|ktor,retrofit]]... " +
"[--new-use-case --name=UseCaseName [--path=TargetPath]]... " +
"[--new-view-model --name=ViewModelName [--path=TargetPath]]...\n" +
"\n" +
"Run with --help or -h for more options.\n",
output.toString()
)
assertEquals(expected, output.toString())
}
}

Expand All @@ -100,7 +102,7 @@ class MainTest {

companion object {
private const val EXPECTED_HELP =
"""usage: cag [--new-project --name=ProjectName --package=PackageName [--no-compose] [--ktlint] [--detekt] [--ktor] [--retrofit]]... [--new-architecture [--no-compose] [--ktlint] [--detekt]]... [--new-feature --name=FeatureName [--package=PackageName]]... [--new-datasource --name=DataSourceName [--with=ktor|retrofit|ktor,retrofit]]... [--new-use-case --name=UseCaseName [--path=TargetPath]]... [--new-view-model --name=ViewModelName [--path=TargetPath]]...
"""$SHORT_USAGE

Note: You must use either long form (--flag) or short form (-f) arguments consistently throughout your command. Mixing both forms is not allowed.

Expand Down Expand Up @@ -135,6 +137,10 @@ Options:
Specify the feature name (required)
--package=PackageName | --package PackageName | -p=PackageName | -p PackageName | -pPackageName
Override the feature package for the preceding feature
--ktlint | -kl
Enable ktlint for the preceding feature (adds plugin and .editorconfig if missing)
--detekt | -d
Enable detekt for the preceding feature (adds plugin and detekt.yml if missing)
--new-datasource | -nds
Generate a new data source
--name=DataSourceName | -n=DataSourceName | -n DataSourceName | -nDataSourceName
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/kotlin/com/mitteloupe/cag/core/Generator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class Generator(
if (request.enableCompose) {
addAll(VersionCatalogConstants.COMPOSE_VERSIONS)
}
if (request.enableKtlint) {
addAll(VersionCatalogConstants.KTLINT_VERSIONS)
}
if (request.enableDetekt) {
addAll(VersionCatalogConstants.DETEKT_VERSIONS)
}
},
libraries = if (request.enableCompose) LibraryConstants.COMPOSE_LIBRARIES else emptyList(),
plugins =
Expand All @@ -86,6 +92,12 @@ class Generator(
if (request.enableCompose) {
add(PluginConstants.COMPOSE_COMPILER)
}
if (request.enableKtlint) {
add(PluginConstants.KTLINT)
}
if (request.enableDetekt) {
add(PluginConstants.DETEKT)
}
}
)
catalogUpdater.updateVersionCatalogIfPresent(
Expand Down Expand Up @@ -170,6 +182,12 @@ class Generator(
featurePackageName = featurePackageName,
featureName = request.featureName
)
if (request.enableDetekt) {
configurationFileCreator.writeDetektConfigurationFile(request.destinationRootDirectory)
}
if (request.enableKtlint) {
configurationFileCreator.writeEditorConfigFile(request.destinationRootDirectory)
}
settingsFileUpdater.updateProjectSettingsIfPresent(
request.destinationRootDirectory,
featureNameLowerCase
Expand Down Expand Up @@ -518,6 +536,8 @@ class Generator(
featurePackageName = "$packageName.${featureName.lowercase()}",
projectNamespace = packageName,
enableCompose = request.enableCompose,
enableKtlint = request.enableKtlint,
enableDetekt = request.enableDetekt,
appModuleDirectory = null
)
generateFeature(featureRequest)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mitteloupe.cag.core.content

import com.mitteloupe.cag.core.content.gradle.GradleFileExtender
import com.mitteloupe.cag.core.generation.versioncatalog.PluginConstants
import com.mitteloupe.cag.core.generation.versioncatalog.VersionCatalogReader
import com.mitteloupe.cag.core.generation.versioncatalog.asAccessor
Expand All @@ -10,11 +11,23 @@ fun buildDataGradleScript(
): String {
val aliasKotlinJvm = catalog.getResolvedPluginAliasFor(PluginConstants.KOTLIN_JVM).asAccessor

val gradleFileExtender = GradleFileExtender()
val ktlintPluginLine = gradleFileExtender.buildKtlintPluginLine(catalog)
val detektPluginLine = gradleFileExtender.buildDetektPluginLine(catalog)
val ktlintConfiguration = gradleFileExtender.buildKtlintConfiguration(catalog)
val detektConfiguration = gradleFileExtender.buildDetektConfiguration(catalog, "../../../detekt.yml")

val configurations = "$ktlintConfiguration$detektConfiguration".trimIndent()
return """plugins {
id("project-java-library")
alias(libs.plugins.$aliasKotlinJvm)
alias(libs.plugins.$aliasKotlinJvm)$ktlintPluginLine$detektPluginLine
}

${ if (configurations.isEmpty()) {
""
} else {
"\n$configurations\n"
}
}
dependencies {
implementation(projects.features.$featureNameLowerCase.domain)
implementation(projects.architecture.domain)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package com.mitteloupe.cag.core.content

import com.mitteloupe.cag.core.content.gradle.GradleFileExtender
import com.mitteloupe.cag.core.generation.versioncatalog.PluginConstants
import com.mitteloupe.cag.core.generation.versioncatalog.VersionCatalogReader
import com.mitteloupe.cag.core.generation.versioncatalog.asAccessor

fun buildDomainGradleScript(catalog: VersionCatalogReader): String {
val aliasKotlinJvm = catalog.getResolvedPluginAliasFor(PluginConstants.KOTLIN_JVM).asAccessor

val gradleFileExtender = GradleFileExtender()
val ktlintPluginLine = gradleFileExtender.buildKtlintPluginLine(catalog)
val detektPluginLine = gradleFileExtender.buildDetektPluginLine(catalog)
val ktlintConfiguration = gradleFileExtender.buildKtlintConfiguration(catalog)
val detektConfiguration = gradleFileExtender.buildDetektConfiguration(catalog, "../../../detekt.yml")

val configurations = "$ktlintConfiguration$detektConfiguration".trimIndent()
return """plugins {
id("project-java-library")
alias(libs.plugins.$aliasKotlinJvm)
alias(libs.plugins.$aliasKotlinJvm)$ktlintPluginLine$detektPluginLine
}

${ if (configurations.isEmpty()) {
""
} else {
"\n$configurations\n"
}
}
dependencies {
implementation(projects.architecture.domain)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mitteloupe.cag.core.content

import com.mitteloupe.cag.core.content.gradle.GradleFileExtender
import com.mitteloupe.cag.core.generation.versioncatalog.PluginConstants
import com.mitteloupe.cag.core.generation.versioncatalog.VersionCatalogReader
import com.mitteloupe.cag.core.generation.versioncatalog.asAccessor
Expand All @@ -10,11 +11,23 @@ fun buildPresentationGradleScript(
): String {
val aliasKotlinJvm = catalog.getResolvedPluginAliasFor(PluginConstants.KOTLIN_JVM).asAccessor

val gradleFileExtender = GradleFileExtender()
val ktlintPluginLine = gradleFileExtender.buildKtlintPluginLine(catalog)
val detektPluginLine = gradleFileExtender.buildDetektPluginLine(catalog)
val ktlintConfiguration = gradleFileExtender.buildKtlintConfiguration(catalog)
val detektConfiguration = gradleFileExtender.buildDetektConfiguration(catalog, "../../../detekt.yml")

val configurations = "$ktlintConfiguration$detektConfiguration".trimIndent()
return """plugins {
id("project-java-library")
alias(libs.plugins.$aliasKotlinJvm)
alias(libs.plugins.$aliasKotlinJvm)$ktlintPluginLine$detektPluginLine
}

${ if (configurations.isEmpty()) {
""
} else {
"\n$configurations\n"
}
}
dependencies {
implementation(projects.features.$featureNameLowerCase.domain)
implementation(projects.architecture.presentation)
Expand Down
Loading