forked from aslakhellesoy/pickler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.rb
517 lines (455 loc) · 13.2 KB
/
runner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
require 'optparse'
class Pickler
class Runner
class Base
attr_reader :argv
def initialize(argv)
@argv = argv
@tty = $stdout.tty?
@opts = OptionParser.new
@opts.version = "0.0"
@opts.banner = "Usage: pickler #{self.class.command_name} #{self.class.banner_arguments}"
@opts.base.long["help"] = OptionParser::Switch::NoArgument.new do
help = @opts.help.chomp.chomp + "\n"
help += "\n#{self.class.description}" if self.class.description
puts help
@exit = 0
end
@opts.separator("")
end
def self.options
@options ||= []
end
def self.on(*args, &block)
options << args
define_method("option_#{args.object_id}", &block)
end
def self.banner_arguments(value = nil)
if value
@banner_arguments = value
else
@banner_arguments || (arity.zero? ? "" : "...")
end
end
def self.summary(value = nil)
if value
@summary = value
else
@summary
end
end
def self.description(value = nil)
if value
@description = value
else
@description || "#@summary."
end
end
def self.command_name
name.split('::').last.gsub(/(.)([A-Z])/) {"#$1-#$2"}.downcase
end
def self.method_name
command_name.gsub('-','_')
end
def self.process(&block)
define_method(:process, &block)
end
def self.arity
instance_method(:process).arity
end
def arity
self.class.arity
end
def pickler
@pickler ||= Pickler.new(Dir.getwd)
end
def abort(message)
raise Error, message
end
def too_many
abort "too many arguments"
end
def run
self.class.options.each do |arguments|
@opts.on(*arguments, &method("option_#{arguments.object_id}"))
end
begin
@opts.parse!(@argv)
rescue OptionParser::InvalidOption
abort $!.message
end
return @exit if @exit
minimum = arity < 0 ? -1 - arity : arity
if arity >= 0 && arity < @argv.size
too_many
elsif minimum > @argv.size
abort "not enough arguments"
end
process(*@argv)
end
def process(*argv)
pickler.send(self.class.method_name,*argv)
end
def color?
case pickler.config["color"]
when "always" then true
when "never" then false
else
@tty && RUBY_PLATFORM !~ /mswin|mingw/
end
end
def colorize(code, string)
if color?
"\e[#{code}m#{string}\e[00m"
else
string.to_s
end
end
def puts_summary(story)
summary = "%6d " % story.id
type = story.estimate || TYPE_SYMBOLS[story.story_type]
state = STATE_SYMBOLS[story.current_state]
summary << colorize("3#{STATE_COLORS[story.current_state]}", state) << ' '
summary << colorize("01;3#{TYPE_COLORS[story.story_type]}", type) << ' '
summary << story.name
puts summary
end
def puts_full(story)
puts colorize("01;3#{TYPE_COLORS[story.story_type]}", story.name)
puts "Type: #{story.story_type}".rstrip
if story.story_type == "release"
puts "Deadline: #{story.deadline}".rstrip
else
puts "Estimate: #{story.estimate}".rstrip
end
puts "State: #{story.current_state}".rstrip
puts "Labels: #{story.labels.join(', ')}".rstrip
puts "Requester: #{story.requested_by}".rstrip
puts "Owner: #{story.owned_by}".rstrip
puts "URL: #{story.url}".rstrip
puts unless story.description.blank?
story.description_lines.each do |line|
puts " #{line}".rstrip
end
story.notes.each do |note|
puts
puts " #{colorize('01', note.author)} (#{note.date})"
puts *note.lines(72).map {|l| " #{l}".rstrip}
end
end
def paginated_output
stdout = $stdout
if @tty && pager = pickler.config["pager"]
# Modeled after git
ENV["LESS"] ||= "FRSX"
IO.popen(pager,"w") do |io|
$stdout = io
yield
end
else
yield
end
rescue Errno::EPIPE
ensure
$stdout = stdout
end
end
def self.[](command)
klass_name = command.to_s.capitalize.gsub(/[-_](.)/) { $1.upcase }
if klass_name =~ /^[A-Z]\w*$/ && const_defined?(klass_name)
klass = const_get(klass_name)
if Class === klass && klass < Base
return klass
end
end
end
def self.commands
constants.map {|c| Runner.const_get(c)}.select {|c| Class === c && c < Runner::Base}.sort_by {|r| r.command_name}.uniq
end
def self.command(name, &block)
const_set(name.to_s.capitalize.gsub(/[-_](.)/) { $1.upcase },Class.new(Base,&block))
end
command :show do
banner_arguments "<story>"
summary "Show details for a story"
on "--full", "default format" do
@format = :full
end
on "--raw", "same as the .feature" do
@format = :raw
end
process do |*args|
case args.size
when 0
puts "#{pickler.project_id} #{pickler.project.name}"
when 1
feature = pickler.feature(args.first)
story = feature.story
case @format
when :raw
puts feature.story.to_s(pickler.format) if feature.story
else
paginated_output do
puts_full feature.story
end
end
else
too_many
end
end
end
command :search do
banner_arguments "[query]"
summary "List all stories matching a query"
def modifications
@modifications ||= {}
end
[:label, :type, :state].each do |o|
on "--#{o} #{o.to_s.upcase}" do |value|
modifications[o] = value
end
end
[:requester, :owner, :mywork].each do |o|
on "--#{o}[=USERNAME]" do |value|
modifications[o] = value || pickler.real_name
end
end
on "--[no-]includedone", "include accepted stories" do |value|
modifications[:includedone] = value
@iterations ||= []
@iterations << :done?
end
on "-b", "--backlog", "filter results to future iterations" do |c|
@iterations ||= []
@iterations << :backlog?
end
on "-c", "--current", "filter results to current iteration" do |b|
@iterations ||= []
@iterations << :current?
end
on "--[no-]full", "show full story, not a summary line" do |b|
@full = b
end
process do |*argv|
argv << modifications unless modifications.empty?
if argv == [{:includedone => true}]
# Bypass the 200 search results limitation
stories = pickler.project.stories
else
stories = pickler.project.stories(*argv)
end
if @iterations && @iterations != [:done?]
stories.reject! {|s| !@iterations.any? {|i| s.send(i)}}
end
paginated_output do
first = true
stories.each do |story|
if @full
puts unless first
puts_full story
else
puts_summary story
end
first = false
end
end
end
end
command :push do
banner_arguments "[story] ..."
summary "Upload stories"
description <<-EOF
Upload the given story or all features with a tracker url in a comment on the
first line. Features with a blank comment in the first line will created as
new stories.
EOF
process do |*args|
args.replace(pickler.local_features) if args.empty?
args.each do |arg|
pickler.feature(arg).push
end
end
end
command :pull do
banner_arguments "[story] ..."
summary "Download stories"
description <<-EOF
Download the given story or all well formed stories to the features/ directory.
EOF
process do |*args|
args.replace(pickler.scenario_features) if args.empty?
args.each do |arg|
pickler.feature(arg).pull
end
end
end
command :start do
banner_arguments "<story> [basename]"
summary "Pull a story and mark it started"
description <<-EOF
Pull a given story and change its state to started. If basename is given
and no local file exists, features/basename.feature will be created. Give a
basename of "-" to use a downcased, underscored version of the story name as
the basename.
EOF
process do |story, *args|
pickler.feature(story).start(args.first)
end
end
command :finish do
banner_arguments "<story>"
summary "Push a story and mark it finished"
process do |story|
pickler.feature(story).finish
end
end
command :deliver do
banner_arguments "[story] ..."
summary "Mark stories delivered"
on "--all-finished", "deliver all finished stories" do
@all = true
end
process do |*args|
if @all
pickler.deliver_all_finished_stories
end
args.each do |arg|
pickler.story(arg).transition!('delivered')
end
end
end
command :unstart do
banner_arguments "[story] ..."
summary "Mark stories unstarted"
on "--all-started", "unstart all started stories" do
@all = true
end
process do |*args|
if @all
pickler.project.stories(:state => "started").each do |story|
story.transition!('unstarted')
end
end
args.each do |arg|
pickler.story(arg).transition!('unstarted')
end
end
end
command :unschedule do
banner_arguments "[story] ..."
summary "Move stories to icebox"
process do |*args|
args.each do |arg|
pickler.story(arg).transition!('unscheduled')
end
end
end
command :browse do
banner_arguments "[story]"
summary "Open a story in the web browser"
description <<-EOF
Open project or a story in the web browser.
Requires launchy (gem install launchy).
EOF
on "--dashboard" do
@special = "dashboard"
end
on "--faq" do
@special = "help"
end
on "--profile", "get your API Token here" do
@special = "profile"
end
on "--time", "not publicly available" do
@special = "time_shifts?project=#{pickler.project_id}"
end
process do |*args|
too_many if args.size > 1 || @special && args.first
if args.first
url = pickler.story(args.first).url
elsif @special
url = "http://www.pivotaltracker.com/#@special"
else
url = "http://www.pivotaltracker.com/projects/#{pickler.project_id}/stories"
end
require 'launchy'
Launchy.open(url)
end
end
command :comment do
banner_arguments "<story> <paragraph> ..."
summary "Post a comment to a story"
process do |story, *paragraphs|
pickler.story(story).comment!(paragraphs.join("\n\n"))
end
end
def initialize(argv)
@argv = argv
end
COLORS = {
:black => 0,
:red => 1,
:green => 2,
:yellow => 3,
:blue => 4,
:magenta => 5,
:cyan => 6,
:white => 7
}
STATE_COLORS = {
nil => COLORS[:black],
"rejected" => COLORS[:red],
"accepted" => COLORS[:green],
"delivered" => COLORS[:yellow],
"unscheduled" => COLORS[:white],
"started" => COLORS[:magenta],
"finished" => COLORS[:cyan],
"unstarted" => COLORS[:blue]
}
STATE_SYMBOLS = {
"unscheduled" => " ",
"unstarted" => ":|",
"started" => ":/",
"finished" => ":)",
"delivered" => ";)",
"rejected" => ":(",
"accepted" => ":D"
}
TYPE_COLORS = {
'chore' => COLORS[:blue],
'feature' => COLORS[:magenta],
'bug' => COLORS[:red],
'release' => COLORS[:cyan]
}
TYPE_SYMBOLS = {
"feature" => "*",
"chore" => "%",
"release" => "!",
"bug" => "/"
}
def run
command = @argv.shift
if klass = self.class[command]
result = klass.new(@argv).run
exit result.respond_to?(:to_int) ? result.to_int : 0
elsif ['help', '--help', '-h', '', nil].include?(command)
puts "usage: pickler <command> [options] [arguments]"
puts
puts "Commands:"
self.class.commands.each do |command|
puts " %-19s %s" % [command.command_name, command.summary]
end
puts
puts "Run pickler <command> --help for help with a given command"
else
raise Error, "Unknown pickler command #{command}"
end
rescue Pickler::Error
$stderr.puts "#$!"
exit 1
rescue Interrupt
$stderr.puts "Interrupted!"
exit 130
end
end
end