console_runner 0.2.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3eeeb11fc8af9181f4f34e73958ec18bf239f573
4
- data.tar.gz: 8ffe91d2115bcfd1cb0c8e1b952b4e9d92f4a113
2
+ SHA256:
3
+ metadata.gz: dc2014166894d41a7a29fcecdbb09d715a26be5c626bf8f674b3cb0d6e7bf397
4
+ data.tar.gz: 734b4658c19fa4cbaa4dcca9669f3ea65c938d4a302d9ad8d198335afece95f4
5
5
  SHA512:
6
- metadata.gz: 9e16be1daab6039f7741ca63d50e536337a935140a19cdd4705f588d1857af5847256ebdef7824a5ccc9d9e85a87d10a3d854bc6287b6f65991edad1fb7f98df
7
- data.tar.gz: 4f808cfdbc2de11cc51e10f55f619e1f9975fed16a76f9fed71b897c1d9f0baa6efee70e0564259ebe1488c6dc51eaf2dbfa92be58f3582daea84c239c48f59e
6
+ metadata.gz: 55722733901c5424f0c194a29b8b9373b82be532dbe766c4eb0b09acf5cc386f949fa253540502f78c0b3dd5404b986049b8b895358e91031be5239b56e85e0d
7
+ data.tar.gz: 961eafb916dd30cc0513fc04f6dfcdf841fb4424ff3baa9b411e1f975b0a8f4cdce4541fe55edd7c5417789de04c3b734cee568e62eb69da9afec6719c92c441
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.2.5
5
- before_install: gem install bundler -v 1.13.6
4
+ - 2.5.1
5
+ before_install: gem install bundler
@@ -32,6 +32,8 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = ['c_run']
33
33
  spec.require_paths = ['lib']
34
34
 
35
+ spec.required_ruby_version = '>= 2.5.1'
36
+
35
37
  spec.add_development_dependency 'rake', '>= 12.3.3'
36
38
  spec.add_development_dependency 'minitest', '~> 5.0'
37
39
  spec.add_development_dependency 'pry', '~> 0'
@@ -4,7 +4,7 @@ require 'colorize'
4
4
 
5
5
  # Parses command line and configure #Optimist
6
6
  class CommandLineParser
7
- attr_reader :method, :initialize_method
7
+ attr_reader :method, :initialize_method, :file_parser
8
8
  @debug = false
9
9
 
10
10
  # Generate tool help menu.
@@ -72,19 +72,44 @@ class CommandLineParser
72
72
  next unless method
73
73
  method.optimist_opts.each { |a| @parser.opt(*a) }
74
74
  maybe_help(method.text, action.name.to_s)
75
- cmd_opts = @parser.parse ARGV
76
- given_attrs = cmd_opts.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
77
- method.cmd_opts = cmd_opts.select { |k, _| given_attrs.include? k }
78
- method.default_values.each do |k, v|
79
- param_name = k.to_sym
80
- next if method.option_tags.map(&:name).include?(param_name.to_s)
81
- method.cmd_opts[param_name] ||= v
75
+
76
+
77
+ # Read code defaults
78
+ # def droplet_create(name, region = 1, image = nil, size = nil, ssh_keys = nil)
79
+ argv_from_code_defaults = []
80
+ method.default_values.each do |k, param_value_from_code|
81
+ param_name_from_code = k.to_sym
82
+ next if method.option_tags.map(&:name).include?(param_name_from_code.to_s)
83
+ next if param_value_from_code == 'nil'
84
+
85
+ argv_from_code_defaults << "--#{param_name_from_code.to_s.gsub('_', '-')}"
86
+ argv_from_code_defaults << param_value_from_code
87
+ end
88
+ # Raise on constant found
89
+ constants = file_parser.constants
90
+ argv_from_code_defaults.map do |x|
91
+ existing_constant = constants.find { |c| c.name.to_s == x }
92
+ raise "Constants as default params values are not supported by console_runner: #{existing_constant.name}" if existing_constant
82
93
  end
94
+ parsed_code_default_args = @parser.parse(argv_from_code_defaults)
95
+ params_names_from_code = parsed_code_default_args.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
96
+ params_with_values_from_code = parsed_code_default_args.select { |k, _| params_names_from_code.include? k }
97
+ params_with_values_from_code.delete_if { |k, v| v == 'nil' }
98
+
99
+
100
+ parsed_cli_args = @parser.parse ARGV
101
+ params_names_from_cl = parsed_cli_args.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
102
+ params_with_values_from_cl = parsed_cli_args.select { |k, _| params_names_from_cl.include? k }
103
+
104
+
105
+ method.cmd_opts = params_with_values_from_code.merge(params_with_values_from_cl)
106
+ missed_params = []
83
107
  method.required_parameters.each do |required_param|
