From 22bcf197b74137d7f93eced1ed4f83d9b931db87 Mon Sep 17 00:00:00 2001
From: Ryan Ernst <ryan@iernst.net>
Date: Sat, 7 Nov 2015 13:06:14 -0800
Subject: [PATCH] Add some comments to particularly nasty groovy code

---
 .../gradle/test/ClusterFormationTasks.groovy       | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy
index dc40f109c44..0b835872ec1 100644
--- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy
+++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy
@@ -99,9 +99,10 @@ class ClusterFormationTasks {
 
         // install plugins
         for (Map.Entry<String, FileCollection> plugin : config.plugins.entrySet()) {
-            String camelName = plugin.getKey().replaceAll(/-(\w)/) { _, c -> c.toUpperCase() }
-            String taskName = "${task.name}#install${camelName.capitalize()}"
-            // delay reading the file location until execution time
+            // replace every dash followed by a character with just the uppercase character
+            String camelName = plugin.getKey().replaceAll(/-(\w)/) { _, c -> c.toUpperCase(Locale.ROOT) }
+            String taskName = "${task.name}#install${camelName[0].toUpperCase(Locale.ROOT) + camelName.substring(1)}"
+            // delay reading the file location until execution time by wrapping in a closure within a GString
             String file = "${ -> new File(pluginsTmpDir, plugin.getValue().singleFile.getName()).toURI().toURL().toString() }"
             Object[] args = [new File(home, 'bin/plugin'), 'install', file]
             setup = configureExecTask(taskName, project, setup, cwd, args)
@@ -172,9 +173,12 @@ class ClusterFormationTasks {
         if (config.plugins.isEmpty()) {
             return setup
         }
+        // collect the files for plugins into a list, but wrap each in a closure to delay
+        // looking for the filename until execution time
+        List files = config.plugins.values().collect { plugin -> return { plugin.singleFile } }
         return project.tasks.create(name: name, type: Copy, dependsOn: setup) {
             into pluginsTmpDir
-            from(*config.plugins.values().collect { plugin -> return { plugin.singleFile } })
+            from(*files) // spread the list into varargs
         }
     }
 
@@ -273,6 +277,7 @@ class ClusterFormationTasks {
     static Task configureCheckPreviousTask(String name, Project project, Object depends, File pidFile) {
         return project.tasks.create(name: name, type: Exec, dependsOn: depends) {
             onlyIf { pidFile.exists() }
+            // the pid file won't actually be read until execution time, since the read is wrapped within an inner closure of the GString
             ext.pid = "${ -> pidFile.getText('UTF-8').trim()}"
             commandLine new File(System.getenv('JAVA_HOME'), 'bin/jps'), '-l'
             standardOutput = new ByteArrayOutputStream()
@@ -296,6 +301,7 @@ class ClusterFormationTasks {
     static Task configureStopTask(String name, Project project, Object depends, File pidFile) {
         return project.tasks.create(name: name, type: Exec, dependsOn: depends) {
             onlyIf { pidFile.exists() }
+            // the pid file won't actually be read until execution time, since the read is wrapped within an inner closure of the GString
             ext.pid = "${ -> pidFile.getText('UTF-8').trim()}"
             doFirst {
                 logger.info("Shutting down external node with pid ${pid}")