pry 0.8.3-java → 0.8.4pre1-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +2 -0
- data/.gitignore +8 -0
- data/.yardopts +1 -0
- data/README.markdown +10 -6
- data/Rakefile +15 -23
- data/TODO +62 -0
- data/bin/pry +3 -1
- data/lib/pry.rb +6 -7
- data/lib/pry/command_context.rb +29 -0
- data/lib/pry/command_processor.rb +15 -28
- data/lib/pry/command_set.rb +234 -0
- data/lib/pry/commands.rb +15 -861
- data/lib/pry/core_extensions.rb +40 -48
- data/lib/pry/default_commands/context.rb +127 -0
- data/lib/pry/default_commands/documentation.rb +145 -0
- data/lib/pry/default_commands/easter_eggs.rb +71 -0
- data/lib/pry/default_commands/gems.rb +59 -0
- data/lib/pry/default_commands/input.rb +38 -0
- data/lib/pry/default_commands/introspection.rb +190 -0
- data/lib/pry/default_commands/ls.rb +199 -0
- data/lib/pry/default_commands/shell.rb +90 -0
- data/lib/pry/helpers.rb +2 -0
- data/lib/pry/{command_base_helpers.rb → helpers/base_helpers.rb} +46 -21
- data/lib/pry/{command_helpers.rb → helpers/command_helpers.rb} +34 -36
- data/lib/pry/pry_class.rb +17 -11
- data/lib/pry/pry_instance.rb +59 -2
- data/lib/pry/version.rb +1 -1
- data/test/{test_helper.rb → helper.rb} +8 -2
- data/test/test_command_helpers.rb +77 -0
- data/test/test_commandset.rb +184 -0
- data/test/{test.rb → test_pry.rb} +164 -132
- data/wiki/Customizing-pry.md +397 -0
- data/wiki/Home.md +4 -0
- metadata +61 -41
- data/lib/pry/command_base.rb +0 -202
data/.document
ADDED
data/.gitignore
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --private
|
data/README.markdown
CHANGED
@@ -420,11 +420,11 @@ You can use `edit-method Class#method` or `edit-method my_method`
|
|
420
420
|
your favorite editor. Pry has knowledge of a few different editors and
|
421
421
|
will attempt to open the file at the line the method is defined.
|
422
422
|
|
423
|
-
You can set the editor to use by assigning to the `Pry.editor
|
423
|
+
You can set the editor to use by assigning to the `Pry.editor`
|
424
424
|
accessor. `Pry.editor` will default to `$EDITOR` or failing that will
|
425
425
|
use `nano` as the backup default. The file that is edited will be
|
426
426
|
automatically reloaded after exiting the editor - reloading can be
|
427
|
-
suppressed by passing the
|
427
|
+
suppressed by passing the `--no-reload` option to `edit-method`
|
428
428
|
|
429
429
|
In the example below we will set our default editor to "emacsclient"
|
430
430
|
and open the `Pry#repl` method for editing:
|
@@ -432,7 +432,6 @@ and open the `Pry#repl` method for editing:
|
|
432
432
|
pry(main)> Pry.editor = "emacsclient"
|
433
433
|
pry(main)> edit-method Pry#repl
|
434
434
|
|
435
|
-
|
436
435
|
### Live Help System
|
437
436
|
|
438
437
|
Many other commands are available in Pry; to see the full list type
|
@@ -442,6 +441,11 @@ help that can be accessed via typing `command_name --help`. A command
|
|
442
441
|
will typically say in its description if the `--help` option is
|
443
442
|
avaiable.
|
444
443
|
|
444
|
+
### Use Pry as your Rails Console
|
445
|
+
|
446
|
+
pry -r./config/environment
|
447
|
+
|
448
|
+
MyArtChannel has kindly provided a hack to replace the `rails console` command in Rails 3: [https://gist.github.com/941174](https://gist.github.com/941174) This is not recommended for code bases with multiple developers, as they may not all want to use Pry.
|
445
449
|
|
446
450
|
### Other Features and limitations
|
447
451
|
|
@@ -482,13 +486,13 @@ invoke any of these methods directly depending on exactly what aspect of the fun
|
|
482
486
|
|
483
487
|
#### Limitations:
|
484
488
|
|
485
|
-
* Some Pry commands (e.g `show-command`) do not work in Ruby 1.8
|
489
|
+
* Some Pry commands (e.g `show-command`) do not work in Ruby 1.8
|
490
|
+
MRI. But many other commands do work in Ruby 1.8 MRI, e.g
|
491
|
+
`show-method`, due to a functional 1.8 source_location implementation.
|
486
492
|
* JRuby not officially supported due to currently too many quirks and
|
487
493
|
strange behaviour. Nonetheless most functionality should still work
|
488
494
|
OK in JRuby. Full JRuby support coming in a future version.
|
489
495
|
* `method_source` functionality does not work in JRuby with Ruby 1.8
|
490
|
-
* Color support does not work in JRuby with Ruby 1.9 (due to a
|
491
|
-
limitation in JRuby's regex).
|
492
496
|
* Tab completion is currently a bit broken/limited this will have a
|
493
497
|
major overhaul in a future version.
|
494
498
|
|
data/Rakefile
CHANGED
@@ -1,42 +1,38 @@
|
|
1
|
-
dlext = Config::CONFIG['DLEXT']
|
2
|
-
direc = File.dirname(__FILE__)
|
3
|
-
|
4
1
|
require 'rake/clean'
|
5
2
|
require 'rake/gempackagetask'
|
6
|
-
require "#{direc}/lib/pry/version"
|
7
3
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
$:.unshift 'lib'
|
5
|
+
require 'pry/version'
|
6
|
+
|
7
|
+
CLOBBER.include("**/*~", "**/*#*", "**/*.log")
|
8
|
+
CLEAN.include("**/*#*", "**/*#*.*", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc")
|
12
9
|
|
13
10
|
def apply_spec_defaults(s)
|
14
11
|
s.name = "pry"
|
15
|
-
s.summary = "
|
12
|
+
s.summary = "an IRB alternative and runtime developer console"
|
16
13
|
s.version = Pry::VERSION
|
17
14
|
s.date = Time.now.strftime '%Y-%m-%d'
|
18
15
|
s.author = "John Mair (banisterfiend)"
|
19
16
|
s.email = 'jrmair@gmail.com'
|
20
17
|
s.description = s.summary
|
21
|
-
s.require_path = 'lib'
|
22
|
-
s.add_dependency("ruby_parser",">=2.0.5")
|
23
|
-
s.add_dependency("coderay",">=0.9.7")
|
24
|
-
s.add_dependency("slop",">=1.5.3")
|
25
|
-
s.add_development_dependency("bacon",">=1.1.0")
|
26
18
|
s.homepage = "http://banisterfiend.wordpress.com"
|
27
19
|
s.has_rdoc = 'yard'
|
28
20
|
s.executables = ["pry"]
|
29
|
-
s.files =
|
30
|
-
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
23
|
+
s.add_dependency("ruby_parser",">=2.0.5")
|
24
|
+
s.add_dependency("coderay",">=0.9.8")
|
25
|
+
s.add_dependency("slop",">=1.5.3")
|
26
|
+
s.add_dependency("method_source",">=0.4.0")
|
27
|
+
s.add_development_dependency("bacon",">=1.1.0")
|
31
28
|
end
|
32
29
|
|
33
30
|
task :test do
|
34
|
-
sh "bacon
|
31
|
+
sh "bacon -Itest -rubygems -a"
|
35
32
|
end
|
36
33
|
|
37
34
|
desc "run pry"
|
38
35
|
task :pry do
|
39
|
-
$LOAD_PATH.unshift "#{direc}/lib"
|
40
36
|
require 'pry'
|
41
37
|
binding.pry
|
42
38
|
end
|
@@ -49,7 +45,6 @@ end
|
|
49
45
|
namespace :ruby do
|
50
46
|
spec = Gem::Specification.new do |s|
|
51
47
|
apply_spec_defaults(s)
|
52
|
-
s.add_dependency("method_source",">=0.4.0")
|
53
48
|
s.platform = Gem::Platform::RUBY
|
54
49
|
end
|
55
50
|
|
@@ -63,7 +58,6 @@ end
|
|
63
58
|
namespace v do
|
64
59
|
spec = Gem::Specification.new do |s|
|
65
60
|
apply_spec_defaults(s)
|
66
|
-
s.add_dependency("method_source",">=0.4.0")
|
67
61
|
s.add_dependency("win32console", ">=1.3.0")
|
68
62
|
s.platform = "i386-#{v}"
|
69
63
|
end
|
@@ -78,7 +72,6 @@ end
|
|
78
72
|
namespace :jruby do
|
79
73
|
spec = Gem::Specification.new do |s|
|
80
74
|
apply_spec_defaults(s)
|
81
|
-
s.add_dependency("method_source",">=0.4.0")
|
82
75
|
s.platform = "java"
|
83
76
|
end
|
84
77
|
|
@@ -88,7 +81,6 @@ namespace :jruby do
|
|
88
81
|
end
|
89
82
|
end
|
90
83
|
|
91
|
-
|
92
84
|
desc "build all platform gems at once"
|
93
85
|
task :gems => [:clean, :rmgems, "ruby:gem", "jruby:gem", "mswin32:gem", "mingw32:gem"]
|
94
86
|
|
@@ -97,7 +89,7 @@ task :rmgems => ["ruby:clobber_package"]
|
|
97
89
|
|
98
90
|
desc "build and push latest gems"
|
99
91
|
task :pushgems => :gems do
|
100
|
-
chdir("#{
|
92
|
+
chdir("#{File.dirname(__FILE__)}/pkg") do
|
101
93
|
Dir["*.gem"].each do |gemfile|
|
102
94
|
sh "gem push #{gemfile}"
|
103
95
|
end
|
data/TODO
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
FUTURE
|
3
|
+
* allows pipes (|) for commands
|
4
|
+
|
5
|
+
0.8.0
|
6
|
+
* allow #{} interpolation of all commands
|
7
|
+
* update documentation! new commands and features and change in behaviour of `run`
|
8
|
+
* add ; at end of line to suppress return value output
|
9
|
+
* Remove message spam (before/after hooks)
|
10
|
+
* stop commands returning a value
|
11
|
+
* use `redo` in the r() method when encounter a command
|
12
|
+
* shell functionality should just use system(), but redirect in and
|
13
|
+
out to Pry.input and Pry.output by reassining $stdin and $stdout
|
14
|
+
for duration of block.
|
15
|
+
* basicobject and no to_s/inspect support
|
16
|
+
* fix documentation, support rdoc and yard properly
|
17
|
+
* only load Ripper if 1.9 AND MRI (support jruby 1.9, using
|
18
|
+
RubyParser)
|
19
|
+
* shell commands invokable file .<command>
|
20
|
+
* supercharge cat-file so it can syntax highlight sourcecode files
|
21
|
+
|
22
|
+
|
23
|
+
0.7.0
|
24
|
+
* add pry-doc support with syntax highlighting for docs
|
25
|
+
* add 'mj' option to ls (restrict to singleton methods)
|
26
|
+
* add _ex_ local to hold last exception raised in an exception
|
27
|
+
|
28
|
+
0.6.8
|
29
|
+
* add whereami command, a la the `ir_b` gem
|
30
|
+
* make .pryrc be loaded by run-time pry sessions
|
31
|
+
|
32
|
+
0.6.7
|
33
|
+
* color support
|
34
|
+
* --simple-prompt for pry commandline
|
35
|
+
* -I mode for pry commandline
|
36
|
+
* --color mode for pry commandline
|
37
|
+
* clean up requires (put them all in one place)
|
38
|
+
* simple-prompt command and toggle-color commandd.
|
39
|
+
|
40
|
+
0.6.1
|
41
|
+
* !@ command alias for exit_all
|
42
|
+
* `cd /` for breaking out to pry top level (jump-to 0)
|
43
|
+
* made `-e` option work in a more effective way for `pry` command line invocation
|
44
|
+
* exit and exit-all commands now accept a parameter, this parameter becomes the return value of repl()
|
45
|
+
* `command` method from CommandBase now accepts a :keep_retval arg that determines if command value is returned to pry session or just `nil` (`nil` was old behaviour)
|
46
|
+
* tests for new :keep_retval and exit-all/exit behaviour; :keep_retval will remain undocumented.
|
47
|
+
|
48
|
+
0.5.0 release:
|
49
|
+
* !!!! UPDATE DOCUMENTATION !!!!
|
50
|
+
* Use clipped version of Pry.view() for large objects
|
51
|
+
* Exit Pry session on ^d
|
52
|
+
* Use Shellwords for breaking up parameters to pry commands
|
53
|
+
* Use OptionParser to parse options for default pry commands
|
54
|
+
* Add version command
|
55
|
+
* Refactor 'status' command: add current method info
|
56
|
+
* Add meth_name_from_binding utility lambda to commands.rb
|
57
|
+
* Add -M, -m, -v(erbose), -a(ll), -s(uper), -l(ocals), -i(ivars), -k(klass vars) options to ls
|
58
|
+
* add -i(nstance) option to show-method
|
59
|
+
* add --help option to most commands
|
60
|
+
* Get rid of ls_method and ls_imethods (subsumed by more powerful ls)
|
61
|
+
* Get rid of show_idoc and show_imethod
|
62
|
+
* Add special eval-file command that evals target file in current context
|
data/bin/pry
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
# (C) John Mair (banisterfiend)
|
4
4
|
# MIT license
|
5
5
|
|
6
|
+
$0 = 'pry'
|
7
|
+
|
6
8
|
begin
|
7
9
|
require 'pry'
|
8
10
|
rescue LoadError
|
@@ -48,7 +50,7 @@ end
|
|
48
50
|
# invoked via cli
|
49
51
|
Pry.cli = true
|
50
52
|
|
51
|
-
|
53
|
+
Pry::Commands.instance_eval do
|
52
54
|
command "reset", "Reset the REPL to a clean state." do
|
53
55
|
output.puts "Pry reset."
|
54
56
|
exec("pry")
|
data/lib/pry.rb
CHANGED
@@ -6,6 +6,9 @@ require 'shellwords'
|
|
6
6
|
require "readline"
|
7
7
|
require "stringio"
|
8
8
|
require "coderay"
|
9
|
+
require "optparse"
|
10
|
+
require "slop"
|
11
|
+
require "rubygems/dependency_installer"
|
9
12
|
|
10
13
|
if RUBY_PLATFORM =~ /mswin/ || RUBY_PLATFORM =~ /mingw/
|
11
14
|
begin
|
@@ -19,17 +22,13 @@ end
|
|
19
22
|
require "pry/version"
|
20
23
|
require "pry/hooks"
|
21
24
|
require "pry/print"
|
22
|
-
require "pry/
|
25
|
+
require "pry/helpers"
|
26
|
+
require "pry/command_set"
|
23
27
|
require "pry/commands"
|
28
|
+
require "pry/command_context"
|
24
29
|
require "pry/prompts"
|
25
30
|
require "pry/custom_completions"
|
26
31
|
require "pry/completion"
|
27
32
|
require "pry/core_extensions"
|
28
33
|
require "pry/pry_class"
|
29
34
|
require "pry/pry_instance"
|
30
|
-
|
31
|
-
|
32
|
-
# TEMPORARY HACK FOR BUG IN JRUBY 1.9 REGEX (which kills CodeRay)
|
33
|
-
if RUBY_VERSION =~ /1.9/ && RUBY_ENGINE =~ /jruby/
|
34
|
-
Pry.color = false
|
35
|
-
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Pry
|
2
|
+
# Command contexts are the objects runing each command.
|
3
|
+
# Helper modules can be mixed into this class.
|
4
|
+
class CommandContext
|
5
|
+
attr_accessor :output
|
6
|
+
attr_accessor :target
|
7
|
+
attr_accessor :opts
|
8
|
+
attr_accessor :command_set
|
9
|
+
attr_accessor :command_processor
|
10
|
+
|
11
|
+
def run(name, *args)
|
12
|
+
if name.start_with? "."
|
13
|
+
cmd = name[1..-1]
|
14
|
+
command_processor.
|
15
|
+
execute_system_command([name, Shellwords.join(args)].join(' '),
|
16
|
+
target)
|
17
|
+
else
|
18
|
+
command_set.run_command(self, name, *args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def commands
|
23
|
+
command_set.commands
|
24
|
+
end
|
25
|
+
|
26
|
+
include Pry::Helpers::BaseHelpers
|
27
|
+
include Pry::Helpers::CommandHelpers
|
28
|
+
end
|
29
|
+
end
|
@@ -68,7 +68,7 @@ class Pry
|
|
68
68
|
else
|
69
69
|
dest = File.expand_path($1)
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
@@cd_history << Dir.pwd
|
73
73
|
Dir.chdir(dest)
|
74
74
|
rescue Errno::ENOENT
|
@@ -119,11 +119,9 @@ class Pry
|
|
119
119
|
return if !pry_command?(val)
|
120
120
|
|
121
121
|
val.replace interpolate_string(val, target)
|
122
|
-
|
122
|
+
command, args_string = command_matched(val)
|
123
123
|
|
124
124
|
args = args_string ? Shellwords.shellwords(args_string) : []
|
125
|
-
action = cmd_data[:action]
|
126
|
-
keep_retval = cmd_data[:keep_retval]
|
127
125
|
|
128
126
|
options = {
|
129
127
|
:val => val,
|
@@ -132,43 +130,32 @@ class Pry
|
|
132
130
|
:commands => commands.commands
|
133
131
|
}
|
134
132
|
|
135
|
-
|
136
|
-
|
137
|
-
# return value of block only if :keep_retval is true
|
138
|
-
ret_value if keep_retval
|
133
|
+
execute_command(target, command.name, options, *args)
|
139
134
|
end
|
140
135
|
|
141
136
|
# Execute a Pry command.
|
142
137
|
# This method should not need to be invoked directly.
|
143
138
|
# @param [Binding] target The target of the Pry session.
|
144
|
-
# @param [
|
139
|
+
# @param [String] command The name of the command to be run.
|
145
140
|
# @param [Hash] options The options to set on the Commands object.
|
146
141
|
# @param [Array] args The command arguments.
|
147
|
-
def execute_command(target,
|
142
|
+
def execute_command(target, command, options, *args)
|
143
|
+
context = CommandContext.new
|
148
144
|
|
149
145
|
# set some useful methods to be used by the action blocks
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
ret_val = commands.instance_exec(*args, &action)
|
159
|
-
when 1, 0
|
160
|
-
|
161
|
-
# ensure that we get the right number of parameters
|
162
|
-
# since 1.8.7 complains about incorrect arity (1.9.2
|
163
|
-
# doesn't care)
|
164
|
-
args_with_corrected_arity = args.values_at *0..(action.arity - 1)
|
165
|
-
ret_val = commands.instance_exec(*args_with_corrected_arity, &action)
|
166
|
-
end
|
146
|
+
context.opts = options
|
147
|
+
context.target = target
|
148
|
+
context.output = output
|
149
|
+
context.command_set = commands
|
150
|
+
|
151
|
+
context.command_processor = self
|
152
|
+
|
153
|
+
ret = commands.run_command(context, command, *args)
|
167
154
|
|
168
155
|
# Tick, tock, im getting rid of this shit soon.
|
169
156
|
options[:val].clear
|
170
157
|
|
171
|
-
|
158
|
+
ret
|
172
159
|
end
|
173
160
|
end
|
174
161
|
end
|
@@ -0,0 +1,234 @@
|
|
1
|
+
class Pry
|
2
|
+
class NoCommandError < StandardError
|
3
|
+
def initialize(name, owner)
|
4
|
+
super "Command '#{name}' not found in command set #{owner}"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
# This class used to create sets of commands. Commands can be impoted from
|
9
|
+
# different sets, aliased, removed, etc.
|
10
|
+
class CommandSet
|
11
|
+
class Command < Struct.new(:name, :description, :options, :block)
|
12
|
+
|
13
|
+
def call(context, *args)
|
14
|
+
if stub_block = options[:stub_info]
|
15
|
+
context.instance_eval(&stub_block)
|
16
|
+
else
|
17
|
+
ret = context.instance_exec(*correct_arg_arity(block.arity, args), &block)
|
18
|
+
ret if options[:keep_retval]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def correct_arg_arity(arity, args)
|
24
|
+
case arity <=> 0
|
25
|
+
when -1
|
26
|
+
args
|
27
|
+
when 1, 0
|
28
|
+
# Keep 1.8 happy
|
29
|
+
args.values_at *0..(arity - 1)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
include Pry::Helpers::BaseHelpers
|
35
|
+
|
36
|
+
attr_reader :commands
|
37
|
+
attr_reader :name
|
38
|
+
attr_reader :helper_module
|
39
|
+
|
40
|
+
# @param [Symbol] name Name of the command set
|
41
|
+
# @param [Array<CommandSet>] imported_sets Sets which will be imported
|
42
|
+
# automatically
|
43
|
+
# @yield Optional block run to define commands
|
44
|
+
def initialize(name, *imported_sets, &block)
|
45
|
+
@name = name
|
46
|
+
@commands = {}
|
47
|
+
@helper_module = Module.new
|
48
|
+
|
49
|
+
define_default_commands
|
50
|
+
import(*imported_sets)
|
51
|
+
|
52
|
+
instance_eval(&block) if block
|
53
|
+
end
|
54
|
+
|
55
|
+
# Defines a new Pry command.
|
56
|
+
# @param [String, Array] names The name of the command (or array of
|
57
|
+
# command name aliases).
|
58
|
+
# @param [String] description A description of the command.
|
59
|
+
# @param [Hash] options The optional configuration parameters.
|
60
|
+
# @option options [Boolean] :keep_retval Whether or not to use return value
|
61
|
+
# of the block for return of `command` or just to return `nil`
|
62
|
+
# (the default).
|
63
|
+
# @yield The action to perform. The parameters in the block
|
64
|
+
# determines the parameters the command will receive. All
|
65
|
+
# parameters passed into the block will be strings. Successive
|
66
|
+
# command parameters are separated by whitespace at the Pry prompt.
|
67
|
+
# @example
|
68
|
+
# MyCommands = Pry::CommandSet.new :mine do
|
69
|
+
# command "greet", "Greet somebody" do |name|
|
70
|
+
# puts "Good afternoon #{name.capitalize}!"
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# # From pry:
|
75
|
+
# # pry(main)> _pry_.commands = MyCommands
|
76
|
+
# # pry(main)> greet john
|
77
|
+
# # Good afternoon John!
|
78
|
+
# # pry(main)> help greet
|
79
|
+
# # Greet somebody
|
80
|
+
def command(names, description="No description.", options={}, &block)
|
81
|
+
first_name = Array(names).first
|
82
|
+
|
83
|
+
options = {:requires_gem => []}.merge(options)
|
84
|
+
|
85
|
+
unless command_dependencies_met? options
|
86
|
+
gems_needed = Array(options[:requires_gem])
|
87
|
+
gems_not_installed = gems_needed.select { |g| !gem_installed?(g) }
|
88
|
+
|
89
|
+
options[:stub_info] = proc do
|
90
|
+
output.puts "\n#{first_name} requires the following gems to be installed: #{(gems_needed.join(", "))}"
|
91
|
+
output.puts "Command not available due to dependency on gems: `#{gems_not_installed.join(", ")}` not being met."
|
92
|
+
output.puts "Type `install #{first_name}` to install the required gems and activate this command."
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
Array(names).each do |name|
|
97
|
+
commands[name] = Command.new(name, description, options, block)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Removes some commands from the set
|
102
|
+
# @param [Array<String>] names name of the commands to remove
|
103
|
+
def delete(*names)
|
104
|
+
names.each { |name| commands.delete name }
|
105
|
+
end
|
106
|
+
|
107
|
+
# Imports all the commands from one or more sets.
|
108
|
+
# @param [Array<CommandSet>] sets Command sets, all of the commands of which
|
109
|
+
# will be imported.
|
110
|
+
def import(*sets)
|
111
|
+
sets.each do |set|
|
112
|
+
commands.merge! set.commands
|
113
|
+
helper_module.send :include, set.helper_module
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# Imports some commands from a set
|
118
|
+
# @param [CommandSet] set Set to import commands from
|
119
|
+
# @param [Array<String>] names Commands to import
|
120
|
+
def import_from(set, *names)
|
121
|
+
helper_module.send :include, set.helper_module
|
122
|
+
names.each { |name| commands[name] = set.commands[name] }
|
123
|
+
end
|
124
|
+
|
125
|
+
# Aliases a command
|
126
|
+
# @param [String] new_name New name of the command.
|
127
|
+
# @param [String] old_name Old name of the command.
|
128
|
+
# @pasam [String, nil] desc New description of the command.
|
129
|
+
def alias_command(new_name, old_name, desc = nil)
|
130
|
+
commands[new_name] = commands[old_name].dup
|
131
|
+
commands[new_name].name = new_name
|
132
|
+
commands[new_name].description = desc if desc
|
133
|
+
end
|
134
|
+
|
135
|
+
# Runs a command.
|
136
|
+
# @param [Object] context Object which will be used as self during the
|
137
|
+
# command.
|
138
|
+
# @param [String] name Name of the command to be run
|
139
|
+
# @param [Array<Object>] args Arguments passed to the command
|
140
|
+
# @raise [NoCommandError] If the command is not defined in this set
|
141
|
+
def run_command(context, name, *args)
|
142
|
+
context.extend helper_module
|
143
|
+
|
144
|
+
if command = commands[name]
|
145
|
+
command.call(context, *args)
|
146
|
+
else
|
147
|
+
raise NoCommandError.new(name, self)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Sets the description for a command (replacing the old
|
152
|
+
# description.)
|
153
|
+
# @param [String] name The command name.
|
154
|
+
# @param [String] description The command description.
|
155
|
+
# @example
|
156
|
+
# MyCommands = Pry::CommandSet.new :test do
|
157
|
+
# desc "help", "help description"
|
158
|
+
# end
|
159
|
+
def desc(name, description)
|
160
|
+
commands[name].description = description
|
161
|
+
end
|
162
|
+
|
163
|
+
# Defines helpers methods for this command sets.
|
164
|
+
# Those helpers are only defined in this command set.
|
165
|
+
#
|
166
|
+
# @yield A block defining helper methods
|
167
|
+
# @example
|
168
|
+
# helpers do
|
169
|
+
# def hello
|
170
|
+
# puts "Hello!"
|
171
|
+
# end
|
172
|
+
#
|
173
|
+
# include OtherModule
|
174
|
+
# end
|
175
|
+
def helpers(&block)
|
176
|
+
helper_module.class_eval(&block)
|
177
|
+
end
|
178
|
+
|
179
|
+
private
|
180
|
+
def define_default_commands
|
181
|
+
command "help", "This menu." do |cmd|
|
182
|
+
if !cmd
|
183
|
+
output.puts
|
184
|
+
help_text = heading("Command List: ") + "\n"
|
185
|
+
|
186
|
+
commands.each do |key, command|
|
187
|
+
if command.description && !command.description.empty?
|
188
|
+
help_text << "#{key}".ljust(18) + command.description + "\n"
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
stagger_output(help_text)
|
193
|
+
else
|
194
|
+
if command = commands[cmd]
|
195
|
+
output.puts command.description
|
196
|
+
else
|
197
|
+
output.puts "No info for command: #{cmd}"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
command "install", "Install a disabled command." do |name|
|
203
|
+
stub_info = commands[name].options[:stub_info]
|
204
|
+
|
205
|
+
if !stub_info
|
206
|
+
output.puts "Not a command stub. Nothing to do."
|
207
|
+
next
|
208
|
+
end
|
209
|
+
|
210
|
+
output.puts "Attempting to install `#{name}` command..."
|
211
|
+
gems_to_install = Array(commands[name].options[:requires_gem])
|
212
|
+
|
213
|
+
gem_install_failed = false
|
214
|
+
gems_to_install.each do |g|
|
215
|
+
next if gem_installed?(g)
|
216
|
+
output.puts "Installing `#{g}` gem..."
|
217
|
+
|
218
|
+
begin
|
219
|
+
Gem::DependencyInstaller.new.install(g)
|
220
|
+
rescue Gem::GemNotFoundException
|
221
|
+
output.puts "Required Gem: `#{g}` not found. Aborting command installation."
|
222
|
+
gem_install_failed = true
|
223
|
+
next
|
224
|
+
end
|
225
|
+
end
|
226
|
+
next if gem_install_failed
|
227
|
+
|
228
|
+
Gem.refresh
|
229
|
+
commands[name].options.delete :stub_info
|
230
|
+
output.puts "Installation of `#{name}` successful! Type `help #{name}` for information"
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|