diff --git a/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt b/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt index 05704648..d22e765b 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt @@ -44,7 +44,7 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, private v Injector.configPojoToBuildTypeConfigsConverter).run() } - @Language("JSON") const val SAMPLE_CONFIG = """ + @Language("JSON") val SAMPLE_CONFIG = """ { "projectName": "genny", "root": "./modules/", @@ -73,7 +73,7 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, private v ], "buildTypes": 6 } - """ + """.trimIndent() } fun run() { diff --git a/src/main/kotlin/com/google/androidstudiopoet/BuildGradle.kt b/src/main/kotlin/com/google/androidstudiopoet/BuildGradle.kt index dd5e6525..cb65b239 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/BuildGradle.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/BuildGradle.kt @@ -14,12 +14,14 @@ package com.google.androidstudiopoet +import com.google.androidstudiopoet.utils.joinLines + annotation class Fancy @Fancy object BuildGradle { - fun print(dependencies: String, useKotlin: Boolean) = "" + + fun print(dependencies: String, useKotlin: Boolean, extraLines: List? = null) = "" + "apply plugin: 'java-library'\n" + (if (useKotlin) "apply plugin: 'kotlin'\n" @@ -51,7 +53,7 @@ object BuildGradle { "repositories {\n" + " mavenCentral()\n" + "}\n" + - if (useKotlin) + (if (useKotlin) "compileKotlin {\n" + " kotlinOptions {\n" + " jvmTarget = \"1.8\"\n" + @@ -63,5 +65,6 @@ object BuildGradle { " }\n" + "}\n" else - "" + "" ) + + extraLines.joinLines() } diff --git a/src/main/kotlin/com/google/androidstudiopoet/ModuleBlueprintFactory.kt b/src/main/kotlin/com/google/androidstudiopoet/ModuleBlueprintFactory.kt index fb42874b..f1f59676 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/ModuleBlueprintFactory.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/ModuleBlueprintFactory.kt @@ -36,7 +36,7 @@ object ModuleBlueprintFactory { } val result = ModuleBlueprint(getModuleNameByIndex(moduleConfig.index), projectRoot, moduleConfig.useKotlin, moduleDependencies, moduleConfig.javaPackageCount, moduleConfig.javaClassCount, moduleConfig.javaMethodsPerClass, - moduleConfig.kotlinPackageCount, moduleConfig.kotlinClassCount, moduleConfig.kotlinMethodsPerClass) + moduleConfig.kotlinPackageCount, moduleConfig.kotlinClassCount, moduleConfig.kotlinMethodsPerClass, moduleConfig.extraLines) synchronized(moduleDependencyLock[moduleConfig.index]) { if (moduleDependencyCache[moduleConfig.index] == null) { moduleDependencyCache[moduleConfig.index] = ModuleDependency(result.name, result.methodToCallFromOutside) @@ -69,7 +69,7 @@ object ModuleBlueprintFactory { */ val tempModuleBlueprint = ModuleBlueprint(getModuleNameByIndex(index), projectRoot, useKotlin, listOf(), javaPackageCount, javaClassCount, javaMethodsPerClass, kotlinPackageCount,kotlinClassCount, - kotlinMethodsPerClass) + kotlinMethodsPerClass, null) return ModuleDependency(tempModuleBlueprint.name, tempModuleBlueprint.methodToCallFromOutside) } @@ -102,7 +102,8 @@ object ModuleBlueprintFactory { moduleDependencies, androidModuleConfig.productFlavorConfigs, androidModuleConfig.buildTypes, androidModuleConfig.javaPackageCount, androidModuleConfig.javaClassCount, androidModuleConfig.javaMethodsPerClass, androidModuleConfig.kotlinPackageCount, - androidModuleConfig.kotlinClassCount, androidModuleConfig.kotlinMethodsPerClass) + androidModuleConfig.kotlinClassCount, androidModuleConfig.kotlinMethodsPerClass, + androidModuleConfig.extraLines) } private fun getModuleNameByIndex(index: Int) = "module$index" diff --git a/src/main/kotlin/com/google/androidstudiopoet/generators/BuildGradleGenerator.kt b/src/main/kotlin/com/google/androidstudiopoet/generators/BuildGradleGenerator.kt index ff66b89a..19f73b3f 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/generators/BuildGradleGenerator.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/generators/BuildGradleGenerator.kt @@ -24,6 +24,6 @@ class BuildGradleGenerator { fun create(moduleBlueprint: ModuleBlueprint): String { return BuildGradle.print(moduleBlueprint.dependencies .map { it -> "compile project(':${it.name}')\n" } - .fold(), moduleBlueprint.useKotlin) + .fold(), moduleBlueprint.useKotlin, moduleBlueprint.extraLines) } } diff --git a/src/main/kotlin/com/google/androidstudiopoet/generators/android_modules/AndroidModuleBuildGradleGenerator.kt b/src/main/kotlin/com/google/androidstudiopoet/generators/android_modules/AndroidModuleBuildGradleGenerator.kt index 86953329..966abe9d 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/generators/android_modules/AndroidModuleBuildGradleGenerator.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/generators/android_modules/AndroidModuleBuildGradleGenerator.kt @@ -20,6 +20,7 @@ import com.google.androidstudiopoet.models.AndroidModuleBlueprint import com.google.androidstudiopoet.models.Flavor import com.google.androidstudiopoet.utils.fold import com.google.androidstudiopoet.utils.isNullOrEmpty +import com.google.androidstudiopoet.utils.joinLines import com.google.androidstudiopoet.utils.joinPath import com.google.androidstudiopoet.writers.FileWriter @@ -87,6 +88,7 @@ dependencies { ${align(moduleDependencies.trimEnd()," ")} } +${blueprint.extraLines.joinLines()} """.trim() fileWriter.writeToFile(gradleText, moduleRoot.joinPath("build.gradle")) diff --git a/src/main/kotlin/com/google/androidstudiopoet/input/AndroidModuleConfig.kt b/src/main/kotlin/com/google/androidstudiopoet/input/AndroidModuleConfig.kt index 99da20d3..f6ef971e 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/input/AndroidModuleConfig.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/input/AndroidModuleConfig.kt @@ -24,4 +24,5 @@ class AndroidModuleConfig(index: Int, config: ConfigPOJO, val productFlavorConfi val hasLaunchActivity = index == 0 val resourcesConfig = ResourcesConfig(activityCount + 2, activityCount + 5, activityCount) + override val extraLines = config.extraAndroidBuildFileLines } \ No newline at end of file diff --git a/src/main/kotlin/com/google/androidstudiopoet/input/ModuleConfig.kt b/src/main/kotlin/com/google/androidstudiopoet/input/ModuleConfig.kt index a64616de..b0f3aa81 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/input/ModuleConfig.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/input/ModuleConfig.kt @@ -31,5 +31,5 @@ open class ModuleConfig(val index: Int, config: ConfigPOJO) { val dependencies = config.resolvedDependencies[index]?.map { it.to } ?: listOf() - + open val extraLines = config.extraBuildFileLines } \ No newline at end of file diff --git a/src/main/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprint.kt b/src/main/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprint.kt index 48a2ce72..81bda006 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprint.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprint.kt @@ -33,7 +33,8 @@ data class AndroidModuleBlueprint(val index: Int, private val productFlavorConfigs: List?, private val buildTypeConfigs: List?, private val javaPackageCount: Int, private val javaClassCount: Int, private val javaMethodsPerClass: Int, - private val kotlinPackageCount: Int, private val kotlinClassCount: Int, private val kotlinMethodsPerClass: Int + private val kotlinPackageCount: Int, private val kotlinClassCount: Int, private val kotlinMethodsPerClass: Int, + val extraLines: List? ) : Blueprint { val name = "androidAppModule" + index diff --git a/src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt b/src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt index 7729a3cd..0eac9cf6 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt @@ -79,6 +79,10 @@ class ConfigPOJO { val topologies: List>? = null + val extraBuildFileLines : List? = null + + val extraAndroidBuildFileLines : List? = null + override fun toString(): String = toJson() private fun toJson(): String { diff --git a/src/main/kotlin/com/google/androidstudiopoet/models/ModuleBlueprint.kt b/src/main/kotlin/com/google/androidstudiopoet/models/ModuleBlueprint.kt index 040b6ec0..de5ff000 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/models/ModuleBlueprint.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/models/ModuleBlueprint.kt @@ -23,7 +23,8 @@ data class ModuleBlueprint(val name: String, val useKotlin: Boolean, val dependencies: List, private val javaPackageCount: Int, private val javaClassCount: Int, private val javaMethodsPerClass: Int, - private val kotlinPackageCount: Int, private val kotlinClassCount: Int, private val kotlinMethodsPerClass: Int) { + private val kotlinPackageCount: Int, private val kotlinClassCount: Int, private val kotlinMethodsPerClass: Int, + val extraLines: List?) { val moduleRoot = root.joinPath(name) val packagesBlueprint = PackagesBlueprint(javaPackageCount, javaClassCount, javaMethodsPerClass, kotlinPackageCount, diff --git a/src/main/kotlin/com/google/androidstudiopoet/utils/collections.kt b/src/main/kotlin/com/google/androidstudiopoet/utils/collections.kt index 74108c17..1674c29c 100644 --- a/src/main/kotlin/com/google/androidstudiopoet/utils/collections.kt +++ b/src/main/kotlin/com/google/androidstudiopoet/utils/collections.kt @@ -14,4 +14,11 @@ package com.google.androidstudiopoet.utils -fun Collection?.isNullOrEmpty() = this == null || this.isEmpty() \ No newline at end of file +fun Collection?.isNullOrEmpty() = this == null || this.isEmpty() + +fun Collection?.joinLines(separator: CharSequence = "\n") : String { + if (this == null) { + return "" + } + return this.joinToString(separator = separator) +} diff --git a/src/test/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprintTest.kt b/src/test/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprintTest.kt index c7e3e75f..30cb0011 100644 --- a/src/test/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprintTest.kt +++ b/src/test/kotlin/com/google/androidstudiopoet/models/AndroidModuleBlueprintTest.kt @@ -11,7 +11,7 @@ class AndroidModuleBlueprintTest { private val dependency: ModuleDependency = mock() private val originalAndroidModuleBlueprint = AndroidModuleBlueprint(1, 1, resourcesConfig, "root", true, false, listOf(dependency), null,null, - 1, 1, 1, 1, 1, 1) + 1, 1, 1, 1, 1, 1, null) @Test fun `blueprint create proper activity names`() {