Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit f39ce6b

Browse files
committed
Add support for topologies.
Now it is possible to add dependencies by specifying topologies. At this moment only FULL, RANDOM and LINEAR are implemented. Once this change is reviewed and accepted more types will be added. See diff on AndroidStudioPoet.kt for an example of a random topology.
1 parent 89427e1 commit f39ce6b

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

src/main/kotlin/com/google/androidstudiopoet/AndroidStudioPoet.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, config: A
4646
" \"androidModules\": \"2\",\n" +
4747
" \"numActivitiesPerAndroidModule\": \"8\",\n" +
4848
" \"productFlavors\": [2, 3, 4],\n" +
49+
" \"topologies\": [{\"type\": \"random\", \"seed\": \"2\"}],\n" +
4950
" \"dependencies\": [{\"from\": 3, \"to\": 2},\n" +
5051
" {\"from\": 4, \"to\": 2}, {\"from\": 4, \"to\": 3}]\n" +
5152
"}"
@@ -123,4 +124,4 @@ class AndroidStudioPoet(private val modulesWriter: SourceModuleWriter, config: A
123124
}
124125
}
125126
}
126-
}
127+
}

src/main/kotlin/com/google/androidstudiopoet/ModuleBlueprintFactory.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import com.google.androidstudiopoet.models.ModuleBlueprint
77

88
object ModuleBlueprintFactory {
99
fun create(index: Int, config: ConfigPOJO, projectRoot: String): ModuleBlueprint {
10-
val dependencies = config.dependencies
11-
?.filter { it.from == index}
12-
?.map { it.to }
10+
val dependencies = config.resolvedDependencies
11+
.filter { it.from == index}
12+
.map { it.to }
1313

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

1717
return ModuleBlueprint(index, getModuleNameByIndex(index), projectRoot, dependenciesNames, methodsToCallWithinModule,
1818
config)

src/main/kotlin/com/google/androidstudiopoet/models/ConfigPOJO.kt

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.androidstudiopoet.models
1616

1717
import com.google.gson.Gson
18+
import java.security.InvalidParameterException
1819

1920
class ConfigPOJO {
2021

@@ -68,6 +69,8 @@ class ConfigPOJO {
6869

6970
val productFlavors: List<Int>? = null
7071

72+
val topologies: List<Map<String, String>>? = null
73+
7174
override fun toString(): String = toJson()
7275

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

7982
val useKotlin: Boolean
8083
get() = kotlinPackageCount!!.toInt() > 0
84+
85+
val resolvedDependencies: List<Dependency> by lazy {
86+
val allDependencies : MutableSet<Dependency> = mutableSetOf()
87+
88+
// Add dependencies generated by topologies
89+
val givenTopologies = topologies
90+
if (givenTopologies != null) {
91+
for (parameters in givenTopologies) {
92+
val type = parameters.get("type") ?: throw InvalidParameterException("No type specified in topology $parameters")
93+
val topology : Topologies = Topologies.valueOf(type.toUpperCase())
94+
allDependencies.addAll(topology.generateDependencies(parameters, this))
95+
}
96+
}
97+
98+
// Add explicit dependencies
99+
val explicitDependencies = dependencies
100+
if (explicitDependencies != null) {
101+
allDependencies.addAll(explicitDependencies)
102+
}
103+
104+
allDependencies.toList()
105+
}
81106
}
82107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.google.androidstudiopoet.models
2+
3+
import java.util.*
4+
5+
/**
6+
* Enum with all supported topologies
7+
*/
8+
enum class Topologies {
9+
10+
FULL {
11+
override fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency> {
12+
val result = mutableListOf<Dependency>()
13+
for (from in 0 until configPOJO.numModules) {
14+
for (to in from + 1 until configPOJO.numModules) {
15+
result.add(Dependency(from, to))
16+
}
17+
}
18+
return result
19+
}
20+
},
21+
22+
RANDOM {
23+
override fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency> {
24+
val seedInput = parameters["seed"]
25+
val seed: Long = seedInput?.toLong() ?: 0
26+
27+
Random().setSeed(seed)
28+
val result = mutableListOf<Dependency>()
29+
for (from in 0 until configPOJO.numModules) {
30+
for (to in from + 1 until configPOJO.numModules) {
31+
if (Random().nextBoolean()) {
32+
result.add(Dependency(from, to))
33+
}
34+
}
35+
}
36+
return result
37+
}
38+
},
39+
40+
LINEAR {
41+
override fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency> = (1 until configPOJO.numModules).map { Dependency(it - 1, it) }
42+
}
43+
;
44+
45+
/**
46+
* Function that should add dependencies to configPOJO based on the given parameters and the
47+
* content of configPOJO
48+
*/
49+
abstract fun generateDependencies(parameters: Map<String, String>, configPOJO: ConfigPOJO): List<Dependency>
50+
}

0 commit comments

Comments
 (0)