squared 0.4.36 → 0.5.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.
@@ -17,13 +17,13 @@ module Squared
17
17
  val = val.strip
18
18
  return [val, '', ''] unless (i = val.index('='))
19
19
 
20
- last = val[(i + 1)..-1].strip
20
+ last = val[i + 1..-1].strip
21
21
  quote = ''
22
22
  if last =~ /\A(["'])(.+)\1\z/
23
23
  last = $2
24
24
  quote = $1
25
25
  end
26
- [val[0..(i - 1)], last, quote]
26
+ [val[0..i - 1], last, quote]
27
27
  end
28
28
 
29
29
  def task_invoke(*cmd, args: [], exception: true, warning: true)
@@ -88,41 +88,16 @@ module Squared
88
88
  end
89
89
  end
90
90
 
91
- def time_offset(val = nil)
92
- val = DateTime.parse(val) if val.is_a?(::String)
93
- cur = DateTime.now
94
- ret = 0
95
- if (r = /^([+-])(\d+):(\d+):(\d+)$/.match((val || cur).strftime('%::z')))
96
- ret += (r[1] == '+' ? -1 : 1) * ((r[2].to_i * 60 * 60) + (r[3].to_i * 60) + r[4].to_i) * 1000
97
- end
98
- return ret unless val
99
-
100
- (cur.strftime('%Q').to_i + time_offset) - (val.strftime('%Q').to_i + ret)
101
- end
102
-
103
91
  def time_since(val, ms: true)
104
92
  s = ms ? '%s%L' : '%s'
105
93
  Time.now.utc.strftime(s).to_i - Time.parse(val).utc.strftime(s).to_i
106
94
  end
107
95
 
108
- def rand_s(size)
109
- if RUBY_VERSION >= '3.1'
110
- require 'random/formatter'
111
- Random.new.alphanumeric(size)
112
- else
113
- (0...size).map { rand(97..122).chr }.join
114
- end
115
- end
116
-
117
96
  def env(key, default = nil, suffix: nil, strict: false, equals: nil, ignore: nil)
118
97
  ret = env_value(key, suffix: suffix, strict: strict)
119
98
  return ret == equals.to_s unless equals.nil?
120
99
 
121
- ret.empty? || (ignore && Array(ignore).any? { |val| val.to_s == ret }) ? default : ret
122
- end
123
-
124
- def env_key(*val)
125
- val.join('_').gsub(/\W+/, '_').upcase
100
+ ret.empty? || (ignore && as_a(ignore).any? { |val| val.to_s == ret }) ? default : ret
126
101
  end
127
102
 
128
103
  def env_value(key, default = '', suffix: nil, strict: false)
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
4
+
3
5
  require_relative 'common/base'
4
- require_relative 'common/class'
5
6
  require_relative 'common/format'
6
7
  require_relative 'common/prompt'
7
8
  require_relative 'common/shell'
@@ -12,20 +12,22 @@ module Squared
12
12
  include Rake::DSL
13
13
 
14
14
  class << self
15
- def parse(gem, namespace, ext = [gem])
15
+ def parse(gem, namespace, ext = [pkg])
16
16
  require gem
17
- [eval(namespace), Array(ext)].tap do |data|
18
- data.last.each { |key| @@mime_obj[key] = data }
19
- end
17
+ obj = eval namespace
18
+ ext = [ext] unless ext.is_a?(Array)
19
+ ext.each { |val| @@mime_obj[val] = [obj, ext] }
20
20
  rescue LoadError, NameError => e
21
21
  warn e
22
22
  nil
23
+ else
24
+ @@mime_obj[ext.first]
23
25
  end
24
26
 
25
- def link(project, main = project.dependfile&.basename, name = nil, **kwargs, &blk)
26
- return unless project.enabled? && main
27
+ def link(project, main = project.dependfile.basename, name = nil, **kwargs, &blk)
28
+ return unless project.enabled?
27
29
 
28
- ret = Viewer.new(main, name, project: project, **kwargs)
30
+ ret = new(main, name, project: project, **kwargs)
29
31
  ret.instance_eval(&blk) if block_given?
30
32
  ret
31
33
  end
@@ -87,16 +89,16 @@ module Squared
87
89
  @required = true
88
90
  project ? [project, 'not found'] : ['name', 'missing']
89
91
  end
90
- warn log_message(Logger::WARN, msg, subject: self.class, hint: hint)
92
+ warn log_message(Logger::WARN, msg, subject: self.class, hint: hint, pass: true)
91
93
  end
92
94
 
93
95
  def build
94
96
  return unless enabled?
95
97
 
96
- namespace task_name(name) do |ns|
98
+ namespace(ns = task_name(name)) do
97
99
  @mime.each do |type, items|
98
100
  items.each do |command, file, opts|
99
- next if Rake::Task.task_defined?("#{ns.scope.path}:#{command}:#{type}")
101
+ next if Rake::Task.task_defined?("#{ns}:#{command}:#{type}")
100
102
 
101
103
  namespace command do
102
104
  unless (data = @@mime_obj[type])
@@ -127,7 +129,7 @@ module Squared
127
129
  require(gem || type)
128
130
  obj = eval namespace
129
131
  else
130
- Array(ext).each do |val|
132
+ as_a(ext).each do |val|
131
133
  next unless (data = @@mime_obj[val])
132
134
 
133
135
  obj = data.first
@@ -135,7 +137,7 @@ module Squared
135
137
  end
136
138
  end
137
139
  if obj
138
- ext << type if (ext = Array(ext)).empty?
140
+ ext << type if (ext = as_a(ext)).empty?
139
141
  if !file && target?
140
142
  ext.each do |val|
141
143
  next unless (out = basepath("#{main}.#{val}")).exist?
@@ -159,7 +161,7 @@ module Squared
159
161
  end
160
162
 
161
163
  def also(path, type = nil, name: nil, **kwargs)
162
- return self unless (file = basepath(path)).exist? && !@mime.frozen?
164
+ return self if @mime.frozen? || !(file = basepath(path)).exist?
163
165
 
164
166
  ext = mimetype file
165
167
  type ||= ext
@@ -168,7 +170,7 @@ module Squared
168
170
  end
169
171
 
170
172
  def style(name, *args)
171
- apply_style(theme, name, args)
173
+ apply_style theme, name, args
172
174
  self
173
175
  end
174
176
 
@@ -195,7 +197,7 @@ module Squared
195
197
  private
196
198
 
197
199
  def puts(*args)
198
- log_console(*args, pipe: pipe)
200
+ puts_oe(*args, pipe: pipe)
199
201
  end
200
202
 
201
203
  def log
@@ -211,8 +213,8 @@ module Squared
211
213
  file = nil
212
214
  ext[0] = mime
213
215
  elsif file
214
- keys.unshift(file)
215
- alt = basepath "#{main}.{#{ext.join(',')}}"
216
+ keys.prepend(file)
217
+ alt = basepath("#{main}.{#{ext.join(',')}}")
216
218
  file = Dir[alt].first
217
219
  else
218
220
  alt = main
@@ -241,15 +243,14 @@ module Squared
241
243
  [
242
244
  { pat: /\A((?:[^:]|(?<! ):(?! ))+)\z/, styles: theme[:banner] },
243
245
  { pat: /\A(.*?)(<[^>]+>)(.+)\z/m, styles: theme[:undefined], index: 2 },
244
- { pat: /\A(.+)( : (?!undefined).+)\z/m, styles: theme[:key] },
245
- { pat: /\A(.+ : )(-?[\d.]+)(\s*)\z/m, styles: theme[:number],
246
+ { pat: /\A((?~ : ))( : (?!undefined).+)\z/m, styles: theme[:key] },
247
+ { pat: /\A((?~: ): )(-?[\d.]+)(\s*)\z/m, styles: theme[:number],
246
248
  index: 2 },
247
- { pat: /\A(.+ : ")(.+)("\s*)\z/m, styles: theme[:string], index: 2 },
248
- { pat: /\A(.+ : \{)(.+)(}\s*)\z/m, styles: theme[:hash], index: 2 },
249
- { pat: /\A(.+ : \[)(.+)(\]\s*)\z/m, styles: theme[:array], index: 2 },
250
- { pat: /\A(.+ : )(true|false)(\s*)\z/m, styles: theme[:boolean],
249
+ { pat: /\A((?~: ): ")(.+)("\s*)\z/m, styles: theme[:string], index: 2 },
250
+ { pat: /\A((?~: ): \{)(.+)(\}\s*)\z/m, styles: theme[:hash], index: 2 },
251
+ { pat: /\A((?~: ): \[)(.+)(\]\s*)\z/m, styles: theme[:array],
251
252
  index: 2 },
252
- { pat: /\A(.+ : (?!undefined))([^"\[{].*)\z/m, styles: theme[:value],
253
+ { pat: /\A((?~: ): (?!undefined))([^"\[{].*)\z/m, styles: theme[:value],
253
254
  index: 2 }
254
255
  ]
255
256
  end, border: theme[:border])
@@ -261,7 +262,7 @@ module Squared
261
262
  symbolize = opts[:symbolize_names]
262
263
  keys.each do |key|
263
264
  begin
264
- items = key.split('.').flat_map { |name| name =~ /^(.+)\[(\d+)\]$/ ? [$1, $2.to_i] : name }
265
+ items = key.split('.')
265
266
  items = items.map(&:to_sym) if symbolize
266
267
  val = data.dig(*items)
267
268
  if val.nil?
@@ -308,7 +309,7 @@ module Squared
308
309
  return unless Rake::TaskManager.record_task_metadata
309
310
 
310
311
  val = "#{ext.first}[#{target ? '' : "file?=#{File.basename(main)}.#{ext.last},"}keys+]"
311
- args = *name.split(':').push(command, val)
312
+ args = *name.split(':').append(command, val)
312
313
  if project
313
314
  project.workspace.task_desc(*args)
314
315
  else
@@ -321,9 +322,7 @@ module Squared
321
322
  end
322
323
 
323
324
  def warning?
324
- return true unless project
325
-
326
- project.workspace.warning
325
+ project ? project.workspace.warning : true
327
326
  end
328
327
 
329
328
  def stdin?
@@ -347,10 +346,8 @@ module Squared
347
346
  basepath(file = main + @ext).to_s rescue file
348
347
  end
349
348
 
350
- def basepath(*args)
351
- return Pathname.pwd.join(*args) unless project
352
-
353
- project.basepath(*args)
349
+ def basepath(file)
350
+ project ? project.basepath(file) : Pathname.pwd + file
354
351
  end
355
352
  end
356
353
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- VERSION = '0.4.36'
4
+ VERSION = '0.5.0'
5
5
  end