squared 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,48 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pathname'
4
+ require 'rake'
5
+
3
6
  module Squared
4
7
  module Common
5
8
  module Utils
6
9
  module_function
7
10
 
8
- def env_value(key, suffix: nil)
9
- if suffix
10
- ret = ENV["#{key}_#{suffix}"]
11
- return ret unless ret.nil?
11
+ def task_invoke(*cmd, args: [], exception: true, warning: true)
12
+ cmd.each { |name| ::Rake::Task[name].invoke(*args) }
13
+ rescue StandardError => e
14
+ raise if exception
15
+
16
+ warn e if warning
17
+ end
18
+
19
+ def task_join(*val)
20
+ case val.size
21
+ when 1
22
+ val[0].to_s
23
+ when 2
24
+ "#{val[0]}:#{val[1]}"
25
+ else
26
+ val.join(':')
12
27
  end
13
- ENV.fetch(key, '')
14
28
  end
15
29
 
16
- def env_bool(key, default = false, suffix: nil)
17
- return default if key.nil?
30
+ def task_invoked?(name)
31
+ ::Rake::Task.tasks.any? { |obj| obj.already_invoked && obj.name == name }
32
+ end
33
+
34
+ def env(key, default = nil, suffix: @envname, equals: nil, ignore: nil)
35
+ ret = env_value(key, suffix: suffix)
36
+ return ret == equals.to_s unless equals.nil?
37
+
38
+ ret.empty? || (ignore && as_a(ignore).any? { |val| ret == val.to_s }) ? default : ret
39
+ end
18
40
 
41
+ def env_value(key, default = '', suffix: nil)
42
+ suffix && (ret = ENV["#{key}_#{suffix}"]) ? ret : ENV.fetch(key, default)
43
+ end
44
+
45
+ def env_bool(key, default = false, suffix: nil)
19
46
  if key.is_a?(::String)
20
47
  case env_value(key, suffix: suffix)
21
48
  when ''
@@ -26,21 +53,29 @@ module Squared
26
53
  true
27
54
  end
28
55
  else
29
- key
56
+ key.nil? ? default : key
30
57
  end
31
58
  end
32
59
 
33
- def env_pipe(key, default = 1, suffix: nil)
34
- if key.is_a?(::String)
60
+ def env_pipe(key, default = 1, suffix: nil, root: nil)
61
+ if default.is_a?(::String)
62
+ begin
63
+ default = (root ? root.join(default) : ::Pathname.new(default)).realdirpath
64
+ rescue StandardError => e
65
+ default = 1
66
+ warn e
67
+ end
68
+ end
69
+ case key
70
+ when ::String
35
71
  case (ret = env_value(key, suffix: suffix))
36
72
  when '0', '1', '2'
37
- ret.to_i
38
- else
39
- default
73
+ return ret.to_i
40
74
  end
41
- else
42
- key.is_a?(::Numeric) && key >= 0 && key <= 2 ? key : default
75
+ when ::Numeric
76
+ return key if key >= 0 && key <= 2
43
77
  end
78
+ default
44
79
  end
45
80
 
46
81
  def env_match(key, default = nil, suffix: nil, options: 0, timeout: nil)
@@ -52,7 +87,7 @@ module Squared
52
87
  when '1'
53
88
  true
54
89
  else
55
- Regexp.new(ret, options, timeout: timeout)
90
+ ::Regexp.new(ret, options, timeout: timeout)
56
91
  end
57
92
  end
58
93
  end
@@ -1,12 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pathname'
4
- require 'rake'
5
-
6
3
  require_relative 'common/base'
7
4
  require_relative 'common/class'
8
5
  require_relative 'common/format'
6
+ require_relative 'common/prompt'
9
7
  require_relative 'common/shell'
10
8
  require_relative 'common/system'
11
- require_relative 'common/task'
12
9
  require_relative 'common/utils'
@@ -7,30 +7,26 @@ require_relative 'common'
7
7
  module Squared
8
8
  module Config
9
9
  class Viewer
10
- include Common
11
- include Format
10
+ include Common::Format
12
11
  include Utils
13
12
  include ::Rake::DSL
14
13
 
15
- class << self
16
- def to_s
17
- super.to_s.match(/[^:]+$/)[0]
18
- end
14
+ def self.to_s
15
+ super.match(/[^:]+$/)[0]
19
16
  end
20
17
 
21
18
  attr_reader :main, :name, :project, :theme
22
19
  attr_accessor :pipe
