dracula 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e148c48f43d0c429b99185e871a93c814ad8cc4
4
+ data.tar.gz: 0e5981929891a953d5e4cd223d0fa0a1365c1e34
5
+ SHA512:
6
+ metadata.gz: 7654ff76f9bdc4853367517802769d970575c704c429c3a1652e133c409751607bc464b88242300b0844c2201db855bbfcf6586ae46db426ad28966d7689c870
7
+ data.tar.gz: e3a49f08e84a54d89353b44dbf4895707d5550c49a45f5f0cc137e3d74c6aae781609b73c8358770d36222f705595ef3b86270b7da8aca3991e6b631b35e9490
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at igor@renderedtext.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dracula.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Igor Šarčević
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,27 @@
1
+ # Dracula
2
+
3
+ [![Build Status](https://semaphoreci.com/api/v1/shiroyasha/dracula/branches/master/badge.svg)](https://semaphoreci.com/shiroyasha/dracula)
4
+
5
+ ## Usage
6
+
7
+ First, define your commands:
8
+
9
+ ``` ruby
10
+ class Login < Dracula::Command
11
+ flag :username, :short => "u", :required => true
12
+ flag :password, :short => "p", :required => true
13
+ flag :verbose, :short => "v", :type => :boolean
14
+
15
+ def run
16
+ if flags[:vebose]
17
+ puts "Running verbosely"
18
+ end
19
+
20
+ puts "Logginng in with #{flags[:username]}:#{flags[:password]}"
21
+ end
22
+ end
23
+ ```
24
+
25
+ ``` ruby
26
+ Login.run("--username Vlad --password Transylvania")
27
+ ```
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 "dracula"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dracula.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dracula/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dracula"
8
+ spec.version = Dracula::VERSION
9
+ spec.authors = ["Igor Šarčević"]
10
+ spec.email = ["igor@renderedtext.com"]
11
+
12
+ spec.summary = %q{Dracula command line tool framework}
13
+ spec.description = %q{Dracula command line tool framework}
14
+ spec.homepage = "https://github.com/renderedtext/dracula"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/lib/dracula.rb ADDED
@@ -0,0 +1,80 @@
1
+ require "dracula/version"
2
+ require "optparse"
3
+
4
+ class Dracula
5
+ require "dracula/command"
6
+ require "dracula/namespace"
7
+ require "dracula/ui"
8
+
9
+ def self.program_name(name = nil)
10
+ if name.nil?
11
+ @@program_name || "dracula" # getter
12
+ else
13
+ @@program_name = name # setter
14
+ end
15
+ end
16
+
17
+ def self.start(args)
18
+ if args.empty? || (args.size == 1 && args[0] == "help")
19
+ namespace.help
20
+ else
21
+ action = args[0] == "help" ? :help : :run
22
+
23
+ if args[0] == "help"
24
+ action = :help
25
+
26
+ args.shift # drop 'help'
27
+
28
+ command = args.shift
29
+ params = args
30
+ else
31
+ action = :run
32
+ command = args.shift
33
+ params = args
34
+ end
35
+
36
+ namespace.dispatch(command.split(":"), params, action)
37
+ end
38
+ end
39
+
40
+ def self.namespace
41
+ @namespace ||= Dracula::Namespace.new(self)
42
+ end
43
+
44
+ def self.option(name, params = {})
45
+ @options ||= []
46
+ @options << Command::Option.new(name, params)
47
+ end
48
+
49
+ def self.long_desc(description)
50
+ @long_desc = description
51
+ end
52
+
53
+ def self.desc(name, description)
54
+ @desc = Command::Desc.new(name, description)
55
+ end
56
+
57
+ def self.register(name, description, klass)
58
+ klass.namespace.name = name
59
+ klass.namespace.description = description
60
+
61
+ namespace.add_subcommand(klass.namespace)
62
+ end
63
+
64
+ private_class_method def self.method_added(method_name)
65
+ command = Command.new(self, method_name, @desc, @long_desc, @options)
66
+
67
+ @desc = nil
68
+ @long_desc = nil
69
+ @options = nil
70
+
71
+ namespace.add_command(command)
72
+ end
73
+
74
+ attr_reader :options
75
+
76
+ def initialize(options = {})
77
+ @options = options
78
+ end
79
+
80
+ end
@@ -0,0 +1,132 @@
1
+ class Dracula
2
+ class Command
3
+
4
+ Desc = Struct.new(:name, :description)
5
+
6
+ class Option < Struct.new(:name, :params)
7
+
8
+ def short_name
9
+ params[:aliases]
10
+ end
11
+
12
+ def type
13
+ params[:type] || :string
14
+ end
15
+
16
+ def boolean?
17
+ type == :boolean
18
+ end
19
+
20
+ def has_default_value?
21
+ params.has_key?(:default) || boolean?
22
+ end
23
+
24
+ def default_value
25
+ if boolean?
26
+ params.key?(:default) ? params[:default] : false
27
+ else
28
+ params[:default]
29
+ end
30
+ end
31
+
32
+ def alias_name
33
+ params[:alias]
34
+ end
35
+
36
+ def banner
37
+ if alias_name.empty?
38
+ "--#{name}"
39
+ else
40
+ "-#{alias_name}, --#{name}"
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ attr_reader :method_name
47
+ attr_reader :desc
48
+ attr_reader :options
49
+ attr_reader :long_desc
50
+
51
+ alias_method :flags, :options
52
+
53
+ def initialize(klass, method_name, desc, long_desc, options)
54
+ @klass = klass
55
+ @method_name = method_name
56
+ @desc = desc
57
+ @long_desc = long_desc
58
+ @options = options || []
59
+ end
60
+
61
+ def name
62
+ desc.name
63
+ end
64
+
65
+ def help
66
+ msg = [
67
+ "Usage: #{Dracula.program_name} #{@klass.namespace.name ? "#{@klass.namespace.name}:" : "" }#{desc.name}",
68
+ "",
69
+ "#{desc.description}",
70
+ ""
71
+ ]
72
+
73
+ if options.size > 0
74
+ msg << "Flags:"
75
+
76
+ options.each { |option| msg << " #{option.banner}" }
77
+
78
+ msg << ""
79
+ end
80
+
81
+ unless long_desc.nil?
82
+ msg << long_desc
83
+ end
84
+
85
+ puts msg.join("\n")
86
+ end
87
+
88
+ def run(params)
89
+ args = params.take_while { |p| p[0] != "-" }
90
+ flags = parse_flags(params.drop_while { |p| p[0] != "-" })
91
+
92
+ @klass.new(flags).public_send(method_name, *args)
93
+ end
94
+
95
+ private
96
+
97
+ def parse_flags(args)
98
+ parsed_flags = {}
99
+
100
+ # set default values
101
+ flags.each do |flag|
102
+ if flag.has_default_value?
103
+ parsed_flags[flag.name] = flag.default_value
104
+ end
105
+ end
106
+
107
+ opt_parser = OptionParser.new do |opts|
108
+ flags.each do |flag|
109
+ if flag.boolean?
110
+ short = "-#{flag.short_name}"
111
+ long = "--#{flag.name}"
112
+
113
+ opts.on(short, long, flag.type) do
114
+ parsed_flags[flag.name] = true
115
+ end
116
+ else
117
+ short = "-#{flag.short_name}"
118
+ long = "--#{flag.name} VALUE"
119
+
120
+ opts.on(short, long, flag.type) do |value|
121
+ parsed_flags[flag.name] = value
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ opt_parser.parse!(args)
128
+
129
+ parsed_flags
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,31 @@
1
+ module Dracula
2
+ class Flag
3
+ attr_reader :name
4
+ attr_reader :short_name
5
+ attr_reader :type
6
+
7
+ def initialize(name, params)
8
+ @name = name
9
+ @params = params
10
+
11
+ @short_name = params.fetch(:short)
12
+ @type = params.fetch(:type, :string)
13
+ end
14
+
15
+ def boolean?
16
+ type == :boolean
17
+ end
18
+
19
+ def has_default_value?
20
+ @params.key?(:default_value) || boolean?
21
+ end
22
+
23
+ def default_value
24
+ if type == :boolean
25
+ @params.key?(:default_value) || false
26
+ else
27
+ @default_value
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,72 @@
1
+ class Dracula
2
+ class Namespace
3
+
4
+ attr_accessor :name
5
+ attr_accessor :description
6
+ attr_accessor :commands
7
+ attr_accessor :subcommands
8
+
9
+ def initialize(klass)
10
+ @klass = klass
11
+ @commands = []
12
+ @subcommands = []
13
+ end
14
+
15
+ def dispatch(route, params, action = :run)
16
+ case route.size
17
+ when 0 then
18
+ action == :run ? run(params) : help
19
+ when 1 then
20
+ handler = commands.find { |c| c.name == route[0] } || subcommands.find { |c| c.name == route[0] }
21
+
22
+ if handler
23
+ action == :run ? handler.run(params) : handler.help
24
+ else
25
+ help
26
+ end
27
+ else
28
+ handler = subcommands.find { |c| c.name == route[0] }
29
+
30
+ if handler
31
+ handler.dispatch(route[1..-1], params, action)
32
+ else
33
+ help
34
+ end
35
+ end
36
+ end
37
+
38
+ def run(params)
39
+ help
40
+ end
41
+
42
+ def help
43
+ prefix = name ? "#{name}:" : ""
44
+
45
+ puts "Usage: #{Dracula.program_name} #{prefix}<command>"
46
+ puts ""
47
+ puts (description || "Help topics, type #{Dracula.program_name} help TOPIC for more details:")
48
+ puts ""
49
+
50
+ command_list = []
51
+
52
+ commands.each do |cmd|
53
+ command_list << ["#{prefix}#{cmd.desc.name}", cmd.desc.description]
54
+ end
55
+
56
+ subcommands.each do |sub_cmd|
57
+ command_list << ["#{prefix}#{sub_cmd.name}", sub_cmd.description]
58
+ end
59
+
60
+ Dracula::UI.print_table(command_list, :indent => 2)
61
+ end
62
+
63
+ def add_command(command)
64
+ @commands << command
65
+ end
66
+
67
+ def add_subcommand(subcommand)
68
+ @subcommands << subcommand
69
+ end
70
+
71
+ end
72
+ end
data/lib/dracula/ui.rb ADDED
@@ -0,0 +1,66 @@
1
+ class Dracula
2
+ class UI
3
+
4
+ # Prints a table. Shamelesly copied from thor.
5
+ #
6
+ # ==== Parameters
7
+ # Array[Array[String, String, ...]]
8
+ #
9
+ # ==== Options
10
+ # indent<Integer>:: Indent the first column by indent value.
11
+ # colwidth<Integer>:: Force the first column to colwidth spaces wide.
12
+ #
13
+ def self.print_table(array, options = {}) # rubocop:disable MethodLength
14
+ return if array.empty?
15
+
16
+ formats = []
17
+ indent = options[:indent].to_i
18
+ colwidth = options[:colwidth]
19
+ options[:truncate] = terminal_width if options[:truncate] == true
20
+
21
+ formats << "%-#{colwidth + 2}s".dup if colwidth
22
+ start = colwidth ? 1 : 0
23
+
24
+ colcount = array.max { |a, b| a.size <=> b.size }.size
25
+
26
+ maximas = []
27
+
28
+ start.upto(colcount - 1) do |index|
29
+ maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max
30
+ maximas << maxima
31
+ formats << if index == colcount - 1
32
+ # Don't output 2 trailing spaces when printing the last column
33
+ "%-s".dup
34
+ else
35
+ "%-#{maxima + 2}s".dup
36
+ end
37
+ end
38
+
39
+ formats[0] = formats[0].insert(0, " " * indent)
40
+ formats << "%s"
41
+
42
+ array.each do |row|
43
+ sentence = "".dup
44
+
45
+ row.each_with_index do |column, index|
46
+ maxima = maximas[index]
47
+
48
+ f = if column.is_a?(Numeric)
49
+ if index == row.size - 1
50
+ # Don't output 2 trailing spaces when printing the last column
51
+ "%#{maxima}s"
52
+ else
53
+ "%#{maxima}s "
54
+ end
55
+ else
56
+ formats[index]
57
+ end
58
+ sentence << f % column.to_s
59
+ end
60
+
61
+ sentence = truncate(sentence, options[:truncate]) if options[:truncate]
62
+ puts sentence
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ class Dracula
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dracula
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Šarčević
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Dracula command line tool framework
56
+ email:
57
+ - igor@renderedtext.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - dracula.gemspec
72
+ - lib/dracula.rb
73
+ - lib/dracula/command.rb
74
+ - lib/dracula/flag.rb
75
+ - lib/dracula/namespace.rb
76
+ - lib/dracula/ui.rb
77
+ - lib/dracula/version.rb
78
+ homepage: https://github.com/renderedtext/dracula
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Dracula command line tool framework
102
+ test_files: []