wip-runner 0.1.0

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: dbc3644fecc21fa0f02ddb63c48f2d1d90b2e239
4
+ data.tar.gz: 600819ca5fd024e601afe9d6badc02f69627e2f3
5
+ SHA512:
6
+ metadata.gz: 4fca4438f06d1c8d9cc9c8c6018f65f67aee88c46be720c8a38b17a39fdca3733b68755c1b6f1a3572b3500ae9fab2de82ca49fdddafb0a9a2b46d6b29590c15
7
+ data.tar.gz: 00f94a5c728be4acf4e5cd29754da119ecac54e715912cc88306039bb70d1b05b376d3e28c82f1b4bf312fed2a7f10dc62f39fd6e9342f917a3ecd8215999076
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wip-runner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Corey Innis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # wip-runner...
2
+
3
+ - A generic CLI (loads context-based commands)
4
+ - A library for building such commands
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'wip-runner'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install wip-runner
21
+
22
+ ## Usage as CLI
23
+
24
+ ``` bash
25
+ wip-runner help
26
+ ```
27
+
28
+ ## Usage as Library
29
+
30
+ TODO
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec wip-runner` to use the gem in this directory, ignoring other installed copies of this gem.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[coreyti]/wip-runner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wip/runner"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/wip-runner ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "wip/runner"
4
+ WIP::Runner::CLI.new.run(ARGV)
@@ -0,0 +1,27 @@
1
+ module WIP
2
+ module Runner
3
+ class CLI::Help < Command
4
+ overview 'Prints help messages'
5
+ argument :command, { overview: 'Command name' }
6
+
7
+ def execute(params, config)
8
+ command_parser(params).help
9
+ rescue InvalidCommand => e
10
+ print_error(e)
11
+ end
12
+
13
+ def validate!(args)
14
+ true
15
+ end
16
+
17
+ private
18
+
19
+ def command_parser(params)
20
+ command = params.command
21
+ return CLI::Parser.new(@io) if command.nil?
22
+
23
+ Commands.locate(command).new(@io).parser
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module WIP
2
+ module Runner
3
+ class CLI::Version < Command
4
+ overview 'Prints version information'
5
+
6
+ def execute(params, options)
7
+ @io.say "#{WIP::Runner::CLI.signature} version #{WIP::Runner::CLI.namespace::VERSION}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,117 @@
1
+ require 'highline'
2
+
3
+ module WIP
4
+ module Runner
5
+ class CLI
6
+ class << self
7
+ def signature=(value)
8
+ @signature = value
9
+ end
10
+
11
+ def signature
12
+ @signature || 'wip-runner'
13
+ end
14
+
15
+ def namespace=(value)
16
+ @namespace = value
17
+ end
18
+
19
+ def namespace
20
+ @namespace || WIP::Runner
21
+ end
22
+ end
23
+
24
+ def initialize(io = HighLine.new)
25
+ @io = io
26
+ @parser = Parser.new(io)
27
+
28
+ trap('INT') { quit }
29
+ trap('TERM') { quit }
30
+ end
31
+
32
+ def quit
33
+ exit
34
+ end
35
+
36
+ def run(args = [])
37
+ recipient = (command(args) || @parser)
38
+ recipient.run(args)
39
+ rescue InvalidCommand => e
40
+ @io.say(e.message)
41
+ @io.newline
42
+ @parser.help
43
+ end
44
+
45
+ private
46
+
47
+ def command(args)
48
+ handler = Commands.locate(args.shift)
49
+ handler.nil? ? nil : handler.new(@io)
50
+ end
51
+
52
+ class Parser
53
+ def initialize(io)
54
+ @io = io
55
+ end
56
+
57
+ def run(args)
58
+ return help if args.empty?
59
+ options.parse!(args)
60
+ end
61
+
62
+ def help
63
+ @io.say(options.help)
64
+ end
65
+
66
+ private
67
+
68
+ def options
69
+ @options ||= OptionParser.new do |parser|
70
+ parser.banner = "Usage: #{WIP::Runner::CLI.signature} <command> [options]"
71
+
72
+ parser.separator ''
73
+ parser.separator 'Commands:'
74
+
75
+ explicit.each do |command|
76
+ display(parser, command)
77
+ end
78
+ if explicit.length > 0 && implicit.length > 0
79
+ parser.separator [parser.summary_indent, '---'].join
80
+ end
81
+ implicit.each do |command|
82
+ display(parser, command)
83
+ end
84
+
85
+ parser.separator ''
86
+ parser.separator 'Options:'
87
+
88
+ parser.on_tail '-h', '--help', 'Prints help messages' do
89
+ @io.say(parser)
90
+ end
91
+ end
92
+ end
93
+
94
+ def display(parser, command)
95
+ signature = command.signature
96
+ overview = command.overview
97
+ padding = ' ' * (parser.summary_width - signature.length + 1)
98
+ parser.separator [parser.summary_indent, signature, padding, overview].join
99
+ end
100
+
101
+ # ---
102
+
103
+ def namespace
104
+ WIP::Runner::CLI.namespace
105
+ end
106
+
107
+ def implicit
108
+ @implicit ||= Commands.within(WIP::Runner::Commands.implicit)
109
+ end
110
+
111
+ def explicit
112
+ @explicit ||= Commands.within(WIP::Runner::Commands.explicit)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,103 @@
1
+ module WIP
2
+ module Runner
3
+ class Command
4
+ class << self
5
+ def signature
6
+ @signature ||= begin
7
+ ns = Commands.name
8
+ parts = self.name
9
+ .sub(/^.*(CLI|Commands)::/, '')
10
+ .split('::')
11
+ parts.map { |part| part.gsub(/([A-Z])/, "-\\1").sub(/^-/, '') }
12
+ .join(' ')
13
+ .downcase
14
+ end
15
+ end
16
+
17
+ def overview(value = nil)
18
+ @overview = value unless value.nil?
19
+ @overview
20
+ end
21
+
22
+ def commands
23
+ @commands ||= Commands.within(self)
24
+ end
25
+
26
+ def argument(name, definition)
27
+ arguments[name] = Options.new(definition)
28
+ end
29
+
30
+ def arguments(value = nil)
31
+ @arguments ||= {}
32
+ end
33
+
34
+ # TODO: test multi-options
35
+ def options(&block)
36
+ @options ||= []
37
+ @options << block if block_given?
38
+ @options
39
+ end
40
+
41
+ def workflow(&block)
42
+ Workflow.define(&block)
43
+ end
44
+ end
45
+
46
+ attr_reader :parser # TODO(?)... :arguments, :options
47
+
48
+ def initialize(io)
49
+ @io = io
50
+ @parser = WIP::Runner::Parser.new(@io, self.class)
51
+ end
52
+
53
+ def run(argv = [])
54
+ parser.run(argv) do |command, arguments, options|
55
+ if command.nil?
56
+ if options.help
57
+ parser.help
58
+ return
59
+ end
60
+
61
+ validate!(arguments)
62
+ execute(arguments, options)
63
+ else
64
+ delegate(command, argv)
65
+ end
66
+ end
67
+ rescue OptionParser::InvalidOption, InvalidArguments, InvalidCommand => e
68
+ print_error(e)
69
+ end
70
+
71
+ protected
72
+
73
+ def execute(arguments, options)
74
+ raise NotImplementedError
75
+ end
76
+
77
+ def validate!(arguments)
78
+ unless parser.arguments.empty? || parser.config.no_validate
79
+ if parser.arguments.keys.any? { |key| arguments[key].nil? }
80
+ lines = []
81
+ arguments.each_pair do |key, value|
82
+ lines << " - #{key} ... (missing)" if value.nil?
83
+ end
84
+ raise InvalidArguments, %Q{\n#{lines.join("\n")}}
85
+ end
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def delegate(command, argv)
92
+ target = Commands.locate(command, self.class).new(@io)
93
+ target.run(argv)
94
+ end
95
+
96
+ def print_error(e)
97
+ @io.say(e.message.capitalize)
98
+ @io.newline
99
+ parser.help
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,47 @@
1
+ module WIP
2
+ module Runner
3
+ module Commands
4
+ class << self
5
+ def locate(name, namespace = nil)
6
+ return nil if name.nil? || name.match(/^-/)
7
+
8
+ command = name.split('-').map(&:capitalize).join
9
+ namespaces = namespace ? [namespace] : [explicit, implicit]
10
+ namespaces.each do |ns|
11
+ return ns.const_get(command) if ns && ns.const_defined?(command)
12
+ end
13
+ raise InvalidCommand, name
14
+ end
15
+
16
+ def within(namespace)
17
+ return [] if namespace.nil?
18
+
19
+ namespace.constants
20
+ .collect { |const|
21
+ namespace.const_get(const)
22
+ }
23
+ .select { |command|
24
+ command < Command # is a subclass of
25
+ }
26
+ end
27
+
28
+ def implicit
29
+ WIP::Runner::CLI
30
+ end
31
+
32
+ def explicit
33
+ @explicit ||= begin
34
+ namespace.const_get(:Commands) if namespace.const_defined?(:Commands)
35
+ end
36
+ end
37
+
38
+ def namespace
39
+ WIP::Runner::CLI.namespace
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ # NOTE: Commands are responsible for requiring sub-commands.
47
+ Dir[File.expand_path('../commands/*.rb', __FILE__)].each { |f| require(f) }
@@ -0,0 +1,24 @@
1
+ module WIP
2
+ # TODO: consider...
3
+ #
4
+ # - OptionParser::ParseError:: errors on parsing
5
+ # - OptionParser::AmbiguousArgument
6
+ # - OptionParser::InvalidArgument
7
+ # - OptionParser::MissingArgument
8
+ # - OptionParser::NeedlessArgument
9
+ # - OptionParser::AmbiguousOption
10
+ # - OptionParser::InvalidOption
11
+ module Runner
12
+ class Error < StandardError
13
+ def message
14
+ prefix = self.class.name.split('::').last
15
+ .gsub(/([A-Z])/, " \\1")
16
+ .strip
17
+ .capitalize
18
+ "#{prefix}: #{super}".sub(/\s\n/, "\n")
19
+ end
20
+ end
21
+ class InvalidArguments < Error; end
22
+ class InvalidCommand < Error; end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'ostruct'
2
+
3
+ module WIP
4
+ module Runner
5
+ class Options < OpenStruct ; end
6
+ end
7
+ end
@@ -0,0 +1,93 @@
1
+ module WIP
2
+ module Runner
3
+ class Parser
4
+ attr_reader :config
5
+
6
+ def initialize(io, command)
7
+ @io = io
8
+ @command = command
9
+ @config = WIP::Runner::Options.new
10
+ end
11
+
12
+ def run(argv)
13
+ unless commands.empty?
14
+ command = argv.shift
15
+ raise InvalidCommand, '(missing)' if command.nil?
16
+ yield(command, nil, nil)
17
+ else
18
+ remaining = options.parse!(argv)
19
+
20
+ @args = WIP::Runner::Options.new.tap do |opts|
21
+ arguments.keys.each_with_index do |key, index|
22
+ opts[key] = remaining[index]
23
+ end
24
+ end
25
+
26
+ yield(nil, @args, @config)
27
+ end
28
+ end
29
+
30
+ def help
31
+ @io.say(options.help)
32
+ end
33
+
34
+ def options
35
+ @options ||= OptionParser.new do |parser|
36
+ @config.help = false
37
+ @config.no_validate = false
38
+
39
+ parser.banner = "Usage: #{WIP::Runner::CLI.signature} #{heading}"
40
+
41
+ section(parser, 'Commands:', commands)
42
+ section(parser, 'Arguments:', arguments)
43
+
44
+ parser.separator ''
45
+ parser.separator 'Options:'
46
+
47
+ @command.options.each do |block|
48
+ block.call(parser, @config)
49
+ end
50
+
51
+ parser.on_tail '-h', '--help', 'Prints help messages' do
52
+ @config.help = true
53
+ end
54
+ end
55
+ end
56
+
57
+ def arguments
58
+ @command.arguments
59
+ end
60
+
61
+ private
62
+
63
+ def commands
64
+ {}.tap do |result|
65
+ @command.commands.each do |command|
66
+ result[command.signature.split(' ').last.intern] = { overview: command.overview }
67
+ end
68
+ end
69
+ end
70
+
71
+ def heading
72
+ sections = [].tap do |result|
73
+ result << '<command>' unless commands.empty?
74
+ result << '<arguments>' unless commands.empty? && arguments.empty?
75
+ end
76
+
77
+ [@command.signature, sections, '[options]'].flatten.join(' ')
78
+ end
79
+
80
+ def section(parser, heading, pairs)
81
+ unless pairs.empty?
82
+ parser.separator ''
83
+ parser.separator heading
84
+
85
+ pairs.each do |name, details|
86
+ padding = ' ' * (parser.summary_width - name.length + 1)
87
+ parser.separator [parser.summary_indent, name, padding, details[:overview]].join('')
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,46 @@
1
+ require 'open3'
2
+
3
+ module WIP
4
+ module Runner
5
+ module Shell
6
+ module Handlers
7
+ class Base
8
+ def initialize(content, &block)
9
+ @content = clean(content)
10
+ instance_exec(&block) if block_given?
11
+ end
12
+
13
+ def description
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def execute(io, env, &block)
18
+ raise NotImplementedError
19
+ end
20
+
21
+ def type
22
+ @type ||= self.class.name.split('::').last
23
+ end
24
+
25
+ protected
26
+
27
+ def executable
28
+ @executable ||= begin
29
+ content = @content.gsub(/"/, '\"').gsub(/\$/, "\\$")
30
+ %Q{bash -c "#{content}"}
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def clean(string)
37
+ return if string.nil?
38
+
39
+ indent = (string.scan(/^[ \t]*(?=\S)/).min || '').size
40
+ string.gsub(/^[ \t]{#{indent}}/, '').strip
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end