From 73cfab7783eb1e867f77d4fece3168f75d808121 Mon Sep 17 00:00:00 2001 From: Basil Peace Date: Sun, 1 Jan 2017 11:58:29 +0300 Subject: [PATCH] fix: Fix errors with Vagrant post-processor and cleaning --- .../gradle/packer/GradlePackerPlugin.groovy | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/groovy/org/fidata/gradle/packer/GradlePackerPlugin.groovy b/src/main/groovy/org/fidata/gradle/packer/GradlePackerPlugin.groovy index 8a7fe8c..b2c41a9 100644 --- a/src/main/groovy/org/fidata/gradle/packer/GradlePackerPlugin.groovy +++ b/src/main/groovy/org/fidata/gradle/packer/GradlePackerPlugin.groovy @@ -91,6 +91,10 @@ class GradlePackerPlugin implements Plugin { group 'Build' ext.builderType = builderType ext.buildName = buildName + ext.fullBuildName = fullBuildName + ext.cleanTask = project.task("clean-$fullBuildName") { + group 'Clean' + } commandLine((['packer', 'build', "-only=$buildName"] + customVariablesCmdLine + (project.gradle.startParameter.logLevel >= LogLevel.DEBUG ? ['-debug'] : []) + [fileName])) inputs.file templateFile } @@ -129,19 +133,16 @@ class GradlePackerPlugin implements Plugin { // Output filename when no post-processors are used - default t.ext.outputFileName = project.file(new File(outputDir, VMName + '.' + (builder['format'] ?: 'ovf'))).toString() project.logger.info(sprintf('gradle-packer-plugin: outputFileName %s', [t.outputFileName])) - /*t.dependsOn*/ project.task("clean-$imageName-$buildName") { + Task unregisterVM = project.task([type: Exec], "unregisterVM-$fullBuildName") { group 'Clean' - Task unregisterVM = project.task([type: Exec], "unregisterVM-$imageName-$buildName") { - group 'Clean' - commandLine 'VBoxManage', 'unregistervm', "$VMName", '--delete' - ignoreExitValue true - } - dependsOn unregisterVM - dependsOn project.task([type: Delete], "deleteOutputDir-$imageName-$buildName") { - group 'Clean' - shouldRunAfter unregisterVM - delete outputDir - } + commandLine 'VBoxManage', 'unregistervm', "$VMName", '--delete' + ignoreExitValue true + } + t.cleanTask.dependsOn unregisterVM + t.cleanTask.dependsOn project.task([type: Delete], "deleteOutputDir-$fullBuildName") { + group 'Clean' + shouldRunAfter unregisterVM + delete outputDir } } @@ -241,11 +242,12 @@ class GradlePackerPlugin implements Plugin { if (p.containsKey('only')) for (buildName in p['only']) processedTasks[buildName] = ts[buildName] - else if (p.containsKey('except')) { + else { for (t in ts) processedTasks[t.key] = t.value - for (buildName in p['except']) - processedTasks.remove buildName + if (p.containsKey('except')) + for (buildName in p['except']) + processedTasks.remove buildName } for (t in processedTasks.values()) { Map postProcessor = new HashMap(p) @@ -262,20 +264,23 @@ class GradlePackerPlugin implements Plugin { if (postProcessor.containsKey('include')) for (include in postProcessor['include']) t.inputs.file parseString(include, variables) - String vargantProvider + String vagrantProvider switch (t.builderType) { case 'virtualbox-iso': case 'virtualbox-ovf': - vargantProvider = 'virtualbox' + vagrantProvider = 'virtualbox' break case 'amazon-ebs': - vargantProvider = 'aws' + vagrantProvider = 'aws' break } - t.ext.outputFileName = parseString(postProcessor['output'] ?: 'packer_{{.BuildName}}_{{.Provider}}.box', variables + ['.Provider': vargantProvider, '.ArtifactId': vagrantProvider, '.BuildName': t.buildName]) + t.ext.outputFileName = parseString(postProcessor['output'] ?: 'packer_{{.BuildName}}_{{.Provider}}.box', variables + ['.Provider': vagrantProvider, '.ArtifactId': vagrantProvider, '.BuildName': t.buildName]) project.logger.info(sprintf('gradle-packer-plugin: outputFileName %s', [t.outputFileName])) + t.cleanTask.dependsOn project.task([type: Delete], "deleteOutputFile-${t.fullBuildName}") { + group 'Clean' + delete t.outputFileName + } } - } }