From dea9d663ff5338bd10c99e21b53915fe9fa27c54 Mon Sep 17 00:00:00 2001
From: Clinton Alexander <clinton.alexander@r3cev.com>
Date: Tue, 1 Nov 2016 14:11:42 +0000
Subject: [PATCH] Fixed node config file being written to the wrong place in
 Cordformation templates.

---
 .../com/r3corda/plugins/Cordform.groovy       |  5 +--
 .../groovy/com/r3corda/plugins/Node.groovy    | 34 ++++++++-----------
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Cordform.groovy b/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Cordform.groovy
index e082834d12..3604b903b7 100644
--- a/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Cordform.groovy
+++ b/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Cordform.groovy
@@ -1,5 +1,6 @@
 package com.r3corda.plugins
 
+import org.apache.tools.ant.filters.FixCrLfFilter
 import org.gradle.api.DefaultTask
 import org.gradle.api.tasks.TaskAction
 import java.nio.file.Path
@@ -53,7 +54,7 @@ class Cordform extends DefaultTask {
      */
     protected Node getNodeByName(String name) {
         for(Node node : nodes) {
-            if(node.name.equals(networkMapNodeName)) {
+            if(node.name == networkMapNodeName) {
                 return node
             }
         }
@@ -69,7 +70,7 @@ class Cordform extends DefaultTask {
             from Cordformation.getPluginFile(project, "com/r3corda/plugins/runnodes")
             filter { String line -> line.replace("JAR_NAME", Node.JAR_NAME) }
             // Replaces end of line with lf to avoid issues with the bash interpreter and Windows style line endings.
-            filter(org.apache.tools.ant.filters.FixCrLfFilter.class, eol: org.apache.tools.ant.filters.FixCrLfFilter.CrLf.newInstance("lf"))
+            filter(FixCrLfFilter.class, eol: FixCrLfFilter.CrLf.newInstance("lf"))
             into "${directory}/"
         }
     }
diff --git a/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Node.groovy b/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Node.groovy
index 019c0d02ab..06bfd5ab98 100644
--- a/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Node.groovy
+++ b/gradle-plugins/cordformation/src/main/groovy/com/r3corda/plugins/Node.groovy
@@ -35,7 +35,7 @@ class Node {
     private Config config = ConfigFactory.empty()
     //private Map<String, Object> config = new HashMap<String, Object>()
     private File nodeDir
-    private def project
+    private Project project
 
     /**
      * Set the name of the node.
@@ -150,7 +150,7 @@ class Node {
      * Installs this project's cordapp to this directory.
      */
     private void installBuiltPlugin() {
-        def pluginsDir = getAndCreateDirectory(nodeDir, "plugins")
+        def pluginsDir = new File(nodeDir, "plugins")
         project.copy {
             from project.jar
             into pluginsDir
@@ -161,7 +161,7 @@ class Node {
      * Installs other cordapps to this node's plugins directory.
      */
     private void installCordapps() {
-        def pluginsDir = getAndCreateDirectory(nodeDir, "plugins")
+        def pluginsDir = new File(nodeDir, "plugins")
         def cordapps = getCordappList()
         project.copy {
             from cordapps
@@ -175,7 +175,7 @@ class Node {
     private void installDependencies() {
         def cordaJar = verifyAndGetCordaJar()
         def cordappList = getCordappList()
-        def depsDir = getAndCreateDirectory(nodeDir, "dependencies")
+        def depsDir = new File(nodeDir, "dependencies")
         def appDeps = project.configurations.runtime.filter { it != cordaJar && !cordappList.contains(it) }
         project.copy {
             from appDeps
@@ -190,9 +190,17 @@ class Node {
         // Adding required default values
         config = config.withValue('extraAdvertisedServiceIds',
                 ConfigValueFactory.fromAnyRef(advertisedServices.join(',')))
-
         def configFileText = config.root().render(new ConfigRenderOptions(false, false, true, false)).split("\n").toList()
-        Files.write(new File(nodeDir, 'node.conf').toPath(), configFileText, StandardCharsets.UTF_8)
+
+        // Need to write a temporary file first to use the project.copy, which resolves directories correctly.
+        def tmpDir = new File(project.buildDir, "tmp")
+        def tmpConfFile = new File(tmpDir, 'node.conf')
+        Files.write(tmpConfFile.toPath(), configFileText, StandardCharsets.UTF_8)
+
+        project.copy {
+            from tmpConfFile
+            into nodeDir
+        }
     }
 
     /**
@@ -223,18 +231,4 @@ class Node {
             return (it != cordaJar) && cordapps.contains(jarName)
         }
     }
-
-    /**
-     * Create a directory if it doesn't exist and return the file representation of it.
-     *
-     * @param baseDir The base directory to create the directory at.
-     * @param subDirName A valid name of the subdirectory to get and create if not exists.
-     * @return A file representing the subdirectory.
-     */
-    private static File getAndCreateDirectory(File baseDir, String subDirName) {
-        File dir = new File(baseDir, subDirName)
-        assert(!dir.exists() || dir.isDirectory())
-        dir.mkdirs()
-        return dir
-    }
 }