From 6ed612e93b6b898adae08510fcfa0f3676034523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brala?= Date: Fri, 7 Oct 2016 17:31:15 +0200 Subject: [PATCH] Move package_setup_files to general lib to minimize code duplication --- .../action/general/package_setup_files.rb | 51 +++++++++++++++++++ .../hyperv/action/package_setup_files.rb | 49 +++--------------- .../virtualbox/action/package_setup_files.rb | 49 +++--------------- 3 files changed, 65 insertions(+), 84 deletions(-) create mode 100644 lib/vagrant/action/general/package_setup_files.rb diff --git a/lib/vagrant/action/general/package_setup_files.rb b/lib/vagrant/action/general/package_setup_files.rb new file mode 100644 index 000000000..b8882f3eb --- /dev/null +++ b/lib/vagrant/action/general/package_setup_files.rb @@ -0,0 +1,51 @@ +module Vagrant + module Action + module General + class PackageSetupFiles + def initialize(app, env) + @app = app + + env["package.include"] ||= [] + env["package.vagrantfile"] ||= nil + end + + def call(env) + files = {} + env["package.include"].each do |file| + source = Pathname.new(file) + dest = nil + + # If the source is relative then we add the file as-is to the include + # directory. Otherwise, we copy only the file into the root of the + # include directory. Kind of strange, but seems to match what people + # expect based on history. + if source.relative? + dest = source + else + dest = source.basename + end + + # Assign the mapping + files[file] = dest + end + + if env["package.vagrantfile"] + # Vagrantfiles are treated special and mapped to a specific file + files[env["package.vagrantfile"]] = "_Vagrantfile" + end + + # Verify the mapping + files.each do |from, _| + raise Vagrant::Errors::PackageIncludeMissing, + file: from if !File.exist?(from) + end + + # Save the mapping + env["package.files"] = files + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/hyperv/action/package_setup_files.rb b/plugins/providers/hyperv/action/package_setup_files.rb index d6371d40d..ea0fd1b6c 100644 --- a/plugins/providers/hyperv/action/package_setup_files.rb +++ b/plugins/providers/hyperv/action/package_setup_files.rb @@ -1,49 +1,14 @@ +require_relative "../../../../lib/vagrant/action/general/package_setup_files" + module VagrantPlugins module HyperV module Action - class PackageSetupFiles - def initialize(app, env) - @app = app - - env["package.include"] ||= [] - env["package.vagrantfile"] ||= nil - end - + class PackageSetupFiles < Vagrant::Action::General::PackageSetupFiles + # 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) - files = {} - env["package.include"].each do |file| - source = Pathname.new(file) - dest = nil - - # If the source is relative then we add the file as-is to the include - # directory. Otherwise, we copy only the file into the root of the - # include directory. Kind of strange, but seems to match what people - # expect based on history. - if source.relative? - dest = source - else - dest = source.basename - end - - # Assign the mapping - files[file] = dest - end - - if env["package.vagrantfile"] - # Vagrantfiles are treated special and mapped to a specific file - files[env["package.vagrantfile"]] = "_Vagrantfile" - end - - # Verify the mapping - files.each do |from, _| - raise Vagrant::Errors::PackageIncludeMissing, - file: from if !File.exist?(from) - end - - # Save the mapping - env["package.files"] = files - - @app.call(env) + general_call(env) end end end diff --git a/plugins/providers/virtualbox/action/package_setup_files.rb b/plugins/providers/virtualbox/action/package_setup_files.rb index a70c7751a..f256b173d 100644 --- a/plugins/providers/virtualbox/action/package_setup_files.rb +++ b/plugins/providers/virtualbox/action/package_setup_files.rb @@ -1,49 +1,14 @@ +require_relative "../../../../lib/vagrant/action/general/package_setup_files" + module VagrantPlugins module ProviderVirtualBox module Action - class PackageSetupFiles - def initialize(app, env) - @app = app - - env["package.include"] ||= [] - env["package.vagrantfile"] ||= nil - end - + class PackageSetupFiles < Vagrant::Action::General::PackageSetupFiles + # 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) - files = {} - env["package.include"].each do |file| - source = Pathname.new(file) - dest = nil - - # If the source is relative then we add the file as-is to the include - # directory. Otherwise, we copy only the file into the root of the - # include directory. Kind of strange, but seems to match what people - # expect based on history. - if source.relative? - dest = source - else - dest = source.basename - end - - # Assign the mapping - files[file] = dest - end - - if env["package.vagrantfile"] - # Vagrantfiles are treated special and mapped to a specific file - files[env["package.vagrantfile"]] = "_Vagrantfile" - end - - # Verify the mapping - files.each do |from, _| - raise Vagrant::Errors::PackageIncludeMissing, - file: from if !File.exist?(from) - end - - # Save the mapping - env["package.files"] = files - - @app.call(env) + general_call(env) end end end