jashmenn-git-style-binaries 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -9,6 +9,12 @@ This gem uses [`trollop`](http://trollop.rubyforge.org/) for option parsing
9
9
 
10
10
  gem install jashmenn-git-style-binaries --source=http://gems.github.com
11
11
 
12
+ ## Try it out
13
+
14
+ cd `gem env gemdir`/gems/jashmenn-git-style-binaries-0.1.4/test/fixtures
15
+ ./wordpress -h
16
+ ./wordpress help post
17
+
12
18
  ## Goal
13
19
 
14
20
  Lets use the imaginary `wordpress` gem. Let's say we have three different
@@ -197,6 +203,12 @@ Option parsing is done by [trollop](http://trollop.rubyforge.org/).
197
203
  documentation](http://trollop.rubyforge.org/) for information on how to setup
198
204
  the options and flags.
199
205
 
206
+ ## Callbacks
207
+
208
+ Callbacks are available on the primary and subcommands. Available callbacks currently
209
+ are before/after_run. These execute before the run block of the command parser and take
210
+ take one argument, which is the command itself
211
+
200
212
  ## The `run` block
201
213
 
202
214
  To get the 'introspection' on the individual binaries every binary is `load`ed
@@ -234,6 +246,9 @@ Play with the examples in the `test/fixtures` directory.
234
246
  * A few places of really ugly code
235
247
  * A feeling that this could be done in 1/2 lines of code
236
248
 
249
+ ## Authors
250
+ By Nate Murray and Ari Lerner
251
+
237
252
  ## Copyright
238
253
 
239
254
  The MIT License
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 4
4
+ :patch: 5
@@ -31,7 +31,7 @@ module GitStyleBinary
31
31
  version "#{version_string} (c) #{Time.now.year}"
32
32
  banner <<-EOS
33
33
  #{"SYNOPSIS".colorize(:red)}
34
- #{command.full_name.colorize(:blue)} #{all_options_string}
34
+ #{command.full_name.colorize(:light_blue)} #{all_options_string}
35
35
 
36
36
  #{"SUBCOMMANDS".colorize(:red)}
37
37
  \#{GitStyleBinary.pretty_known_subcommands.join("\n ")}
@@ -67,11 +67,11 @@ module GitStyleBinary
67
67
  end
68
68
 
69
69
  def run
70
- GitStyleBinary.load_primary unless is_primary?
70
+ GitStyleBinary.load_primary unless is_primary?
71
71
  GitStyleBinary.load_subcommand if is_primary? && running_subcommand?
72
72
  load_all_parser_constraints
73
73
  @opts = process_args_with_subcmd
74
- call_parser_run_block
74
+ call_parser_run_block
75
75
  self
76
76
  end
77
77
 
@@ -109,7 +109,10 @@ module GitStyleBinary
109
109
 
110
110
  def call_parser_run_block
111
111
  runs = GitStyleBinary.current_command.parser.runs
112
+
113
+ parser.run_callbacks(:before_run, self)
112
114
  parser.runs.last.call(self) # ... not too happy with this
115
+ parser.run_callbacks(:after_run, self)
113
116
  end
114
117
 
115
118
  def process_args_with_subcmd(args = ARGV, *a, &b)
@@ -181,6 +184,11 @@ module GitStyleBinary
181
184
  Trollop.instance_eval { @p = p }
182
185
  Trollop::die(arg, msg)
183
186
  end
187
+
188
+ # Helper to return the option
189
+ def [](k)
190
+ opts[k]
191
+ end
184
192
 
185
193
  end
186
194
 
@@ -69,7 +69,7 @@ module Helpers
69
69
  GitStyleBinary.known_commands.collect do |k,cmd|
70
70
  next if k == basename
71
71
  cmd.process_parser!
72
- ("%-s%s%-10s" % [basename, '-', k]).colorize(:blue) + "\n " + ("%s" % [cmd.short_desc]) + "\n"
72
+ ("%-s%s%-10s" % [basename, '-', k]).colorize(:light_blue) + "\n " + ("%s" % [cmd.short_desc]) + "\n"
73
73
  end.compact.sort
74
74
  end
75
75
 
@@ -1,12 +1,27 @@
1
1
  module GitStyleBinary
2
2
  class Parser < Trollop::Parser
3
- attr_reader :runs
3
+ attr_reader :runs, :callbacks
4
4
  attr_reader :short_desc
5
5
  attr_accessor :command
6
6
 
7
7
  def initialize *a, &b
8
8
  super
9
9
  @runs = []
10
+ setup_callbacks
11
+ end
12
+
13
+ def setup_callbacks
14
+ @callbacks = {}
15
+ %w(run).each do |event|
16
+ %w(before after).each do |time|
17
+ @callbacks["#{time}_#{event}".to_sym] = []
18
+ instance_eval "def #{time}_#{event}(&block);@callbacks[:#{time}_#{event}] << block;end"
19
+ end
20
+ end
21
+ end
22
+
23
+ def run_callbacks(at, from)
24
+ @callbacks[at].each {|c| c.call(from) }
10
25
  end
11
26
 
12
27
  def banner s=nil; @banner = s if s; @banner end
@@ -175,7 +190,7 @@ class Parser < Trollop::Parser
175
190
 
176
191
  def colorize_known_words(txt)
177
192
  txt = txt.gsub(/^([A-Z]+\s*)$/, '\1'.colorize(:red)) # all caps words on their own line
178
- txt = txt.gsub(/\b(#{bin_name})\b/, '\1'.colorize(:blue)) # the current command name
193
+ txt = txt.gsub(/\b(#{bin_name})\b/, '\1'.colorize(:light_blue)) # the current command name
179
194
  txt = txt.gsub(/\[([^\s]+)\]/, "[".colorize(:magenta) + '\1'.colorize(:green) + "]".colorize(:magenta)) # synopsis options
180
195
  end
181
196
 
@@ -199,6 +214,10 @@ class Parser < Trollop::Parser
199
214
  def run(&block)
200
215
  @runs << block
201
216
  end
217
+
218
+ def action(name = :action, &block)
219
+ block.call(self) if block
220
+ end
202
221
 
203
222
  end
204
223
  end
@@ -5,6 +5,18 @@ require 'git-style-binary/command'
5
5
  GitStyleBinary.primary do
6
6
  version "0.0.1 (c) 2009 Nate Murray - local"
7
7
  opt :test_primary, "test an option on the primary", :type => String
8
+
9
+ action do
10
+ @categories = ["sports", "news"]
11
+ end
12
+
13
+ before_run do |cmd|
14
+ puts "before_run command #{cmd}"
15
+ end
16
+
17
+ after_run do |cmd|
18
+ puts "after_run command #{cmd}"
19
+ end
8
20
 
9
21
  run do |command|
10
22
  puts "Primary Options: #{command.opts.inspect}"
@@ -13,5 +13,6 @@ Does something with categories
13
13
  EOS
14
14
  run do |command|
15
15
  puts "does something with categories"
16
+ puts @categories.join(" ")
16
17
  end
17
18
  end
@@ -12,5 +12,6 @@ class CommandTest < Test::Unit::TestCase
12
12
  @c.constraints << "foo"
13
13
  assert_equal @c.constraints, ["foo"]
14
14
  end
15
+
15
16
  end
16
17
  end
@@ -62,6 +62,45 @@ class RunningBinariesTest < Test::Unit::TestCase
62
62
  end
63
63
  should "be able to require 'primary' and run just fine"
64
64
  end
65
+
66
+ context "when running with an action" do
67
+ # should be the same for both formats
68
+ ["wordpress-categories", "wordpress categories"].each do |bin_format|
69
+ context "#{bin_format}" do
70
+
71
+ context "with action block" do
72
+ setup { @stdout, @stderr = bin("#{bin_format}") }
73
+ should "have the parsed action items in the help output" do
74
+ output_matches /sports news/m
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ context "callbacks" do
82
+ context "on a binary" do
83
+ setup { @stdout, @stderr = bin("wordpress") }
84
+
85
+ %w(before after).each do |time|
86
+ should "run the callback #{time}_run}" do
87
+ assert @stdout.match /#{time}_run command/
88
+ end
89
+ end
90
+ end
91
+
92
+ context "on help" do
93
+ setup { @stdout, @stderr = bin("wordpress -h") }
94
+
95
+ %w(before after).each do |time|
96
+ should "not run the callback #{time}_run" do
97
+ assert_nil @stdout.match /#{time}_run command/
98
+ end
99
+ end
100
+ end
101
+
102
+ end
103
+
65
104
 
66
105
  context "when running the subcommand" do
67
106
  # should be the same for both formats
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jashmenn-git-style-binaries
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Murray
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-08 00:00:00 -07:00
12
+ date: 2009-05-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency