console_runner 0.1.6 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92620ae23eba7d062d9302b75f3bf3f9b97d8b18
4
- data.tar.gz: e7b17cb4a69c44f876315e2dbeaba1a87cf48a7d
3
+ metadata.gz: d0fdcaea27d9c7a47a33276fe97eaadb5063a1bb
4
+ data.tar.gz: f67cf5ec2116676fce4b14a7a8b4807f0a42fb17
5
5
  SHA512:
6
- metadata.gz: 2efe3e3cdfe82eb5cecccac7a4a861efd59916824b7f1ea15da7df29a04265d0fccf596044311d2f4580ef9c22c6a7c8f9bd84abc638fdde41c127ffa89affdb
7
- data.tar.gz: e179152ad9a70a59cce95f871a528077352a53b006d878c6c0bc3702fdcfac02183ab742938b094aea382f7c8547a6ce2dfb81af29931366ce2776b25967c307
6
+ metadata.gz: 50ff21a11dbb5b47c459807520468000c69c2279d426444a9a2d37376cf1d5695edbb5fd04253c9de4c3c059c28786491da6cbc4666f15778f5f4e09574731a3
7
+ data.tar.gz: 6dc150a981b78e4ff5d572efc948e92f179223f85196ea32a9cc4b370c7521bfc9c68e3db8ff646dea3e70f14dfcc0c5524f7546947a0486127ef690ccb6f64a
data/README.md CHANGED
@@ -131,4 +131,4 @@ The gem is available as open source under the terms of the [MIT License](http://
131
131
  [BS img]: https://travis-ci.org/yuri-karpovich/console_runner.svg?branch=master
132
132
  [DS img]: https://gemnasium.com/badges/github.com/yuri-karpovich/console_runner.svg
133
133
  [CC img]: https://codeclimate.com/github/yuri-karpovich/console_runner.png
134
- [CS img]: https://coveralls.io/repos/github/yuri-karpovich/console_runner/badge.svg
134
+ [CS img]: https://coveralls.io/repos/github/yuri-karpovich/console_runner/badge.svg?branch=master
data/lib/cmd_parser.rb CHANGED
@@ -1,95 +1,21 @@
1
1
  require 'trollop'
2
+ require 'method_parser'
2
3
 
3
- # Parses command line
4
+ # Parses command line and configure #Trollop
4
5
  class CmdParser
5
-
6
- # Parses method code
7
- class ParsedMethod
8
- attr_reader :method, :name, :param_tags, :option_tags
9
- attr_accessor :cmd_opts
10
-
11
- def initialize(method)
12
- @method = method
13
- @name = @method.name
14
- @parameters = @method.parameters
15
- @param_tags = FileParser.select_param_tags @method
16
- @option_tags = FileParser.select_option_tags @method
17
- @cmd_opts = nil
18
- end
19
-
20
- def params_array
21
- expected_params = @parameters.map(&:first).map.with_index { |p, i| [i, p] }.to_h
22
- options_groups = {}
23
- get_params = {}
24
-
25
- expected_params.each do |index, name|
26
- if options_group?(name)
27
- options_groups[index] = name
28
- get_params[index] = option_as_hash(name)
29
- else
30
- get_params[index] = @cmd_opts[name.to_sym]
31
- end
32
- end
33
- get_params = get_params.to_a.sort_by { |a| a[0] }.reverse
34
-
35
- stop_delete = false
36
- get_params.delete_if do |a|
37
- index = a[0]
38
- value = a[1]
39
- result = false
40
-
41
- if options_groups[index]
42
- result = value == {}
43
- else
44
- result = value == nil
45
- end
46
- stop_delete = true unless result
47
- next if stop_delete
48
- result
49
- end
50
-
51
- get_params.sort_by { |a| a[0] }.map { |a| a[1] }
52
- end
53
-
54
- def param_tags_names
55
- param_tags.map { |t| t.name }
56
- end
57
-
58
- def option_tags_names
59
- option_tags.map { |t| t.pair.name.gsub(':', '') }
60
- end
61
-
62
- def options_group?(param_name)
63
- option_tags.any? { |t| t.name == param_name }
64
- end
65
-
66
- def default_params
67
- @parameters.map do |array|
68
- array.map do |a|
69
- if a
70
- ['"', "'"].include?(a[0]) && ['"', "'"].include?(a[-1]) ? a[1..-2] : a
71
- else
72
- a
73
- end
74
- end
75
- end.to_h
76
- end
77
-
78
- private
79
-
80
- def option_as_hash(options_group_name)
81
- result = {}
82
- option_tags.select { |t| t.name == options_group_name }.each do |t|
83
- option_name = t.pair.name.gsub(':', '')
84
- option_value = @cmd_opts[option_name.to_sym]
85
- result[option_name] = option_value if option_value
86
- end
87
- result
88
- end
89
- end
90
-
91
6
  attr_reader :global_opts, :method, :init_method
92
-
7
+ TYPES_MAPPINGS = {
8
+ 'String' => :string,
9
+ 'Integer' => :int,
10
+ 'Fixnum' => :int,
11
+ 'Float' => :float,
12
+ 'Boolean' => :boolean,
13
+ 'Array(String)' => :strings,
14
+ 'Array(Integer)' => :ints,
15
+ 'Array(Fixnum)' => :ints,
16
+ 'Array(Float)' => :floats,
17
+ 'Array(Boolean)' => :booleans
18
+ }.freeze
93
19
  # Should be executed before ARGV.shift
94
20
  def initialize(runnable_methods, init_method= nil)
95
21
  clazz = runnable_methods.first.parent
@@ -99,7 +25,7 @@ class CmdParser
99
25
  @parser.stop_on sub_commands
100
26
 
101
27
  if init_method
102
- @init_method = ParsedMethod.new init_method
28
+ @init_method = MethodParser.new init_method
103
29
  same_methods = @init_method.param_tags_names & @init_method.option_tags_names
104
30
  raise "You have the same name for @param and @option attribute(s): #{same_methods.join(', ')}.
105
31
  Use different names to `console_runner` be able to run #{@init_method.name} method." if same_methods.count > 0
@@ -113,7 +39,7 @@ Use different names to `console_runner` be able to run #{@init_method.name} meth
113
39
  options = option_tags.select { |t| t.name == tag.name }
114
40
  if options.count > 0
115
41
  options.each do |option|
116
- option_name = option.pair.name.gsub(':', '')
42
+ option_name = option.pair.name.delete(':')
117
43
  option_text = option.pair.text
118
44
  option_type = option.pair.type
119
45
  @parser.opt(option_name.to_sym, "(Ruby class: #{option_type}) " + option_text.to_s, type: CmdParser.parse_type(option_type))
@@ -138,14 +64,14 @@ Use different names to `console_runner` be able to run #{@init_method.name} meth
138
64
 
139
65
  if @init_method
140
66
  given_attrs = @global_opts.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
141
- @init_method.cmd_opts = @global_opts.select { |k, v| given_attrs.include? k }
67
+ @init_method.cmd_opts = @global_opts.select { |k, _| given_attrs.include? k }
142
68
  end
143
69
  end
144
70
 
145
-
71
+ # Parse method and configure #Trollop
146
72
  def parse_method(method)
147
73
  ARGV.shift
148
- @method = ParsedMethod.new method
74
+ @method = MethodParser.new method
149
75
  same_methods = @method.param_tags_names & @method.option_tags_names
150
76
  raise "You have the same name for @param and @option attribute(s): #{same_methods.join(', ')}.
151
77
  Use different names to `console_runner` be able to run #{@method.name} method." if same_methods.count > 0
@@ -159,7 +85,7 @@ Use different names to `console_runner` be able to run #{@method.name} method."
159
85
  options = @method.option_tags.select { |t| t.name == tag.name }
160
86
  if options.count > 0
161
87
  options.each do |option|
162
- option_name = option.pair.name.gsub(':', '')
88
+ option_name = option.pair.name.delete(':')
163
89
  option_text = option.pair.text
164
90
  option_type = option.pair.type
165
91
  @parser.opt(option_name.to_sym, "(Ruby class: #{option_type}) " + option_text.to_s, type: CmdParser.parse_type(option_type))
@@ -179,7 +105,7 @@ Use different names to `console_runner` be able to run #{@method.name} method."
179
105
  @parser.parse ARGV
180
106
  end
181
107
  given_attrs = cmd_opts.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
182
- @method.cmd_opts = cmd_opts.select { |k, v| given_attrs.include? k }
108
+ @method.cmd_opts = cmd_opts.select { |k, _| given_attrs.include? k }
183
109
  @method.default_params.each do |k, v|
184
110
  @method.cmd_opts[k.to_sym] ||= v
185
111
  end
@@ -193,30 +119,9 @@ Use different names to `console_runner` be able to run #{@method.name} method."
193
119
  end
194
120
 
195
121
  def self.parse_type(yard_type)
196
- case yard_type
197
- when 'String'
198
- :string
199
- when 'Integer'
200
- :int
201
- when 'Fixnum'
202
- :int
203
- when 'Float'
204
- :float
205
- when 'Boolean'
206
- :boolean
207
- when 'Array(String)'
208
- :strings
209
- when 'Array(Integer)'
210
- :ints
211
- when 'Array(Fixnum)'
212
- :ints
213
- when 'Array(Float)'
214
- :floats
215
- when 'Array(Boolean)'
216
- :booleans
217
- else
218
- raise "Unsupported YARD type: #{yard_type}"
219
- end
122
+ result = TYPES_MAPPINGS[yard_type]
123
+ raise "Unsupported YARD type: #{yard_type}" unless result
124
+ result
220
125
  end
221
126
 
222
127
  end
@@ -1,3 +1,3 @@
1
1
  module ConsoleRunner
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
3
3
  end
@@ -1,13 +1,13 @@
1
- module ConsoleRunner
1
+ require 'file_parser'
2
+ require 'cmd_parser'
3
+ require 'runner'
4
+ require 'console_runner/version'
2
5
 
3
- require 'file_parser'
4
- require 'cmd_parser'
5
- require 'runner'
6
6
 
7
+ module ConsoleRunner
7
8
  file_from_arg = ARGV.shift
8
9
  raise ConsoleRunnerError, 'Specify file to be executed' unless file_from_arg
9
- file_path ||= File.realpath file_from_arg
10
- raise ConsoleRunnerError, "File doesn't exist: #{file_path}" unless File.exist? file_path
10
+ file_path = File.realpath file_from_arg
11
11
  file_parser = FileParser.new(file_path)
12
12
 
13
13
 
@@ -59,7 +59,9 @@ Runnable class should be marked with @#{FileParser::RUNNABLE_TAG} tag"
59
59
  when :class
60
60
  klass_obj.send(action_method.name, *method_params)
61
61
  when :instance
62
- init_params = cmd_parser.init_method.params_array
62
+ init_method = cmd_parser.init_method
63
+ init_params = []
64
+ init_params = init_method.params_array if init_method
63
65
  # TODO catch errors
64
66
  obj = klass_obj.new(*init_params)
65
67
  obj.send(action_method.name, *method_params)
data/lib/file_parser.rb CHANGED
@@ -1,17 +1,16 @@
1
1
  require 'yard'
2
- require 'console_runner/version'
3
2
  require 'console_runner_error'
4
3
 
5
4
  # Parses code in a file
6
5
  class FileParser
7
- attr_reader :all_objects
8
-
9
6
  RUNNABLE_TAG = :runnable
10
7
 
8
+ attr_reader :all_objects
9
+
11
10
  # Parse file with #YARD::CLI::Stats
12
11
  #
13
- # @param [String] file_path path to the file to be parsed,
14
- # @return[Array]
12
+ # @param [String] file_path path to the file to be parsed
13
+ # @return[Array(YARD::CodeObjects)] array of #YARD::CodeObjects classes
15
14
  def initialize(file_path)
16
15
  raise ConsoleRunnerError "Cannot find file #{file_path}" unless File.exist?(file_path)
17
16
  code = YARD::CLI::Stats.new
@@ -19,11 +18,11 @@ class FileParser
19
18
  @all_objects = code.all_objects
20
19
  end
21
20
 
22
-
23
- # List methods
21
+ # List of methods
24
22
  # @param [Symbol] scope :all - list all methods, :runnable - list only runnable methods
25
23
  # @param [YARD::CodeObjects::ClassObject, nil] clazz list methods of specified class only
26
- def list_methods(scope= :all, clazz = nil)
24
+ # @return [Array(YARD::CodeObjects::MethodObject)]
25
+ def list_methods(scope = :all, clazz = nil)
27
26
  all_methods = @all_objects.select { |o| o.type == :method }
28
27
  all_class_methods = []
29
28
  all_class_methods = clazz.children.select { |m| m.class == YARD::CodeObjects::MethodObject } if clazz
@@ -42,12 +41,14 @@ class FileParser
42
41
  all_methods.select { |m| m.has_tag? RUNNABLE_TAG }
43
42
  end
44
43
  else
45
- raise ":key can be :all or #{RUNNABLE_TAG}"
44
+ raise ":scope can be :all or #{RUNNABLE_TAG}"
46
45
  end
47
46
  end
48
47
 
49
48
  # List classes
49
+ #
50
50
  # @param [Symbol] scope :all - list all classes, :runnable - list only runnable classes
51
+ # @return [Array(YARD::CodeObjects::ClassObject)]
51
52
  def list_classes(scope= :all)
52
53
  all_classes = @all_objects.select { |o| o.type == :class }
53
54
  case scope
@@ -56,18 +57,30 @@ class FileParser
56
57
  when RUNNABLE_TAG
57
58
  all_classes.select { |m| m.has_tag? RUNNABLE_TAG }
58
59
  else
59
- raise ':key can be :all or :runnable'
60
+ raise ":scope can be :all or #{RUNNABLE_TAG}"
60
61
  end
61
62
  end
62
63
 
64
+ # Select all @runnable tags from of specified object
65
+ #
66
+ # @param [YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject] yard_object YARD object
67
+ # @return [Array(YARD::Tags::Tag)]
63
68
  def self.select_runnable_tags(yard_object)
64
69
  yard_object.tags.select { |t| t.tag_name == RUNNABLE_TAG.to_s }
65
70
  end
66
71
 
72
+ # Select all @option tags from of specified object
73
+ #
74
+ # @param [YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject] yard_object YARD object
75
+ # @return [Array(YARD::Tags::Tag)]
67
76
  def self.select_option_tags(yard_object)
68
77
  yard_object.tags.select { |t| t.tag_name == 'option' }
69
78
  end
70
79
 
80
+ # Select all @param tags from of specified object
81
+ #
82
+ # @param [YARD::CodeObjects::MethodObject, YARD::CodeObjects::ClassObject] yard_object YARD object
83
+ # @return [Array(YARD::Tags::Tag)]
71
84
  def self.select_param_tags(yard_object)
72
85
  yard_object.tags.select { |t| t.tag_name == 'param' }
73
86
  end
@@ -85,4 +98,4 @@ module YARD
85
98
  def print_undocumented_objects; end
86
99
  end
87
100
  end
88
- end
101
+ end
@@ -0,0 +1,99 @@
1
+ # Parses method code
2
+ class MethodParser
3
+ attr_reader :method,
4
+ :name,
5
+ :param_tags, # All method parameters tags
6
+ :option_tags # Only options tags
7
+
8
+ attr_accessor :cmd_opts
9
+
10
+ # @param [YARD::CodeObjects::MethodObject] method YARD method object to be parsed
11
+ def initialize(method)
12
+ @method = method
13
+ @name = @method.name
14
+ @parameters = @method.parameters
15
+ @param_tags = FileParser.select_param_tags @method
16
+ @option_tags = FileParser.select_option_tags @method
17
+ @cmd_opts = nil
18
+ end
19
+
20
+ # Prepare
21
+ def params_array
22
+ expected_params = @parameters.map(&:first).map.with_index { |p, i| [i, p] }.to_h
23
+ options_groups = {}
24
+ get_params = {}
25
+
26
+ expected_params.each do |index, name|
27
+ if options_group?(name)
28
+ options_groups[index] = name
29
+ get_params[index] = option_as_hash(name)
30
+ else
31
+ get_params[index] = @cmd_opts[name.to_sym]
32
+ end
33
+ end
34
+ get_params = get_params.to_a.sort_by { |a| a[0] }.reverse
35
+
36
+ stop_delete = false
37
+ get_params.delete_if do |a|
38
+ index = a[0]
39
+ value = a[1]
40
+ result = false
41
+
42
+ if options_groups[index]
43
+ result = value == {}
44
+ else
45
+ result = value == nil
46
+ end
47
+ stop_delete = true unless result
48
+ next if stop_delete
49
+ result
50
+ end
51
+
52
+ get_params.sort_by { |a| a[0] }.map { |a| a[1] }
53
+ end
54
+
55
+
56
+ # @return [Array(String)] Names of parameters
57
+ def param_tags_names
58
+ param_tags.map(&:name)
59
+ end
60
+
61
+ # @return [Array(String)] Names of options
62
+ def option_tags_names
63
+ option_tags.map { |t| t.pair.name.delete(':') }
64
+ end
65
+
66
+ # Check if the name is an option
67
+ #
68
+ # @param [String] param_name name of parameter to be verified
69
+ # @return [Boolean] true if current parameter name is an option key
70
+ def options_group?(param_name)
71
+ option_tags.any? { |t| t.name == param_name }
72
+ end
73
+
74
+ # @return [Hash] default values for parameters
75
+ def default_params
76
+ @parameters.map do |array|
77
+ array.map do |a|
78
+ if a
79
+ ['"', "'"].include?(a[0]) && ['"', "'"].include?(a[-1]) ? a[1..-2] : a
80
+ else
81
+ a
82
+ end
83
+ end
84
+ end.to_h
85
+ end
86
+
87
+ private
88
+
89
+ # @return [Hash] options parameter as Hash
90
+ def option_as_hash(options_group_name)
91
+ result = {}
92
+ option_tags.select { |t| t.name == options_group_name }.each do |t|
93
+ option_name = t.pair.name.delete(':')
94
+ option_value = @cmd_opts[option_name.to_sym]
95
+ result[option_name] = option_value if option_value
96
+ end
97
+ result
98
+ end
99
+ end
data/lib/runner.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Runner
2
2
 
3
- def self.run &block
3
+ def self.run
4
4
  start_time = Time.now
5
5
  puts "Start Time: #{start_time}"
6
6
  Thread.current[:id] = 'main'
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.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Karpovich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-11 00:00:00.000000000 Z
11
+ date: 2017-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -162,6 +162,7 @@ files:
162
162
  - lib/console_runner/version.rb
163
163
  - lib/console_runner_error.rb
164
164
  - lib/file_parser.rb
165
+ - lib/method_parser.rb
165
166
  - lib/runner.rb
166
167
  homepage: https://github.com/yuri-karpovich/console_runner
167
168
  licenses: