toolrack 0.22.0 → 0.23.2

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
  SHA256:
3
- metadata.gz: 61af82cfa37677d8c6c2896f69b0c0469adb30906db903a6885075136c25bd93
4
- data.tar.gz: 1687d3b4a80e392b36a1e51f24d90545e097fde2645ef6c9e3c7d57479d9fd42
3
+ metadata.gz: 7829bd272823dbd7bdf754bca5be1287b074486f99a33ee15bd2607cb391a2f5
4
+ data.tar.gz: ea82bc8cca37d760813fb276e7540df2d80b11624c8e9760d6936cd4c86bf347
5
5
  SHA512:
6
- metadata.gz: 7a43d658eda38670b396064a5462e17321b28dac13c084654d966334dc25f090fc541fbc37055d947e8ea97e6fa2a4b9fed0ed4c2059f94358af671ebcf4802c
7
- data.tar.gz: c1509319be87e61699c7fff10ad1034730c7ef9d87c2fa2186e66def319d984a1f2ce1586a05ec04c336d85d3842bfb06d8f9516421714e0012938510a3c5e30
6
+ metadata.gz: 2dc8a2048cb53ea6d6ed2bacaf3cbd7e2a1c8ea8a725c9657b5dd69fdcb49d9f4b6e3aeea12a36772ba54449d3b4cd47bf5be376d77e1b1fb6091ad5f68261d2
7
+ data.tar.gz: 1ceb5fd6b3bdd488b1961bff52c7cce4d4e68f31f842f7029f4eeb73452d52c19a20b772d7ed1a8a11fbb6ffc83dd0474943131d304991e905ac87d97e95a2e1
data/.release_history.yml CHANGED
@@ -30,3 +30,5 @@ toolrack:
30
30
  :timestamp: 1679815976.724487
31
31
  - :version: 0.21.0
32
32
  :timestamp: 1680235248.152352
