From 7710fd16fa46070c6a454e2d32fa01f953a2e6a1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Mar 2014 09:44:29 -0800 Subject: [PATCH] core: load system plugins if they are installed --- lib/vagrant.rb | 8 ----- lib/vagrant/plugin/manager.rb | 33 ++++++++++++++------- lib/vagrant/plugin/state_file.rb | 7 +++++ lib/vagrant/shared_helpers.rb | 8 +++++ test/unit/vagrant/plugin/state_file_test.rb | 6 ++++ test/unit/vagrant/shared_helpers_test.rb | 14 +++++++++ 6 files changed, 58 insertions(+), 18 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 3fc960fd4..c714e0c55 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -125,14 +125,6 @@ module Vagrant c.register([:"2", :synced_folder]) { Plugin::V2::SyncedFolder } end - # This returns a true/false showing whether we're running from the - # environment setup by the Vagrant installers. - # - # @return [Boolean] - def self.in_installer? - !!ENV["VAGRANT_INSTALLER_ENV"] - end - # Configure a Vagrant environment. The version specifies the version # of the configuration that is expected by the block. The block, based # on that version, configures the environment. diff --git a/lib/vagrant/plugin/manager.rb b/lib/vagrant/plugin/manager.rb index 1480282b8..8023fbea3 100644 --- a/lib/vagrant/plugin/manager.rb +++ b/lib/vagrant/plugin/manager.rb @@ -1,3 +1,4 @@ +require "pathname" require "set" require_relative "../bundler" @@ -8,20 +9,31 @@ module Vagrant module Plugin # The Manager helps with installing, listing, and initializing plugins. class Manager - # Returns the path to the [StateFile] for global plugins. + # Returns the path to the [StateFile] for user plugins. # # @return [Pathname] - def self.global_plugins_file + def self.user_plugins_file Vagrant.user_data_path.join("plugins.json") end - def self.instance - @instance ||= self.new(global_plugins_file) + # Returns the path to the [StateFile] for system plugins. + def self.system_plugins_file + dir = Vagrant.installer_embedded_dir + return nil if !dir + Pathname.new(dir).join("plugins.json") end - # @param [Pathname] global_file - def initialize(global_file) - @global_file = StateFile.new(global_file) + def self.instance + @instance ||= self.new(user_plugins_file) + end + + # @param [Pathname] user_file + def initialize(user_file) + @user_file = StateFile.new(user_file) + + system_path = self.class.system_plugins_file + @system_file = nil + @system_file = StateFile.new(system_path) if system_path && system_path.file? end # Installs another plugin into our gem directory. @@ -65,7 +77,7 @@ module Vagrant opts.delete(:version) if opts[:version] && opts[:version] =~ /^\d/ # Add the plugin to the state file - @global_file.add_plugin( + @user_file.add_plugin( result.name, version: opts[:version], require: opts[:require], @@ -83,7 +95,7 @@ module Vagrant # # @param [String] name def uninstall_plugin(name) - @global_file.remove_plugin(name) + @user_file.remove_plugin(name) # Clean the environment, removing any old plugins Vagrant::Bundler.instance.clean(installed_plugins) @@ -102,7 +114,8 @@ module Vagrant # # @return [Hash] def installed_plugins - @global_file.installed_plugins + system = @system_file ? @system_file.installed_plugins : {} + system.merge(@user_file.installed_plugins) end # This returns the list of plugins that are installed as diff --git a/lib/vagrant/plugin/state_file.rb b/lib/vagrant/plugin/state_file.rb index b9161672f..faf92c0ec 100644 --- a/lib/vagrant/plugin/state_file.rb +++ b/lib/vagrant/plugin/state_file.rb @@ -57,6 +57,13 @@ module Vagrant @data["installed"] end + # Returns true/false if the plugin is present in this state file. + # + # @return [Boolean] + def has_plugin?(name) + @data["installed"].has_key?(name) + end + # Remove a plugin that is installed from the state file. # # @param [String] name The name of the plugin. diff --git a/lib/vagrant/shared_helpers.rb b/lib/vagrant/shared_helpers.rb index a49871395..5a054fafc 100644 --- a/lib/vagrant/shared_helpers.rb +++ b/lib/vagrant/shared_helpers.rb @@ -8,6 +8,14 @@ module Vagrant # @return [String] DEFAULT_SERVER_URL = "https://www.vagrantcloud.com" + # This returns a true/false showing whether we're running from the + # environment setup by the Vagrant installers. + # + # @return [Boolean] + def self.in_installer? + !!ENV["VAGRANT_INSTALLER_ENV"] + end + # Returns the path to the embedded directory of the Vagrant installer, # if there is one (if we're running in an installer). # diff --git a/test/unit/vagrant/plugin/state_file_test.rb b/test/unit/vagrant/plugin/state_file_test.rb index 6b37198ef..0b0418d70 100644 --- a/test/unit/vagrant/plugin/state_file_test.rb +++ b/test/unit/vagrant/plugin/state_file_test.rb @@ -38,6 +38,12 @@ describe Vagrant::Plugin::StateFile do }) end + it "should check for plugins" do + expect(subject.has_plugin?("foo")).to be_false + subject.add_plugin("foo") + expect(subject.has_plugin?("foo")).to be_true + end + it "should remove plugins" do subject.add_plugin("foo") subject.remove_plugin("foo") diff --git a/test/unit/vagrant/shared_helpers_test.rb b/test/unit/vagrant/shared_helpers_test.rb index e4bd86b4f..67772c79f 100644 --- a/test/unit/vagrant/shared_helpers_test.rb +++ b/test/unit/vagrant/shared_helpers_test.rb @@ -8,6 +8,20 @@ describe Vagrant do subject { described_class } + describe "#in_installer?" do + it "is not if env is not set" do + with_temp_env("VAGRANT_INSTALLER_ENV" => nil) do + expect(subject.in_installer?).to be_false + end + end + + it "is if env is set" do + with_temp_env("VAGRANT_INSTALLER_ENV" => "/foo") do + expect(subject.in_installer?).to be_true + end + end + end + describe "#installer_embedded_dir" do it "returns nil if not in an installer" do Vagrant.stub(in_installer?: false)