Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.
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 @@ -46,6 +46,7 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, config: A
" \"androidModules\": \"2\",\n" +
" \"numActivitiesPerAndroidModule\": \"8\",\n" +
" \"productFlavors\": [2, 3, 4],\n" +
" \"topologies\": [{\"type\": \"random\", \"seed\": \"2\"}],\n" +
" \"dependencies\": [{\"from\": 3, \"to\": 2},\n" +
" {\"from\": 4, \"to\": 2}, {\"from\": 4, \"to\": 3}]\n" +
"}"
Expand Down Expand Up @@ -123,4 +124,4 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, config: A
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import com.google.androidstudiopoet.models.ModuleBlueprint

object ModuleBlueprintFactory {
fun create(index: Int, config: ConfigPOJO, projectRoot: String): ModuleBlueprint {
val dependencies = config.dependencies
?.filter { it.from == index}
?.map { it.to }
val dependencies = config.resolvedDependencies
.filter { it.from == index}
.map { it.to }

val dependenciesNames = dependencies?.map { getModuleNameByIndex(it) } ?: listOf()
val methodsToCallWithinModule = dependencies?.map { getMethodToCallForDependency(it, config, projectRoot) } ?: listOf()
val dependenciesNames = dependencies.map { getModuleNameByIndex(it) }
val methodsToCallWithinModule = dependencies.map { getMethodToCallForDependency(it, config, projectRoot) }

return ModuleBlueprint(index, getModuleNameByIndex(index), projectRoot, dependenciesNames, methodsToCallWithinModule,
config)
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.androidstudiopoet.models

import com.google.gson.Gson
import java.security.InvalidParameterException

class ConfigPOJO {

Expand Down Expand Up @@ -68,6 +69,8 @@ class ConfigPOJO {

val productFlavors: List<Int>? = null

val topologies: List<Map<String, String>>? = null

override fun toString(): String = toJson()

private fun toJson(): String {
Expand All @@ -78,5 +81,27 @@ class ConfigPOJO {

val useKotlin: Boolean
get() = kotlinPackageCount!!.toInt() > 0

val resolvedDependencies: List<Dependency> by lazy {
val allDependencies : MutableSet<Dependency> = mutableSetOf()

// Add dependencies generated by topologies
val givenTopologies = topologies
if (givenTopologies != null) {
for (parameters in givenTopologies) {
val type = parameters.get("type") ?: throw InvalidParameterException("No type specified in topology $parameters")
val topology : Topologies = Topologies.valueOf(type.toUpperCase())
allDependencies.addAll(topology.generateDependencies(parameters, this))
}
}

// Add explicit dependencies
val explicitDependencies = dependencies
if (explicitDependencies != null) {
allDependencies.addAll(explicitDependencies)
}

allDependencies.toList()
}
}

50 changes: 50 additions & 0 deletions src/main/kotlin/com/google/androidstudiopoet/models/Topologies.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.google.androidstudiopoet.models

import java.util.*

/**
* Enum with all supported topologies
*/
enum class Topologies {

FULL {
override fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency> {
val result = mutableListOf<Dependency>()
for (from in 0 until configPOJO.numModules) {
for (to in from + 1 until configPOJO.numModules) {
result.add(Dependency(from, to))
}
}
return result
}
},

RANDOM {
override fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency> {
val seedInput = parameters["seed"]
val seed: Long = seedInput?.toLong() ?: 0

Random().setSeed(seed)
val result = mutableListOf<Dependency>()
for (from in 0 until configPOJO.numModules) {
for (to in from + 1 until configPOJO.numModules) {
if (Random().nextBoolean()) {
result.add(Dependency(from, to))
}
}
}
return result
}
},

LINEAR {
override fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency> = (1 until configPOJO.numModules).map { Dependency(it - 1, it) }
}
;

/**
* Function that should add dependencies to configPOJO based on the given parameters and the
* content of configPOJO
*/
abstract fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency>
}