Skip to content

Commit

Permalink
Don't use FileOrUriNotationConverter (#3288)
Browse files Browse the repository at this point in the history
* Don't use FileOrUriNotationConverter

It's an internal API in Gradle.

* Fix non-empty check

* Fixup failing test

---------

Co-authored-by: Jesse Wilson <[email protected]>
  • Loading branch information
swankjesse and squarejesse authored Feb 12, 2025
1 parent 7d3be25 commit 8dddcef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.internal.catalog.DelegatingProjectDependency
import org.gradle.api.internal.file.FileOrUriNotationConverter
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderConvertible

Expand Down Expand Up @@ -299,11 +298,10 @@ open class WireExtension(
isCanBeConsumed = false
isTransitive = false
}
internal val sourceDirectoriesAndLocalJars = mutableListOf<File>()

/** Calling this will resolve the configuration. */
internal val roots: Set<File>
get() = configuration.files + sourceDirectoriesAndLocalJars
get() = configuration.files
private val files: ConfigurableFileCollection by lazy(NONE) {
val files = project.files()
project.dependencies.add(configuration.name, files)
Expand Down Expand Up @@ -333,16 +331,17 @@ open class WireExtension(

/** Sets a local or a remote jar. Examples: "libs/protos.jar", or "com.example:protos:1.0.0". */
fun srcJar(jar: String) {
srcFileOrConfiguration(jar)
}
// Attempt to parse 'jar' as a path or 'file:' URI.
val fileOrNull = try {
project.file(jar)
} catch (e: Exception) {
null // Probably a dependency string like "com.example:protos:1.0.0".
}

private fun srcFileOrConfiguration(jar: String) {
isEmpty = false
val parser = FileOrUriNotationConverter.parser()
val converted = parser.parseNotation(jar)
when (converted) {
is File -> sourceDirectoriesAndLocalJars += project.file(jar)
else -> addDependency(jar)
if (fileOrNull != null) {
addDependency(project.files(fileOrNull))
} else {
addDependency(jar)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalStdlibApi::class)

package com.squareup.wire.gradle

import com.squareup.wire.VERSION
Expand All @@ -31,7 +29,6 @@ import java.util.concurrent.atomic.AtomicBoolean
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.UnknownConfigurationException
import org.gradle.api.internal.file.FileOrUriNotationConverter
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.compile.JavaCompile
Expand Down Expand Up @@ -195,20 +192,15 @@ class WirePlugin : Plugin<Project> {
task.group = GROUP
task.description = "Generate protobuf implementation for ${source.name}"

var addedSourcesDependencies = 0
// Flatten all the input files here. Changes to any of them will cause the task to re-run.
for (rootSet in protoSourceProtoRootSets) {
task.source(rootSet.configuration)
val sourceDirectoriesAndLocalJars = rootSet.sourceDirectoriesAndLocalJars.toTypedArray()
addedSourcesDependencies += sourceDirectoriesAndLocalJars.size
task.source(*sourceDirectoriesAndLocalJars)
}
// We only want to add ProtoPath sources if we have other sources already. The WireTask
// would otherwise run even through we have no sources.
if (addedSourcesDependencies > 0) {
if (!task.source.isEmpty) {
for (rootSet in protoPathProtoRootSets) {
task.source(rootSet.configuration)
task.source(*rootSet.sourceDirectoriesAndLocalJars.toTypedArray())
}
}

Expand Down Expand Up @@ -339,13 +331,9 @@ class WirePlugin : Plugin<Project> {
}

private fun defaultSourceFolders(source: Source): Set<String> {
val parser = FileOrUriNotationConverter.parser()
return source.sourceSets.map { "src/$it/proto" }.filter { path ->
val converted = parser.parseNotation(path) as File
val file =
if (!converted.isAbsolute) File(project.projectDir, converted.path) else converted
return@filter file.exists()
}.toSet()
return source.sourceSets.map { "src/$it/proto" }
.filter { path -> File(project.projectDir, path).exists() }
.toSet()
}

internal companion object {
Expand Down

0 comments on commit 8dddcef

Please sign in to comment.