Class: Squared::Workspace::Application

Inherits:
Object
  • Object
show all
Includes:
Format, Rake::DSL, Common, Repo
Defined in:
lib/squared/workspace/application.rb

Constant Summary

Constants included from Common

Common::ARG

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Repo

#manifest, #manifest_url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Repo

empty?, read_manifest, #repo

Constructor Details

#initialize(home = Dir.pwd, main: nil, prefix: nil, verbose: nil, common: , pipe: , exception: ) ⇒ Application

Returns a new instance of Application.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/squared/workspace/application.rb', line 61

def initialize(home = Dir.pwd, *, main: nil, prefix: nil, verbose: nil,
               common: ARG[:COMMON], pipe: ARG[:PIPE], exception: ARG[:FAIL], **)
  @home = Pathname.new(home).realdirpath
  @root = @home.parent
  @main = (main || @home.basename).to_s.freeze
  @prefix = prefix
  @series = Application.impl_series.new(self)
  @project = {}
  @extensions = []
  @pipe = env_pipe(pipe, (ARG[:OUT] && env(ARG[:OUT])) || 1, root: @home)
  @exception = env_bool(exception)
  @verbose = verbose.nil? ? @pipe != 0 : verbose
  @warning = @verbose != false
  @closed = false
  if common
    @theme = __get__(:theme)[:workspace]
    ARG[:COLOR] = false if @pipe == 0 || @pipe.is_a?(::Pathname)
  else
    @theme = {}
  end
  @script = {
    group: {},
    ref: {},
    group!: {},
    ref!: {}
  }.freeze
  @banner = {
    group: {},
    ref: {}
  }.freeze
  initialize_session
end

Class Attribute Details

.attr_bannerObject (readonly)

Returns the value of attribute attr_banner.



52
53
54
# File 'lib/squared/workspace/application.rb', line 52

def attr_banner
  @attr_banner
end

.base_projectObject

Returns the value of attribute base_project.



53
54
55
# File 'lib/squared/workspace/application.rb', line 53

def base_project
  @base_project
end

.impl_projectObject (readonly)

Returns the value of attribute impl_project.



52
53
54
# File 'lib/squared/workspace/application.rb', line 52

def impl_project
  @impl_project
end

.impl_seriesObject

Returns the value of attribute impl_series.



53
54
55
# File 'lib/squared/workspace/application.rb', line 53

def impl_series
  @impl_series
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def closed
  @closed
end

#exceptionObject (readonly)

Returns the value of attribute exception.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def exception
  @exception
end

#homeObject (readonly)

Returns the value of attribute home.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def home
  @home
end

#mainObject (readonly)

Returns the value of attribute main.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def main
  @main
end

#pipeObject (readonly)

Returns the value of attribute pipe.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def pipe
  @pipe
end

#prefixObject (readonly)

Returns the value of attribute prefix.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def prefix
  @prefix
end

#rootObject (readonly)

Returns the value of attribute root.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def root
  @root
end

#seriesObject (readonly)

Returns the value of attribute series.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def series
  @series
end

#themeObject (readonly)

Returns the value of attribute theme.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def theme
  @theme
end

#verboseObject (readonly)

Returns the value of attribute verbose.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def verbose
  @verbose
end

#warningObject (readonly)

Returns the value of attribute warning.



59
60
61
# File 'lib/squared/workspace/application.rb', line 59

def warning
  @warning
end

Class Method Details

.baserefObject



44
45
46
# File 'lib/squared/workspace/application.rb', line 44

def baseref
  base_project.ref
end

