thor 0.13.3 → 0.13.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -3,6 +3,7 @@
3
3
  * Several bug fixes
4
4
  * Decoupled Thor::Group and Thor, so it's easier to vendor
5
5
  * Added check_unknown_options! in case you want error messages to be raised in valid switches.
6
+ * run(command) should return the results of command
6
7
 
7
8
  == 0.12, released 2010-01-02
8
9
 
data/lib/thor/actions.rb CHANGED
@@ -184,7 +184,7 @@ class Thor
184
184
  shell.padding -= 1 if verbose
185
185
  end
186
186
 
187
- # Executes a command.
187
+ # Executes a command returning the contents of the command.
188
188
  #
189
189
  # ==== Parameters
190
190
  # command<String>:: the command to be executed.
@@ -209,7 +209,7 @@ class Thor
209
209
  end
210
210
 
211
211
  say_status :run, desc, config.fetch(:verbose, true)
212
- system(command) unless options[:pretend]
212
+ `#{command}` unless options[:pretend]
213
213
  end
214
214
 
215
215
  # Executes a ruby script (taking into account WIN32 platform quirks).
@@ -220,7 +220,7 @@ class Thor
220
220
  #
221
221
  def run_ruby_script(command, config={})
222
222
  return unless behavior == :invoke
223
- run "#{command}", config.merge(:with => Thor::Util.ruby_command)
223
+ run command, config.merge(:with => Thor::Util.ruby_command)
224
224
  end
225
225
 
226
226
  # Run a thor command. A hash of options can be given and it's converted to
@@ -243,12 +243,13 @@ class Thor
243
243
  def thor(task, *args)
244
244
  config = args.last.is_a?(Hash) ? args.pop : {}
245
245
  verbose = config.key?(:verbose) ? config.delete(:verbose) : true
246
+ pretend = config.key?(:pretend) ? config.delete(:pretend) : false
246
247
 
247
248
  args.unshift task
248
249
  args.push Thor::Options.to_switches(config)
249
250
  command = args.join(' ').strip
250
251
 
251
- run command, :with => :thor, :verbose => verbose
252
+ run command, :with => :thor, :verbose => verbose, :pretend => pretend
252
253
  end
253
254
 
254
255
  protected
@@ -48,7 +48,7 @@ class Thor
48
48
  #
49
49
  def get(source, destination=nil, config={}, &block)
50
50
  source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\//
51
- render = File.binread(source)
51
+ render = open(source).binmode.read
52
52
 
53
53
  destination ||= if block_given?
54
54
  block.arity == 1 ? block.call(render) : block.call
data/lib/thor/group.rb CHANGED
@@ -253,22 +253,19 @@ class Thor::Group
253
253
  # Shortcut to invoke with padding and block handling. Use internally by
254
254
  # invoke and invoke_from_option class methods.
255
255
  def _invoke_for_class_method(klass, task=nil, *args, &block) #:nodoc:
256
- shell.padding += 1
257
-
258
- result = if block_given?
259
- case block.arity
260
- when 3
261
- block.call(self, klass, task)
262
- when 2
263
- block.call(self, klass)
264
- when 1
265
- instance_exec(klass, &block)
256
+ with_padding do
257
+ if block
258
+ case block.arity
259
+ when 3
260
+ block.call(self, klass, task)
261
+ when 2
262
+ block.call(self, klass)
263
+ when 1
264
+ instance_exec(klass, &block)
265
+ end
266
+ else
267
+ invoke klass, task, *args
266
268
  end
267
- else
268
- invoke klass, task, *args
269
269
  end
270
-
271
- shell.padding -= 1
272
- result
273
270
  end
274
271
  end
@@ -119,6 +119,11 @@ class Thor
119
119
  end
120
120
  end
121
121
 
122
+ # Invokes using shell padding.
123
+ def invoke_with_padding(*args)
124
+ with_padding { invoke(*args) }
125
+ end
126
+
122
127
  protected
123
128
 
124
129
  # Configuration values that are shared between invocations.
@@ -51,6 +51,10 @@ class Thor
51
51
 
52
52
  private
53
53
 
54
+ def last?
55
+ @pile.empty?
56
+ end
57
+
54
58
  def peek
55
59
  @pile.first
56
60
  end
@@ -3,9 +3,9 @@ class Thor
3
3
  # under Ruby's license.
4
4
  #
5
5
  class Options < Arguments #:nodoc:
6
- LONG_RE = /^(--\w+[-\w+]*)$/
6
+ LONG_RE = /^(--\w+(?:-\w+)*)$/
7
7
  SHORT_RE = /^(-[a-z])$/i
