From 22a971036f7b31ffea95bf3b233323cb13cd9004 Mon Sep 17 00:00:00 2001 From: Steffen Yount Date: Sat, 23 Apr 2016 16:16:31 -0700 Subject: [PATCH] Avoid resolving protobufToolsLocator_* dependencies at configruation time Fixes google/protobuf-gradle-plugin#42 by deferring the ExecutableLocator dependency resolution and downloads till the affected tasks are called in the execution phase. --- .../protobuf/gradle/ProtobufPlugin.groovy | 2 +- .../protobuf/gradle/ToolsLocator.groovy | 51 ++++++++++++------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index 4743b86f..c6e02e3a 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -122,7 +122,7 @@ class ProtobufPlugin implements Plugin { linkGenerateProtoTasksToJavaCompile() // protoc and codegen plugin configuration may change through the protobuf{} // block. Only at this point the configuration has been finalized. - project.protobuf.tools.resolve() + project.protobuf.tools.registerTaskDependencies(project.protobuf.getGenerateProtoTasks().all()) } } diff --git a/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy b/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy index cc11807f..8473e90f 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy @@ -51,26 +51,29 @@ class ToolsLocator { } /** - * For every ExecutableLocator that points to an artifact spec, resolves the + * For every ExecutableLocator that points to an artifact spec: creates a + * project configuration dependency for that artifact, registers the + * configuration dependency as an input dependency with the specified tasks, + * and adds a doFirst {} block to the specified tasks which resolves the * spec, downloads the artifact, and point to the local path. */ - void resolve() { - resolve(protoc) - if (protoc.path == null) { + void registerTaskDependencies(Collection protoTasks) { + if (protoc.artifact != null) { + registerDependencyWithTasks(protoc, protoTasks) + } else if (protoc.path == null) { protoc.path = 'protoc' } - plugins.each { plugin -> - resolve(plugin) - if (plugin.path == null) { - plugin.path = "protoc-gen-${plugin.name}" + for (ExecutableLocator pluginLocator in plugins) { + if (pluginLocator.artifact != null) { + registerDependencyWithTasks(pluginLocator, protoTasks) + } else if (pluginLocator.path == null) { + pluginLocator.path = "protoc-gen-${pluginLocator.name}" } } } - void resolve(ExecutableLocator locator) { - if (locator.artifact == null) { - return - } + void registerDependencyWithTasks(ExecutableLocator locator, Collection protoTasks) { + // create a project configuration dependency for the artifact Configuration config = project.configurations.create("protobufToolsLocator_${locator.name}") { visible = false transitive = false @@ -83,13 +86,25 @@ class ToolsLocator { version: version, classifier: project.osdetector.classifier, ext: 'exe'] - project.logger.info("Resolving artifact: ${notation}") Dependency dep = project.dependencies.add(config.name, notation) - File file = config.fileCollection(dep).singleFile - if (!file.canExecute() && !file.setExecutable(true)) { - throw new GradleException("Cannot set ${file} as executable") + + for (GenerateProtoTask protoTask in protoTasks) { + if (locator == protoc || protoTask.plugins.findByName(locator.name) != null) { + // register the configuration dependency as a task input + protoTask.inputs.files(config) + + protoTask.doFirst { + if (locator.path == null) { + project.logger.info("Resolving artifact: ${notation}") + File file = config.fileCollection(dep).singleFile + if (!file.canExecute() && !file.setExecutable(true)) { + throw new GradleException("Cannot set ${file} as executable") + } + project.logger.info("Resolved artifact: ${file}") + locator.path = file.path + } + } + } } - project.logger.info("Resolved artifact: ${file}") - locator.path = file.path } }