84
108
  next if method.options_group? required_param
85
109
  next if method.cmd_opts[required_param.to_sym]
86
- raise ConsoleRunnerError, "You must specify required parameter: #{required_param}"
110
+ missed_params << required_param
87
111
  end
112
+ raise ConsoleRunnerError, "You must specify required parameter: #{missed_params.join(', ')}" if missed_params.count.positive?
88
113
  ARGV.shift
89
114
  end
90
115
  end
@@ -40,7 +40,7 @@ module ConsoleRunner
40
40
  c_line_parser.initialize_method.cmd_opts.map { |k, v| LOGGER.debug "\t#{k} = #{v}" }
41
41
  end
42
42
  LOGGER.debug "Executing ##{action.name} method..."
43
- c_line_parser.method.cmd_opts.map { |k, v| LOGGER.debug "\t#{k} = #{v}" }
43
+ c_line_parser.method.cmd_opts.map { |k, v| LOGGER.debug "\t#{k} = #{v}" }
44
44
  LOGGER.debug("Remaining arguments: #{ARGV.inspect}") if ARGV != []
45
45
  LOGGER.info SEPARATOR
46
46
 
@@ -75,6 +75,8 @@ module ConsoleRunner
75
75
  LOGGER.success 'Execution status: ' + status
76
76
  else
77
77
  LOGGER.error 'Execution status: ' + status
78
+ LOGGER.error e.message if e
79
+ LOGGER.error SEPARATOR
78
80
  end
79
81
 
80
82
  end
@@ -1,3 +1,3 @@
1
1
  module ConsoleRunner
2
- VERSION = '0.2.3'.freeze
2
+ VERSION = '0.3.1'.freeze
3
3
  end
data/lib/file_parser.rb CHANGED
@@ -6,6 +6,7 @@ class FileParser
6
6
  RUNNABLE_TAG = :runnable
7
7
 
8
8
  attr_reader :clazz,
9
+ :constants,
9
10
  :runnable_methods,
10
11
  :initialize_method,
11
12
  :run_method
@@ -21,6 +22,7 @@ class FileParser
21
22
  runnable_classes = list_classes(:runnable)
22
23
  raise ConsoleRunnerError, 'At least one runnable Class should be specified in file' if runnable_classes.count != 1
23
24
  @clazz = runnable_classes.first
25
+ @constants = @clazz.constants
24
26
  all_methods = list_methods(:all, clazz)
25
27
  @runnable_methods = list_methods(:runnable, clazz)
26
28
  @initialize_method = all_methods.find { |m| m.name == :initialize }
@@ -47,20 +49,20 @@ class FileParser
47
49
  all_class_methods = clazz.children.select { |m| m.class == YARD::CodeObjects::MethodObject } if clazz
48
50
 
49
51
  case scope
50
- when :all
51
- if clazz
52
- all_class_methods
53
- else
54
- all_methods
55
- end
56
- when RUNNABLE_TAG
57
- if clazz
58
- all_class_methods.select { |m| m.has_tag? RUNNABLE_TAG }
59
- else
60
- all_methods.select { |m| m.has_tag? RUNNABLE_TAG }
61
- end
52
+ when :all
53
+ if clazz
54
+ all_class_methods
62
55
  else
63
- raise ":scope can be :all or #{RUNNABLE_TAG}"
56
+ all_methods
57
+ end
58
+ when RUNNABLE_TAG
59
+ if clazz
60
+ all_class_methods.select { |m| m.has_tag? RUNNABLE_TAG }
61
+ else
62
+ all_methods.select { |m| m.has_tag? RUNNABLE_TAG }
63
+ end
64
+ else
65
+ raise ":scope can be :all or #{RUNNABLE_TAG}"
64
66
  end
65
67
  end
66
68
 
@@ -68,15 +70,15 @@ class FileParser
68
70
  #
69
71
  # @param [Symbol] scope :all - list all classes, :runnable - list only runnable classes
70
72
  # @return [Array(YARD::CodeObjects::ClassObject)]
71
- def list_classes(scope= :all)
73
+ def list_classes(scope = :all)
72
74
  all_classes = @all_objects.select { |o| o.type == :class }
73
75
  case scope
74
- when :all
75
- all_classes
76
- when RUNNABLE_TAG
77
- all_classes.select { |m| m.has_tag? RUNNABLE_TAG }
78
- else
79
- raise ":scope can be :all or #{RUNNABLE_TAG}"
76
+ when :all
77
+ all_classes
78
+ when RUNNABLE_TAG
79
+ all_classes.select { |m| m.has_tag? RUNNABLE_TAG }
80
+ else
81
+ raise ":scope can be :all or #{RUNNABLE_TAG}"
80
82
  end
81
83
  end
82
84
 