8
- EQ_RE = /^(--\w+[-\w+]*|-[a-z])=(.*)$/i
8
+ EQ_RE = /^(--\w+(?:-\w+)*|-[a-z])=(.*)$/i
9
9
  SHORT_SQ_RE = /^-([a-z]{2,})$/i # Allow either -x -v or -xv style for single char args
10
10
  SHORT_NUM = /^(-[a-z])#{NUMERIC}$/i
11
11
 
@@ -68,7 +68,7 @@ class Thor
68
68
  switch = normalize_switch(switch)
69
69
  option = switch_option(switch)
70
70
  @assigns[option.human_name] = parse_peek(switch, option)
71
- elsif peek =~ /^\-/
71
+ elsif current_is_switch_formatted?
72
72
  @unknown << shift
73
73
  else
74
74
  shift
@@ -99,6 +99,19 @@ class Thor
99
99
  end
100
100
  end
101
101
 
102
+ def switch_formatted?(arg)
103
+ case arg
104
+ when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
105
+ true
106
+ else
107
+ false
108
+ end
109
+ end
110
+
111
+ def current_is_switch_formatted?
112
+ switch_formatted? peek
113
+ end
114
+
102
115
  def switch?(arg)
103
116
  switch_option(arg) || @shorts.key?(arg)
104
117
  end
@@ -135,13 +148,14 @@ class Thor
135
148
  # Parse the value at the peek analyzing if it requires an input or not.
136
149
  #
137
150
  def parse_peek(switch, option)
138
- unless current_is_value?
151
+ if current_is_switch_formatted? || last?
139
152
  if option.boolean?
140
153
  # No problem for boolean types
141
154
  elsif no_or_skip?(switch)
142
155
  return nil # User set value to nil
143
156
  elsif option.string? && !option.required?
144
- return option.human_name # Return the option name
157
+ # Return the default if there is one, else the human name
158
+ return option.default || option.human_name
145
159
  else
146
160
  raise MalformattedArgumentError, "No value provided for option '#{switch}'"
147
161
  end
data/lib/thor/shell.rb CHANGED
@@ -45,19 +45,16 @@ class Thor
45
45
 
46
46
  # Holds the shell for the given Thor instance. If no shell is given,
47
47
  # it gets a default shell from Thor::Base.shell.
48
- #
49
48
  def shell
50
49
  @shell ||= Thor::Base.shell.new
51
50
  end
52
51
 
53
52
  # Sets the shell for this thor class.
54
- #
55
53
  def shell=(shell)
56
54
  @shell = shell
57
55
  end
58
56
 
59
57
  # Common methods that are delegated to the shell.
60
- #
61
58
  SHELL_DELEGATED_METHODS.each do |method|
62
59
  module_eval <<-METHOD, __FILE__, __LINE__
63
60
  def #{method}(*args)
@@ -66,6 +63,14 @@ class Thor
66
63
  METHOD
67
64
  end
68
65
 
66
+ # Yields the given block with padding.
67
+ def with_padding
68
+ shell.padding += 1
69
+ yield
70
+ ensure
71
+ shell.padding -= 1
72
+ end
73
+
69
74
  protected
70
75
 
71
76
  # Allow shell to be shared between invocations.
data/lib/thor/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Thor
2
- VERSION = "0.13.3".freeze
2
+ VERSION = "0.13.4".freeze
3
3
  end
@@ -105,6 +105,15 @@ describe Thor::Actions do
105
105
  it "logs status" do
106
106
  action(:get, "doc/README", "docs/README").must == " create docs/README\n"
107
107
  end
108
+
109
+ it "accepts http remote sources" do
110
+ body = "__start__\nHTTPFILE\n__end__\n"
111
+ FakeWeb.register_uri(:get, 'http://example.com/file.txt', :body => body)
112
+ action :get, 'http://example.com/file.txt' do |content|
113
+ content.must == body
114
+ end
115
+ FakeWeb.clean_registry
116
+ end
108
117
  end
109
118
 
110
119
  describe "#template" do
data/spec/actions_spec.rb CHANGED
@@ -220,7 +220,7 @@ describe Thor::Actions do
220
220
 
221
221
  describe "#run" do
222
222
  before(:each) do
223
- runner.should_receive(:system).with("ls")
223
+ runner.should_receive(:`).with("ls")
224
224
  end
225
225
 
226
226
  it "executes the command given" do
@@ -244,7 +244,7 @@ describe Thor::Actions do
244
244
  describe "#run_ruby_script" do
245
245
  before(:each) do
246
246
  Thor::Util.stub!(:ruby_command).and_return("/opt/jruby")
247
- runner.should_receive(:system).with("/opt/jruby script.rb")
247
+ runner.should_receive(:`).with("/opt/jruby script.rb")
248
248
  end
