diff --git a/CHANGELOG.md b/CHANGELOG.md index aaa45b1..6f8f29b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Module Maker Changelog +## [1.1.0] +- Support Multiplatform modules + ## [1.0.26] - Platform updates diff --git a/gradle.properties b/gradle.properties index 88cc931..ce50647 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = com.joetr.modulemaker pluginName = ModuleMaker pluginRepositoryUrl = https://github.com/j-roskopf/ModuleMakerPlugin # SemVer format -> https://semver.org -pluginVersion = 1.0.26 +pluginVersion = 1.1.0 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 222 diff --git a/src/main/kotlin/com/joetr/modulemaker/ModuleMakerDialogWrapper.kt b/src/main/kotlin/com/joetr/modulemaker/ModuleMakerDialogWrapper.kt index bf25588..b4f977f 100644 --- a/src/main/kotlin/com/joetr/modulemaker/ModuleMakerDialogWrapper.kt +++ b/src/main/kotlin/com/joetr/modulemaker/ModuleMakerDialogWrapper.kt @@ -1,6 +1,9 @@ package com.joetr.modulemaker +import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.ExperimentalLayoutApi +import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth @@ -21,6 +24,7 @@ import androidx.compose.material.Text import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Info import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment @@ -53,12 +57,13 @@ import javax.swing.Action import javax.swing.JComponent private const val WINDOW_WIDTH = 840 -private const val WINDOW_HEIGHT = 600 +private const val WINDOW_HEIGHT = 800 private const val FILE_TREE_WIDTH = 300 private const val CONFIGURATION_PANEL_WIDTH = 540 const val ANDROID = "Android" -const val KOTLIN = "Kotlin" +const val MULTIPLATFORM = "Multiplatform" +const val KOTLIN = "Kotlin / JVM" private const val DEFAULT_MODULE_NAME = ":repository:database (as an example)" private const val DEFAULT_SRC_VALUE = "EMPTY" @@ -82,8 +87,10 @@ class ModuleMakerDialogWrapper( private val addReadme = mutableStateOf(preferenceService.preferenceState.addReadme) private val addGitIgnore = mutableStateOf(preferenceService.preferenceState.addGitIgnore) private val moduleTypeSelection = mutableStateOf(ANDROID) + private val platformTypeSelection = mutableStateOf(ANDROID) private val moduleName = mutableStateOf("") private val packageName = mutableStateOf(preferenceService.preferenceState.packageName) + private val sourceSets = mutableStateListOf() // Segment's write key isn't really a secret private var analytics: Analytics = Analytics("CNghGjhOHipwGB9YdWMBwkMTJbRFtizc") { @@ -121,7 +128,8 @@ class ModuleMakerDialogWrapper( modifier = Modifier.height(startingHeight.value.dp).width(fileTreeWidth.value.dp) ) ConfigurationPanel( - modifier = Modifier.height(startingHeight.value.dp).width(configurationPanelWidth.value.dp) + modifier = Modifier.height(startingHeight.value.dp) + .width(configurationPanelWidth.value.dp) ) } } @@ -222,6 +230,7 @@ class ModuleMakerDialogWrapper( ) } + @OptIn(ExperimentalLayoutApi::class) @Composable private fun ConfigurationPanel( modifier: Modifier = Modifier @@ -295,6 +304,7 @@ class ModuleMakerDialogWrapper( val radioOptions = listOf(ANDROID, KOTLIN) val moduleTypeSelectionState = remember { moduleTypeSelection } Column { + Text("Module Type") radioOptions.forEach { text -> Row( modifier = Modifier.selectable( @@ -324,6 +334,85 @@ class ModuleMakerDialogWrapper( } } + val platformTypeRadioOptions = listOf(ANDROID, MULTIPLATFORM) + val platformTypeRadioOptionsState = remember { platformTypeSelection } + Column { + Text("Platform Type") + platformTypeRadioOptions.forEach { text -> + Row( + modifier = Modifier.selectable( + selected = (text == platformTypeRadioOptionsState.value), + onClick = { + platformTypeRadioOptionsState.value = text + } + ).padding(end = 16.dp), + verticalAlignment = Alignment.CenterVertically + ) { + RadioButton( + colors = RadioButtonDefaults.colors( + selectedColor = MaterialTheme.colors.primary, + unselectedColor = MaterialTheme.colors.primaryVariant + ), + selected = (text == platformTypeRadioOptionsState.value), + onClick = { + platformTypeRadioOptionsState.value = text + } + ) + Text( + text = text, + style = MaterialTheme.typography.body1.merge(), + modifier = Modifier.padding(start = 8.dp) + ) + } + } + + val selectedSourceSets = remember { + sourceSets + } + + AnimatedVisibility( + platformTypeSelection.value == MULTIPLATFORM + ) { + Column { + Text(modifier = Modifier.padding(vertical = 8.dp), text = "Selected Source Sets: ${selectedSourceSets.joinToString(separator = ", ")}") + + FlowRow(Modifier.padding(vertical = 8.dp)) { + kotlinMultiplatformSourceSets.forEach { sourceSet -> + LabelledCheckbox( + label = sourceSet, + checked = selectedSourceSets.contains(sourceSet), + onCheckedChange = { + if (it) { + selectedSourceSets.add(sourceSet) + } else { + selectedSourceSets.remove(sourceSet) + } + } + ) + } + } + + Text("Test Source Sets") + + FlowRow(Modifier.padding(vertical = 8.dp)) { + kotlinMultiplatformTestSourceSets.forEach { sourceSet -> + LabelledCheckbox( + label = sourceSet, + checked = selectedSourceSets.contains(sourceSet), + onCheckedChange = { + if (it) { + selectedSourceSets.add(sourceSet) + } else { + selectedSourceSets.remove(sourceSet) + } + } + ) + } + } + } + } + } + val packageNameState = remember { packageName } OutlinedTextField( label = { Text("Package Name") }, @@ -420,7 +509,9 @@ class ModuleMakerDialogWrapper( packageName = packageName.value, addReadme = addReadme.value, addGitIgnore = addGitIgnore.value, - previewMode = previewMode + previewMode = previewMode, + platformType = platformTypeSelection.value, + sourceSets = sourceSets.toList() ) return filesCreated @@ -476,3 +567,7 @@ class ModuleMakerDialogWrapper( return path.split(File.separator).last() } } + +/* +kotlin multiplatform template in settings + create folders for each source set*/ diff --git a/src/main/kotlin/com/joetr/modulemaker/MultiplatformSourceSets.kt b/src/main/kotlin/com/joetr/modulemaker/MultiplatformSourceSets.kt new file mode 100644 index 0000000..8f9f23d --- /dev/null +++ b/src/main/kotlin/com/joetr/modulemaker/MultiplatformSourceSets.kt @@ -0,0 +1,66 @@ +/* + * Copyrightest (c) 2025 Joseph Roskopf + */ + +package com.joetr.modulemaker + +val kotlinMultiplatformSourceSets = listOf( + // Main Source Sets + "commonMain", + "androidMain", + "jvmMain", + "nativeMain", + "iosMain", + "jsMain", + "wasmJsMain", + "iosArm64Main", + "iosX64Main", + "iosSimulatorArm64Main", + "macosMain", + "macosX64Main", + "macosArm64Main", + "linuxMain", + "linuxX64Main", + "linuxArm64Main", + "mingwMain", + "mingwX64Main", + "tvosMain", + "tvosArm64Main", + "tvosX64Main", + "tvosSimulatorArm64Main", + "watchosMain", + "watchosArm32Main", + "watchosArm64Main", + "watchosX64Main", + "watchosSimulatorArm64Main" +) + +val kotlinMultiplatformTestSourceSets = listOf( + "commonTest", + "androidTest", + "jvmTest", + "nativeTest", + "iosTest", + "jsTest", + "wasmTest", + "iosArm64Test", + "iosX64Test", + "iosSimulatorArm64Test", + "macosTest", + "macosX64Test", + "macosArm64Test", + "linuxTest", + "linuxX64Test", + "linuxArm64Test", + "mingwTest", + "mingwX64Test", + "tvosTest", + "tvosArm64Test", + "tvosX64Test", + "tvosSimulatorArm64Test", + "watchosTest", + "watchosArm32Test", + "watchosArm64Test", + "watchosX64Test", + "watchosSimulatorArm64Test" +) diff --git a/src/main/kotlin/com/joetr/modulemaker/SettingsDialogWrapper.kt b/src/main/kotlin/com/joetr/modulemaker/SettingsDialogWrapper.kt index 04439eb..825ba19 100644 --- a/src/main/kotlin/com/joetr/modulemaker/SettingsDialogWrapper.kt +++ b/src/main/kotlin/com/joetr/modulemaker/SettingsDialogWrapper.kt @@ -40,6 +40,7 @@ import com.joetr.modulemaker.template.AndroidModuleTemplate import com.joetr.modulemaker.template.GitIgnoreTemplate import com.joetr.modulemaker.template.KotlinModuleKtsTemplate import com.joetr.modulemaker.template.KotlinModuleTemplate +import com.joetr.modulemaker.template.MultiplatformKtsTemplate import com.joetr.modulemaker.template.TemplateVariable import com.joetr.modulemaker.ui.LabelledCheckbox import com.joetr.modulemaker.ui.theme.WidgetTheme @@ -109,6 +110,8 @@ class SettingsDialogWrapper( private val kotlinTemplateTextArea = mutableStateOf(TextFieldValue(preferenceService.preferenceState.kotlinTemplate)) + private val multiplatformTemplateTextArea = + mutableStateOf(TextFieldValue(preferenceService.preferenceState.multiplatformTemplate)) init { title = "Settings" init() @@ -128,7 +131,13 @@ class SettingsDialogWrapper( fun SettingsTab() { var tabIndex by remember { mutableStateOf(0) } - val tabs = listOf("Module Template Defaults", "Enhanced Template Defaults", ".gitignore Template Defaults", "General") + val tabs = listOf( + "Module Template Defaults", + "Enhanced Template Defaults", + "Multiplatform Template Defaults", + ".gitignore Template Defaults", + "General" + ) WidgetTheme { Surface { @@ -145,20 +154,51 @@ class SettingsDialogWrapper( when (tabIndex) { 0 -> TemplateDefaultComponent() 1 -> EnhancedTemplateDefaultComponent() - 2 -> GitIgnoreTemplateDefaultPanel() - 3 -> GeneralPanel() + 2 -> MultiplatformDefaultComponent() + 3 -> GitIgnoreTemplateDefaultPanel() + 4 -> GeneralPanel() } } } } } + @Composable + private fun MultiplatformDefaultComponent() { + Column(modifier = Modifier.fillMaxSize().padding(8.dp)) { + val settingExplanationText = + """ + You can override the multiplatform gradle templates created with your own project specific defaults. + + If nothing is specified here, a sensible default will be generated for you. + """.trimIndent() + + Text( + modifier = Modifier.padding(8.dp), + text = settingExplanationText + ) + val multiplatformModuleTemplateState = remember { multiplatformTemplateTextArea } + + OutlinedTextField( + modifier = Modifier.fillMaxSize().padding(8.dp), + value = multiplatformModuleTemplateState.value, + onValueChange = { + multiplatformModuleTemplateState.value = it + } + ) + } + } + @Composable private fun GitIgnoreTemplateDefaultPanel() { Column(modifier = Modifier.fillMaxSize().padding(8.dp)) { Text( modifier = Modifier.padding(8.dp), - text = "You can override the .gitignore templates created with your own project specific default.\n\nIf nothing is specified here, a sensible default will be generated for you." + text = """ + You can override the .gitignore templates created with your own project specific default. + + If nothing is specified here, a sensible default will be generated for you. + """.trimIndent() ) val gitIgnoreTemplateState = remember { gitignoreTemplateTextArea } @@ -288,13 +328,23 @@ class SettingsDialogWrapper( modifier = Modifier.fillMaxSize().padding(8.dp).verticalScroll(rememberScrollState()) ) { val settingExplanationText = - "You can override the gradle templates created with your own project specific defaults./n/n If nothing is specified here, a sensible default will be generated for you." + """ + You can override the gradle templates created with your own project specific defaults. + + If nothing is specified here, a sensible default will be generated for you. + """.trimIndent() val supportedVariablesString = TemplateVariable.values().joinToString("\n") { it.templateVariable } val supportedVariablesLabel = - "If you do have a custom template, there are some variable names that will be automatically replaced for you.\n\n Supported variables are:\n\n $supportedVariablesString" + """ + If you do have a custom template, there are some variable names that will be automatically replaced for you. + + Supported variables are: + + $supportedVariablesString + """.trimIndent() Text(settingExplanationText) @@ -418,6 +468,7 @@ class SettingsDialogWrapper( androidTemplateTextArea.value = TextFieldValue(state.androidTemplate) kotlinTemplateTextArea.value = TextFieldValue(state.kotlinTemplate) + multiplatformTemplateTextArea.value = TextFieldValue(state.multiplatformTemplate) apiTemplateTextArea.value = TextFieldValue(state.apiTemplate) implTemplateTextArea.value = TextFieldValue(state.implTemplate) glueTemplateTextArea.value = TextFieldValue(state.glueTemplate) @@ -458,6 +509,7 @@ class SettingsDialogWrapper( private fun getJsonFromSettings(): String { val androidTemplate: String = androidTemplateTextArea.value.text val kotlinTemplate: String = kotlinTemplateTextArea.value.text + val multiplatformTemplate: String = multiplatformTemplateTextArea.value.text val apiTemplate: String = apiTemplateTextArea.value.text val glueTemplate: String = glueTemplateTextArea.value.text val implTemplate: String = implTemplateTextArea.value.text @@ -475,6 +527,7 @@ class SettingsDialogWrapper( val newState = PreferenceServiceImpl.Companion.State( androidTemplate = androidTemplate, kotlinTemplate = kotlinTemplate, + multiplatformTemplate = multiplatformTemplate, apiTemplate = apiTemplate, glueTemplate = glueTemplate, implTemplate = implTemplate, @@ -512,6 +565,7 @@ class SettingsDialogWrapper( preferenceService.preferenceState = preferenceService.preferenceState.copy( androidTemplate = androidTemplateTextArea.value.text, kotlinTemplate = kotlinTemplateTextArea.value.text, + multiplatformTemplate = multiplatformTemplateTextArea.value.text, apiTemplate = apiTemplateTextArea.value.text, implTemplate = implTemplateTextArea.value.text, glueTemplate = glueTemplateTextArea.value.text, @@ -533,6 +587,7 @@ class SettingsDialogWrapper( private fun clearData() { androidTemplateTextArea.value = TextFieldValue(getDefaultTemplate()) kotlinTemplateTextArea.value = TextFieldValue(getDefaultTemplate(isKotlin = true)) + multiplatformTemplateTextArea.value = TextFieldValue(getDefaultTemplate(isMultiplatform = true)) apiTemplateTextArea.value = TextFieldValue(getDefaultTemplate()) implTemplateTextArea.value = TextFieldValue(getDefaultTemplate()) glueTemplateTextArea.value = TextFieldValue(getDefaultTemplate()) @@ -551,7 +606,10 @@ class SettingsDialogWrapper( apiModuleNameTextArea.value = TextFieldValue(DEFAULT_API_MODULE_NAME) } - private fun getDefaultTemplate(isKotlin: Boolean = false): String { + private fun getDefaultTemplate(isKotlin: Boolean = false, isMultiplatform: Boolean = false): String { + if (isMultiplatform) { + return MultiplatformKtsTemplate.data + } if (isKotlin) { return if (isKtsCurrentlyChecked) { KotlinModuleKtsTemplate.data diff --git a/src/main/kotlin/com/joetr/modulemaker/file/FileWriter.kt b/src/main/kotlin/com/joetr/modulemaker/file/FileWriter.kt index 5320060..c7718b9 100644 --- a/src/main/kotlin/com/joetr/modulemaker/file/FileWriter.kt +++ b/src/main/kotlin/com/joetr/modulemaker/file/FileWriter.kt @@ -1,5 +1,7 @@ package com.joetr.modulemaker.file +import com.joetr.modulemaker.ANDROID +import com.joetr.modulemaker.MULTIPLATFORM import com.joetr.modulemaker.persistence.PreferenceService import com.joetr.modulemaker.template.GitIgnoreTemplate import com.joetr.modulemaker.template.TemplateWriter @@ -40,7 +42,9 @@ class FileWriter( addReadme: Boolean, addGitIgnore: Boolean, rootPathString: String, - previewMode: Boolean = false + previewMode: Boolean = false, + platformType: String = ANDROID, + sourceSets: List = emptyList() ): List { val filesCreated = mutableListOf() @@ -82,7 +86,9 @@ class FileWriter( packageName = packageName, addReadme = addReadme, addGitIgnore = addGitIgnore, - previewMode = previewMode + previewMode = previewMode, + platformType = platformType, + sourceSets = sourceSets ) } else { filesCreated += createDefaultModuleStructure( @@ -94,7 +100,9 @@ class FileWriter( packageName = packageName, addReadme = addReadme, addGitIgnore = addGitIgnore, - previewMode = previewMode + previewMode = previewMode, + platformType = platformType, + sourceSets = sourceSets ) } @@ -113,7 +121,9 @@ class FileWriter( packageName: String, addReadme: Boolean, addGitIgnore: Boolean, - previewMode: Boolean + previewMode: Boolean, + platformType: String, + sourceSets: List ): List { val filesCreated = mutableListOf() @@ -132,14 +142,17 @@ class FileWriter( defaultKey = GLUE_KEY, gradleFileFollowModule = gradleFileFollowModule, packageName = packageName.plus(".${preferenceService.preferenceState.glueModuleName}"), - previewMode = previewMode + previewMode = previewMode, + platformType = platformType ) // create default packages filesCreated += createDefaultPackages( moduleFile = this, packageName = packageName.plus(".${preferenceService.preferenceState.glueModuleName}"), - previewMode = previewMode + previewMode = previewMode, + platformType = platformType, + sourceSets = sourceSets ) if (addGitIgnore) { @@ -163,14 +176,17 @@ class FileWriter( defaultKey = IMPL_KEY, gradleFileFollowModule = gradleFileFollowModule, packageName = packageName.plus(".${preferenceService.preferenceState.implModuleName}"), - previewMode = previewMode + previewMode = previewMode, + platformType = platformType ) // create default packages filesCreated += createDefaultPackages( moduleFile = this, packageName = packageName.plus(".${preferenceService.preferenceState.implModuleName}"), - previewMode = previewMode + previewMode = previewMode, + platformType = platformType, + sourceSets = sourceSets ) if (addGitIgnore) { @@ -194,7 +210,8 @@ class FileWriter( defaultKey = API_KEY, gradleFileFollowModule = gradleFileFollowModule, packageName = packageName.plus(".${preferenceService.preferenceState.apiModuleName}"), - previewMode = previewMode + previewMode = previewMode, + platformType = platformType ) if (addReadme) { @@ -210,7 +227,9 @@ class FileWriter( filesCreated += createDefaultPackages( moduleFile = this, packageName = packageName.plus(".${preferenceService.preferenceState.apiModuleName}"), - previewMode = previewMode + previewMode = previewMode, + platformType = platformType, + sourceSets = sourceSets ) if (addGitIgnore) { @@ -233,7 +252,9 @@ class FileWriter( packageName: String, addReadme: Boolean, addGitIgnore: Boolean, - previewMode: Boolean + previewMode: Boolean, + platformType: String, + sourceSets: List ): List { val filesCreated = mutableListOf() @@ -246,7 +267,8 @@ class FileWriter( defaultKey = null, gradleFileFollowModule = gradleFileFollowModule, packageName = packageName, - previewMode = previewMode + previewMode = previewMode, + platformType = platformType ) if (addReadme) { @@ -262,7 +284,9 @@ class FileWriter( filesCreated += createDefaultPackages( moduleFile = moduleFile, packageName = packageName, - previewMode = previewMode + previewMode = previewMode, + platformType = platformType, + sourceSets = sourceSets ) if (addGitIgnore) { @@ -304,20 +328,39 @@ class FileWriter( private fun createDefaultPackages( moduleFile: File, packageName: String, - previewMode: Boolean + previewMode: Boolean, + platformType: String, + sourceSets: List ): List { + fun makePath(srcPath: File): File { + val packagePath = Paths.get(srcPath.path, packageName.split(".").joinToString(File.separator)).toFile() + // create default package + val stringBuilder = StringBuilder() + val filePath = Paths.get(srcPath.absolutePath, stringBuilder.toString()).toFile() + if (previewMode.not()) { + packagePath.mkdirs() + filePath.mkdirs() + } + return packagePath + } // create src/main - val srcPath = Paths.get(moduleFile.absolutePath, "src/main/kotlin").toFile() - val packagePath = Paths.get(srcPath.path, packageName.split(".").joinToString(File.separator)).toFile() - // create default package - val stringBuilder = StringBuilder() - val filePath = Paths.get(srcPath.absolutePath, stringBuilder.toString()).toFile() - if (previewMode.not()) { - packagePath.mkdirs() - filePath.mkdirs() + val packagePaths = if (platformType == ANDROID) { + val srcPath = Paths.get(moduleFile.absolutePath, "src/main/kotlin").toFile() + val packagePath = makePath(srcPath) + listOf(packagePath) + } else if (platformType == MULTIPLATFORM) { + val paths = mutableListOf() + sourceSets.forEach { + val srcPath = Paths.get(moduleFile.absolutePath, "src/$it/kotlin").toFile() + val packagePath = makePath(srcPath) + paths.add(packagePath) + } + paths + } else { + throw IllegalArgumentException("Unknown platform type $platformType") } - return listOf(packagePath) + return packagePaths } /** diff --git a/src/main/kotlin/com/joetr/modulemaker/persistence/PreferenceServiceImpl.kt b/src/main/kotlin/com/joetr/modulemaker/persistence/PreferenceServiceImpl.kt index 94eaa4a..fe31089 100644 --- a/src/main/kotlin/com/joetr/modulemaker/persistence/PreferenceServiceImpl.kt +++ b/src/main/kotlin/com/joetr/modulemaker/persistence/PreferenceServiceImpl.kt @@ -45,6 +45,7 @@ class PreferenceServiceImpl : PersistentStateComponent + iosTarget.binaries.framework { + baseName = "shared" + isStatic = true + } + } + + applyDefaultHierarchyTemplate() + + sourceSets { + all { + languageSettings { + optIn("org.jetbrains.compose.resources.ExperimentalResourceApi") + } + } + + commonMain.dependencies { + implementation(compose.runtime) + implementation(compose.foundation) + implementation(compose.material) + implementation(compose.components.resources) + } + + androidMain.dependencies { + + } + + val jsWasmMain by creating { + dependsOn(commonMain.get()) + } + + val jsMain by getting { + dependsOn(jsWasmMain) + } + + val wasmJsMain by getting { + dependsOn(jsWasmMain) + } + + val desktopMain by getting + desktopMain.dependencies { + implementation(compose.desktop.common) + + } + val desktopTest by getting + desktopTest.dependencies { + implementation(compose.desktop.currentOs) + implementation(compose.desktop.uiTestJUnit4) + } + } + } + """.trimIndent() +} diff --git a/src/main/kotlin/com/joetr/modulemaker/template/TemplateWriter.kt b/src/main/kotlin/com/joetr/modulemaker/template/TemplateWriter.kt index 9ced6e0..11b90c5 100644 --- a/src/main/kotlin/com/joetr/modulemaker/template/TemplateWriter.kt +++ b/src/main/kotlin/com/joetr/modulemaker/template/TemplateWriter.kt @@ -3,6 +3,7 @@ package com.joetr.modulemaker.template import com.joetr.modulemaker.ANDROID import com.joetr.modulemaker.FREEMARKER_VERSION import com.joetr.modulemaker.KOTLIN +import com.joetr.modulemaker.MULTIPLATFORM import com.joetr.modulemaker.file.ANDROID_KEY import com.joetr.modulemaker.file.API_KEY import com.joetr.modulemaker.file.GLUE_KEY @@ -37,7 +38,8 @@ class TemplateWriter( defaultKey: String?, gradleFileFollowModule: Boolean, packageName: String, - previewMode: Boolean + previewMode: Boolean, + platformType: String ): List { try { // Build the data-model @@ -47,7 +49,7 @@ class TemplateWriter( // load gradle file from template folder val gradleTemplate: Template = when (moduleType) { KOTLIN -> { - val customPreferences = getPreferenceFromKey(defaultKey, KOTLIN_KEY) + val customPreferences = getPreferenceFromKey(defaultKey, if (platformType == MULTIPLATFORM) MULTIPLATFORM else KOTLIN_KEY) if (customPreferences.isNotEmpty()) { Template( null, @@ -68,7 +70,8 @@ class TemplateWriter( } } ANDROID -> { - val customPreferences = getPreferenceFromKey(defaultKey, ANDROID_KEY) + val customPreferences = getPreferenceFromKey(defaultKey, if (platformType == MULTIPLATFORM) MULTIPLATFORM else ANDROID_KEY) + if (customPreferences.isNotEmpty()) { Template( null, @@ -76,10 +79,16 @@ class TemplateWriter( cfg ) } else { - val template = if (useKtsBuildFile) { - AndroidModuleKtsTemplate.data + val template = if (platformType == ANDROID) { + if (useKtsBuildFile) { + AndroidModuleKtsTemplate.data + } else { + AndroidModuleTemplate.data + } + } else if (platformType == MULTIPLATFORM) { + MultiplatformKtsTemplate.data } else { - AndroidModuleTemplate.data + throw IllegalArgumentException("Unknown platform type $platformType") } Template( null, @@ -165,6 +174,7 @@ class TemplateWriter( API_KEY -> preferenceService.preferenceState.apiTemplate GLUE_KEY -> preferenceService.preferenceState.glueTemplate ANDROID_KEY -> preferenceService.preferenceState.androidTemplate + MULTIPLATFORM -> preferenceService.preferenceState.multiplatformTemplate KOTLIN_KEY -> preferenceService.preferenceState.kotlinTemplate else -> "" } diff --git a/src/test/kotlin/com/joetr/modulemaker/AndroidModuleMakerTest.kt b/src/test/kotlin/com/joetr/modulemaker/AndroidModuleMakerTest.kt index 41e7012..7874c88 100644 --- a/src/test/kotlin/com/joetr/modulemaker/AndroidModuleMakerTest.kt +++ b/src/test/kotlin/com/joetr/modulemaker/AndroidModuleMakerTest.kt @@ -63,6 +63,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert it was added to settings.gradle @@ -219,6 +220,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert it was added to settings.gradle @@ -279,6 +281,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert build.gradle.kts is generated @@ -313,6 +316,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert it was added to settings.gradle @@ -347,6 +351,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert readme exists @@ -381,6 +386,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert readme does not exists @@ -414,6 +420,7 @@ class AndroidModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was not generated @@ -447,6 +454,7 @@ class AndroidModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was generated and has the expected contents @@ -488,6 +496,7 @@ class AndroidModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was generated and has the expected contents diff --git a/src/test/kotlin/com/joetr/modulemaker/EnhancedModuleMakerTest.kt b/src/test/kotlin/com/joetr/modulemaker/EnhancedModuleMakerTest.kt index e158bc1..6163bec 100644 --- a/src/test/kotlin/com/joetr/modulemaker/EnhancedModuleMakerTest.kt +++ b/src/test/kotlin/com/joetr/modulemaker/EnhancedModuleMakerTest.kt @@ -63,6 +63,7 @@ class EnhancedModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert it was added to settings.gradle @@ -162,6 +163,7 @@ class EnhancedModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert build.gradle is generated for all 3 modules @@ -226,6 +228,7 @@ class EnhancedModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert build.gradle is generated for all 3 modules @@ -284,6 +287,7 @@ class EnhancedModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert readme was not generated in the api module @@ -317,6 +321,7 @@ class EnhancedModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was not generated in any of the modules module @@ -358,6 +363,7 @@ class EnhancedModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val apiGitIgnore = File(folder.root.path + File.separator + modulePathAsFile + File.separator + "api" + File.separator + ".gitignore") @@ -413,6 +419,7 @@ class EnhancedModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val apiGitIgnore = File(folder.root.path + File.separator + modulePathAsFile + File.separator + "api" + File.separator + ".gitignore") @@ -465,6 +472,7 @@ class EnhancedModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val settingsGradleFileContents = readFromFile(file = settingsGradleFile) @@ -510,6 +518,7 @@ class EnhancedModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert it was added to settings.gradle diff --git a/src/test/kotlin/com/joetr/modulemaker/KotlinModuleMakerTest.kt b/src/test/kotlin/com/joetr/modulemaker/KotlinModuleMakerTest.kt index bd56a39..30bddcb 100644 --- a/src/test/kotlin/com/joetr/modulemaker/KotlinModuleMakerTest.kt +++ b/src/test/kotlin/com/joetr/modulemaker/KotlinModuleMakerTest.kt @@ -63,6 +63,7 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert it was added to settings.gradle @@ -116,10 +117,12 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert build.gradle is generated - val buildGradleFile = File(folder.root.path + File.separator + modulePathAsFile + File.separator + buildGradleFileName) + val buildGradleFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + buildGradleFileName) assert( // root/repository/build.gradle buildGradleFile.exists() @@ -158,6 +161,7 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert build.gradle.kts is generated @@ -193,6 +197,7 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert build.gradle.kts is generated @@ -228,6 +233,7 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert readme is NOT generated @@ -262,6 +268,7 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert readme is generated @@ -296,6 +303,7 @@ class KotlinModuleMakerTest { addGitIgnore = false, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was not generated @@ -329,10 +337,12 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was generated and has the expected contents - val gitignoreFile = File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore") + val gitignoreFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore") val gitignoreFileContents = readFromFile(file = gitignoreFile) assertEquals( GitIgnoreTemplate.data, @@ -370,10 +380,12 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) // assert gitignore was generated and has the expected contents - val gitignoreFile = File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore") + val gitignoreFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore") val gitignoreFileContents = readFromFile(file = gitignoreFile) assertEquals( template, @@ -408,6 +420,7 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val settingsGradleFileContents = readFromFile(file = settingsGradleFile) @@ -444,6 +457,7 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val settingsGradleFileContents = readFromFile(file = settingsGradleFile) @@ -478,6 +492,7 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val settingsGradleFileContents = readFromFile(file = settingsGradleFile) @@ -514,6 +529,7 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val settingsGradleFileContents = readFromFile(file = settingsGradleFile) @@ -548,6 +564,7 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = false + ) val settingsGradleFileContents = readFromFile(file = settingsGradleFile) @@ -586,6 +603,7 @@ class KotlinModuleMakerTest { addGitIgnore = true, rootPathString = folder.root.toString(), previewMode = true + ) val settingsGradleFileContentsAfter = readFromFile(file = settingsGradleFile) diff --git a/src/test/kotlin/com/joetr/modulemaker/MultiplatformModuleMakerTest.kt b/src/test/kotlin/com/joetr/modulemaker/MultiplatformModuleMakerTest.kt new file mode 100644 index 0000000..b647196 --- /dev/null +++ b/src/test/kotlin/com/joetr/modulemaker/MultiplatformModuleMakerTest.kt @@ -0,0 +1,647 @@ +package com.joetr.modulemaker + +import com.joetr.modulemaker.file.FileWriter +import com.joetr.modulemaker.persistence.PreferenceService +import com.joetr.modulemaker.persistence.PreferenceServiceImpl +import com.joetr.modulemaker.template.GitIgnoreTemplate +import org.junit.Assert +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class MultiplatformModuleMakerTest { + + @JvmField + @Rule + var folder = TemporaryFolder() + + var testState = PreferenceServiceImpl.Companion.State() + + private val fakePreferenceService = object : PreferenceService { + override var preferenceState: PreferenceServiceImpl.Companion.State + get() = testState + set(value) { + testState = value + } + } + + private val fileWriter = FileWriter( + preferenceService = fakePreferenceService + ) + + private lateinit var settingsGradleFile: File + + @Before + fun before() { + settingsGradleFile = folder.populateSettingsGradleKtsWithFakeData() + } + + @Test + fun `multiplatform module created successfully`() { + val modulePath = ":multiplatform-module" + val modulePathAsFile = "multiplatform-module" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = ANDROID, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = true, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + + ) + + // assert it was added to settings.gradle + val settingsGradleFileContents = readFromFile(file = settingsGradleFile) + assert( + settingsGradleFileContents.contains("include(\":multiplatform-module\")") + ) + + // assert readme was generated + assert( + // root/repository/README.md + File(folder.root.path + File.separator + modulePathAsFile + File.separator + readmeFile).exists() + ) + + // assert build.gradle is generated + assert( + // root/repository/build.gradle + File(folder.root.path + File.separator + modulePathAsFile + File.separator + buildGradleFileName).exists() + ) + + // assert the correct package structure is generated + assert( + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "src/androidMain/kotlin/com/joetr/test").exists() + ) + assert( + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "src/iosMain/kotlin/com/joetr/test").exists() + ) + assert( + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "src/jvmMain/kotlin/com/joetr/test").exists() + ) + } + + @Test + fun `when a template is set, that is used instead of default for creating build gradle`() { + val modulePath = ":repository" + val modulePathAsFile = "repository" + val template = "test template" + fakePreferenceService.preferenceState.multiplatformTemplate = template + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert build.gradle is generated + val buildGradleFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + buildGradleFileName) + assert( + // root/repository/build.gradle + buildGradleFile.exists() + ) + + // assert package name is included in build.gradle + val buildGradleFileContents = readFromFile(buildGradleFile) + assert( + buildGradleFileContents.contains( + template + ) + ) + } + + @Test + fun `android module created successfully with a kts build file named after module`() { + val modulePath = ":repository:database" + val modulePathAsFile = "repository/database" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = ANDROID, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = true, + gradleFileFollowModule = true, + packageName = testPackageName, + addReadme = false, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert build.gradle.kts is generated + val buildGradleFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "database.gradle.kts") + assert( + // root/repository/database/build.gradle + buildGradleFile.exists() + ) + } + + @Test + fun `kotlin module created successfully with a gradle build file named after module`() { + val modulePath = ":repository:database" + val modulePathAsFile = "repository/database" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = true, + packageName = testPackageName, + addReadme = false, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert build.gradle.kts is generated + val buildGradleFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "database.gradle") + assert( + // root/repository/database/build.gradle + buildGradleFile.exists() + ) + } + + @Test + fun `readme is not added to kotlin module when setting is disabled`() { + val modulePath = ":repository:database" + val modulePathAsFile = "repository/database" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = true, + packageName = testPackageName, + addReadme = false, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert readme is NOT generated + val buildGradleFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "README.md") + assert( + buildGradleFile.exists().not() + ) + } + + @Test + fun `readme is added to kotlin module when setting is enabled`() { + val modulePath = ":repository:database" + val modulePathAsFile = "repository/database" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = true, + packageName = testPackageName, + addReadme = true, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert readme is generated + val buildGradleFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + "README.md") + assert( + buildGradleFile.exists() + ) + } + + @Test + fun `gitignore is not generated in kotlin module when setting is disabled`() { + val modulePath = ":repository" + val modulePathAsFile = "repository" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = false, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert gitignore was not generated + assert( + File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore").exists() + .not() + ) + } + + @Test + fun `gitignore is generated in kotlin module with default settings when setting is enabled`() { + val modulePath = ":repository" + val modulePathAsFile = "repository" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert gitignore was generated and has the expected contents + val gitignoreFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore") + val gitignoreFileContents = readFromFile(file = gitignoreFile) + assertEquals( + GitIgnoreTemplate.data, + gitignoreFileContents.joinToString("\n") + ) + } + + @Test + fun `gitignore is generated in kotlin module with custom settings when setting is enabled`() { + val modulePath = ":repository" + val modulePathAsFile = "repository" + + val template = """ + this is a custom template + """.trimIndent() + + fakePreferenceService.preferenceState.gitignoreTemplate = template + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + // assert gitignore was generated and has the expected contents + val gitignoreFile = + File(folder.root.path + File.separator + modulePathAsFile + File.separator + File.separator + ".gitignore") + val gitignoreFileContents = readFromFile(file = gitignoreFile) + assertEquals( + template, + gitignoreFileContents.joinToString("\n") + ) + } + + @Test + fun `create module works with 2 parameters`() { + settingsGradleFile.delete() + settingsGradleFile = folder.populateSettingsGradleKtsWithFakeFilePathData() + val modulePath = ":repository:network" + val modulePathAsFile = "repository/network" + val rootPathString = folder.root.toString().removePrefix("/") + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + val settingsGradleFileContents = readFromFile(file = settingsGradleFile) + assertEquals( + "include(\"$modulePath\", \"$rootPathString/$modulePathAsFile\")", + settingsGradleFileContents[56] + ) + } + + @Test + fun `create module works with 2 parameters and custom include for modules`() { + settingsGradleFile.delete() + settingsGradleFile = folder.populateSettingsGradleKtsWithFakeFilePathDataAndCustomInclude() + val modulePath = ":repository:network" + val modulePathAsFile = "repository/network" + val rootPathString = folder.root.toString().removePrefix("/") + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + val settingsGradleFileContents = readFromFile(file = settingsGradleFile) + assertEquals( + "includeBuild(\"$modulePath\", \"$rootPathString/$modulePathAsFile\")", + settingsGradleFileContents[56] + ) + } + + @Test + fun `module added correctly settings gradle with one big include`() { + settingsGradleFile.delete() + settingsGradleFile = folder.populateSettingsGradleKtsWithTiviSettingsGradleKts() + val modulePath = ":repository:network" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + val settingsGradleFileContents = readFromFile(file = settingsGradleFile) + assertEquals( + "include(\"$modulePath\")", + settingsGradleFileContents[45] + ) + } + + @Test + fun `custom include keyword is used when specified`() { + settingsGradleFile.delete() + settingsGradleFile = folder.populateSettingsGradleKtsWithTiviWithCustomIncludeSettingsGradleKts() + val modulePath = ":repository:network" + + fakePreferenceService.preferenceState.includeProjectKeyword = "testIncludeProject" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + val settingsGradleFileContents = readFromFile(file = settingsGradleFile) + assertEquals( + "testIncludeProject(\"$modulePath\")", + settingsGradleFileContents[45] + ) + } + + @Test + fun `module added correctly settings gradle with one include statement`() { + settingsGradleFile.delete() + settingsGradleFile = folder.populateSettingsGradleWithFakeData() + val modulePath = ":repository:network" + + fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = false, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + val settingsGradleFileContents = readFromFile(file = settingsGradleFile) + assertEquals( + "include(\"$modulePath\")", + settingsGradleFileContents[16] + ) + } + + @Test + fun `no files created in preview mode`() { + settingsGradleFile.delete() + settingsGradleFile = folder.populateSettingsGradleWithFakeData() + val modulePath = ":repository:network" + + val rootFiles = folder.root.listFiles() + + val settingsGradleFileContentsBefore = readFromFile(file = settingsGradleFile) + + val filesToReturn = fileWriter.createModule( + settingsGradleFile = settingsGradleFile, + workingDirectory = folder.root, + modulePathAsString = modulePath, + moduleType = KOTLIN, + showErrorDialog = { + Assert.fail("No errors should be thrown") + }, + showSuccessDialog = { + assert(true) + }, + enhancedModuleCreationStrategy = false, + useKtsBuildFile = false, + gradleFileFollowModule = false, + packageName = testPackageName, + addReadme = false, + addGitIgnore = true, + rootPathString = folder.root.toString(), + previewMode = true, + platformType = MULTIPLATFORM, + sourceSets = listOf("jvmMain", "iosMain", "androidMain") + ) + + val settingsGradleFileContentsAfter = readFromFile(file = settingsGradleFile) + + assertEquals( + settingsGradleFileContentsBefore, + settingsGradleFileContentsAfter + ) + + assertEquals( + filesToReturn.size, + 5 + ) + + assertEquals( + rootFiles!!.size, + folder.root.listFiles()!!.size + ) + } +}