Skip to content

Commit 32eb8f1

Browse files
committed
Seed the generated schemas dir with existing schemas
This allows other annotation processors that rely on finding older schemas to locate them via the room schema location processor argument.
1 parent 3d57ec4 commit 32eb8f1

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

src/main/groovy/org/gradle/android/workarounds/RoomSchemaLocationWorkaround.groovy

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ class RoomSchemaLocationWorkaround implements Workaround {
105105

106106
// Register the generated schemas to be merged back to the original specified schema directory
107107
task.project.roomSchemaMergeLocations.registerMerge(schemaDestinationDir, taskSpecificSchemaDir)
108+
109+
// Seed the task-specific generated schema dir with the existing schemas
110+
task.doFirst {
111+
copyExistingSchemasToTaskSpecificTmpDir(task, schemaDestinationDir, taskSpecificSchemaDir)
112+
}
108113
}
109114
)
110115

@@ -126,7 +131,12 @@ class RoomSchemaLocationWorkaround implements Workaround {
126131
}
127132

128133
doFirst {
129-
setRoomSchemaLocationToTaskSpecificDirForKaptWithoutKotlinc(task)
134+
// Setup a task-specific tmp dir and populate it with the existing schemas
135+
setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithoutKotlinc(task)
136+
}
137+
doLast {
138+
// Copy the generated schemas from the tmp dir to the tracked output dir
139+
copyGeneratedSchemasToOutputDir(task, getTaskSpecificSchemaTmpDir(task), getTaskSpecificSchemaDir(task))
130140
}
131141
}
132142

@@ -143,7 +153,12 @@ class RoomSchemaLocationWorkaround implements Workaround {
143153
}
144154

145155
doFirst {
146-
setRoomSchemaLocationToTaskSpecificDirForKaptWithKotlinc(task)
156+
// Setup a task-specific tmp dir and populate it with the existing schemas
157+
setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithKotlinc(task)
158+
}
159+
doLast {
160+
// Copy the generated schemas from the tmp dir to the tracked output dir
161+
copyGeneratedSchemasToOutputDir(task, getTaskSpecificSchemaTmpDir(task), getTaskSpecificSchemaDir(task))
147162
}
148163
}
149164
}
@@ -154,6 +169,11 @@ class RoomSchemaLocationWorkaround implements Workaround {
154169
return new File(schemaBaseDir, task.name)
155170
}
156171

