A really basic "list" command

This commit is contained in:
Mitchell Hashimoto 2013-02-02 23:52:34 -08:00
parent d404eee770
commit 8ac7b62075
3 changed files with 61 additions and 10 deletions

View File

@ -13,10 +13,19 @@ module VagrantPlugins
end
end
# This middleware sequence will list all installed plugins.
def self.action_list
Vagrant::Action::Builder.new.tap do |b|
b.use BundlerCheck
b.use ListPlugins
end
end
# The autoload farm
action_root = Pathname.new(File.expand_path("../action", __FILE__))
autoload :BundlerCheck, action_root.join("bundler_check")
autoload :InstallGem, action_root.join("install_gem")
autoload :ListPlugins, action_root.join("list_plugins")
end
end
end

View File

@ -0,0 +1,25 @@
require "rubygems"
module VagrantPlugins
module CommandPlugin
module Action
class ListPlugins
def initialize(app, env)
@app = app
end
def call(env)
env[:gem_helper].with_environment do
specs = Gem::Specification.find_all
specs.each do |spec|
env[:ui].info spec.name
end
end
@app.call(env)
end
end
end
end
end

View File

@ -13,6 +13,11 @@ module VagrantPlugins
@logger = Log4r::Logger.new("vagrant::plugins::plugincommand::gemhelper")
end
# This executes the `gem` command with the given arguments. Under
# the covers this is actually using the RubyGems API directly,
# instead of shelling out, which allows for more fine-grained control.
#
# @param [Array<String>] argv The arguments to send to the `gem` command.
def cli(argv)
# Initialize the UI to use for RubyGems. This allows us to capture
# the stdout/stderr without actually going to the real STDOUT/STDERR.
@ -21,13 +26,11 @@ module VagrantPlugins
gem_ui = Gem::StreamUI.new(StringIO.new, StringIO.new, StringIO.new, false)
# Set the GEM_HOME so that it is installed into our local gems path
old_gem_home = ENV["GEM_HOME"]
ENV["GEM_HOME"] = @gem_home
@logger.debug("Set GEM_HOME to: #{ENV["GEM_HOME"]}")
@logger.info("Calling gem with argv: #{argv.inspect}")
Gem.clear_paths
Gem::DefaultUserInteraction.use_ui(gem_ui) do
Gem::GemRunner.new.run(argv)
with_environment do
@logger.info("Calling gem with argv: #{argv.inspect}")
Gem::DefaultUserInteraction.use_ui(gem_ui) do
Gem::GemRunner.new.run(argv)
end
end
rescue Gem::SystemExitException => e
# This means that something forced an exit within RubyGems.
@ -36,13 +39,27 @@ module VagrantPlugins
raise Vagrant::Errors::PluginGemError,
:output => gem_ui.errs.string.chomp if e.exit_code != 0
ensure
# Restore the old GEM_HOME
ENV["GEM_HOME"] = old_gem_home
# Log the output properly
@logger.debug("Gem STDOUT: #{gem_ui.outs.string}")
@logger.debug("Gem STDERR: #{gem_ui.errs.string}")
end
# This will yield the given block with the proper ENV setup so
# that RubyGems only sees the gems in the Vagrant-managed gem
# path.
def with_environment
old_gem_home = ENV["GEM_HOME"]
ENV["GEM_HOME"] = @gem_home
@logger.debug("Set GEM_HOME to: #{ENV["GEM_HOME"]}")
# Clear paths so that it reads the new GEM_HOME setting
Gem.clear_paths
yield
ensure
# Restore the old GEM_HOME
ENV["GEM_HOME"] = old_gem_home
end
end
end
end