Class: Squared::Workspace::Project::Base

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL, Shell, Common::Format, System, Utils
Defined in:
lib/squared/workspace/project/base.rb

Direct Known Subclasses

Git

Constant Summary collapse

SEM_VER =
/(\d+)(?:(\.)(\d+))?(?:(\.)(\d+)(\S+)?)?/.freeze
@@tasks =
{
  "#{ref}": {
    graph: %i[run print].freeze
  }.freeze
}
0

Constants included from Common

Common::ARG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Format

#enable_aixterm

Constructor Details

#initialize(workspace, path, name, group: nil, graph: nil, pass: nil, exclude: nil, common: , **kwargs) ⇒ Base

Returns a new instance of Base.



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
93
94
95
96
97
98
99
100
# File 'lib/squared/workspace/project/base.rb', line 64

def initialize(workspace, path, name, *, group: nil, graph: nil, pass: nil, exclude: nil,
               common: ARG[:COMMON], **kwargs)
  @path = path
  @workspace = workspace
  @name = name.to_s.freeze
  @project = @path.basename.to_s.freeze
  @group = group&.to_s.freeze
  @depend = kwargs[:depend]
  @doc = kwargs[:doc]
  @test = kwargs[:test]
  @copy = kwargs[:copy]
  @clean = kwargs[:clean]
  @exception = kwargs.key?(:exception) ? env_bool(kwargs[:exception]) : workspace.exception
  @pipe = kwargs.key?(:pipe) ? env_pipe(kwargs[:pipe]) : workspace.pipe
  @verbose = kwargs.key?(:verbose) ? kwargs[:verbose] : workspace.verbose
  @theme = if !@verbose
             {}
           elsif common
             workspace.theme
           else
             __get__(:theme)[:project][to_sym] ||= {}
           end
  @output = []
  @ref = []
  @children = []
  @graph = if graph
             as_a(graph, workspace.prefix ? ->(val) { workspace.task_name(val).to_sym } : :to_sym).freeze
           end
  @pass = (pass ? as_a(pass, :to_sym) : []).freeze
  @exclude = (exclude ? as_a(exclude, :to_sym) : []).freeze
  @envname = @name.gsub(/[^\w]+/, '_').upcase.freeze
  @desc = (@name.include?(':') ? @name.split(':').join(ARG[:SPACE]) : @name).freeze
  @parent = nil
  @global = false
  run_set(kwargs[:run], kwargs[:env], opts: kwargs.fetch(:opts, true))
  initialize_ref(Base.ref)
end

Instance Attribute Details

#dependfileObject (readonly)

Returns the value of attribute dependfile.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def dependfile
  @dependfile
end

#exceptionObject (readonly)

Returns the value of attribute exception.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def exception
  @exception
end

#groupObject (readonly)

Returns the value of attribute group.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def group
  @group
end

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def path
  @path
end

#pipeObject (readonly)

Returns the value of attribute pipe.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def pipe
  @pipe
end

#projectObject (readonly)

Returns the value of attribute project.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def theme
  @theme
end

#verboseObject (readonly)

Returns the value of attribute verbose.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def verbose
  @verbose
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



61
62
63
# File 'lib/squared/workspace/project/base.rb', line 61

def workspace
  @workspace
end

Class Method Details

.aliasargsObject



25
# File 'lib/squared/workspace/project/base.rb', line 25

def aliasargs(*); end

.as_path(val) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/squared/workspace/project/base.rb', line 32

def as_path(val)
  case val
  when ::Pathname
    val
  when ::String
    Pathname.new(val)
  end
end

.bannerargsObject



26
# File 'lib/squared/workspace/project/base.rb', line 26

def bannerargs(*); end

.batchargsObject



24
# File 'lib/squared/workspace/project/base.rb', line 24

def batchargs(*); end

.config?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/squared/workspace/project/base.rb', line 49

def config?(*)
  false
end

.populateObject



