Skip to content

Commit 4e411f4

Browse files
Update config
1 parent 1cc137e commit 4e411f4

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ pubspec.lock
117117
# Ignore the `tmp` directory used for building dependant repositories.
118118
/tmp
119119

120-
old-dot-gradle-test-kit/
120+
.gradle-test-kit/

buildSrc/src/main/kotlin/io/spine/dependency/Dependency.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,3 @@ private fun ResolutionStrategy.forceWithLogging(
114114
force(artifact)
115115
project.log { "Forced the version of `$artifact` in " + configuration.diagSuffix(project) }
116116
}
117-
118-

buildSrc/src/main/kotlin/io/spine/gradle/protobuf/ProtoTaskExtensions.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
package io.spine.gradle.protobuf
3131

3232
import com.google.protobuf.gradle.GenerateProtoTask
33+
import com.google.protobuf.gradle.ProtobufExtension
3334
import io.spine.gradle.sourceSets
3435
import java.io.File
3536
import java.nio.file.Files
3637
import java.nio.file.Path
3738
import java.nio.file.Paths
3839
import java.nio.file.StandardOpenOption.TRUNCATE_EXISTING
40+
import kotlin.io.path.Path
3941
import org.gradle.api.Project
4042
import org.gradle.api.file.SourceDirectorySet
4143
import org.gradle.api.tasks.SourceSet
@@ -329,18 +331,42 @@ fun IdeaModule.printSourceDirectories() {
329331
excludeDirs.forEach { println(it) }
330332
}
331333

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+
332340
/**
333341
* Obtains the directory where the Protobuf Gradle Plugin should place the generated code.
334342
*
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
337349
* [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.
341355
*/
342356
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+
}
344370

345371
/**
346372
* Ensures that the sources generated by Protobuf Gradle Plugin

0 commit comments

Comments
 (0)