From 563f6e89a5639707c65e86326349b3018395f8b1 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 1 Mar 2012 16:36:59 -0800 Subject: [PATCH] Group command-commands into their own set --- lib/pry/command_set.rb | 48 --------------------- lib/pry/commands.rb | 2 + lib/pry/default_commands/basic.rb | 7 ---- lib/pry/default_commands/commands.rb | 62 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 lib/pry/default_commands/commands.rb diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index 6170c435b..0ada9d937 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -21,7 +21,6 @@ def initialize(*imported_sets, &block) @commands = {} @helper_module = Module.new - define_default_commands import(*imported_sets) instance_eval(&block) if block @@ -340,53 +339,6 @@ def default_options(name) :takes_block => false } end - - def define_default_commands - create_command "install-command", "Install a disabled command." do |name| - - banner <<-BANNER - Usage: install-command COMMAND - - Installs the gems necessary to run the given COMMAND. You will generally not - need to run this unless told to by an error message. - BANNER - - def process(name) - require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller - command = find_command(name) - - if command_dependencies_met?(command.options) - output.puts "Dependencies for #{command.name} are met. Nothing to do." - return - end - - output.puts "Attempting to install `#{name}` command..." - gems_to_install = Array(command.options[:requires_gem]) - - 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 - raise CommandError, "Required Gem: `#{g}` not found. Aborting command installation." - end - end - - Gem.refresh - gems_to_install.each do |g| - begin - require g - rescue LoadError - raise CommandError, "Required Gem: `#{g}` installed but not found?!. Aborting command installation." - end - end - - output.puts "Installation of `#{name}` successful! Type `help #{name}` for information" - end - end - end end # Wraps the return result of process_commands, indicates if the diff --git a/lib/pry/commands.rb b/lib/pry/commands.rb index e9d0ae333..5ab648f52 100644 --- a/lib/pry/commands.rb +++ b/lib/pry/commands.rb @@ -2,6 +2,7 @@ require "pry/default_commands/help" require "pry/default_commands/gems" require "pry/default_commands/context" +require "pry/default_commands/commands" require "pry/default_commands/input_and_output" require "pry/default_commands/introspection" require "pry/default_commands/editing" @@ -23,5 +24,6 @@ class Pry import DefaultCommands::InputAndOutput import DefaultCommands::Introspection import DefaultCommands::EasterEggs + import DefaultCommands::Commands end end diff --git a/lib/pry/default_commands/basic.rb b/lib/pry/default_commands/basic.rb index a83f0428c..271826f93 100644 --- a/lib/pry/default_commands/basic.rb +++ b/lib/pry/default_commands/basic.rb @@ -20,13 +20,6 @@ module DefaultCommands output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}." end - command "import-set", "Import a command set" do |command_set_name| - raise CommandError, "Provide a command set name" if command_set.nil? - - set = target.eval(arg_string) - _pry_.commands.import set - end - command "reload-method", "Reload the source file that contains the specified method" do |meth_name| meth = get_method_or_raise(meth_name, target, {}, :omit_help) diff --git a/lib/pry/default_commands/commands.rb b/lib/pry/default_commands/commands.rb new file mode 100644 index 000000000..5e3eb1c86 --- /dev/null +++ b/lib/pry/default_commands/commands.rb @@ -0,0 +1,62 @@ +class Pry + module DefaultCommands + Commands = Pry::CommandSet.new do + create_command "import-set", "Import a command set" do + group "Commands" + def process(command_set_name) + raise CommandError, "Provide a command set name" if command_set.nil? + + set = target.eval(arg_string) + _pry_.commands.import set + end + end + + create_command "install-command", "Install a disabled command." do |name| + group 'Commands' + + banner <<-BANNER + Usage: install-command COMMAND + + Installs the gems necessary to run the given COMMAND. You will generally not + need to run this unless told to by an error message. + BANNER + + def process(name) + require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller + command = find_command(name) + + if command_dependencies_met?(command.options) + output.puts "Dependencies for #{command.name} are met. Nothing to do." + return + end + + output.puts "Attempting to install `#{name}` command..." + gems_to_install = Array(command.options[:requires_gem]) + + 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 + raise CommandError, "Required Gem: `#{g}` not found. Aborting command installation." + end + end + + Gem.refresh + gems_to_install.each do |g| + begin + require g + rescue LoadError + raise CommandError, "Required Gem: `#{g}` installed but not found?!. Aborting command installation." + end + end + + output.puts "Installation of `#{name}` successful! Type `help #{name}` for information" + end + end + end + end +end +