23
# File 'lib/squared/workspace/project/base.rb', line 23

def populate(*); end

.refObject



41
42
43
# File 'lib/squared/workspace/project/base.rb', line 41

def ref
  @ref ||= to_s.downcase.to_sym
end

.tasksObject



28
29
30
# File 'lib/squared/workspace/project/base.rb', line 28

def tasks
  [].freeze
end

.to_sObject



45
46
47
# File 'lib/squared/workspace/project/base.rb', line 45

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

Instance Method Details

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/squared/workspace/project/base.rb', line 278

def add(path, name = nil, **kwargs, &blk)
  if path.is_a?(::String) && (data = %r{^(.+)[\\/]\*+$}.match(path))
    path = basepath(data[1]).children.select(&:directory?)
  end
  if path.is_a?(::Array)
    name = @name if name == true
    path.each { |val| add(val, name && task_join(name, File.basename(val)), **kwargs, &blk) }
    return self
  elsif !projectpath?(path = basepath(path))
    return self
  elsif name.is_a?(::Symbol)
    name = name.to_s
  elsif !name.is_a?(::String)
    name = nil
  end
  if @withargs
    data = @withargs.dup
    data.merge!(kwargs)
    kwargs = data
  end
  kwargs[:group] = group unless kwargs.key?(:group)
  kwargs[:ref] = ref unless kwargs.key?(:ref)
  parent = self
  proj = nil
  workspace.add(path, name || path.basename, **kwargs) do
    variable_set :parent, parent
    proj = self
  end
  @children << proj
  proj.instance_eval(&blk) if block_given?
  self
end

#allrefObject



438
439
440
# File 'lib/squared/workspace/project/base.rb', line 438

def allref
  @ref.reverse_each
end

#basepath(*args, ascend: nil) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
# File 'lib/squared/workspace/project/base.rb', line 393

def basepath(*args, ascend: nil)
  ret = path.join(*args)
  return ret unless ascend && !ret.exist?

  path.parent.ascend.each do |dir|
    target = dir.join(*args)
    return target if target.exist?
    break if (ascend.is_a?(String) && dir.join(ascend).exist?) || workspace.root == dir || parent&.path == dir
  end
  ret
end

#build(*args, sync: invoked_sync?('build')) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/squared/workspace/project/base.rb', line 311

def build(*args, sync: invoked_sync?('build'), **)
  if args.empty?
    cmd, opts, var, flags = @output
    banner = verbose == 1
  else
    cmd = args.shift
    opts = args.map { |val| shell_quote(val, force: false) }.join(' ')
    banner = verbose
  end
  if cmd
    case opts
    when ::String
      cmd = "#{cmd} #{opts}"
    when ::Array
      cmd = cmd.join(' && ')
    else
      cmd = [cmd, compose(opts, script: false)].compact.join(' ') unless opts == false || !respond_to?(:compose)
    end
  else
    return unless respond_to?(:compose)

    cmd = compose(opts, flags, script: true)
  end
  run(cmd, var, banner: banner, sync: sync)
end

#build?Boolean

Returns:

  • (Boolean)


476
477
478
# File 'lib/squared/workspace/project/base.rb', line 476

def build?
  !!@output[0] || script?
end

#cleanObject



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/squared/workspace/project/base.rb', line 353

def clean(*)
  case @clean
  when ::String
    run_s(@clean, sync: invoked_sync?('clean'))
  when ::Enumerable
    as_a(@clean).each do |val|
      if (val = val.to_s) =~ %r{[\\/]$}
        dir = Pathname.new(val)
        dir = basepath(dir) unless dir.absolute?
        next unless dir.directory?

        log.warn "rm -rf #{dir}"
        dir.rmtree(verbose: true)
      else
        files = val.include?('*') ? Dir[basepath(val)] : [basepath(val)]
        files.each do |file|
          begin
            File.delete(file) if File.file?(file)
          rescue StandardError => e
            log.error e
          end
        end
      end
    end
  end