.find(ref = nil, path: nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/squared/workspace/application.rb', line 36

def find(ref = nil, path: nil)
  if ref && (ret = impl_project.find { |proj| proj.ref == ref.to_sym })
    ret
  elsif path
    impl_project.find { |proj| proj.config?(path) }
  end
end

.implement(*objs) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/squared/workspace/application.rb', line 21

def implement(*objs)
  objs.each do |obj|
    next unless obj < base_project

    impl_project.unshift(obj)
    obj.tasks&.each { |task| impl_series.add(task, obj) }
    if (args = obj.batchargs)
      impl_series.batch(*args)
    end
    if (args = obj.aliasargs)
      impl_series.alias(*args)
    end
  end
end

.to_sObject



48
49
50
# File 'lib/squared/workspace/application.rb', line 48

def to_s
  super.to_s.match(/[^:]+$/)[0]
end

Instance Method Details

#add(path, val = nil, **kwargs, &blk) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/squared/workspace/application.rb', line 214

def add(path, val = nil, **kwargs, &blk)
  if @withargs
    data = @withargs.dup
    data.merge!(kwargs)
    kwargs = data
  end
  ref = if kwargs.key?(:ref)
          kwargs = kwargs.dup unless @withargs
          kwargs.delete(:ref)
        elsif @ref.is_a?(::Symbol)
          @ref
        end
  if @group.is_a?(::String) && !kwargs.key?(:group)
    kwargs = kwargs.dup unless @withargs
    kwargs[:group] = @group
  end
  path = root_path(path)
  val = (val || path.basename).to_s
  name = val.to_sym
  index = 0
  while @project[name]
    index += 1
    name = :"#{val}-#{index}"
  end
  proj = ((if !ref.is_a?(::Class)
             Application.find(ref, path: path)
           elsif ref < Project::Base
             ref
           end) || Project::Base).new(self, path, task_name(name), **kwargs)
  @project[name] = proj
  __get__(:project)[name] = proj unless kwargs[:private]
  proj.instance_eval(&blk) if block_given?
  self
end

#apply(&blk) ⇒ Object



269
270
271
272
# File 'lib/squared/workspace/application.rb', line 269

def apply(&blk)
  instance_eval(&blk)
  self
end


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/squared/workspace/application.rb', line 175

def banner(*args, command: true, styles: nil, border: nil, group: @group, ref: @ref)
  data = { command: command, order: [], styles: check_style(styles, empty: false), border: check_style(border) }
  args.each do |meth|
    if meth.is_a?(::Array)
      found = false
      meth = meth.select do |val|
        case val
        when ::Symbol
          found = true
          Application.attr_banner.include?(val)
        when ::String
          true
        else
          false
        end
      end
      if !found
        next
      elsif meth.size == 1
        meth = meth.first
      end
    elsif !Application.attr_banner.include?(meth = meth.to_sym)
      next
    end
    data[:order] << meth
  end
  unless !command && data[:order].empty?
    if group
      label = :group
      items = as_a(group)
    else
      label = :ref
      items = ref ? as_a(ref) : %i[_]
    end
    items.each { |val| @banner[label][val.to_sym] = data }
  end
  self
end


383
384
385
386
387
388
389
# File 'lib/squared/workspace/application.rb', line 383

def banner_get(*ref, group: nil)
  ret = nil
  return ret if group && (ret = @banner[:group][group.to_sym])

  ref.reverse_each { |val| return ret if (ret = @banner[:ref][val]) }
  @banner[:ref][:_]
end

#baserefObject



431
432
433
# File 'lib/squared/workspace/application.rb', line 431

def baseref
  Application.baseref
end

#build(parallel: [], **kwargs) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/squared/workspace/application.rb', line 102

def build(parallel: [], **kwargs)
  kwargs[:parallel] = parallel.map(&:to_s)
  return if @closed || !enabled?

  @project.each_value do |proj|
    next unless proj.enabled?

    proj.populate(**kwargs)
    series.populate(proj)
  end

  Application.impl_project.each { |obj| obj.populate(self, **kwargs) }
  @extensions.each { |ext| __send__(ext, **kwargs) }

  series.build(**kwargs)
  __build__(**kwargs)

  yield self if block_given?
  @closed = true
end

#clean(script, group: @group, ref: @ref) ⇒ Object



155
156
157
# File 'lib/squared/workspace/application.rb', line 155

def clean(script, group: @group, ref: @ref)
  script_command :clean, script, group, ref
end

#compose(name, &blk) ⇒ Object



264
265
266
267
# File 'lib/squared/workspace/application.rb', line 264

def compose(name, &blk)
  namespace(task_name(name), &blk) if block_given?
  self
end

#depend(script, group: @group, ref: @ref) ⇒ Object



151
152
153
# File 'lib/squared/workspace/application.rb', line 151

def depend(script, group: @group, ref: @ref)
  script_command :depend, script, group, ref
end

#dev?(**kwargs) ⇒ Boolean

Returns:

  • (Boolean)


419
420
421
# File 'lib/squared/workspace/application.rb', line 419

def dev?(**kwargs)
  script?(:dev, **kwargs)
end

#doc(script, group: @group, ref: @ref) ⇒ Object



159
160
161
# File 'lib/squared/workspace/application.rb', line 159

def doc(script, group: @group, ref: @ref)
  script_command :doc, script, group, ref
end

#enabled?Boolean

Returns:

  • (Boolean)


399
400
401
# File 'lib/squared/workspace/application.rb', line 399

def enabled?
  !@extensions.empty? || @project.values.any?(&:enabled?)
end

#exclude(base, group: @group, ref: @ref) ⇒ Object



171
172
173
# File 'lib/squared/workspace/application.rb', line 171

def exclude(base, group: @group, ref: @ref)
  script_command :exclude, as_a(base, :to_sym).freeze, group, ref
end

#find(path = nil, name: nil) {|ret| ... } ⇒ Object

Yields:

  • (ret)


289
290
291
292
293
294
295
296
# File 'lib/squared/workspace/application.rb', line 289

def find(path = nil, name: nil)
  path = root_path(path) if path
  ret = @project.find { |_, item| (path && item.path == path) || (name && item.name == name.to_s) }&.last
  return ret unless block_given?

  yield ret if ret
  self
end

#find_base(obj) ⇒ Object



298
299
300
# File 'lib/squared/workspace/application.rb', line 298

def find_base(obj)
  Application.impl_project.find { |proj| obj.instance_of?(proj) }
end

#group(path, val, override: {}, **kwargs, &blk) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/squared/workspace/application.rb', line 249

def group(path, val, override: {}, **kwargs, &blk)
  root_path(path).children.map do |dir|
    next unless dir.directory?

    basename = dir.basename.to_s.to_sym
    [dir, basename, override[basename]]
  end
  .each do |dir, basename, opts|
    args = kwargs.dup
    args.merge!(opts) if opts
    add(dir, basename, group: val, **args, &blk)
  end
  self
end

#home?Boolean

Returns:

  • (Boolean)


427
428
429
# File 'lib/squared/workspace/application.rb', line 427

def home?
  !(proj = find(home)).nil? && proj.enabled?
end

#home_path(*args) ⇒ Object



362
363
364
# File 'lib/squared/workspace/application.rb', line 362

def home_path(*args)
  home.join(*args)
end

#initialize_sessionObject



94
95
96
97
98
99
100
# File 'lib/squared/workspace/application.rb', line 94

def initialize_session
  @envname = @main.gsub(/[^\w]+/, '_').upcase.freeze
  return unless @pipe.is_a?(::Pathname)

  bord = '#' * Project.line_width
  puts bord, format('Session started on %s by %s', Time.now.to_s, @main), bord
end

#inspectObject



395
396
397
# File 'lib/squared/workspace/application.rb', line 395

def inspect
  "#<#{self.class}: #{main} => #{self}>"
end

#invokeargsObject



435
436
437
# File 'lib/squared/workspace/application.rb', line 435

def invokeargs
  { exception: exception, warning: warning }
end

#log(script, group: @group, ref: @ref) ⇒ Object



167
168
169
# File 'lib/squared/workspace/application.rb', line 167

def log(script, group: @group, ref: @ref)
  script_command :log, script, group, ref
end

#prod?(**kwargs) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
# File 'lib/squared/workspace/application.rb', line 423

def prod?(**kwargs)
  script?(:prod, **kwargs)
end

#root_path(*args) ⇒ Object



358
359
360
# File 'lib/squared/workspace/application.rb', line 358

def root_path(*args)
  root.join(*args)
end

#run(script, group: @group, ref: @ref) ⇒ Object



147
148
149
# File 'lib/squared/workspace/application.rb', line 147

def run(script, group: @group, ref: @ref)
  script_command :run, script, group, ref
end

#script_find(*args) ⇒ Object



366
367
368
369
370
371
372
373
# File 'lib/squared/workspace/application.rb', line 366

def script_find(*args)
  args.reverse_each do |val|
    next unless val && (ret = val.is_a?(::Symbol) ? @script[:ref!][val] : @script[:group!][val.to_sym])

    return ret
  end
  @script[:ref!][:_] || SCRIPT_OBJ
end

#script_get(group: nil, ref: nil) ⇒ Object



375
376
377
378
379
380
381
# File 'lib/squared/workspace/application.rb', line 375

def script_get(group: nil, ref: nil)
  if group
    @script[:group][group.to_sym]
  elsif ref
    @script[:ref][ref]
  end
end

#style(name, *args, target: nil, empty: false) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/squared/workspace/application.rb', line 274

def style(name, *args, target: nil, empty: false)
  data = nil
  if target
    as_a(target).each_with_index do |key, i|
      if i == 0
        break unless (data = __get__(:theme)[key.to_sym])
      else
        data = data[key.to_sym] ||= {}
      end
    end
  end
  apply_style(data || theme, name, args, empty: empty) unless target && !data
  self
end

#task_base?(key) ⇒ Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/squared/workspace/application.rb', line 403

def task_base?(key)
  series.base?(key)
end

#task_defined?(*key) ⇒ Boolean

Returns:

  • (Boolean)


415
416
417
# File 'lib/squared/workspace/application.rb', line 415

def task_defined?(*key)
  ::Rake::Task.task_defined?(key.size == 1 ? key.first : task_join(*key))
end

#task_extend?(obj, key) ⇒ Boolean

Returns:

  • (Boolean)


407
408
409
# File 'lib/squared/workspace/application.rb', line 407

def task_extend?(obj, key)
  series.extend?(obj, key)
end

#task_include?(obj, key, ref = nil) ⇒ Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/squared/workspace/application.rb', line 411

def task_include?(obj, key, ref = nil)
  task_base?(key) ? obj.has?(key, ref || baseref) : task_extend?(obj, key)
end

#task_join(*val) ⇒ Object



313
314
315
# File 'lib/squared/workspace/application.rb', line 313

def task_join(*val)
  val.join(':')
end

#task_name(val, desc: false) ⇒ Object



302
303
304
305
# File 'lib/squared/workspace/application.rb', line 302

def task_name(val, desc: false)
  ret = @prefix ? task_join(@prefix, val) : val.to_s
  desc ? ret.split(':').join(ARG[:SPACE]) : ret
end

#task_namespace(val, first: false) ⇒ Object



307
308
309
310
311
# File 'lib/squared/workspace/application.rb', line 307

def task_namespace(val, first: false)
  return nil unless (ret = val.to_s.split(':')).size > 1

  first ? ret.first : task_join(*ret[0..-2])
end

#task_resolve(obj, key) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/squared/workspace/application.rb', line 317

def task_resolve(obj, key)
  tasks = []
  if (base = task_base?(key))
    tasks << key if obj.has?(key, baseref)
  elsif (batch = series.batch_get(key))
    obj.allref.each do |ref|
      next unless (data = batch[ref])

      data.each do |val|
        if (items = task_resolve(obj, val)).empty?
          tasks.clear
          break
        end
        tasks += items
      end
      return tasks unless tasks.empty?
    end
  elsif task_extend?(obj, key)
    tasks << key
  end
  ret = []
  if tasks.empty?
    return [] if (base && !obj.ref?(baseref)) || !(data = series.alias_get(key))

    obj.allref.each do |ref|
      next unless (alt = data[ref])

      ret = task_resolve(obj, alt)
      break unless ret.empty?
    end
  else
    tasks.each do |val|
      target = task_join(obj.name, series.name_get(val))
      return [] unless task_defined?(target)

      ret << target
    end
  end
  ret
end

#test(script, group: @group, ref: @ref) ⇒ Object



163
164
165
# File 'lib/squared/workspace/application.rb', line 163

def test(script, group: @group, ref: @ref)
  script_command :test, script, group, ref
end

#to_sObject



391
392
393
# File 'lib/squared/workspace/application.rb', line 391

def to_s
  (home? ? home : root).to_s
end

#with(*val, group: nil, **kwargs, &blk) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/squared/workspace/application.rb', line 123

def with(*val, group: nil, **kwargs, &blk)
  @group = nil
  @ref = nil
  @withargs = kwargs.empty? ? nil : kwargs
  val = [group || kwargs[:ref]].compact.flatten if val.empty?
  kind = val.first
  val = kind if val.size == 1
  case kind
  when ::String
    @group = val
  when ::Symbol
    @ref = val
  else
    raise_error('group{Symbol} | ref{String}', hint: 'missing') if block_given?
  end
  if block_given?
    instance_eval(&blk)
    @group = nil
    @ref = nil
    @withargs = nil
  end
  self
end