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.
@@ -1,202 +0,0 @@
1
- require 'rubygems/dependency_installer'
2
- require "pry/command_base_helpers"
3
-
4
- class Pry
5
-
6
- # Basic command functionality. All user-defined commands must
7
- # inherit from this class. It provides the `command` method.
8
- class CommandBase
9
- class << self
10
- include CommandBaseHelpers
11
-
12
- attr_accessor :commands
13
- attr_accessor :opts, :output, :target
14
-
15
- # private because we want to force function style invocation. We require
16
- # that the location where the block is defined has the `opts`
17
- # method in scope.
18
- private
19
-
20
- # Defines a new Pry command.
21
- # @param [String, Array] names The name of the command (or array of
22
- # command name aliases).
23
- # @param [String] description A description of the command.
24
- # @param [Hash] options The optional configuration parameters.
25
- # @option options [Boolean] :keep_retval Whether or not to use return value
26
- # of the block for return of `command` or just to return `nil`
27
- # (the default).
28
- # @yield The action to perform. The parameters in the block
29
- # determines the parameters the command will receive. All
30
- # parameters passed into the block will be strings. Successive
31
- # command parameters are separated by whitespace at the Pry prompt.
32
- # @example
33
- # class MyCommands < Pry::CommandBase
34
- # command "greet", "Greet somebody" do |name|
35
- # puts "Good afternoon #{name.capitalize}!"
36
- # end
37
- # end
38
- #
39
- # # From pry:
40
- # # pry(main)> _pry_.commands = MyCommands
41
- # # pry(main)> greet john
42
- # # Good afternoon John!
43
- # # pry(main)> help greet
44
- # # Greet somebody
45
- def command(names, description="No description.", options={}, &block)
46
- options = {
47
- :keep_retval => false,
48
- :requires_gem => nil
49
- }.merge!(options)
50
-
51
- @commands ||= {}
52
-
53
- if command_dependencies_met?(options)
54
- Array(names).each do |name|
55
- commands[name] = {
56
- :description => description,
57
- :action => block,
58
- :keep_retval => options[:keep_retval]
59
- }
60
- end
61
- else
62
- create_command_stub(names, description, options, block)
63
- end
64
- end
65
-
66
- # Delete a command or an array of commands.
67
- # Useful when inheriting from another command set and pruning
68
- # those commands down to the ones you want.
69
- # @param [Array<String>] names The command name or array
70
- # of command names you want to delete
71
- # @example Deleteing inherited commands
72
- # class MyCommands < Pry::Commands
73
- # delete "show_method", "show_imethod", "show_doc", "show_idoc"
74
- # end
75
- # Pry.commands = MyCommands
76
- def delete(*names)
77
- names.each { |name| commands.delete(name) }
78
- end
79
-
80
- # Execute a command (this enables commands to call other commands).
81
- # @param [String] name The command to execute
82
- # @param [Array] args The parameters to pass to the command.
83
- # @example Wrap one command with another
84
- # class MyCommands < Pry::Commands
85
- # command "ls2" do
86
- # output.puts "before ls"
87
- # run "ls"
88
- # output.puts "after ls"
89
- # end
90
- # end
91
- def run(name, *args)
92
- command_processor = CommandProcessor.new(target.eval('_pry_'))
93
-
94
- if command_processor.system_command?(name)
95
- command_processor.execute_system_command("#{name} #{args.join(' ')}", target)
96
- else
97
- raise "#{name.inspect} is not a valid pry command." unless opts[:commands].include? name
98
- action = opts[:commands][name][:action]
99
- instance_exec(*args, &action)
100
- end
101
- end
102
-
103
- # Import commands from another command object.
104
- # @param [Pry::CommandBase] klass The class to import from (must
105
- # be a subclass of `Pry::CommandBase`)
106
- # @param [Array<String>] names The commands to import.
107
- # @example
108
- # class MyCommands < Pry::CommandBase
109
- # import_from Pry::Commands, "ls", "show_method", "cd"
110
- # end
111
- def import_from(klass, *names)
112
- imported_hash = Hash[klass.commands.select { |k, v| names.include?(k) }]
113
- commands.merge!(imported_hash)
114
- end
115
-
116
- # Create an alias for a command.
117
- # @param [String] new_command The alias name.
118
- # @param [String] orig_command The original command name.
119
- # @param [String] desc The optional description.
120
- # @example
121
- # class MyCommands < Pry::CommandBase
122
- # alias_command "help_alias", "help"
123
- # end
124
- def alias_command(new_command_name, orig_command_name, desc=nil)
125
- commands[new_command_name] = commands[orig_command_name].dup
126
- commands[new_command_name][:description] = desc if desc
127
- end
128
-
129
- # Set the description for a command (replacing the old
130
- # description.)
131
- # @param [String] name The command name.
132
- # @param [String] description The command description.
133
- # @example
134
- # class MyCommands < Pry::CommandBase
135
- # desc "help", "help description"
136
- # end
137
- def desc(name, description)
138
- commands[name][:description] = description
139
- end
140
- end
141
-
142
- command "help", "This menu." do |cmd|
143
- command_info = opts[:commands]
144
-
145
- if !cmd
146
- output.puts
147
- help_text = heading("Command List:") + "\n"
148
- command_info.each do |k, data|
149
- if !data[:stub_info]
150
- help_text << ("#{k}".ljust(18) + data[:description] + "\n") if !data[:description].empty?
151
- else
152
- help_text << (bold("#{k}".ljust(18) + data[:description] + "\n")) if !data[:description].empty?
153
- end
154
- end
155
- stagger_output(help_text)
156
- else
157
- if command_info[cmd]
158
- output.puts command_info[cmd][:description]
159
- else
160
- output.puts "No info for command: #{cmd}"
161
- end
162
- end
163
- end
164
-
165
- command "install", "Install a disabled command." do |name|
166
- stub_info = commands[name][:stub_info]
167
-
168
- if !stub_info
169
- output.puts "Not a command stub. Nothing to do."
170
- next
171
- end
172
-
173
- output.puts "Attempting to install `#{name}` command..."
174
- gems_to_install = Array(stub_info[:requires_gem])
175
-
176
- gem_install_failed = false
177
- gems_to_install.each do |g|
178
- next if gem_installed?(g)
179
- output.puts "Installing `#{g}` gem..."
180
-
181
- begin
182
- Gem::DependencyInstaller.new.install(g)
183
- rescue Gem::GemNotFoundException
184
- output.puts "Required Gem: `#{g}` not found. Aborting command installation."
185
- gem_install_failed = true
186
- next
187
- end
188
- end
189
- next if gem_install_failed
190
-
191
- Gem.refresh
192
- load "#{File.dirname(__FILE__)}/commands.rb"
193
- output.puts "Installation of `#{name}` successful! Type `help #{name}` for information"
194
- end
195
-
196
- # Ensures that commands can be inherited
197
- def self.inherited(klass)
198
- klass.commands = commands.dup
199
- end
200
-
201
- end
202
- end