end

#clean?Boolean

Returns:

  • (Boolean)


504
505
506
# File 'lib/squared/workspace/project/base.rb', line 504

def clean?
  runnable?(@clean) || workspace.task_defined?(name, 'clean')
end

#color(val) ⇒ Object



405
406
407
408
# File 'lib/squared/workspace/project/base.rb', line 405

def color(val)
  ret = theme[val]
  ret && !ret.empty? ? ret : [val]
end

#copy(sync: invoked_sync?('copy')) ⇒ Object



341
342
343
# File 'lib/squared/workspace/project/base.rb', line 341

def copy(*, sync: invoked_sync?('copy'), **)
  run_s(@copy, sync: sync) if @copy
end

#copy?Boolean

Returns:

  • (Boolean)


492
493
494
# File 'lib/squared/workspace/project/base.rb', line 492

def copy?
  runnable?(@copy) || workspace.task_defined?(name, 'copy')
end

#depend(sync: invoked_sync?('depend')) ⇒ Object



337
338
339
# File 'lib/squared/workspace/project/base.rb', line 337

def depend(*, sync: invoked_sync?('depend'), **)
  run(@depend, sync: sync) if @depend
end

#depend?Boolean

Returns:

  • (Boolean)


484
485
486
# File 'lib/squared/workspace/project/base.rb', line 484

def depend?
  !!@depend
end

#dependtypeObject



442
443
444
# File 'lib/squared/workspace/project/base.rb', line 442

def dependtype(*)
  @dependindex ? @dependindex.succ : 0
end

#dev?Boolean

Returns:

  • (Boolean)


508
509
510
# File 'lib/squared/workspace/project/base.rb', line 508

def dev?
  @dev != false && workspace.dev?(pat: @dev, **scriptargs)
end

#doc(sync: invoked_sync?('doc')) ⇒ Object



345
346
347
# File 'lib/squared/workspace/project/base.rb', line 345

def doc(*, sync: invoked_sync?('doc'), **)
  build(@doc, sync: sync) if @doc
end

#doc?Boolean

Returns:

  • (Boolean)


496
497
498
# File 'lib/squared/workspace/project/base.rb', line 496

def doc?
  !!@doc
end

#enabled?(ref = nil) ⇒ Boolean

Returns:

  • (Boolean)


460
461
462
463
464
# File 'lib/squared/workspace/project/base.rb', line 460

def enabled?(ref = nil)
  return false if ref && !ref?(ref)

  path.directory? && !path.empty?
end

#graph(sync: invoked_sync?('graph'), pass: [], out: nil) ⇒ Object



380
381
382
383
384
385
# File 'lib/squared/workspace/project/base.rb', line 380

def graph(*, sync: invoked_sync?('graph'), pass: [], out: nil)
  data = graph_collect(self)
  data[name] << self unless out
  run_graph(data, name, out, sync: sync, pass: pass)
  out
end

#graph?Boolean

Returns:

  • (Boolean)


488
489
490
# File 'lib/squared/workspace/project/base.rb', line 488

def graph?
  @graph.is_a?(::Array) && !@graph.empty?
end

#has?(meth, ref = nil) ⇒ Boolean

Returns:

  • (Boolean)


466
467
468
469
470
# File 'lib/squared/workspace/project/base.rb', line 466

def has?(meth, ref = nil)
  return false if ref && !ref?(ref)

  respond_to?(meth = :"#{meth}?") && __send__(meth)
end

#initialize_build(ref, **kwargs) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/squared/workspace/project/base.rb', line 106