23
20
 
24
- def initialize(main, name = nil,
25
- project: nil, prefix: nil, dump: nil, opts: {}, auto: true,
26
- common: Common::KEY[:COMMON], pipe: Common::KEY[:PIPE], **)
21
+ def initialize(main, name = nil, project: nil, prefix: nil, dump: nil, opts: {}, auto: true,
22
+ common: ARG[:COMMON], pipe: ARG[:PIPE], **)
27
23
  if project
28
- main = @project.base_path(main).to_s if (@project = __get__(:project)[project.to_sym])
24
+ main = @project.basepath(main).to_s if (@project = __get__(:project)[project.to_s])
29
25
  @required = true
30
26
  end
31
27
  @name = name&.to_s || @project&.name
32
28
  @prefix = prefix
33
- @ext = File.extname(main)
29
+ @ext = File.extname(main).downcase
34
30
  @dump = dump
35
31
  @mime = {}
36
32
  @theme = common ? __get__(:theme)[:viewer] : {}
@@ -63,10 +59,9 @@ module Squared
63
59
  def build
64
60
  return unless enabled?
65
61
 
66
- params = ->(args) { exist? ? [realpath, [args.keys] + args.extras] : [args.keys, args.extras] }
67
-
68
62
  namespace(ns = task_name(name)) do
69
63
  view = @command && @command != name ? @command : 'view'
64
+ params = ->(args) { exist? ? [realpath, [args.keys] + args.extras] : [args.keys, args.extras] }
70
65
  namespace view do
71
66
  if @mime['json'] && (exist? || !::Rake::Task.task_defined?("#{ns}:#{view}:json"))
72
67
  desc format_desc(view, 'json')
@@ -84,7 +79,6 @@ module Squared
84
79
  end
85
80
  end
86
81
  end
87
-
88
82
  yield self if block_given?
89
83
  end
90
84
 
@@ -96,11 +90,11 @@ module Squared
96
90
  require(gem || type)
97
91
  obj = eval(parse)
98
92
  ext << type if (ext = as_a(ext)).empty?
99
- file = realpath if file.nil? && exist?
93
+ file = realpath if !file && exist?
100
94
 
101
95
  namespace task_name(name) do
102
- desc format_desc(command, *ext, exist: exist)
103
96
  namespace command do
97
+ desc format_desc(command, *ext, exist: exist)
104
98
  task type, [:keys] do |_, args|
105
99
  if file
106
100
  read_keys(obj, type, file.to_s, args.to_a, ext: ext)
@@ -124,7 +118,7 @@ module Squared
124
118
  end
125
119
 
126
120
  def also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {})
127
- return self if @mime.frozen? || !(file = base_path(path)).exist?
121
+ return self if @mime.frozen? || !(file = basepath(path)).exist?
128
122
 
129
123
  ext = mime_type(file)
130
124
  type = type&.to_s || ext
@@ -142,7 +136,7 @@ module Squared
142
136
  end
143
137
 
144
138
  def style(name, *args)
145
- apply_style(theme, name, *args)
139
+ apply_style(theme, name, args)
146
140
  self
147
141
  end
148
142
 
@@ -163,25 +157,35 @@ module Squared
163
157
  def enabled?
164
158
  return File.exist?(realpath) if exist?
165
159
 
166
- !@required || (!project.nil? && project.enabled?)
160
+ !@required || !!project&.enabled?
167
161
  end
168
162
 
169
163
  private
170
164
 
165
+ def puts(*args)
166
+ puts_oe(*args, pipe: pipe)
167
+ end
168
+
171
169
  def read_keys(reader, type, file, keys, ext: [type])
172
- if (mime = mime_type(file)) && base_path(file).exist?
170
+ if file && (mime = mime_type(file)) && basepath(file).exist?
173
171
  raise_error(file, mime, hint: 'invalid') unless ext.include?(mime)
174
172
  else
175
173
  if ext.include?(mime)
176
174
  alt = file
177
175
  file = nil
178
176
  ext[0] = mime
179
- else
177
+ elsif file
180
178
  keys.unshift(file)
181
- alt = base_path("#{main}.{#{ext.join(',')}}")
179
+ alt = basepath("#{main}.{#{ext.join(',')}}")
182
180
  file = Dir[alt].first
181
+ else
182
+ alt = main
183
+ args = { hint: 'no keys' }
184
+ end
185
+ unless file
186
+ args ||= { hint: 'not found', kind: LoadError }
187
+ raise_error(reader.name, "#{File.basename(alt, '.*')}.#{ext.first}", **args)
183
188
  end
184
- raise_error(reader.name, "#{File.basename(alt, '.*')}.#{ext.first}", hint: 'not found') unless file
185
189
  end
186
190
  project&.log&.info "#{Viewer}(#{type}) => #{file} {#{keys.join(', ')}}"
187
191
  doc = if reader.respond_to?(:load_file)
@@ -196,19 +200,19 @@ module Squared
196
200
  .realpath
197
201
  .to_s
198
202
  .sub(Regexp.new("^#{Regexp.escape(File.join(Dir.pwd, ''))}"), '')
199
- sub = unless stdin?
200
- [
201
- { pat: /^((?:[^:]|(?<! ):(?! ))+)$/, styles: theme[:banner] },
202
- { pat: /^(.*?)(<[^>]+>)(.+)$/m, styles: theme[:undefined], index: 2 },
203
- { pat: /^(.+)( : (?!undefined).+)$/m, styles: theme[:key] },
204
- { pat: /^(.+ : )(-?[\d.]+)(\s*)$/m, styles: theme[:number], index: 2 },
205
- { pat: /^(.+ : ")(.+)("\s*)$/m, styles: theme[:string], index: 2 },
206
- { pat: /^(.+ : \{)(.+)(\}\s*)$/m, styles: theme[:hash], index: 2 },
207
- { pat: /^(.+ : \[)(.+)(\]\s*)$/m, styles: theme[:array], index: 2 },
208
- { pat: /^(.+ : (?!undefined))([^"\[{].*)$/m, styles: theme[:value], index: 2 }
209
- ]
210
- end
211
- emphasize(lines, title: title, sub: sub, pipe: pipe == 2 ? $stderr : nil)
203
+ emphasize(lines, title: title, sub: unless stdin?
204
+ [
205
+ { pat: /^((?:[^:]|(?<! ):(?! ))+)$/, styles: theme[:banner] },
206
+ { pat: /^(.*?)(<[^>]+>)(.+)$/m, styles: theme[:undefined], index: 2 },
207
+ { pat: /^(.+)( : (?!undefined).+)$/m, styles: theme[:key] },
208
+ { pat: /^(.+ : )(-?[\d.]+)(\s*)$/m, styles: theme[:number], index: 2 },
209
+ { pat: /^(.+ : ")(.+)("\s*)$/m, styles: theme[:string], index: 2 },
210
+ { pat: /^(.+ : \{)(.+)(\}\s*)$/m, styles: theme[:hash], index: 2 },
211
+ { pat: /^(.+ : \[)(.+)(\]\s*)$/m, styles: theme[:array], index: 2 },
212
+ { pat: /^(.+ : (?!undefined))([^"\[{].*)$/m, styles: theme[:value],
213
+ index: 2 }
214
+ ]
215
+ end)
212
216
  end
213
217
 
214
218
  def print_keys(type, data, keys, file: nil)
@@ -252,8 +256,8 @@ module Squared
252
256
  end
253
257
  end
254
258
 
255
- def base_path(file)
256
- project ? project.base_path(file) : Pathname.new(file).realdirpath
259
+ def basepath(file)
260
+ project ? project.basepath(file) : Pathname.new(file).realdirpath
257
261
  end
258
262
 
259
263
  def task_name(val)
@@ -266,19 +270,20 @@ module Squared
266
270
  'yaml'
267
271
  when 'js'
268
272
  'json'
273
+ when ''
274
+ nil
269
275
  else
270
- ret.empty? ? nil : ret
276
+ ret
271
277
  end
272
278
  end
273
279
 
274
280
  def format_desc(command, *ext, exist: exist?)
275
- message(@prefix || '', *name.split(':'), command,
276
- "#{ext.first}[#{exist ? '' : "file?=#{File.basename(main)}.#{ext.last},"}keys*]")
281
+ val = "#{ext.first}[#{exist ? '' : "file?=#{File.basename(main)}.#{ext.last},"}keys+]"
282
+ message(@prefix, *name.split(':'), command, val, empty: true)
277
283
  end
278
284
 
279
285
  def realpath
280
- file = main + @ext
281
- Pathname.new(file).realdirpath.to_s rescue file
286
+ basepath(file = main + @ext).to_s rescue file
282
287
  end
283
288
 
284
289
  def exist?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- VERSION = '0.0.10'
4
+ VERSION = '0.0.12'
5
5
  end