data/lib/method_parser.rb CHANGED
@@ -13,16 +13,16 @@ class MethodParser
13
13
  attr_accessor :cmd_opts
14
14
 
15
15
  TYPES_MAPPINGS = {
16
- 'String' => :string,
17
- 'Integer' => :int,
18
- 'Fixnum' => :int,
19
- 'Float' => :float,
20
- 'Boolean' => :boolean,
21
- 'Array(String)' => :strings,
22
- 'Array(Integer)' => :ints,
23
- 'Array(Fixnum)' => :ints,
24
- 'Array(Float)' => :floats,
25
- 'Array(Boolean)' => :booleans
16
+ 'String' => :string,
17
+ 'Integer' => :int,
18
+ 'Fixnum' => :int,
19
+ 'Float' => :float,
20
+ 'Boolean' => :boolean,
21
+ 'Array(String)' => :strings,
22
+ 'Array(Integer)' => :ints,
23
+ 'Array(Fixnum)' => :ints,
24
+ 'Array(Float)' => :floats,
25
+ 'Array(Boolean)' => :booleans
26
26
  }.freeze
27
27
 
28
28
  # @param [YARD::CodeObjects::MethodObject] method YARD method object to be parsed
@@ -31,16 +31,16 @@ class MethodParser
31
31
  @name = @method.name
32
32
  @text = FileParser.select_runnable_tags(@method).map(&:text).join("\n")
33
33
  @parameters = @method.parameters
34
- @default_values = default_params
34
+ @default_values = default_params.reject { |k, v| v.nil? }
35
35
  @param_tags = FileParser.select_param_tags @method
36
36
  @option_tags = FileParser.select_option_tags @method
37
- @required_parameters = @param_tags.select { |t| t.tag_name == 'param' }.map(&:name)
37
+ @required_parameters = @param_tags.select { |t| t.tag_name == 'param' && !@default_values.keys.include?(t.name) }.map(&:name)
38
38
  @cmd_opts = nil
39
39
  same_params = param_tags_names & option_tags_names
40
40
  unless same_params.count.zero?
41
41
  raise(
42
- ConsoleRunnerError,
43
- "You have the same name for @param and @option attribute(s): #{same_params.join(', ')}.
42
+ ConsoleRunnerError,
43
+ "You have the same name for @param and @option attribute(s): #{same_params.join(', ')}.
44
44
  Use different names to `console_runner` be able to run #{@name} method."
45
45
  )
46
46
  end
@@ -61,23 +61,23 @@ Use different names to `console_runner` be able to run #{@name} method."
61
61
  option_text = option.pair.text
62
62
  option_type = option.pair.type
63
63
  result << [
64
- option_name.to_sym,
65
- "(Ruby class: #{option_type}) " + option_text.to_s,
66
- type: parse_type(option_type)
64
+ option_name.to_sym,
65
+ "(Ruby class: #{option_type}) " + option_text.to_s,
66
+ type: parse_type(option_type)
67
67
  ]
68
68
  end
69
69
  else
70
70
  result << [
71
- tag_name.to_sym,
72
- "(Ruby class: #{tag_type}) " + tag_text.to_s,
73
- type: parse_type(tag_type)
71
+ tag_name.to_sym,
72
+ "(Ruby class: #{tag_type}) " + tag_text.to_s,
73
+ type: parse_type(tag_type)
74
74
  ]
75
75
  end
76
76
  else
77
77
  result << [
78
- tag_name.to_sym,
79
- "(Ruby class: #{tag_type}) " + tag_text.to_s,
80
- type: parse_type(tag_type)
78
+ tag_name.to_sym,
79
+ "(Ruby class: #{tag_type}) " + tag_text.to_s,
80
+ type: parse_type(tag_type)
81
81
  ]
82
82
  end
83
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Karpovich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-19 00:00:00.000000000 Z
11
+ date: 2020-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -162,6 +162,7 @@ extra_rdoc_files: []
162
162
  files:
163
163
  - ".coveralls.yml"
164
164
  - ".gitignore"
165
+ - ".ruby-version"
165
166
  - ".travis.yml"
166
167
  - Gemfile
167
168
  - LICENSE.txt
@@ -190,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
191
  requirements:
191
192
  - - ">="
192
193
  - !ruby/object:Gem::Version
193
- version: '0'
194
+ version: 2.5.1
194
195
  required_rubygems_version: !ruby/object:Gem::Requirement
195
196
  requirements:
196
197
  - - ">="
@@ -198,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
199
  version: '0'
199
200
  requirements: []
200
201
  rubyforge_project:
201
- rubygems_version: 2.4.5.1
202
+ rubygems_version: 2.7.6
202
203
  signing_key:
203
204
  specification_version: 4
204
205
  summary: Command-line runner for ruby code.