thor-addons 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: 80548135fbb1ebf5c423d1d1d6731492f49118d7
4
+ data.tar.gz: 46cb775c8c4ec804973d00762fda59f220316f45
5
+ SHA512:
6
+ metadata.gz: ad7e7c79a0af510cde5bf08624d52203039f44da8269ee761da05604b2ef4196588d4cee7c3dc367d4a782aa9f13097eaf65d4248ca7c540090f4a5beb40e49d
7
+ data.tar.gz: 2a047a6cd2c7701b712f512c9d9686871eb058a1eeedf4d65a762d4e4383a79e85bd6b34364c288527a5bfbc9bd6a09d96155b161a2b592b5bf537483bee33a1
data/Dockerfile ADDED
@@ -0,0 +1,9 @@
1
+ FROM ruby:2.3.3
2
+
3
+ ENV WORKDIR /thor-addons
4
+
5
+ WORKDIR $WORKDIR
6
+
7
+ COPY . ./
8
+
9
+ RUN bundle install
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ thor-addons (0.1.0)
5
+ thor (~> 0.19)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (12.0.0)
12
+ rspec (3.6.0)
13
+ rspec-core (~> 3.6.0)
14
+ rspec-expectations (~> 3.6.0)
15
+ rspec-mocks (~> 3.6.0)
16
+ rspec-core (3.6.0)
17
+ rspec-support (~> 3.6.0)
18
+ rspec-expectations (3.6.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.6.0)
21
+ rspec-mocks (3.6.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.6.0)
24
+ rspec-support (3.6.0)
25
+ thor (0.19.4)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler
32
+ rake
33
+ rspec
34
+ thor-addons!
35
+
36
+ BUNDLED WITH
37
+ 1.14.6
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Thor Add-ons
2
+
3
+ Extend [Thor](https://github.com/erikhuda/thor) with useful Add-ons
@@ -0,0 +1,5 @@
1
+ require "yaml"
2
+ require "thor"
3
+
4
+ require "thor_addons/version"
5
+ require "thor_addons/options"
@@ -0,0 +1,117 @@
1
+ module ThorAddons
2
+ module Options
3
+ attr_reader :current_command_name, :defaults, :invoked_via_subcommand, :invocations
4
+
5
+ def initialize(args = [], local_options = {}, config = {})
6
+ @current_command_name = config[:current_command].name
7
+ @defaults = load_defaults(config.dup)
8
+ @invoked_via_subcommand = config[:invoked_via_subcommand]
9
+ @invocations = config[:invocations]
10
+
11
+ super(args, local_options, config)
12
+ end
13
+
14
+ def with_env?
15
+ true
16
+ end
17
+
18
+ def with_config_file?
19
+ true
20
+ end
21
+
22
+ def options
23
+ original = super
24
+ config_file = original[:config_file]
25
+
26
+ return original unless with_env? || with_config_file?
27
+
28
+ new_options = remove_defaults(original)
29
+
30
+ if with_config_file? && !config_file.nil?
31
+ merge(new_options, options_from_config(config_file))
32
+ end
33
+
34
+ if with_env?
35
+ merge(new_options, options_from_env)
36
+ end
37
+
38
+ ::Thor::CoreExt::HashWithIndifferentAccess.new(add_defaults(new_options))
39
+ end
40
+
41
+ private
42
+
43
+ def merge(options_a, options_b)
44
+ options_b.each do |key, value|
45
+ next if value.to_s.empty? || !options_a[key].to_s.empty?
46
+
47
+ options_a[key] = value
48
+ end
49
+
50
+ options_a
51
+ end
52
+
53
+ def options_from_env
54
+ defaults.keys.inject({}) do |memo, option|
55
+ env = option.to_s.upcase
56
+ memo[option] = ENV[env] unless ENV[env].nil?
57
+
58
+ memo
59
+ end
60
+ end
61
+
62
+ def options_from_config(config_file)
63
+ unless File.file?(config_file)
64
+ STDERR.puts("[WARNING] Unable to read 'config_file' '#{config_file}' not found.")
65
+ return {}
66
+ end
67
+
68
+ data = YAML.load_file(config_file)
69
+ command_options = {}
70
+ if invoked_via_subcommand
71
+ invocations.map { |k,v| v }.flatten.each do |subcommand|
72
+ next if data[subcommand].nil? || data[subcommand][current_command_name].nil?
73
+
74
+ command_options.merge!(data[subcommand][current_command_name])
75
+ end
76
+ end
77
+
78
+ ::Thor::CoreExt::HashWithIndifferentAccess.new(data["global"].merge(command_options))
79
+ end
80
+
81
+ def add_defaults(hash)
82
+ hash.inject({}) do |memo, (k, v)|
83
+ memo[k] = v.nil? ? defaults[k] : v
84
+
85
+ memo
86
+ end
87
+ end
88
+
89
+ def remove_defaults(hash)
90
+ hash.inject({}) do |memo, (k, v)|
91
+ if defaults[k] == v
92
+ memo[k] = nil
93
+ else
94
+ memo[k] = v
95
+ end
96
+
97
+ memo
98
+ end
99
+ end
100
+
101
+ def load_defaults(config)
102
+ parse_options = self.class.class_options.dup
103
+ parse_options.merge!(config[:class_options]) if config[:class_options]
104
+ parse_options.merge!(config[:command_options]) if config[:command_options]
105
+
106
+ parse_options.inject({}) do |memo, (key, value)|
107
+ begin
108
+ memo[key] = value.default
109
+ rescue NoMethodError
110
+ memo[key] = nil
111
+ end
112
+
113
+ memo
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ module ThorAddons
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "thor_addons/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thor-addons"
8
+ spec.version = ThorAddons::VERSION
9
+ spec.authors = ["Jacopo Scrinzi"]
10
+ spec.email = ["scrinzi.jcopo@gmail.com"]
11
+
12
+ spec.summary = %q{Thor CLI Add-ons}
13
+ spec.description = %q{Useful Add-ons Thor CLI}
14
+ spec.homepage = "https://github.com/eredi93/thor-addons"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "thor", "~> 0.19"
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thor-addons
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacopo Scrinzi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Useful Add-ons Thor CLI
70
+ email:
71
+ - scrinzi.jcopo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Dockerfile
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - README.md
80
+ - lib/thor_addons.rb
81
+ - lib/thor_addons/options.rb
82
+ - lib/thor_addons/version.rb
83
+ - thor-addons.gemspec
84
+ homepage: https://github.com/eredi93/thor-addons
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.12
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Thor CLI Add-ons
108
+ test_files: []