def initialize_build(ref, **kwargs)
  initialize_ref(ref)
  if (@script = @workspace.script_get(group: @group, ref: ref))
    if @script[:log] && !kwargs.key?(:log)
      kwargs[:log] = @script[:log]
      @log = nil
    end
    @depend = @script[:depend] if @depend.nil?
    @doc = @script[:doc] if @doc.nil?
    @test = @script[:test] if @test.nil?
    @clean = @script[:clean] if @clean.nil?
    @exclude = @script[:exclude] if @exclude.empty? && @script.key?(:exclude)
  end
  initialize_logger(**kwargs)
  return if @output[0] == false

  data = @workspace.script_find(*@ref, @group)
  if @output[0].nil?
    if (scr = data[:script])
      @global = true
      script_set(scr, prod: kwargs[:prod])
    elsif (run = data[:run])
      @global = true
      run_set run
    end
    unless data[:env]
      if (scr = kwargs[:script])
        @global = false
        script_set scr
      elsif @script && !data[:global]
        if (scr = @script[:script])
          @global = false
          script_set scr
        elsif (run = @script[:run])
          @global = false
          run_set run
        end
      end
    end
  elsif data[:env] && data[:run]
    @global = true
    run_set data[:run]
  end
end

#initialize_env(dev: nil, prod: nil) ⇒ Object



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/project/base.rb', line 185

def initialize_env(dev: nil, prod: nil, **)
  pre = "BUILD_#{@envname}"
  @dev = env_match("#{pre}_DEV", dev)
  @prod = env_match("#{pre}_PROD", prod)
  cmd = @output[0]
  unless cmd == false || cmd.is_a?(::Array) || (val = env('BUILD', suffix: 'OPTS')).nil?
    @output[cmd ? 1 : 3] = shell_split(val, join: true)
  end
  unless @output[2] == false || (val = env('BUILD', suffix: 'ENV')).nil?
    begin
      data = JSON.parse(val)
      raise_error('invalid JSON object', val, hint: "#{prefix}_ENV") unless data.is_a?(::Hash)
      @output[2] = data
    rescue StandardError => e
      log.warn e
    end
  end
  return unless (val = env('BUILD', strict: true))

  @global = false
  if val == '0'
    @output = [false]
  elsif script?
    script_set val
  else
    run_set val
  end
end

#initialize_logger(log: nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/squared/workspace/project/base.rb', line 151

def initialize_logger(log: nil, **)
  return if @log

  log = log.is_a?(::Hash) ? log.dup : { file: log }
  unless (file = env('LOG_FILE'))
    file = case env('LOG_AUTO')
           when 'y', 'year'
             "#{@name}-#{Date.today.year}.log"
           when 'm', 'month'
             "#{@name}-#{Date.today.strftime('%Y-%m')}.log"
           when 'd', 'day', '1'
             "#{@name}-#{Date.today}.log"
           end
  end
  if file ||= log[:file]
    file = Date.today.strftime(file)
    file = (dir = env('LOG_DIR')) ? @workspace.home.join(dir, file) : @workspace.home.join(file)
    begin
      file = file.realdirpath
    rescue StandardError => e
      raise if @exception

      file = nil
      warn log_message(::Logger::WARN, e) if warning?
    end
  end
  log[:progname] ||= @name
  if (val = env('LOG_LEVEL', ignore: false))
    log[:level] = val
  end
  log.delete(:file)
  @log = [file, log]
end

#initialize_ref(ref) ⇒ Object



102
103
104
# File 'lib/squared/workspace/project/base.rb', line 102

def initialize_ref(ref)
  @ref << ref unless @exclude.include?(ref)
end

#inspectObject



448
449
450
# File 'lib/squared/workspace/project/base.rb', line 448

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

#logObject



387
388
389
390
391
# File 'lib/squared/workspace/project/base.rb', line 387

def log
  return @log unless @log.is_a?(::Array)

  @log = Logger.new(enabled? ? @log[0] : nil, **@log[1])
end

#populateObject



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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/squared/workspace/project/base.rb', line 218