172+
static File getTaskSpecificSchemaTmpDir(Task task) {
173+
def schemaBaseDir = task.project.layout.buildDirectory.dir("roomSchemas/tmp").get().asFile
174+
return new File(schemaBaseDir, task.name)
175+
}
176+
157177
static def getCompilerPluginOptions(Task task) {
158178
def processorOptionsField = getAccessibleField(task.class, "processorOptions")
159179
def compilerPluginOptions = processorOptionsField.get(task)
@@ -213,20 +233,44 @@ class RoomSchemaLocationWorkaround implements Workaround {
213233
task.project.roomSchemaMergeLocations.registerMerge(schemaDestinationDir, taskSpecificSchemaDir)
214234
}
215235

216-
private static void setRoomSchemaLocationToTaskSpecificDirForKaptWithoutKotlinc(Task task) {
236+
private static void copyExistingSchemasToTaskSpecificTmpDir(Task task, File existingSchemaDir, File taskSpecificTmpDir) {
237+
// populate the task-specific tmp dir with any existing (non-generated) schemas
238+
// this allows other annotation processors that might operate on these schemas
239+
// to find them via the schema location argument
240+
task.project.sync {
241+
from existingSchemaDir
242+
into taskSpecificTmpDir
243+
}
244+
}
245+
246+
private static void copyGeneratedSchemasToOutputDir(Task task, File taskSpecificTmpDir, File outputDir) {
247+
// Copy the generated generated schemas from the task-specific tmp dir to the
248+
// task-specific output dir. This dance prevents the kapt task from clearing out
249+
// the existing schemas before the annotation processors run
250+
task.project.sync {
251+
from taskSpecificTmpDir
252+
into outputDir
253+
}
254+
}
255+
256+
private static void setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithoutKotlinc(Task task) {
217257
def processorOptions = getCompilerPluginOptions(task)
218258
processorOptions.each { option ->
219259
if (option.key == ROOM_SCHEMA_LOCATION) {
220-
setOptionValue(option, getTaskSpecificSchemaDir(task).absolutePath)
260+
def taskSpecificTmpDir = getTaskSpecificSchemaTmpDir(task)
261+
copyExistingSchemasToTaskSpecificTmpDir(task, task.project.file(option.value), taskSpecificTmpDir)
262+
setOptionValue(option, taskSpecificTmpDir.absolutePath)
221263
}
222264
}
223265
}
224266

225-
private static void setRoomSchemaLocationToTaskSpecificDirForKaptWithKotlinc(Task task) {
267+
private static void setRoomSchemaLocationToTaskSpecificTmpDirForKaptWithKotlinc(Task task) {
226268
def encodedOptions = getEncodedCompilerPluginOptions(task)
227269
def apOptions = decode(encodedOptions.value)
228270
if (apOptions.containsKey(ROOM_SCHEMA_LOCATION)) {
229-
apOptions[ROOM_SCHEMA_LOCATION] = getTaskSpecificSchemaDir(task).absolutePath
271+
def taskSpecificTmpDir = getTaskSpecificSchemaTmpDir(task)
272+
copyExistingSchemasToTaskSpecificTmpDir(task, task.project.file(apOptions[ROOM_SCHEMA_LOCATION]), taskSpecificTmpDir)
273+
apOptions[ROOM_SCHEMA_LOCATION] = taskSpecificTmpDir.absolutePath
230274
setOptionValue(encodedOptions, encode(apOptions))
231275
}
232276
}

src/test/groovy/org/gradle/android/RoomSchemaLocationWorkaroundTest.groovy

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,25 +215,34 @@ class RoomSchemaLocationWorkaroundTest extends AbstractTest {
215215
216216
void assertKaptSchemaOutputsExist() {
217217
// Task specific schemas
218-
assert file("app/build/roomSchemas/kaptDebugKotlin/org.gradle.android.example.app.AppDatabase/2.json").exists()
219-
assert file("app/build/roomSchemas/kaptReleaseKotlin/org.gradle.android.example.app.AppDatabase/2.json").exists()
220-
assert file("library/build/roomSchemas/kaptDebugKotlin/org.gradle.android.example.library.AppDatabase/2.json").exists()
221-
assert file("library/build/roomSchemas/kaptReleaseKotlin/org.gradle.android.example.library.AppDatabase/2.json").exists()
218+
assertKaptSchemaOutputsExistFor("debug")
219+
assertKaptSchemaOutputsExistFor("release")
220+
}
221+
222+
void assertKaptSchemaOutputsExistFor(String variant) {
223+
assertSchemasExist("app", "build/roomSchemas/kapt${variant.capitalize()}Kotlin")
224+
assertSchemasExist("library", "build/roomSchemas/kapt${variant.capitalize()}Kotlin")
222225
}
223226
224227
void assertCompileJavaSchemaOutputsExist() {
225228
// Task specific schemas
226-
assert file("app/build/roomSchemas/compileDebugJavaWithJavac/org.gradle.android.example.app.AppDatabase/2.json").exists()
227-
assert file("app/build/roomSchemas/compileReleaseJavaWithJavac/org.gradle.android.example.app.AppDatabase/2.json").exists()
228-
assert file("library/build/roomSchemas/compileDebugJavaWithJavac/org.gradle.android.example.library.AppDatabase/2.json").exists()
229-
assert file("library/build/roomSchemas/compileReleaseJavaWithJavac/org.gradle.android.example.library.AppDatabase/2.json").exists()
229+
assertCompileJavaSchemaOutputExistsFor("debug")
230+
assertCompileJavaSchemaOutputExistsFor("release")
231+
}
232+
233+
void assertCompileJavaSchemaOutputExistsFor(String variant) {
234+
assertSchemasExist("app", "build/roomSchemas/compile${variant.capitalize()}JavaWithJavac")
235+
assertSchemasExist("library", "build/roomSchemas/compile${variant.capitalize()}JavaWithJavac")
230236
}
231237
232238
void assertMergedSchemaOutputsExist() {
233239
// Merged schemas
234-
assert file("app/schemas/org.gradle.android.example.app.AppDatabase/1.json").exists()
235-
assert file("app/schemas/org.gradle.android.example.app.AppDatabase/2.json").exists()
236-
assert file("library/schemas/org.gradle.android.example.library.AppDatabase/1.json").exists()
237-
assert file("library/schemas/org.gradle.android.example.library.AppDatabase/2.json").exists()
240+
assertSchemasExist("app", "schemas")
241+
assertSchemasExist("library", "schemas")
242+
}
243+
244+
void assertSchemasExist(String project, String baseDirPath) {
245+
assert file("${project}/${baseDirPath}/org.gradle.android.example.${project}.AppDatabase/1.json").exists()
246+
assert file("${project}/${baseDirPath}/org.gradle.android.example.${project}.AppDatabase/2.json").exists()
238247
}
239248
}

0 commit comments

Comments
 (0)