249
249
 
250
250
  it "executes the ruby script" do
@@ -262,30 +262,30 @@ describe Thor::Actions do
262
262
 
263
263
  describe "#thor" do
264
264
  it "executes the thor command" do
265
- runner.should_receive(:system).with("thor list")
265
+ runner.should_receive(:`).with("thor list")
266
266
  action :thor, :list, :verbose => true
267
267
  end
268
268
 
269
269
  it "converts extra arguments to command arguments" do
270
- runner.should_receive(:system).with("thor list foo bar")
270
+ runner.should_receive(:`).with("thor list foo bar")
271
271
  action :thor, :list, "foo", "bar"
272
272
  end
273
273
 
274
274
  it "converts options hash to switches" do
275
- runner.should_receive(:system).with("thor list foo bar --foo")
275
+ runner.should_receive(:`).with("thor list foo bar --foo")
276
276
  action :thor, :list, "foo", "bar", :foo => true
277
277
 
278
- runner.should_receive(:system).with("thor list --foo 1 2 3")
278
+ runner.should_receive(:`).with("thor list --foo 1 2 3")
279
279
  action :thor, :list, :foo => [1,2,3]
280
280
  end
281
281
 
282
282
  it "logs status" do
283
- runner.should_receive(:system).with("thor list")
283
+ runner.should_receive(:`).with("thor list")
284
284
  action(:thor, :list).must == " run thor list from \".\"\n"
285
285
  end
286
286
 
287
287
  it "does not log status if required" do
288
- runner.should_receive(:system).with("thor list --foo 1 2 3")
288
+ runner.should_receive(:`).with("thor list --foo 1 2 3")
289
289
  action(:thor, :list, :foo => [1,2,3], :verbose => false).must be_empty
290
290
  end
291
291
  end
@@ -103,6 +103,18 @@ describe Thor::Options do
103
103
  parse("--bar", "baz", "--baz", "unknown")
104
104
  lambda { check_unknown! }.must raise_error(Thor::UnknownArgumentError, "Unknown switches '--baz'")
105
105
  end
106
+
107
+ it "skips leading non-switches" do
108
+ create(:foo => "baz")
109
+
110
+ parse("asdf", "--foo", "bar").must == {"foo" => "bar"}
111
+ end
112
+
113
+ it "correctly recognizes things that look kind of like options, but aren't, as not options" do
114
+ create(:foo => "baz")
115
+ parse("--asdf---asdf", "baz", "--foo", "--asdf---dsf--asdf").must == {"foo" => "--asdf---dsf--asdf"}
116
+ check_unknown!
117
+ end
106
118
 
107
119
  describe "with no input" do
108
120
  it "and no switches returns an empty hash" do
@@ -165,9 +177,14 @@ describe Thor::Options do
165
177
  end
166
178
 
167
179
  it "accepts a --switch format on non required types" do
168
- create "--foo" => "bar"
180
+ create "--foo" => :string
169
181
  parse("--foo")["foo"].must == "foo"
170
182
  end
183
+
184
+ it "accepts a --switch format on non required types with default values" do
185
+ create "--baz" => :string, "--foo" => "bar"
186
+ parse("--baz", "bang", "--foo")["foo"].must == "bar"
187
+ end
171
188
 
172
189
  it "overwrites earlier values with later values" do
173
190
  parse("--foo=bar", "--foo", "12")["foo"].must == "12"
data/spec/shell_spec.rb CHANGED
@@ -22,4 +22,13 @@ describe Thor::Shell do
22
22
  MyCounter.new([1,2]).shell.must be_kind_of(Thor::Base.shell)
23
23
  end
24
24
  end
25
+
26
+ describe "with_padding" do
27
+ it "uses padding for inside block outputs" do
28
+ base = MyCounter.new([1,2])
29
+ base.with_padding do
30
+ capture(:stdout){ base.say_status :padding, "cool" }.strip.must == "padding cool"
31
+ end
32
+ end
33
+ end
25
34
  end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,7 @@ require 'stringio'
8
8
  require 'rubygems'
9
9
  require 'rdoc'
10
10
  require 'diff/lcs' # You need diff/lcs installed to run specs (but not to run Thor).
11
+ require 'fakeweb' # You need fakeweb installed to run specs (but not to run Thor).
11
12
 
12
13
  $thor_runner = true
13
14
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.3
4
+ version: 0.13.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-17 00:00:00 +01:00
13
+ date: 2010-02-26 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16