def populate(*)
  namespace name do
    workspace.series.each_key do |key|
      next unless workspace.task_include?(self, key)

      s = workspace.series.name_get(key)
      unless workspace.task_defined?(name, s)
        desc message(@desc, s)
        task s do
          __send__(key)
        end
      end
      next if (items = @children.select { |item| workspace.task_include?(item, key) }).empty?

      desc message(@desc, s, 'workspace')
      task task_join(s, 'workspace') => items.map { |item| task_join(item.name, s) }
    end
    next unless ref?(Base.ref)

    @@tasks[Base.ref].each do |action, flags|
      namespace action do
        flags.each do |flag|
          case action
          when :graph
            next unless graph?

            if flag == :run
              desc format_desc(action, flag, 'pass*')
              task flag, [:pass] do |_, args|
                graph(pass: args.to_a)
              end
            else
              desc format_desc(action, flag)
              task flag do
                out = graph(out: [])
                emphasize(out, title: path, right: true, border: borderstyle,
                               footer: "dependencies: #{out.size - 1}",
                               sub: [
                                 { pat: /^(#{Regexp.escape(path.to_s)})(.*)$/, styles: theme[:header] },
                                 { pat: /^(#{Regexp.escape(name)})(.*)$/, styles: theme[:active] },
                                 { pat: /^(\s*)([a-z]+: )(\d+)$/, styles: theme[:inline], index: 3 }
                               ])
              end
            end
          end
        end
      end
    end
  end
end

#prod?Boolean

Returns:

  • (Boolean)


512
513
514
# File 'lib/squared/workspace/project/base.rb', line 512

def prod?
  @prod != false && workspace.prod?(pat: @prod, **scriptargs)
end

#refObject



214
215
216
# File 'lib/squared/workspace/project/base.rb', line 214

def ref
  Base.ref
end

#ref?(val) ⇒ Boolean

Returns:

  • (Boolean)


472
473
474
# File 'lib/squared/workspace/project/base.rb', line 472

def ref?(val)
  @ref.include?(val)
end

#script?Boolean

Returns:

  • (Boolean)


480
481
482
# File 'lib/squared/workspace/project/base.rb', line 480

def script?
  @output[0].nil? && !!@output[1] && respond_to?(:compose)
end

#test(sync: invoked_sync?('test')) ⇒ Object



349
350
351
# File 'lib/squared/workspace/project/base.rb', line 349

def test(*, sync: invoked_sync?('test'), **)
  build(@test, sync: sync) if @test
end

#test?Boolean

Returns:

  • (Boolean)


500
501
502
# File 'lib/squared/workspace/project/base.rb', line 500

def test?
  !!@test
end

#to_sObject



452
453
454
# File 'lib/squared/workspace/project/base.rb', line 452

def to_s
  path.to_s
end

#to_symObject



456
457
458
# File 'lib/squared/workspace/project/base.rb', line 456

def to_sym
  name.to_sym
end

#variable_set(key, *val, **kwargs) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/squared/workspace/project/base.rb', line 410

def variable_set(key, *val, **kwargs)
  if variables.include?(key)
    case key
    when :run
      run_set(*val, **kwargs)
    when :script
      script_set(*val, **kwargs)
    when :env
      run_set(output[0], *val, **kwargs)
    when :parent
      @parent = val if (val = val.first).is_a?(Project::Base)
    when :graph
      @graph = case val.first
               when nil, false
                 nil
               else
                 val.flatten.map!(&:to_s).freeze
               end
    when :dependfile
      @dependfile = basepath(*val)
    else
      instance_variable_set :"@#{key}", val.first
    end
  else
    log.warn "variable_set: @#{key} (private)"
  end
end

#versionObject



446
# File 'lib/squared/workspace/project/base.rb', line 446

def version(*); end

#with(**kwargs, &blk) ⇒ Object



269
270
271
272
273
274
275
276
# File 'lib/squared/workspace/project/base.rb', line 269

def with(**kwargs, &blk)
  @withargs = kwargs.empty? ? nil : kwargs
  if block_given?
    instance_eval(&blk)
    @withargs = nil
  end
  self
end