stack_loop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc67ea44a103f6c3d7321520c5e513711bdf3483
4
+ data.tar.gz: 9eed716be05d7546f0d9b924f7a66a561bb131e1
5
+ SHA512:
6
+ metadata.gz: 2219b1e2e8138275698578f17b4335af98f76c954aeb361cf15766555334b05aa266d21edd6f0c743ae01b2c892fb1b9ebeff9d7e17f8038d530d2eba5e1350b
7
+ data.tar.gz: 738046750d39cb3ab65c879283029d121d4f6f81c8d6fdeab1a58298c22b2e3bf0e15ae69382d59682180a061e3cf833a67e6e5ab2c544211dca3e1c5c8083f0
data/bin/stack_loop ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'stack_loop/app'
4
+ require 'stack_loop/optparse'
5
+
6
+ app = StackLoop::App.new
7
+
8
+ parser = StackLoop::OptParse.new(app)
9
+ parser.parse(ARGV)
10
+
11
+ app.run
@@ -0,0 +1,156 @@
1
+ require 'abbrev'
2
+
3
+ module StackLoop
4
+ class App
5
+ class Command
6
+ attr_accessor :name, :desc
7
+
8
+ def initialize(name, desc=nil, &action)
9
+ @name = name
10
+ @desc = desc
11
+ @action = action
12
+ end
13
+
14
+ def run
15
+ @action.call
16
+ end
17
+ end
18
+
19
+ def commands
20
+ [
21
+ Command.new("run"){ run_stack_loop },
22
+ Command.new("collect"){ push_new; run_stack_loop },
23
+ Command.new("pop"){ pop_argset },
24
+ Command.new("quit"){ raise "Quitting!" },
25
+ Command.new("help"){ puts "Don't know that one - try: #{command_names.join(", ")}" }
26
+ ]
27
+ end
28
+
29
+ attr_accessor :command, :stack_file, :collect_file, :default_args
30
+
31
+ def initialize
32
+ @default_args = ""
33
+ end
34
+
35
+ def validate_options!
36
+ raise "Need a command!" if command.nil?
37
+ raise "Need a stack file!" if stack_file.nil?
38
+ end
39
+
40
+ def push_new
41
+ unless collect_file.nil?
42
+ push_argset File::read(collect_file).split("\n")
43
+ end
44
+ end
45
+
46
+ def read_stack
47
+ stack_string = File.read(stack_file)
48
+ @stack = stack_string.split("\n\n").map do |item|
49
+ item.split("\n")
50
+ end.reject do |item|
51
+ item.empty?
52
+ end
53
+ rescue Errno::ENOENT
54
+ @stack = []
55
+ end
56
+
57
+ def write_stack
58
+ File::write(stack_file, @stack.map do |argset|
59
+ argset.join("\n")
60
+ end.join("\n\n") + "\n")
61
+ end
62
+
63
+ def get_argset
64
+ read_stack
65
+ if @stack.empty?
66
+ [default_args]
67
+ else
68
+ @stack.last
69
+ end
70
+ end
71
+
72
+ def push_argset(args)
73
+ read_stack
74
+ @stack.push(args)
75
+ write_stack
76
+ end
77
+
78
+ def pop_argset
79
+ read_stack
80
+ @stack.pop
81
+ write_stack
82
+ end
83
+
84
+ def stack_depth
85
+ read_stack
86
+ @stack.length
87
+ end
88
+
89
+ def run
90
+ validate_options!
91
+
92
+ puts "Starting with: " + current_command_line.join(" ")
93
+ puts
94
+ run_stack_loop
95
+
96
+ abbreviations = Abbrev.abbrev(command_names)
97
+
98
+ loop do
99
+ prompt
100
+ command_name = $stdin.gets.chomp
101
+ if command_name.empty?
102
+ command_name = "run"
103
+ end
104
+
105
+ command_name = abbreviations[command_name]
106
+ command = command_hash.fetch(command_name, command_hash["help"])
107
+ command.run
108
+ end
109
+ end
110
+
111
+ def command_list
112
+ @command_list ||= command.split(/\s/)
113
+ end
114
+
115
+ def current_command_line
116
+ args = get_argset
117
+
118
+ command_list + get_argset
119
+ end
120
+
121
+ def prompt
122
+ puts
123
+ puts "Next: " + current_command_line.join(" ")
124
+ print "#{stack_depth} > "
125
+ end
126
+
127
+ def command_names
128
+ @command_names ||= command_hash.keys
129
+ end
130
+
131
+ def command_hash
132
+ @command_hash ||= Hash[ commands.map{|cmd| [cmd.name, cmd] } ]
133
+ end
134
+
135
+ def run_stack_loop
136
+ while run_stack
137
+ puts
138
+ puts "Success - running again..."
139
+ puts
140
+ end
141
+ end
142
+
143
+ def run_stack
144
+ success = system(*current_command_line)
145
+
146
+ if success.nil?
147
+ raise "#{[command, *args].inspect} couldn't be run"
148
+ end
149
+ pop_argset if success
150
+
151
+ return false if @stack.empty?
152
+
153
+ return success
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,33 @@
1
+ require 'optparse'
2
+
3
+ module StackLoop
4
+ class OptParse < OptionParser
5
+ def initialize(app)
6
+ @app = app
7
+ super do |opti|
8
+ opti.banner = "Usage: #$0 [options]"
9
+
10
+ opti.on("-c", "--command COMMAND", "The command string prefix to run") do |command|
11
+ app.command = command
12
+ end
13
+
14
+ opti.on("-s", "--stack-file FILE", "The file to read for argument sets") do |stack|
15
+ app.stack_file = stack
16
+ end
17
+
18
+ opti.on("-p", "--push-file FILE", "A path to read for new argument sets") do |file| #ok
19
+ app.collect_file = file
20
+ end
21
+
22
+ opti.on("-d", "--default FILE", "Argument to use if the stack is empty") do |default|
23
+ app.default_args = default
24
+ end
25
+
26
+ opti.on_tail("-h", "--help", "Show this message") do
27
+ puts opti
28
+ exit
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
File without changes
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stack_loop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Judson Lester
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ The idea here is to take arguments off of a stack, run a command, and proceed down the stack iff the command
15
+ exits 0.
16
+
17
+ Why? Consider running rspec with a tight focus, adding bredth on success
18
+ email:
19
+ - nyarly@gmail.com
20
+ executables:
21
+ - stack_loop
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - lib/stack_loop/app.rb
26
+ - lib/stack_loop/optparse.rb
27
+ - bin/stack_loop
28
+ - spec_help/gem_test_suite.rb
29
+ homepage: http://nyarly.github.com/stack_loop
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --inline-source
36
+ - --main
37
+ - doc/README
38
+ - --title
39
+ - stack_loop-0.0.1 Documentation
40
+ require_paths:
41
+ - lib/
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project: stack_loop
54
+ rubygems_version: 2.0.14
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Run related commands in a loop
58
+ test_files:
59
+ - spec_help/gem_test_suite.rb