From 8904319beb9b60fbda19dec8a885cc5ed5656bc4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 5 Jan 2014 20:47:02 -0800 Subject: [PATCH] commands/plugin: install version and entrypoints work --- lib/vagrant/bundler.rb | 14 ++++++++++++-- lib/vagrant/plugin/manager.rb | 17 ++++++++++++----- lib/vagrant/plugin/state_file.rb | 14 +++++++------- plugins/commands/plugin/action/install_gem.rb | 4 +++- test/unit/vagrant/plugin/manager_test.rb | 4 +++- test/unit/vagrant/plugin/state_file_test.rb | 7 +++++++ 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/vagrant/bundler.rb b/lib/vagrant/bundler.rb index 9a43c5bbf..e44d58f10 100644 --- a/lib/vagrant/bundler.rb +++ b/lib/vagrant/bundler.rb @@ -97,9 +97,19 @@ module Vagrant gemfile.puts(%Q[source "http://gems.hashicorp.com"]) gemfile.puts(%Q[gem "vagrant", "= #{Vagrant::VERSION}"]) gemfile.puts("group :plugins do") - plugins.each do |plugin| - gemfile.puts(%Q[gem "#{plugin}"]) + + plugins.each do |name, plugin| + version = plugin["gem_version"] + version = nil if version == "" + + opts = {} + if plugin["require"] && plugin["require"] != "" + opts[:require] = plugin["require"] + end + + gemfile.puts(%Q[gem "#{name}", #{version.inspect}, #{opts.inspect}]) end + gemfile.puts("end") gemfile.close end diff --git a/lib/vagrant/plugin/manager.rb b/lib/vagrant/plugin/manager.rb index 7c67d4372..21672cc1d 100644 --- a/lib/vagrant/plugin/manager.rb +++ b/lib/vagrant/plugin/manager.rb @@ -28,16 +28,23 @@ module Vagrant # # @param [String] name Name of the plugin (gem) # @return [Gem::Specification] - def install_plugin(name) + def install_plugin(name, **opts) + plugins = installed_plugins + plugins[name] = { + "require" => opts[:require], + "gem_version" => opts[:version], + } + result = nil - Vagrant::Bundler.instance.install(installed_plugins.push(name)).each do |spec| + Vagrant::Bundler.instance.install(plugins).each do |spec| next if spec.name != name next if result && result.version >= spec.version result = spec end # Add the plugin to the state file - @global_file.add_plugin(result.name) + @global_file.add_plugin( + result.name, version: opts[:version], require: opts[:require]) result rescue ::Bundler::GemNotFound @@ -58,7 +65,7 @@ module Vagrant # # @return [Array] def installed_plugins - @global_file.installed_plugins.keys + @global_file.installed_plugins end # This returns the list of plugins that are installed as @@ -66,7 +73,7 @@ module Vagrant # # @return [Array] def installed_specs - installed = Set.new(installed_plugins) + installed = Set.new(installed_plugins.keys) # Go through the plugins installed in this environment and # get the latest version of each. diff --git a/lib/vagrant/plugin/state_file.rb b/lib/vagrant/plugin/state_file.rb index a73bedb5a..7263e9ee1 100644 --- a/lib/vagrant/plugin/state_file.rb +++ b/lib/vagrant/plugin/state_file.rb @@ -27,13 +27,13 @@ module Vagrant # Add a plugin that is installed to the state file. # # @param [String] name The name of the plugin - def add_plugin(name) - if !@data["installed"].has_key?(name) - @data["installed"][name] = { - "ruby_version" => RUBY_VERSION, - "vagrant_version" => Vagrant::VERSION, - } - end + def add_plugin(name, **opts) + @data["installed"][name] = { + "ruby_version" => RUBY_VERSION, + "vagrant_version" => Vagrant::VERSION, + "gem_version" => opts[:version] || "", + "require" => opts[:require] || "", + } save! end diff --git a/plugins/commands/plugin/action/install_gem.rb b/plugins/commands/plugin/action/install_gem.rb index 82e963d24..bb5d37423 100644 --- a/plugins/commands/plugin/action/install_gem.rb +++ b/plugins/commands/plugin/action/install_gem.rb @@ -22,6 +22,7 @@ module VagrantPlugins end def call(env) + entrypoint = env[:plugin_entry_point] plugin_name = env[:plugin_name] version = env[:plugin_version] @@ -51,7 +52,8 @@ module VagrantPlugins # TODO: support version, pre-release, custom sources manager = Vagrant::Plugin::Manager.instance - plugin_spec = manager.install_plugin(plugin_name) + plugin_spec = manager.install_plugin( + plugin_name, version: version, require: entrypoint) # Tell the user env[:ui].success(I18n.t("vagrant.commands.plugin.installed", diff --git a/test/unit/vagrant/plugin/manager_test.rb b/test/unit/vagrant/plugin/manager_test.rb index 34fdf66b6..c32c0ce90 100644 --- a/test/unit/vagrant/plugin/manager_test.rb +++ b/test/unit/vagrant/plugin/manager_test.rb @@ -39,7 +39,9 @@ describe Vagrant::Plugin::Manager do describe "#installed_plugins" do it "has the plugins" do - expect(subject.installed_plugins).to eql(["foo"]) + plugins = subject.installed_plugins + expect(plugins.length).to eql(1) + expect(plugins).to have_key("foo") end end diff --git a/test/unit/vagrant/plugin/state_file_test.rb b/test/unit/vagrant/plugin/state_file_test.rb index 8e94a9f52..4dc97fdc6 100644 --- a/test/unit/vagrant/plugin/state_file_test.rb +++ b/test/unit/vagrant/plugin/state_file_test.rb @@ -32,6 +32,8 @@ describe Vagrant::Plugin::StateFile do expect(plugins["foo"]).to eql({ "ruby_version" => RUBY_VERSION, "vagrant_version" => Vagrant::VERSION, + "gem_version" => "", + "require" => "", }) end @@ -50,6 +52,11 @@ describe Vagrant::Plugin::StateFile do instance = described_class.new(path) expect(instance.installed_plugins.keys).to eql(["foo"]) end + + it "should store metadata" do + subject.add_plugin("foo", version: "1.2.3") + expect(subject.installed_plugins["foo"]["gem_version"]).to eql("1.2.3") + end end context "with an old-style file" do