33
+ - :version: 0.22.0
34
+ :timestamp: 1680773287.074721
data/Gemfile.lock CHANGED
@@ -1,11 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- toolrack (0.22.0)
4
+ toolrack (0.23.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ colorize (1.1.0)
9
10
  devops_assist (0.3.11)
10
11
  git_cli
11
12
  git_cli_prompt (~> 0.3.3)
@@ -14,8 +15,7 @@ GEM
14
15
  toolrack
15
16
  tty-prompt
16
17
  diff-lcs (1.5.0)
17
- git_cli (0.11.2)
18
- gvcs
18
+ git_cli (0.12.0)
19
19
  ptools (~> 1.4.0)
20
20
  teLogger
21
21
  toolrack
@@ -23,27 +23,37 @@ GEM
23
23
  teLogger
24
24
  toolrack
25
25
  tty-prompt
26
- gvcs (0.1.1)
27
- minitest (5.18.0)
26
+ gvcs (0.1.2)
27
+ minitest (5.19.0)
28
28
  pastel (0.8.0)
29
29
  tty-color (~> 0.5)
30
30
  ptools (1.4.3)
31
31
  rake (12.3.3)
32
+ release-gem (0.1.25)
33
+ colorize
34
+ git_cli
35
+ gvcs
36
+ teLogger
37
+ toolrack
38
+ tty-command
39
+ tty-prompt
32
40
  rspec (3.12.0)
33
41
  rspec-core (~> 3.12.0)
34
42
  rspec-expectations (~> 3.12.0)
35
43
  rspec-mocks (~> 3.12.0)
36
- rspec-core (3.12.1)
44
+ rspec-core (3.12.2)
37
45
  rspec-support (~> 3.12.0)
38
- rspec-expectations (3.12.2)
46
+ rspec-expectations (3.12.3)
39
47
  diff-lcs (>= 1.2.0, < 2.0)
40
48
  rspec-support (~> 3.12.0)
41
- rspec-mocks (3.12.5)
49
+ rspec-mocks (3.12.6)
42
50
  diff-lcs (>= 1.2.0, < 2.0)
43
51
  rspec-support (~> 3.12.0)
44
- rspec-support (3.12.0)
52
+ rspec-support (3.12.1)
45
53
  teLogger (0.2.2)
46
54
  tty-color (0.6.0)
55
+ tty-command (0.10.1)
56
+ pastel (~> 0.8)
47
57
  tty-cursor (0.7.1)
48
58
  tty-prompt (0.23.1)
49
59
  pastel (~> 0.8)
@@ -62,6 +72,7 @@ DEPENDENCIES
62
72
  devops_assist
63
73
  minitest (~> 5.0)
64
74
  rake (~> 12.0)
75
+ release-gem
65
76
  rspec
66
77
  toolrack!
67
78
 
data/Rakefile CHANGED
@@ -10,3 +10,4 @@ Rake::TestTask.new(:test) do |t|
10
10
  end
11
11
 
12
12
  task :default => :test
13
+ require 'release/gem'
@@ -0,0 +1,254 @@
1
+
2
+
3
+ module Antrapol
4
+ module ToolRack
5
+
6
+ module ArgUtils
7
+ include Antrapol::ToolRack::ConditionUtils
8
+
9
+ class ArgParsingException < StandardError; end
10
+ class RequiredFieldEmpty < ArgParsingException; end
11
+ class DuplicatedField < ArgParsingException; end
12
+ class InvalidKey < ArgParsingException; end
13
+
14
+ module ClassMethods
15
+ include Antrapol::ToolRack::ConditionUtils
16
+
17
+ def arg_spec(&block)
18
+ class_eval(&block)
19
+ end
20
+
21
+ def opt(key, desc, opts = {}, &block)
22
+ raise RequiredFieldEmpty, "Key field cannot be empty" if is_empty?(key)
23
+ raise DuplicatedField, "Given key '#{key}' already exist" if options.keys.include?(key)
24
+ raise RequiredFieldEmpty, "Block is require" if not block
25
+
26
+ options[key] = { key: key, desc: desc, options: opts, callback: block }
27
+ required << key if opts[:required] == true
28
+ end
29
+
30
+ def opt_alias(existing, new)
31
+ raise InvalidKey, "Existing key '#{existing}'to map to new key '#{new}' not found" if not options.keys.include?(existing)
32
+ aliases[new] = existing
33
+ end
34
+
35
+ def is_all_required_satisfy?(arr)
36
+ if arr.is_a?(Array)
37
+ res = true
38
+ required.each do |f|
39
+ res = arr.include?(f)
40
+ if not res
41
+ al = aliases.invert[f]
42
+ res = arr.include?(al)
43
+ raise RequiredFieldEmpty, "Required parameter '#{f}' is not given" if not res
44
+ end
45
+ end
46
+ res
47
+
48
+ else
49
+ raise ArgParsingException, "Given array to check for required fields is not an array. Got #{arr}"
50
+ if arr.is_a?(ArgParsingException)
51
+ STDERR.puts "Error throwing into the method was : #{arr.message}"
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ def callback(evt, opts = {}, &block)
58
+ raise ArgParsingException, "Event should not be nil" if evt.nil?
59
+ callbacks[evt] = { opts: opts, cb: block }
60
+ end
61
+
62
+ def required
63
+ if @_req.nil?
64
+ @_req = []
65
+ end
66
+ @_req
67
+ end
68
+
69
+ def options
70
+ if @_options.nil?
71
+ @_options = {}
72
+ end
73
+ @_options
74
+ end
75
+
76
+ def aliases
77
+ if @_aliases.nil?
78
+ @_aliases = {}
79
+ end
80
+ @_aliases
81
+ end
82
+
83
+ def callbacks
84
+ if @_cb.nil?
85
+ @_cb = {}
86
+ end
87
+ @_cb
88
+ end
89
+
90
+ #def value_separator=(val)
91
+ # logger.debug "Setting value separator #{val}"
92
+ # @_valSep = val
93
+ #end
94
+ def set_value_separator(val)
95
+ @_valSep = val
96
+ end
97
+
98
+ def value_separator
99
+ if @_valSep.nil?
100
+ @_valSep = " "
101
+ end
102
+ @_valSep
103
+ end
104
+
105
+ def logger
106
+ Antrapol::ToolRack.logger(:c_arg_utils)
107
+ end
108
+
109
+ end # module ClassMethods
110
+ def self.included(klass)
111
+ klass.extend(ClassMethods)
112
+ end
113
+
114
+ def parse_argv(argv, &block)
115
+ cb = self.class.callbacks[:pre_processing]
116
+ if not_empty?(cb) and cb[:cb]
117
+ #logger.debug "Calling pre-processing for class #{self}"
118
+ @parse_argv_block = block
119
+ # here will engage the DSL method of the included class
120
+ update_argv, val = instance_exec(argv, &cb[:cb])
121
+ #logger.debug "Preprocessing return update flag : #{update_argv} and list #{val} for class #{self}"
122
+ argv = val if update_argv
123
+ end
124
+
125
+ # split the key and value if given
126
+ # > app -n:john
127
+ if self.class.value_separator == " " and argv.length > 0
128
+ parse_argv_space(argv)
129
+ else
130
+ parse_argv_key_value_mixed(argv)
131
+ end
132
+
133
+ cbp = self.class.callbacks[:post_processing]
134
+ if not_empty?(cbp) and cbp[:cb]
135
+ #logger.debug "Post processing got #{argv}"
136
+ instance_exec(argv, &cbp[:cb])
137
+ end
138
+ end
139
+
140
+ private
141
+ def parse_argv_space(argv)
142
+ logger.debug "got argv (space) : #{argv}"
143
+ cls = self.class
144
+ clear = cls.is_all_required_satisfy?(argv)
145
+ logger.debug "All required fields are there? : #{clear}"
146
+ if clear
147
+
148
+ go = true
149
+ i = 0
150
+ while i < argv.length
151
+ a = argv[i]
152
+ logger.debug "Processing : #{a}"
153
+
154
+ key = a
155
+ conf = cls.options[key]
156
+ if is_empty?(conf)
157
+ al = cls.aliases[key]
158
+ conf = cls.options[al] if not_empty?(al)
159
+ end
160
+
161
+ if is_empty?(conf)
162
+ logger.warn "Given key to select ('#{key}') has not defined before"
163
+ # ignore if the given parameter not recognized
164
+ i += 1
165
+ next
166
+ end
167
+
168
+ if not conf[:callback].nil?
169
+ # expecting parameter
170
+ paramsCount = conf[:callback].arity
171
+ if paramsCount > 0
172
+ # look ahead token to load the parameters
173
+ val = []
174
+ (0...paramsCount).each do |ii|
175
+ val << argv[i+1+ii]
176
+ end
177
+ val.delete_if { |e| e.nil? }
178
+ logger.debug "Parameter for callback : #{val}"
179
+ raise RequiredFieldEmpty, "Key '#{a}' requires #{paramsCount} parameter(s) but got #{val.length}" if paramsCount != val.length
180
+ instance_exec(*val, &conf[:callback])
181
+ i += paramsCount+1
182
+ else
183
+ instance_eval(&conf[:callback])
184
+ i += 1
185
+ end
186
+
187
+ else
188
+ i += 1
189
+
190
+ end
191
+
192
+ end # argv.each
193
+
194
+ end # if all required satisfy
195
+
196
+ end
197
+
198
+ def parse_argv_key_value_mixed(argv)
199
+ logger.debug "got argv : #{argv}"
200
+ cls = self.class
201
+ if cls.is_all_required_satisfy?(argv)
202
+
203
+ argv.each do |a|
204
+ logger.debug "Processing : #{a}"
205
+
206
+ key = a
207
+ val = []
208
+ #p a
209
+ #p cls.value_separator
210
+ #p a =~ /#{cls.value_separator}/
211
+ if (a =~ /#{cls.value_separator}/) != nil
212
+ keys = a.split(cls.value_separator)
213
+ key = keys.first
214
+ val = keys[1..-1]
215
+ end
216
+
217
+ logger.debug "After separation : #{key}"
218
+
219
+ conf = cls.options[key]
220
+ if is_empty?(conf)
221
+ al = cls.aliases[key]
222
+ conf = cls.options[al] if not_empty?(al)
223
+ end
224
+
225
+ if is_empty?(conf)
226
+ logger.warn "Given key to select ('#{key}') has not defined before"
227
+ # ignore if the given parameter not recognized
228
+ next
229
+ end
230
+
231
+ if not conf[:callback].nil?
232
+ # expecting parameter
233
+ paramsCount = conf[:callback].arity
234
+ if paramsCount > 0
235
+ raise RequiredFieldEmpty, "Key '#{conf[:key]}' requires #{paramsCount} value(s) to be given but got #{val.length}." if val.length != paramsCount
236
+ instance_exec(*val, &conf[:callback])
237
+ else
238
+ instance_eval(&conf[:callback])
239
+ end
240
+ end
241
+
242
+ end # argv.each
243
+
244
+ end # if all required satisfy
245
+ end
246
+
247
+ def logger
248
+ Antrapol::ToolRack.logger(:arg_utils)
249
+ end
250
+
251
+ end
252
+
253
+ end
254
+ end
@@ -56,7 +56,7 @@ module Antrapol
56
56
  possible.each do |app|
57
57
  avail << app if not File.which(app).nil?
58
58
  end
59
- elsif RuntimeUtils..on_windows?
59
+ elsif RuntimeUtils.on_windows?
60
60
  avail << "cmd.exe"
61
61
  elsif RuntimeUtils.on_mac?
62
62
  avail << "Terminal"
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.22.0"
3
+ VERSION = "0.23.2"
4
4
  end
5
5
  end
6
6
 
data/lib/toolrack.rb CHANGED
@@ -19,11 +19,40 @@ require_relative 'toolrack/version_utils'
19
19
  require_relative 'toolrack/file_utils'
20
20
  require_relative 'toolrack/block_params_utils'
21
21
  require_relative 'toolrack/terminal_utils'
22
+ require_relative 'toolrack/arg_utils'
23
+
22
24
 
23
25
  module Antrapol
24
26
  module ToolRack
27
+ include ConditionUtils
28
+
25
29
  class Error < StandardError; end
26
30
  # Your code goes here...
31
+ #
32
+ def self.logger(tag = nil, &block)
33
+ if @_logger.nil?
34
+ @_logger = TeLogger::Tlogger.new
35
+ end
36
+
37
+ if block
38
+ if not_empty?(tag)
39
+ @_logger.with_tag(tag, &block)
40
+ else
41
+ @_logger.with_tag(@_logger.tag, &block)
42
+ end
43
+ else
44
+ if is_empty?(tag)
45
+ @_logger.tag = :tr
46
+ @_logger
47
+ else
48
+ # no block but tag is given? hmm
49
+ @_logger.tag = tag
50
+ @_logger
51
+ end
52
+ end
53
+
54
+ end
55
+
27
56
  end
28
57
  end
29
58
 
@@ -68,3 +97,5 @@ TR::BlockParamsUtils = ToolRack::BlockParamsUtils
68
97
 
69
98
  TR::TerminalUtils = ToolRack::TerminalUtils
70
99
 
100
+ TR::ArgUtils = ToolRack::ArgUtils
101
+
data/toolrack.gemspec CHANGED
@@ -32,4 +32,6 @@ Gem::Specification.new do |spec|
32
32
  #spec.add_development_dependency "devops_helper" #, "~> 0.1.0"
33
33
  spec.add_development_dependency "devops_assist"
34
34
  spec.add_development_dependency "rspec"
35
+ spec.add_development_dependency "release-gem"
36
+
35
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toolrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.0
4
+ version: 0.23.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-06 00:00:00.000000000 Z
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devops_assist
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: release-gem
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'
41
55
  description: Just collections of utilities
42
56
  email:
43
57
  - chrisliaw@antrapol.com
@@ -58,6 +72,7 @@ files:
58
72
  - bin/console
59
73
  - bin/setup
60
74
  - lib/toolrack.rb
75
+ - lib/toolrack/arg_utils.rb
61
76
  - lib/toolrack/block_params_utils.rb
62
77
  - lib/toolrack/cli_utils.rb
63
78
  - lib/toolrack/condition_utils.rb
@@ -99,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
114
  - !ruby/object:Gem::Version
100
115
  version: '0'
101
116
  requirements: []
102
- rubygems_version: 3.4.10
117
+ rubygems_version: 3.4.18
103
118
  signing_key:
104
119
  specification_version: 4
105
120
  summary: Collection of simple utilities but I find it increase clarity