Skip to content

Commit d0145b6

Browse files
committed
- added scheme builder
1 parent be53b2d commit d0145b6

File tree

10 files changed

+116
-31
lines changed

10 files changed

+116
-31
lines changed

RoomigrantCompiler/src/main/java/dev/matrix/roomigrant/compiler/Database.kt

+66-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package dev.matrix.roomigrant.compiler
33
import com.squareup.kotlinpoet.*
44
import dev.matrix.roomigrant.compiler.data.Scheme
55
import dev.matrix.roomigrant.compiler.rules.Rules
6+
import dev.matrix.roomigrant.model.IndexInfo
7+
import dev.matrix.roomigrant.model.SchemeInfo
8+
import dev.matrix.roomigrant.model.TableInfo
69
import javax.annotation.processing.ProcessingEnvironment
710
import javax.lang.model.element.TypeElement
811
import javax.tools.StandardLocation
912

1013
/**
1114
* @author matrixdev
1215
*/
13-
@Suppress("UNCHECKED_CAST", "MemberVisibilityCanBePrivate")
16+
@Suppress("UNCHECKED_CAST", "MemberVisibilityCanBePrivate", "FunctionName")
1417
class Database(val environment: ProcessingEnvironment, element: TypeElement) {
1518

1619
val migrationType = ClassName("android.arch.persistence.room.migration", "Migration")
@@ -23,37 +26,85 @@ class Database(val environment: ProcessingEnvironment, element: TypeElement) {
2326
val migrationListClassName = ClassName(packageName, "${elementClassName}_Migrations")
2427

2528
val rules = Rules(this, element)
29+
val schemes = ArrayList<Scheme>()
2630
val migrations = ArrayList<Migration>()
2731

32+
fun addScheme(scheme: Scheme) {
33+
schemes.add(scheme)
34+
}
35+
2836
fun addMigration(database1: Scheme, database2: Scheme): Migration {
2937
return Migration(this, database1, database2).also { migrations.add(it) }
3038
}
3139

3240
fun generate() {
33-
// migration class
3441
val typeSpec = TypeSpec.objectBuilder(migrationListClassName)
42+
.addProperties(generate_rules())
43+
.addFunction(generate_build())
44+
.addFunction(generate_buildScheme())
3545

36-
// "rules" fields
37-
for (holder in rules.getProvidersFields()) {
38-
typeSpec.addProperty(holder.propertySpec)
39-
}
46+
val fileSpec = FileSpec.builder(packageName, migrationListClassName.simpleName())
47+
.addType(typeSpec.build())
48+
.build()
4049

41-
// "build" function
50+
environment.filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, "${migrationListClassName.simpleName()}.kt")
51+
.openWriter()
52+
.use { fileSpec.writeTo(it) }
53+
}
54+
55+
private fun generate_rules() = rules.getProvidersFields().map {
56+
PropertySpec.builder(it.name, it.type)
57+
.initializer("%T()", it.type)
58+
.build()
59+
}
60+
61+
private fun generate_build(): FunSpec {
4262
val funcSpec = FunSpec.builder("build").addStatement("val list = %T()", migrationListType)
4363
for (migration in migrations) {
4464
funcSpec.addStatement("list.add(%T)", migration.className)
4565
}
4666
funcSpec.returns(migrationArrayType).addStatement("return list.toTypedArray()")
47-
typeSpec.addFunction(funcSpec.build())
67+
return funcSpec.build()
68+
}
4869

49-
// writing to file
50-
val fileSpec = FileSpec.builder(packageName, migrationListClassName.simpleName())
51-
.addType(typeSpec.build())
52-
.build()
70+
private fun generate_buildScheme(): FunSpec {
71+
val code = FunSpec.builder("buildScheme")
72+
.returns(ParameterizedTypeName.get(Map::class, Int::class, SchemeInfo::class))
5373

54-
environment.filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, "${migrationListClassName.simpleName()}.kt")
55-
.openWriter()
56-
.use { fileSpec.writeTo(it) }
74+
var varIndex = 0
75+
fun generateVarName() = "var${++varIndex}"
76+
77+
val schemesVar = generateVarName()
78+
val schemesType = ParameterizedTypeName.get(HashMap::class, Int::class, SchemeInfo::class)
79+
code.addStatement("val %L = %T()", schemesVar, schemesType)
80+
81+
for (scheme in schemes) {
82+
val tablesVar = generateVarName()
83+
val tablesType = ParameterizedTypeName.get(HashMap::class, String::class, TableInfo::class)
84+
code.addStatement("val %L = %T()", tablesVar, tablesType)
85+
86+
val schemeVar = generateVarName()
87+
code.addStatement("val %L = %T(%L, %L)", schemeVar, SchemeInfo::class, scheme.version, tablesVar)
88+
code.addStatement("%L.put(%L, %L)", schemesVar, scheme.version, schemeVar)
89+
90+
for (table in scheme.tables) {
91+
val indicesVar = generateVarName()
92+
val indicesType = ParameterizedTypeName.get(HashMap::class, String::class, IndexInfo::class)
93+
code.addStatement("val %L = %T()", indicesVar, indicesType)
94+
95+
val tableVar = generateVarName()
96+
code.addStatement("val %L = %T(%L, %S, %S, %L)", tableVar, TableInfo::class, schemeVar, table.name, table.createSql(), indicesVar)
97+
code.addStatement("%L.put(%S, %L)", tablesVar, table.name, tableVar)
98+
99+
for (index in table.indices) {
100+
code.addStatement("%L.put(%S, %T(%L, %S, %S))", indicesVar, index.name, IndexInfo::class, tableVar, index.name, index.createSql(table.name))
101+
}
102+
}
103+
}
104+
105+
code.addStatement("return %L", schemesVar)
106+
107+
return code.build()
57108
}
58109

