From 5877e8fe2231db5a78ab505d5828934e0eb27415 Mon Sep 17 00:00:00 2001 From: Bjorn Brala Date: Thu, 6 Oct 2016 08:55:59 +0200 Subject: [PATCH] Sart refactor duplicate classes --- .../action/general/package_setup_folders.rb | 37 +++++++++++++++++++ .../hyperv/action/package_setup_folders.rb | 32 +++------------- 2 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 lib/vagrant/action/general/package_setup_folders.rb diff --git a/lib/vagrant/action/general/package_setup_folders.rb b/lib/vagrant/action/general/package_setup_folders.rb new file mode 100644 index 000000000..387c11999 --- /dev/null +++ b/lib/vagrant/action/general/package_setup_folders.rb @@ -0,0 +1,37 @@ +require "fileutils" +require_relative "package" + +module Vagrant + module Action + module General + class PackageSetupFolders + include Vagrant::Util::Presence + + def initialize(app, env) + @app = app + end + + def call(env) + env["package.output"] ||= "package.box" + env["package.directory"] ||= Dir.mktmpdir("vagrant-package-", env[:tmp_path]) + + # Match up a couple environmental variables so that the other parts of + # Vagrant will do the right thing. + env["export.temp_dir"] = env["package.directory"] + + Vagrant::Action::General::Package.validate!( + env["package.output"], env["package.directory"]) + + @app.call(env) + end + + def recover(env) + dir = env["package.directory"] + if File.exist?(dir) + FileUtils.rm_rf(dir) + end + end + end + end + end +end diff --git a/plugins/providers/hyperv/action/package_setup_folders.rb b/plugins/providers/hyperv/action/package_setup_folders.rb index a4c3ebeb5..12a2ff190 100644 --- a/plugins/providers/hyperv/action/package_setup_folders.rb +++ b/plugins/providers/hyperv/action/package_setup_folders.rb @@ -1,36 +1,16 @@ require "fileutils" -require_relative "../../../../lib/vagrant/action/general/package" +require_relative "../../../../lib/vagrant/action/general/package_setup_folders" module VagrantPlugins module HyperV module Action - class PackageSetupFolders - include Vagrant::Util::Presence - - def initialize(app, env) - @app = app - end - + class PackageSetupFolders < Vagrant::Action::General::PackageSetupFolders + # Doing this so that we can test that the parent is properly + # called in the unit tests. + alias_method :general_call, :call def call(env) - env["package.output"] ||= "package.box" - env["package.directory"] ||= Dir.mktmpdir("vagrant-package-", env[:tmp_path]) - - # Match up a couple environmental variables so that the other parts of - # Vagrant will do the right thing. - env["export.temp_dir"] = env["package.directory"] - - Vagrant::Action::General::Package.validate!( - env["package.output"], env["package.directory"]) - - @app.call(env) - end - - def recover(env) - dir = env["package.directory"] - if File.exist?(dir) - FileUtils.rm_rf(dir) - end + general_call(env) end end end