@@ -108,15 +108,18 @@ spotless {
108
108
}
109
109
}
110
110
111
+ @DisableCachingByDefault(because = " Code generation is very fast and not worth caching" )
111
112
abstract class DataConnectGenerateSourcesTask : DefaultTask () {
112
113
113
- @get:InputDirectory abstract val inputDirectory: DirectoryProperty
114
+ @get:InputDirectory abstract val inputDirectory: Property < DirectoryTree >
114
115
115
- @get:OutputDirectory abstract val outputDirectory: DirectoryProperty
116
+ @get:Input abstract val firebaseToolsVersion: Property <String >
117
+
118
+ @get:Input abstract val firebaseCommand: Property <String >
116
119
117
- @get:Internal abstract val nodeExecutableDirectory: DirectoryProperty
120
+ @get:Input @get:Optional abstract val nodeExecutableDirectory: Property < String >
118
121
119
- @get:Internal abstract val firebaseCommand : Property < String >
122
+ @get:OutputDirectory abstract val outputDirectory : DirectoryProperty
120
123
121
124
@get:Internal abstract val workDirectory: DirectoryProperty
122
125
@@ -127,40 +130,26 @@ abstract class DataConnectGenerateSourcesTask : DefaultTask() {
127
130
@get:Inject protected abstract val fileSystemOperations: FileSystemOperations
128
131
129
132
@TaskAction
130
- fun run (inputChanges : InputChanges ) {
131
- if (inputChanges.isIncremental) {
132
- val onlyLogFilesChanged =
133
- inputChanges.getFileChanges(inputDirectory).all { it.file.name.endsWith(" .log" ) }
134
- if (onlyLogFilesChanged) {
135
- didWork = false
136
- return
137
- }
138
- }
139
-
140
- val inputDirectory: File = inputDirectory.get().asFile
133
+ fun run () {
134
+ val inputDirectory: File = inputDirectory.get().dir
135
+ val firebaseToolsVersion: String = firebaseToolsVersion.get()
136
+ val firebaseCommand: String = firebaseCommand.get()
137
+ val nodeExecutableDirectory: String? = nodeExecutableDirectory.orNull
141
138
val outputDirectory: File = outputDirectory.get().asFile
142
- val nodeExecutableDirectory: File ? = nodeExecutableDirectory.orNull?.asFile
143
- val firebaseCommand: String? = firebaseCommand.orNull
144
139
val workDirectory: File = workDirectory.get().asFile
145
140
141
+ logger.info(" inputDirectory: {}" , inputDirectory.absolutePath)
142
+ logger.info(" firebaseToolsVersion: {}" , firebaseToolsVersion)
143
+ logger.info(" firebaseCommand: {}" , firebaseCommand)
144
+ logger.info(" nodeExecutableDirectory: {}" , nodeExecutableDirectory)
145
+ logger.info(" outputDirectory: {}" , outputDirectory.absolutePath)
146
+ logger.info(" workDirectory: {}" , workDirectory.absolutePath)
147
+
146
148
outputDirectory.deleteRecursively()
147
149
outputDirectory.mkdirs()
148
150
workDirectory.deleteRecursively()
149
151
workDirectory.mkdirs()
150
152
151
- val newPath: String? =
152
- if (nodeExecutableDirectory == = null ) {
153
- null
154
- } else {
155
- val nodeExecutableDirectoryAbsolutePath = nodeExecutableDirectory.absolutePath
156
- val oldPath = providerFactory.environmentVariable(" PATH" ).orNull
157
- if (oldPath == = null ) {
158
- nodeExecutableDirectoryAbsolutePath
159
- } else {
160
- nodeExecutableDirectoryAbsolutePath + File .pathSeparator + oldPath
161
- }
162
- }
163
-
164
153
val logFile =
165
154
if (logger.isInfoEnabled) {
166
155
null
@@ -172,12 +161,15 @@ abstract class DataConnectGenerateSourcesTask : DefaultTask() {
172
161
logFile?.outputStream().use { logStream ->
173
162
execOperations.runCatching {
174
163
exec {
175
- commandLine(firebaseCommand ? : " firebase" , " --debug" , " dataconnect:sdk:generate" )
164
+ configureFirebaseCommand(
165
+ this ,
166
+ firebaseCommand = firebaseCommand,
167
+ nodeExecutableDirectory = nodeExecutableDirectory,
168
+ path = providerFactory.environmentVariable(" PATH" ).orNull,
169
+ )
170
+ args(" --debug" , " dataconnect:sdk:generate" )
176
171
workingDir(inputDirectory)
177
172
isIgnoreExitValue = false
178
- if (newPath != = null ) {
179
- environment(" PATH" , newPath)
180
- }
181
173
if (logStream != = null ) {
182
174
standardOutput = logStream
183
175
errorOutput = logStream
@@ -193,29 +185,59 @@ abstract class DataConnectGenerateSourcesTask : DefaultTask() {
193
185
throw exception
194
186
}
195
187
}
188
+
189
+ companion object {
190
+
191
+ fun configureFirebaseCommand (
192
+ execSpec : ExecSpec ,
193
+ firebaseCommand : String ,
194
+ nodeExecutableDirectory : String? ,
195
+ path : String? ,
196
+ ) {
197
+ execSpec.setCommandLine(firebaseCommand)
198
+
199
+ val newPath: String? =
200
+ if (nodeExecutableDirectory == = null ) {
201
+ null
202
+ } else {
203
+ if (path == = null ) {
204
+ nodeExecutableDirectory
205
+ } else {
206
+ nodeExecutableDirectory + File .pathSeparator + path
207
+ }
208
+ }
209
+
210
+ if (newPath != = null ) {
211
+ execSpec.environment(" PATH" , newPath)
212
+ }
213
+ }
214
+ }
196
215
}
197
216
217
+ @DisableCachingByDefault(
218
+ because = " Copying files is not worth caching, just like org.gradle.api.tasks.Copy"
219
+ )
198
220
abstract class CopyDirectoryTask : DefaultTask () {
199
221
200
- @get:InputDirectory abstract val srcDirectory: DirectoryProperty
222
+ @get:InputDirectory abstract val srcDirectory: Property < DirectoryTree >
201
223
202
224
@get:OutputDirectory abstract val destDirectory: DirectoryProperty
203
225
204
226
@get:Inject protected abstract val fileSystemOperations: FileSystemOperations
205
227
206
228
@TaskAction
207
229
fun run () {
208
- val srcDirectory : File = srcDirectory.get().asFile
230
+ val srcDirectoryTree : DirectoryTree = srcDirectory.get()
209
231
val destDirectory: File = destDirectory.get().asFile
210
232
211
- logger.info(" srcDirectory: {}" , srcDirectory .absolutePath)
233
+ logger.info(" srcDirectory: {}" , srcDirectoryTree.dir .absolutePath)
212
234
logger.info(" destDirectory: {}" , destDirectory.absolutePath)
213
235
214
236
destDirectory.deleteRecursively()
215
237
destDirectory.mkdirs()
216
238
217
239
fileSystemOperations.copy {
218
- from(srcDirectory )
240
+ from(srcDirectoryTree )
219
241
into(destDirectory)
220
242
}
221
243
}
@@ -231,14 +253,39 @@ run {
231
253
description =
232
254
" Run firebase dataconnect:sdk:generate to generate the Data Connect Kotlin SDK sources"
233
255
234
- inputDirectory = projectDirectory.dir(" firebase" )
235
- outputDirectory = projectDirectory.dir(" dataConnectGeneratedSources" )
256
+ inputDirectory =
257
+ fileTree(layout.projectDirectory.dir(" firebase" )).apply { exclude(" **/*.log" ) }
258
+
259
+ outputDirectory = layout.buildDirectory.dir(" dataConnect/generatedSources" )
260
+
261
+ firebaseCommand =
262
+ project.providers
263
+ .gradleProperty(" dataConnect.minimalApp.firebaseCommand" )
264
+ .orElse(" firebase" )
236
265
237
266
nodeExecutableDirectory =
238
267
project.providers.gradleProperty(" dataConnect.minimalApp.nodeExecutableDirectory" ).map {
239
- projectDirectory.dir(it)
268
+ projectDirectory.dir(it).asFile.absolutePath
269
+ }
270
+
271
+ val path = providers.environmentVariable(" PATH" )
272
+ firebaseToolsVersion =
273
+ providers.provider {
274
+ providers
275
+ .exec {
276
+ DataConnectGenerateSourcesTask .configureFirebaseCommand(
277
+ this ,
278
+ firebaseCommand = firebaseCommand.get(),
279
+ nodeExecutableDirectory = nodeExecutableDirectory.orNull,
280
+ path = path.orNull,
281
+ )
282
+ args(" --version" )
283
+ }
284
+ .standardOutput
285
+ .asText
286
+ .get()
287
+ .trim()
240
288
}
241
- firebaseCommand = project.providers.gradleProperty(" dataConnect.minimalApp.firebaseCommand" )
242
289
243
290
workDirectory = layout.buildDirectory.dir(name)
244
291
}
@@ -247,13 +294,22 @@ run {
247
294
androidComponents.onVariants { variant ->
248
295
val variantNameTitleCase = variant.name[0 ].uppercase() + variant.name.substring(1 )
249
296
val copyTaskName = " dataConnectCopy${variantNameTitleCase} GeneratedSources"
297
+ val objectFactory = objects
250
298
val copyTask =
251
299
tasks.register<CopyDirectoryTask >(copyTaskName) {
252
300
group = dataConnectTaskGroupName
253
301
description =
254
302
" Copy the generated Data Connect Kotlin SDK sources into the " +
255
303
" generated code directory for the \" ${variant.name} \" variant."
256
- srcDirectory = generateSourcesTask.flatMap { it.outputDirectory }
304
+ srcDirectory =
305
+ generateSourcesTask.flatMap {
306
+ it.outputDirectory.map { outputDirectory ->
307
+ objectFactory.fileTree().apply {
308
+ setDir(outputDirectory)
309
+ exclude(" **/*.log" )
310
+ }
311
+ }
312
+ }
257
313
}
258
314
259
315
variant.sources.java!! .addGeneratedSourceDirectory(copyTask, CopyDirectoryTask ::destDirectory)
0 commit comments