|
30 | 30 | package io.spine.gradle.protobuf |
31 | 31 |
|
32 | 32 | import com.google.protobuf.gradle.GenerateProtoTask |
| 33 | +import com.google.protobuf.gradle.ProtobufExtension |
33 | 34 | import io.spine.gradle.sourceSets |
34 | 35 | import java.io.File |
35 | 36 | import java.nio.file.Files |
36 | 37 | import java.nio.file.Path |
37 | 38 | import java.nio.file.Paths |
38 | 39 | import java.nio.file.StandardOpenOption.TRUNCATE_EXISTING |
| 40 | +import kotlin.io.path.Path |
39 | 41 | import org.gradle.api.Project |
40 | 42 | import org.gradle.api.file.SourceDirectorySet |
41 | 43 | import org.gradle.api.tasks.SourceSet |
@@ -329,18 +331,42 @@ fun IdeaModule.printSourceDirectories() { |
329 | 331 | excludeDirs.forEach { println(it) } |
330 | 332 | } |
331 | 333 |
|
| 334 | +/** |
| 335 | + * Obtains the extension of Protobuf Gradle Plugin in the given project. |
| 336 | + */ |
| 337 | +val Project.protobufExtension: ProtobufExtension? |
| 338 | + get() = extensions.findByType(ProtobufExtension::class.java) |
| 339 | + |
332 | 340 | /** |
333 | 341 | * Obtains the directory where the Protobuf Gradle Plugin should place the generated code. |
334 | 342 | * |
335 | | - * The directory is fixed to be `$buildDir/generated/source/proto` and cannot be |
336 | | - * changed by the settings of the plugin. Even though [ProtobufExtension] has a property |
| 343 | + * The directory is fixed to be `$buildDir/generated/source/proto` in versions pre v0.9.5 |
| 344 | + * and cannot be changed by the settings of the plugin. |
| 345 | + * In the v0.9.5 the path was changed to |
| 346 | + * [`$buildDir/generated/sources/proto`](https://github.com/google/protobuf-gradle-plugin/releases/tag/v0.9.5). |
| 347 | + * |
| 348 | + * Even though [ProtobufExtension] has a property |
337 | 349 | * [generatedFilesBaseDir][ProtobufExtension.getGeneratedFilesBaseDir], which is supposed |
338 | | - * to be used for this purpose, it is declared with `@PackageScope` and thus cannot be |
339 | | - * accessed from outside the plugin. The Protobuf Gradle Plugin (at v0.9.2) does not |
340 | | - * modify the value of the property either. |
| 350 | + * to be used for this purpose, it is declared with `@PackageScope` (again in earlier versions) |
| 351 | + * and thus cannot be accessed from outside the plugin. |
| 352 | + * The Protobuf Gradle Plugin (at v0.9.2) does not modify the value of the property either. |
| 353 | + * Therefore, we try getting the path using the newer version API and resort to the "legacy" |
| 354 | + * convention if the call fails. |
341 | 355 | */ |
342 | 356 | val Project.generatedSourceProtoDir: Path |
343 | | - get() = layout.buildDirectory.dir("generated/source/proto").get().asFile.toPath() |
| 357 | + get() { |
| 358 | + val legacyPath = layout.buildDirectory.dir("generated/source/proto").get().asFile.toPath() |
| 359 | + protobufExtension?.let { |
| 360 | + return try { |
| 361 | + it.generatedFilesBaseDir.let { Path(it) } |
| 362 | + } catch (_: Throwable) { |
| 363 | + // Probably we're running on an older version of the Protobuf Gradle Plugin |
| 364 | + // which has `package-access` for the `getGeneratedFilesDir()` method. |
| 365 | + legacyPath |
| 366 | + } |
| 367 | + } |
| 368 | + return legacyPath |
| 369 | + } |
344 | 370 |
|
345 | 371 | /** |
346 | 372 | * Ensures that the sources generated by Protobuf Gradle Plugin |
|
0 commit comments