59110
}

RoomigrantCompiler/src/main/java/dev/matrix/roomigrant/compiler/Migration.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ class Migration(
5858
fun generate() {
5959
state.environment.filer.createResource(StandardLocation.SOURCE_OUTPUT, state.packageName, "${className.simpleName()}.kt")
6060
.openWriter()
61-
.use {
62-
fileSpec.writeTo(it)
63-
}
61+
.use { fileSpec.writeTo(it) }
6462
}
6563

6664
private fun migrate() {
@@ -168,7 +166,7 @@ class Migration(
168166
}
169167

170168
private fun createTable(table: Table) {
171-
execSql(table.createSql.replace("\${TABLE_NAME}", table.name))
169+
execSql(table.createSql(table.name))
172170
}
173171

174172
private fun dropTableIndex(index: Index) {
@@ -180,6 +178,6 @@ class Migration(
180178
}
181179

182180
private fun createTableIndex(table: Table, index: Index) {
183-
execSql(index.createSql.replace("\${TABLE_NAME}", table.name))
181+
execSql(index.createSql(table.name))
184182
}
185183
}

RoomigrantCompiler/src/main/java/dev/matrix/roomigrant/compiler/Processor.kt

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class Processor : AbstractProcessor() {
3939
val schemes = folder.listFiles().mapNotNull { readScheme(it) }.sortedBy { it.version }
4040

4141
val database = Database(processingEnv, element)
42+
for (scheme in schemes) {
43+
database.addScheme(scheme)
44+
}
4245
for (index in 1 until schemes.size) {
4346
database.addMigration(schemes[index - 1], schemes[index]).generate()
4447
}

RoomigrantCompiler/src/main/java/dev/matrix/roomigrant/compiler/data/Index.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ data class Index(
1313
val unique: Boolean,
1414

1515
@SerializedName("createSql")
16-
val createSql: String,
16+
val createSqlTemplate: String,
1717

1818
@SerializedName("columnNames")
19-
val columns: List<String>)
19+
val columns: List<String>) {
20+
21+
fun createSql(tableName: String) = createSqlTemplate.replace("\${TABLE_NAME}", tableName)
22+
23+
}

RoomigrantCompiler/src/main/java/dev/matrix/roomigrant/compiler/data/Table.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data class Table(
1010
val name: String,
1111

1212
@SerializedName("createSql")
13-
val createSql: String,
13+
val createSqlTemplate: String,
1414

1515
@SerializedName("primaryKey")
1616
val primaryKey: PrimaryKey,
@@ -22,10 +22,11 @@ data class Table(
2222
val indices: List<Index>) {
2323

2424
val fieldsMap by lazy { fields.associateBy { it.name } }
25+
fun createSql(tableName: String = name) = createSqlTemplate.replace("\${TABLE_NAME}", tableName)
2526

2627
constructor(table: Table, name: String) : this(
2728
name = name,
28-
createSql = table.createSql,
29+
createSqlTemplate = table.createSqlTemplate,
2930
primaryKey = table.primaryKey,
3031
fields = table.fields,
3132
indices = table.indices)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.matrix.roomigrant.compiler.rules
22

3-
import com.squareup.kotlinpoet.PropertySpec
43
import com.squareup.kotlinpoet.TypeName
54
import dev.matrix.roomigrant.compiler.Database
65

@@ -10,9 +9,4 @@ import dev.matrix.roomigrant.compiler.Database
109
data class RulesProviderField(
1110
val database: Database,
1211
val name: String,
13-
val type: TypeName) {
14-
15-
val propertySpec = PropertySpec.builder(name, type)
16-
.initializer("%T()", type)
17-
.build()
18-
}
12+
val type: TypeName)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.matrix.roomigrant.compiler.utils
2+
3+
/**
4+
* @author matrixdev
5+
*/
6+
7+
fun String.asVarName() = "`$this`"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.matrix.roomigrant.model
2+
3+
/**
4+
* @author matrixdev
5+
*/
6+
data class IndexInfo(
7+
val table: TableInfo,
8+
val name: String,
9+
val createSql: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dev.matrix.roomigrant.model
2+
3+
/**
4+
* @author matrixdev
5+
*/
6+
data class SchemeInfo(
7+
val version: Int,
8+
val tables: Map<String, TableInfo>)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dev.matrix.roomigrant.model
2+
3+
/**
4+
* @author matrixdev
5+
*/
6+
data class TableInfo(
7+
val scheme: SchemeInfo,
8+
val name: String,
9+
val createSql: String,
10+
val indices: Map<String, IndexInfo>)

0 commit comments

Comments
 (0)