mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Made command sets take dependencies in account
This commit is contained in:
parent
5a3f5c2eb1
commit
1dccc80f2d
2 changed files with 56 additions and 4 deletions
|
@ -19,8 +19,8 @@ end
|
|||
require "pry/version"
|
||||
require "pry/hooks"
|
||||
require "pry/print"
|
||||
require "pry/command_base" # to be removed
|
||||
require "pry/command_set"
|
||||
require "pry/command_base" # to be romved
|
||||
require "pry/commands"
|
||||
require "pry/command_context"
|
||||
require "pry/prompts"
|
||||
|
|
|
@ -10,11 +10,17 @@ class Pry
|
|||
class CommandSet
|
||||
class Command < Struct.new(:name, :description, :options, :block)
|
||||
def call(context, *args)
|
||||
ret = context.instance_exec(*args, &block)
|
||||
ret if options[:keep_retval]
|
||||
if stub_block = options[:stub_info]
|
||||
context.instance_eval(&stub_block)
|
||||
else
|
||||
ret = context.instance_exec(*args, &block)
|
||||
ret if options[:keep_retval]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include Pry::CommandBase::CommandBaseHelpers
|
||||
|
||||
attr_reader :commands
|
||||
attr_reader :name
|
||||
|
||||
|
@ -45,7 +51,7 @@ class Pry
|
|||
# parameters passed into the block will be strings. Successive
|
||||
# command parameters are separated by whitespace at the Pry prompt.
|
||||
# @example
|
||||
# MyCommands Pry::CommandSet.new :mine do
|
||||
# MyCommands = Pry::CommandSet.new :mine do
|
||||
# command "greet", "Greet somebody" do |name|
|
||||
# puts "Good afternoon #{name.capitalize}!"
|
||||
# end
|
||||
|
@ -58,6 +64,21 @@ class Pry
|
|||
# # pry(main)> help greet
|
||||
# # Greet somebody
|
||||
def command(names, description="No description.", options={}, &block)
|
||||
first_name = Array(names).first
|
||||
|
||||
options = {:requires_gem => []}.merge(options)
|
||||
|
||||
unless command_dependencies_met? options
|
||||
gems_needed = Array(options[:requires_gem])
|
||||
gems_not_installed = gems_needed.select { |g| !gem_installed?(g) }
|
||||
|
||||
options[:stub_info] = proc do
|
||||
output.puts "\n#{first_name} requires the following gems to be installed: #{(gems_needed.join(", "))}"
|
||||
output.puts "Command not available due to dependency on gems: `#{gems_not_installed.join(", ")}` not being met."
|
||||
output.puts "Type `install #{first_name}` to install the required gems and activate this command."
|
||||
end
|
||||
end
|
||||
|
||||
Array(names).each do |name|
|
||||
commands[name] = Command.new(name, description, options, block)
|
||||
end
|
||||
|
@ -141,6 +162,37 @@ class Pry
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
command "install", "Install a disabled command." do |name|
|
||||
stub_info = commands[name].options[:stub_info]
|
||||
|
||||
if !stub_info
|
||||
output.puts "Not a command stub. Nothing to do."
|
||||
next
|
||||
end
|
||||
|
||||
output.puts "Attempting to install `#{name}` command..."
|
||||
gems_to_install = Array(commands[name].options[:requires_gem])
|
||||
|
||||
gem_install_failed = false
|
||||
gems_to_install.each do |g|
|
||||
next if gem_installed?(g)
|
||||
output.puts "Installing `#{g}` gem..."
|
||||
|
||||
begin
|
||||
Gem::DependencyInstaller.new.install(g)
|
||||
rescue Gem::GemNotFoundException
|
||||
output.puts "Required Gem: `#{g}` not found. Aborting command installation."
|
||||
gem_install_failed = true
|
||||
next
|
||||
end
|
||||
end
|
||||
next if gem_install_failed
|
||||
|
||||
Gem.refresh
|
||||
commands[name].options.delete :stub_info
|
||||
output.puts "Installation of `#{name}` successful! Type `help #{name}` for information"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue