squared 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,16 @@ module Squared
7
7
  include Format
8
8
  include ::Rake::DSL
9
9
 
10
+ SCRIPT_OBJ = {
11
+ run: nil,
12
+ script: nil,
13
+ dev: nil,
14
+ prod: nil,
15
+ global: false,
16
+ env: false
17
+ }.freeze
18
+ private_constant :SCRIPT_OBJ
19
+
10
20
  class << self
11
21
  def implement(*objs)
12
22
  objs.each do |obj|
@@ -38,9 +48,11 @@ module Squared
38
48
  @impl_series = Workspace::Series
39
49
 
40
50
  attr_reader :root, :home, :main, :prefix, :series, :theme
41
- attr_accessor :exception, :warning, :pipe, :verbose
51
+ attr_accessor :exception, :pipe, :verbose, :warning
42
52
 
43
- def initialize(home = Dir.pwd, *, main: nil, prefix: nil, exception: false, pipe: false, verbose: nil, **kwargs)
53
+ def initialize(home = Dir.pwd, *,
54
+ main: nil, prefix: nil, verbose: nil,
55
+ common: Common::KEY[:COMMON], pipe: Common::KEY[:PIPE], exception: Common::KEY[:FAIL], **)
44
56
  @home = (home = Pathname.new(home)).realdirpath
45
57
  @main = (main || home.basename).to_s
46
58
  @root = @home.parent
@@ -48,25 +60,27 @@ module Squared
48
60
  @series = Application.impl_series.new(self)
49
61
  @project = {}
50
62
  @extensions = []
51
- @script = {
52
- group: {},
53
- ref: {},
54
- build: nil,
55
- dev: nil,
56
- prod: nil,
57
- env: false
58
- }
59
- @exception = exception.is_a?(String) ? !env(exception, ignore: '0').nil? : exception
60
- @pipe = pipe.is_a?(String) ? !env(pipe, ignore: '0').nil? : pipe
61
- @verbose = verbose.nil? ? !@pipe : verbose
62
- @warning = @verbose
63
- if kwargs.fetch(:common, true)
63
+ @pipe = env_pipe(pipe)
64
+ @exception = env_bool(exception)
65
+ @verbose = verbose.nil? ? @pipe > 0 : verbose
66
+ @warning = @verbose != false
67
+ @envname = @main.gsub(/[^\w]+/, '_').upcase
68
+ if common
64
69
  @theme = __get__(:theme)[:workspace]
65
- __set__(:no_color, true) if @pipe
70
+ __set__(:no_color, true) if @pipe == 0
66
71
  else
67
72
  @theme = {}
68
73
  end
69
- @banner = {}
74
+ @script = {
75
+ group: {},
76
+ ref: {},
77
+ group!: {},
78
+ ref!: {}
79
+ }.freeze
80
+ @banner = {
81
+ group: {},
82
+ ref: {}
83
+ }.freeze
70
84
  end
71
85
 
72
86
  def build(**kwargs)
@@ -89,16 +103,19 @@ module Squared
89
103
  yield self if block_given?
90
104
  end
91
105
 
92
- def with(val, &blk)
106
+ def with(*val, &blk)
93
107
  @group = nil
94
108
  @ref = nil
95
- if val.is_a?(::String)
109
+ kind = val.first
110
+ val = kind if val.size == 1
111
+ case kind
112
+ when ::String
96
113
  @group = val
97
114
  if block_given?
98
115
  instance_eval(&blk)
99
116
  @group = nil
100
117
  end
101
- elsif val.is_a?(::Symbol)
118
+ when ::Symbol
102
119
  @ref = val
103
120
  if block_given?
104
121
  instance_eval(&blk)
@@ -128,18 +145,23 @@ module Squared
128
145
  script_command :test, script, group, ref
129
146
  end
130
147
 
148
+ def log(script, group: @group, ref: @ref)
149
+ script_command :log, script, group, ref
150
+ end
151
+
131
152
  def exclude(base, group: @group, ref: @ref)
132
153
  script_command :exclude, as_a(base, :to_sym).freeze, group, ref
133
154
  end
134
155
 
135
- def add(name, path = nil, **kwargs)
156
+ def add(path, name = nil, **kwargs, &blk)
136
157
  ref = if kwargs.key?(:ref)
137
158
  kwargs.delete(:ref)
138
- elsif !@ref.is_a?(::Array)
159
+ elsif @ref.is_a?(::Symbol)
139
160
  @ref
140
161
  end
141
- kwargs[:group] = @group if !kwargs.key?(:group) && !@group.is_a?(::Array)
142
- path = root_path(path || name.to_s)
162
+ kwargs[:group] = @group if !kwargs.key?(:group) && @group.is_a?(::String)
163
+ path = root_path(path)
164
+ name ||= path.basename.to_s
143
165
  proj = ((if !ref.is_a?(::Class)
144
166
  Application.find(ref, path: path)
145
167
  elsif ref < Project::Base
@@ -147,10 +169,11 @@ module Squared
147
169
  end) || Project::Base).new(task_name(name), path, self, **kwargs)
148
170
  @project[name = name.to_sym] = proj
149
171
  __get__(:project)[name] = proj unless kwargs[:private]
172
+ proj.instance_eval(&blk) if block_given?
150
173
  self
151
174
  end
152
175
 
153
- def group(name, path, override: {}, **kwargs)
176
+ def group(path, name, override: {}, **kwargs)
154
177
  root_path(path).children.map do |ent|
155
178
  next unless (dir = Pathname.new(ent)).directory?
156
179
 
@@ -196,13 +219,10 @@ module Squared
196
219
  end
197
220
 
198
221
  def banner(*order, command: true, styles: [], border: [], group: nil, ref: nil)
199
- return (group && @banner[group.to_sym]) || @banner[:"_#{ref || ''}"] if order.empty?
222
+ if order.empty?
223
+ return (group && @banner[:group][group.to_sym]) || (ref && @banner[:ref][ref]) || @banner[:ref][:_]
224
+ end
200
225
 
201
- key = if group ||= @group
202
- group.to_sym
203
- else
204
- :"_#{ref || @ref || ''}"
205
- end
206
226
  data = { command: command, order: [], styles: check_style(styles, empty: false), border: check_style(border) }
207
227
  order.flatten.each do |val|
208
228
  case (val = val.to_sym)
@@ -210,27 +230,33 @@ module Squared
210
230
  data[:order] << val
211
231
  end
212
232
  end
213
- @banner[key] = data if command || !data[:order].empty?
233
+ if command || !data[:order].empty?
234
+ if group ||= @group
235
+ label = :group
236
+ items = as_a(group)
237
+ else
238
+ label = :ref
239
+ items = as_a(ref || @ref || :_)
240
+ end
241
+ items.each { |val| @banner[label][val.to_sym] = data }
242
+ end
214
243
  self
215
244
  end
216
245
 
217
246
  def script(*args, group: nil, ref: nil)
218
- if group
219
- @script[:group][group.to_sym]
220
- elsif ref
221
- @script[:ref][ref.to_sym]
222
- elsif (val = @script[:build])
223
- r = @script[:ref][:_]
224
- g = @script[:group][:_]
225
- args.empty? || (!r && !g) || (r && args.include?(r)) || (g && args.include?(g)) ? [val, @script[:env]] : []
226
- else
227
- []
247
+ return @script[:group][group.to_sym] if group
248
+ return @script[:ref][ref] if ref
249
+
250
+ args.reverse_each do |val|
251
+ next unless val && (ret = val.is_a?(::Symbol) ? @script[:ref!][val] : @script[:group!][val.to_sym])
252
+
253
+ return ret
228
254
  end
255
+ @script[:ref][:_] || SCRIPT_OBJ
229
256
  end
230
257
 
231
258
  def env(key, default = nil, equals: nil, ignore: nil)
232
- @env ||= main.gsub(/[^\w]+/, '_').upcase
233
- ret = ENV.fetch("#{key}_#{@env}", ENV[key]).to_s
259
+ ret = (ENV["#{key}_#{@envname}"] || ENV[key]).to_s
234
260
  return ret == equals.to_s unless equals.nil?
235
261
 
236
262
  ret.empty? || (ignore && as_a(ignore).any? { |val| ret == val.to_s }) ? default : ret
@@ -254,9 +280,8 @@ module Squared
254
280
  end
255
281
 
256
282
  def task_namespace(val, first: false)
257
- return nil unless (val = val.to_s).include?(':')
283
+ return nil unless (ret = val.to_s.split(':')).size > 1
258
284
 
259
- ret = val.split(':')
260
285
  first ? ret.first : ret[0..-2].join(':')
261
286
  end
262
287
 
@@ -269,7 +294,7 @@ module Squared
269
294
  end
270
295
 
271
296
  def enabled?
272
- !@extensions.empty? || @project.any? { |_, proj| proj.enabled? }
297
+ !@extensions.empty? || @project.values.any?(&:enabled?)
273
298
  end
274
299
 
275
300
  def task_extend?(obj, key)
@@ -280,33 +305,35 @@ module Squared
280
305
  ::Rake::Task.task_defined?(key)
281
306
  end
282
307
 
283
- def dev?(global: nil, **kwargs)
284
- script?(:dev, global: global.nil? ? kwargs[:pat].nil? : global, **kwargs)
308
+ def dev?(**kwargs)
309
+ script?(:dev, **kwargs)
285
310
  end
286
311
 
287
- def prod?(global: false, **kwargs)
288
- return false if global && (@script[:dev] == true || !@script[:prod])
289
-
290
- script?(:prod, global: global, **kwargs)
312
+ def prod?(**kwargs)
313
+ script?(:prod, **kwargs)
291
314
  end
292
315
 
293
316
  private
294
317
 
295
- def __build__(**kwargs)
296
- if !task_defined?('default') && (target = kwargs[:default]) && task_defined?(target = task_name(target))
318
+ def __build__(default: nil, **)
319
+ if !task_defined?('default') && default && task_defined?(target = task_name(default))
297
320
  task 'default' => target
298
321
  end
299
322
  return unless series.some?(:build)
300
323
 
301
- init = [task_name('depend'), task_name(dev? && series.some?(:refresh) ? 'refresh' : 'build')]
324
+ init = [task_name('depend'), task_name(dev?(global: true) && series.some?(:refresh) ? 'refresh' : 'build')]
302
325
 
303
- task 'default' => init[1] unless task_defined?('default')
326
+ task 'default' => init[1] unless default || task_defined?('default')
304
327
  return unless series.some?(:depend) && !task_defined?(key = task_name('init'))
305
328
 
306
329
  desc key
307
330
  task key => init
308
331
  end
309
332
 
333
+ def puts(*args)
334
+ @pipe == 2 ? $stderr.puts(*args) : $stdout.puts(*args)
335
+ end
336
+
310
337
  def script_command(task, val, group, ref)
311
338
  if group
312
339
  label = :group
@@ -319,23 +346,30 @@ module Squared
319
346
  self
320
347
  end
321
348
 
322
- def script?(state, script: nil, pat: nil, ref: nil, group: nil, global: false)
323
- r = @script[:ref][:_]
324
- g = @script[:group][:_]
325
- if global && ((!ref && !group) || (!r && !g) || (ref && contains?(r, ref)) || (group && contains?(g, group)))
326
- pat = @script[state] if pat.nil?
327
- script ||= @script[:build]
328
- end
329
- if pat.is_a?(::Regexp)
330
- script = script[state == :prod ? 1 : 0] if script.is_a?(::Array)
331
- pat.match?(script)
349
+ def script_set(data, group: nil, ref: nil)
350
+ data.freeze
351
+ if group
352
+ as_a(group).each { |val| @script[:group!][val.to_sym] = data }
353
+ elsif ref
354
+ as_a(ref).each { |val| @script[:ref!][val.to_sym] = data }
332
355
  else
333
- pat == true
356
+ @script[:ref][:_] = data
334
357
  end
335
358
  end
336
359
 
337
- def contains?(data, val)
338
- as_a(data).any? { |item| item.to_s == val.to_s }
360
+ def script_obj
361
+ SCRIPT_OBJ.dup
362
+ end
363
+
364
+ def script?(state, target: nil, pat: nil, group: nil, ref: Project::Base.ref, global: false)
365
+ data = script(ref, group)
366
+ if global
367
+ target = data[:script] || data[:run] if target.nil?
368
+ pat = data[state] if pat.nil?
369
+ end
370
+ return false if state == :prod && data[:dev] == true && data[:global]
371
+
372
+ target && pat.is_a?(::Regexp) ? as_a(target).any? { |val| pat.match?(val) } : pat == true
339
373
  end
340
374
  end
341
375
  end