Class: Squared::Workspace::Project::Base
- Inherits:
-
Object
- Object
- Squared::Workspace::Project::Base
- Includes:
- Comparable, Prompt, Rake::DSL, Shell, Common::Format, Support, System, Utils
- Defined in:
- lib/squared/workspace/project/base.rb
Constant Summary collapse
- @@tasks =
{}
- @@graph =
{ _: [] }
- @@print_order =
0
Constants included from Common
Instance Attribute Summary collapse
-
#dependfile ⇒ Object
readonly
Returns the value of attribute dependfile.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#pipe ⇒ Object
readonly
Returns the value of attribute pipe.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Class Method Summary collapse
- .aliasargs ⇒ Object
- .as_path(val) ⇒ Object
- .bannerargs ⇒ Object
- .batchargs ⇒ Object
- .config? ⇒ Boolean
- .populate ⇒ Object
- .ref ⇒ Object
- .subtasks(val = nil, &blk) ⇒ Object
- .tasks ⇒ Object
- .to_s ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add(path, name = nil, **kwargs, &blk) ⇒ Object
- #allref ⇒ Object
- #archive(sync: invoked_sync?('archive')) ⇒ Object
- #archive? ⇒ Boolean
- #as(cmd, script, to = nil) ⇒ Object
- #basepath(*args) ⇒ Object
- #build(*args, sync: invoked_sync?('build'), from: :run) ⇒ Object
- #build? ⇒ Boolean
- #chain(*args, **kwargs) ⇒ Object
- #clean(*args, sync: invoked_sync?('clean'), pass: false, **kwargs) ⇒ Object
- #clean? ⇒ Boolean
- #copy(sync: invoked_sync?('copy')) ⇒ Object
- #copy? ⇒ Boolean
- #depend(sync: invoked_sync?('depend')) ⇒ Object
- #depend? ⇒ Boolean
- #dependtype ⇒ Object
- #dev? ⇒ Boolean
- #doc(sync: invoked_sync?('doc')) ⇒ Object
- #doc? ⇒ Boolean
- #enabled?(ref = nil) ⇒ Boolean
- #error(key, *args, **kwargs, &blk) ⇒ Object
- #event(name, key, *args, override: false, **kwargs, &blk) ⇒ Object
- #exclude?(*refs) ⇒ Boolean
- #first(key, *args, **kwargs, &blk) ⇒ Object
- #generate(keys) ⇒ Object
- #graph(start = [], tasks = nil, sync: invoked_sync?('graph'), pass: [], out: nil) ⇒ Object
- #graph? ⇒ Boolean
- #has?(meth, ref = nil) ⇒ Boolean
-
#initialize(workspace, path, name, group: nil, archive: nil, first: {}, last: {}, error: {}, common: , **kwargs) ⇒ Base
constructor
A new instance of Base.
- #initialize_build(ref, **kwargs) ⇒ Object
- #initialize_env(dev: nil, prod: nil) ⇒ Object
- #initialize_events(ref) ⇒ Object
- #initialize_logger(log: nil) ⇒ Object
- #initialize_ref(ref) ⇒ Object
- #inject(obj, *args, **kwargs, &blk) ⇒ Object
- #inspect ⇒ Object
- #last(key, *args, **kwargs, &blk) ⇒ Object
- #lint(sync: invoked_sync?('lint')) ⇒ Object
- #lint? ⇒ Boolean
- #localname ⇒ Object
- #log ⇒ Object
- #populate(keys) ⇒ Object
- #prereqs(sync: invoked_sync?('prereqs')) ⇒ Object
- #prereqs? ⇒ Boolean
- #prod? ⇒ Boolean
- #ref ⇒ Object
- #ref?(val) ⇒ Boolean
- #rootpath(*args, ascend: nil) ⇒ Object
- #script? ⇒ Boolean
- #series(key, override: false, &blk) ⇒ Object
- #task_include?(key, ref = nil) ⇒ Boolean
- #test(sync: invoked_sync?('test')) ⇒ Object
- #test? ⇒ Boolean
- #to_s ⇒ Object
- #to_sym ⇒ Object
- #unpack(target, uri:, sync: true, digest: nil, ext: nil, force: false, depth: 1, headers: {}, from: :unpack) ⇒ Object
- #variable_set(key, *args, **kwargs, &blk) ⇒ Object
- #version ⇒ Object
- #with(**kwargs, &blk) ⇒ Object
Methods included from Common::Format
#enable_aixterm, #enable_drawing
Constructor Details
#initialize(workspace, path, name, group: nil, archive: nil, first: {}, last: {}, error: {}, common: , **kwargs) ⇒ Base
Returns a new instance of Base.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/squared/workspace/project/base.rb', line 79 def initialize(workspace, path, name, *, group: nil, archive: nil, first: {}, last: {}, error: {}, 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] @lint = kwargs[:lint] @test = kwargs[:test] @copy = kwargs[:copy] @clean = kwargs[:clean] @version = kwargs[:version] @archive = case archive when String, Array { uri: archive } when Hash archive end @release = kwargs[:release] @envname = @name.gsub(/[^\w]+/, '_').upcase.freeze @output = [] @ref = [] @children = [] @events = { first: first, last: last, error: error } @desc = (@name.include?(':') ? @name.split(':').join(ARG[:SPACE]) : @name).freeze @parent = nil @global = false @index = -1 run_set(kwargs[:run], kwargs[:env], opts: kwargs.fetch(:opts, true)) exception_set kwargs[:exception] pipe_set kwargs[:pipe] verbose_set kwargs[:verbose] theme_set common graph_set kwargs[:graph] pass_set kwargs[:pass] exclude_set kwargs[:exclude] initialize_ref Base.ref end |
Instance Attribute Details
#dependfile ⇒ Object (readonly)
Returns the value of attribute dependfile.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def dependfile @dependfile end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def exception @exception end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def group @group end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def path @path end |
#pipe ⇒ Object (readonly)
Returns the value of attribute pipe.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def pipe @pipe end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def project @project end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def theme @theme end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def verbose @verbose end |
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
76 77 78 |
# File 'lib/squared/workspace/project/base.rb', line 76 def workspace @workspace end |
Class Method Details
.aliasargs ⇒ Object
32 |
# File 'lib/squared/workspace/project/base.rb', line 32 def aliasargs(*); end |
.as_path(val) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/squared/workspace/project/base.rb', line 39 def as_path(val) case val when Pathname val when String Pathname.new(val) end end |
.bannerargs ⇒ Object
33 |
# File 'lib/squared/workspace/project/base.rb', line 33 def (*); end |
.batchargs ⇒ Object
31 |
# File 'lib/squared/workspace/project/base.rb', line 31 def batchargs(*); end |
.config? ⇒ Boolean
58 59 60 |
# File 'lib/squared/workspace/project/base.rb', line 58 def config?(*) false end |
.populate ⇒ Object
30 |
# File 'lib/squared/workspace/project/base.rb', line 30 def populate(*); end |
.ref ⇒ Object
48 49 50 |
# File 'lib/squared/workspace/project/base.rb', line 48 def ref @ref ||= to_s.downcase.to_sym end |
.subtasks(val = nil, &blk) ⇒ Object
52 53 54 55 56 |
# File 'lib/squared/workspace/project/base.rb', line 52 def subtasks(val = nil, &blk) return @@tasks[val || ref].each(&blk) if block_given? @@tasks[ref] = val.freeze end |
.tasks ⇒ Object
35 36 37 |
# File 'lib/squared/workspace/project/base.rb', line 35 def tasks (%i[build archive graph prereqs] + BLK_SET).freeze end |
.to_s ⇒ Object
62 63 64 |
# File 'lib/squared/workspace/project/base.rb', line 62 def to_s super[/[^:]+\z/, 0] end |
Instance Method Details
#<=>(other) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/squared/workspace/project/base.rb', line 246 def <=>(other) return 0 unless workspace == other.workspace a, b = graph_deps return 1 if a.include?(other) c, d = graph_deps other e = b.reject { |val| d.include?(val) } f = d.reject { |val| b.include?(val) } if parent == other.parent g = [] h = [] else g = a.reject { |val| c.include?(val) } h = c.reject { |val| a.include?(val) } end g << self h << other e.concat(g) f.concat(h) if g.any? { |val| f.include?(val) } -1 elsif h.any? { |val| e.include?(val) } 1 elsif e.any? { |val| f.include?(val) } -1 elsif f.any? { |val| e.include?(val) } 1 elsif @index != -1 && (i = other.instance_variable_get(:@index)) != -1 @index < i ? -1 : 1 else 0 end rescue StandardError => e log&.debug e 0 end |
#add(path, name = nil, **kwargs, &blk) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/squared/workspace/project/base.rb', line 371 def add(path, name = nil, **kwargs, &blk) if path.is_a?(String) && (seg = path[%r{\A(.+)[\\/]\*+\z}, 1]) return self unless checkdir?(path = basepath(seg)) path = path.children.select { |val| checkdir?(val) } 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)) || !checkdir?(path) return self else name = case name when String, Symbol name.to_s end 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 name = case name when String, Symbol name.to_s else path.basename end workspace.add(path, name, **kwargs) do __send__ :parent_set, parent proj = self end @children << proj proj.instance_eval(&blk) if block_given? self end |
#allref ⇒ Object
932 933 934 |
# File 'lib/squared/workspace/project/base.rb', line 932 def allref @ref.reverse_each end |
#archive(sync: invoked_sync?('archive')) ⇒ Object
528 529 530 531 532 |
# File 'lib/squared/workspace/project/base.rb', line 528 def archive(*, sync: invoked_sync?('archive'), **) return unless @archive.is_a?(Hash) unpack(path, **@archive, sync: sync, from: :archive) end |
#archive? ⇒ Boolean
865 866 867 |
# File 'lib/squared/workspace/project/base.rb', line 865 def archive? @archive.is_a?(Hash) && (!path.exist? || path.empty?) end |
#as(cmd, script, to = nil) ⇒ Object
772 773 774 775 776 777 |
# File 'lib/squared/workspace/project/base.rb', line 772 def as(cmd, script, to = nil) script = { "#{script}": to } if to data = (@as ||= {})[cmd.to_sym] ||= {} script.each { |key, val| data[key.to_s] = val } self end |
#basepath(*args) ⇒ Object
936 937 938 |
# File 'lib/squared/workspace/project/base.rb', line 936 def basepath(*args) path.join(*args) end |
#build(*args, sync: invoked_sync?('build'), from: :run) ⇒ Object
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/squared/workspace/project/base.rb', line 430 def build(*args, sync: invoked_sync?('build'), from: :run, **) = verbose if args.empty? return unless from == :run run_b(@run, sync: sync, from: from) if series?(@run) args = @output = verbosetype > 1 if from_base?('build') end if args.first.is_a?(Struct) f, blk = args.first.to_a args[0] = instance_eval(&blk) || f return unless args[0] end if args.all? { |val| val.is_a?(Array) } cmd = [] var = {} args.each do |val| next instance_exec(*val[1..-1], &val[0]) if val.first.is_a?(Proc) a, b, c, d, e = val case b when Hash b = append_hash(b, build: true).join(' ') when Enumerable b = b.to_a.join(' ') end d = append_hash(d).join(' ') if d.is_a?(Hash) if a cmd << [a, d, b].compact.join(' ') else next unless respond_to?(:compose) cmd << a if (a = compose(as_get(b), d, script: true, args: e, from: from)) end var.merge!(c) if c.is_a?(Hash) end cmd = cmd.join(' && ') else cmd, opts, var, flags, extra = args end if cmd cmd = as_get cmd opts = compose(opts, script: false) if opts && respond_to?(:compose) flags = append_hash(flags).join(' ') if flags.is_a?(Hash) case opts when Hash opts = append_hash(opts, build: true) cmd = as_a(cmd).push(flags).concat(opts).compact.join(' ') when Enumerable cmd = as_a(cmd).concat(opts.to_a) cmd.map! { |val| "#{val} #{flags}" } if flags cmd = cmd.join(' && ') else cmd = [cmd, flags, opts].compact.join(' ') if opts || flags end else return unless (opts || extra) && respond_to?(:compose) cmd = compose(as_get(opts), flags, script: true, args: extra, from: from) from = :script if from == :run && script? end run(cmd, var, sync: sync, from: from, banner: ) end |
#build? ⇒ Boolean
853 854 855 |
# File 'lib/squared/workspace/project/base.rb', line 853 def build? !!@output[0] || script? || series?(@run) end |
#chain(*args, **kwargs) ⇒ Object
413 414 415 416 |
# File 'lib/squared/workspace/project/base.rb', line 413 def chain(*args, **kwargs) workspace.chain(*args, project: self, **kwargs) self end |
#clean(*args, sync: invoked_sync?('clean'), pass: false, **kwargs) ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/squared/workspace/project/base.rb', line 550 def clean(*args, sync: invoked_sync?('clean'), pass: false, **kwargs) return unless @clean on :first, :clean unless pass case @clean when Struct if (any = instance_eval(&@clean.block) || @clean.run) temp = @clean @clean = any clean(*args, sync: sync, pass: true, **kwargs) @clean = temp end when String run_s(@clean, sync: sync) when Hash begin @clean.each { |cmd, opts| build(cmd.to_s, opts, sync: sync) } rescue StandardError => e log&.error e ret = on(:error, from, e) raise if exception && ret != true end else if @clean.is_a?(Enumerable) && !series?(@clean) @clean.each do |val| entry = path + (val = val.to_s) if entry.directory? && val.match?(%r{[\\/]\z}) log&.warn "rm -rf #{entry}" rm_rf(entry, verbose: verbose) else log&.warn "rm #{entry}" (val.include?('*') ? Dir[entry] : [entry]).each do |file| next unless File.file?(file) begin File.delete(file) rescue StandardError => e log&.error e end end end end else run_b(@clean, sync: sync) end end on :last, :clean unless pass end |
#clean? ⇒ Boolean
898 899 900 |
# File 'lib/squared/workspace/project/base.rb', line 898 def clean? runnable?(@clean) || workspace.task_defined?(name, 'clean') end |
#copy(sync: invoked_sync?('copy')) ⇒ Object
546 547 548 |
# File 'lib/squared/workspace/project/base.rb', line 546 def copy(*, sync: invoked_sync?('copy'), **) run_b(@copy, sync: sync, from: :copy) end |
#copy? ⇒ Boolean
882 883 884 |
# File 'lib/squared/workspace/project/base.rb', line 882 def copy? runnable?(@copy) || workspace.task_defined?(name, 'copy') end |
#depend(sync: invoked_sync?('depend')) ⇒ Object
495 496 497 |
# File 'lib/squared/workspace/project/base.rb', line 495 def depend(*, sync: invoked_sync?('depend'), **) run_b(@depend, sync: sync, from: :depend) end |
#depend? ⇒ Boolean
861 862 863 |
# File 'lib/squared/workspace/project/base.rb', line 861 def depend? !!@depend end |
#dependtype ⇒ Object
922 923 924 |
# File 'lib/squared/workspace/project/base.rb', line 922 def dependtype(*) @dependindex ? @dependindex.succ : 0 end |
#dev? ⇒ Boolean
902 903 904 |
# File 'lib/squared/workspace/project/base.rb', line 902 def dev? @dev != false && workspace.dev?(pat: @dev, **scriptargs) end |
#doc(sync: invoked_sync?('doc')) ⇒ Object
534 535 536 |
# File 'lib/squared/workspace/project/base.rb', line 534 def doc(*, sync: invoked_sync?('doc'), **) run_b(@doc, sync: sync, from: :doc, banner: from_base?('doc') ? verbosetype > 1 : verbose) end |
#doc? ⇒ Boolean
886 887 888 |
# File 'lib/squared/workspace/project/base.rb', line 886 def doc? !!@doc end |
#enabled?(ref = nil) ⇒ Boolean
837 838 839 840 841 |
# File 'lib/squared/workspace/project/base.rb', line 837 def enabled?(ref = nil, **) return false if ref && !ref?(ref) (path.directory? && !path.empty?) || archive? end |
#error(key, *args, **kwargs, &blk) ⇒ Object
757 758 759 |
# File 'lib/squared/workspace/project/base.rb', line 757 def error(key, *args, **kwargs, &blk) event(:error, key, *args, **kwargs, &blk) end |
#event(name, key, *args, override: false, **kwargs, &blk) ⇒ Object
761 762 763 764 765 766 767 768 769 770 |
# File 'lib/squared/workspace/project/base.rb', line 761 def event(name, key, *args, override: false, **kwargs, &blk) data = @events[name.to_sym] ||= {} items = if override data[key.to_sym] = [] else data[key.to_sym] ||= [] end items << [block_given? ? [blk] + args : args, kwargs] self end |
#exclude?(*refs) ⇒ Boolean
910 911 912 |
# File 'lib/squared/workspace/project/base.rb', line 910 def exclude?(*refs) !@exclude.empty? && has_value?(@exclude, refs.flatten) end |
#first(key, *args, **kwargs, &blk) ⇒ Object
749 750 751 |
# File 'lib/squared/workspace/project/base.rb', line 749 def first(key, *args, **kwargs, &blk) event(:first, key, *args, **kwargs, &blk) end |
#generate(keys) ⇒ Object
358 359 360 |
# File 'lib/squared/workspace/project/base.rb', line 358 def generate(keys, **) task_build keys end |
#graph(start = [], tasks = nil, sync: invoked_sync?('graph'), pass: [], out: nil) ⇒ Object
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/squared/workspace/project/base.rb', line 599 def graph(start = [], tasks = nil, *, sync: invoked_sync?('graph'), pass: [], out: nil, **) if (val = env('GRAPH', strict: true)) tasks ||= [] split_escape(val).each do |task| if ref?(task.to_sym) && (script = workspace.script_get(:graph, ref: task.to_sym)) tasks.concat(script[:graph]) else tasks << task end end end pass.concat(split_escape(val)) if (val = env('GRAPH', suffix: 'PASS')) start, neg = start.partition { |name| !name.start_with?('-') } data = graph_collect(self, start, pass: neg.map! { |name| name[1..-1] }) unless out data[name] << self on :first, :graph end ret = graph_branch(self, data, tasks, out, sync: sync, pass: pass) rescue StandardError => e ret = on(:error, :graph, e) raise unless ret == true else if out [out, ret] else on :last, :graph end end |
#graph? ⇒ Boolean
869 870 871 |
# File 'lib/squared/workspace/project/base.rb', line 869 def graph? @graph.is_a?(Array) && !@graph.empty? end |
#has?(meth, ref = nil) ⇒ Boolean
843 844 845 846 847 |
# File 'lib/squared/workspace/project/base.rb', line 843 def has?(meth, ref = nil) return false if ref && !ref?(ref) respond_to?(meth = :"#{meth}?") && __send__(meth) end |
#initialize_build(ref, **kwargs) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/squared/workspace/project/base.rb', line 128 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? @lint = @script[:lint] if @lint.nil? @test = @script[:test] if @test.nil? @clean = @script[:clean] if @clean.nil? @exclude = @script[:exclude] if @exclude.empty? && @script.key?(:exclude) end initialize_events(ref, **kwargs) 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, args: data.fetch(:args, kwargs[:args]), 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, args: kwargs[:args]) elsif @script && !data[:global] if (scr = @script[:script]) @global = false script_set(scr, args: @script.fetch(:args, kwargs[:args])) elsif (run = @script[:run]) @global = false run_set run end end end elsif data[:env] && data[:run] @global = true run_set data[:run] end @global = true end |
#initialize_env(dev: nil, prod: nil) ⇒ Object
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 |
# File 'lib/squared/workspace/project/base.rb', line 218 def initialize_env(dev: nil, prod: nil, **) @dev = env_match('BUILD', dev, suffix: 'DEV', strict: true) @prod = env_match('BUILD', prod, suffix: 'PROD', strict: true) if @output[2] != false && (val = env('BUILD', suffix: 'ENV')) @output[2] = parse_json(val, hint: "BUILD_#{@envname}_ENV") || @output[2] end unless @output[0] == false || @output[0].is_a?(Array) if (val = env('BUILD', suffix: 'OPTS')) n = @output[0] ? 1 : 3 @output[n] = merge_opts(@output[n], shell_split(val)) end if (val = env(ref.to_s.upcase, suffix: 'OPTS')) @output[4] = merge_opts(@output[4], shell_split(val)) end end @version = val if (val = env('BUILD', suffix: 'VERSION')) 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_events(ref) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/squared/workspace/project/base.rb', line 176 def initialize_events(ref, **) return unless (events = @workspace.events_get(group: @group, ref: ref)) events.each do |task, data| data.each { |ev, blk| (@events[ev] ||= {})[task] ||= [blk] } end end |
#initialize_logger(log: nil) ⇒ Object
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 213 214 215 216 |
# File 'lib/squared/workspace/project/base.rb', line 184 def initialize_logger(log: nil, **) return if @log log = log.is_a?(Hash) ? log.dup : { file: log } if (file = env('LOG_FILE')) file = Time.now.strftime(file) elsif (val = env('LOG_AUTO')) file = "#{@name}-%s.log" % [case val when 'y', 'year' Date.today.year when 'm', 'month' Date.today.strftime('%Y-%m') when 'd', 'day', '1' Date.today else val.include?('%') ? Time.now.strftime(val) : Time.now.strftime('%FT%T%:z') end] elsif (val = log[:file]) file = val.is_a?(String) ? Time.now.strftime(val) : "#{@name}-#{Date.today}.log" end begin file &&= @workspace.home.join(env('LOG_DIR', ''), file).realdirpath rescue StandardError => e file = nil warn (Logger::WARN, e, pass: true) if warning? end log[:progname] ||= @name if (val = env('LOG_LEVEL', ignore: false)) log[:level] = val.match?(/\d/) ? log_sym(val.to_i) : val end log.delete(:file) @log = [file, log] end |
#initialize_ref(ref) ⇒ Object
124 125 126 |
# File 'lib/squared/workspace/project/base.rb', line 124 def initialize_ref(ref) @ref << ref unless @exclude.include?(ref) end |
#inject(obj, *args, **kwargs, &blk) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/squared/workspace/project/base.rb', line 418 def inject(obj, *args, **kwargs, &blk) return self unless enabled? out = obj.link(self, *args, **kwargs, &blk) if obj.respond_to?(:link) if !out warn (Logger::WARN, 'link not compatible', subject: obj.to_s, hint: name, pass: true) elsif out.respond_to?(:build) out.build end self end |
#inspect ⇒ Object
956 957 958 |
# File 'lib/squared/workspace/project/base.rb', line 956 def inspect "#<#{self.class}: #{name} => #{self}>" end |
#last(key, *args, **kwargs, &blk) ⇒ Object
753 754 755 |
# File 'lib/squared/workspace/project/base.rb', line 753 def last(key, *args, **kwargs, &blk) event(:last, key, *args, **kwargs, &blk) end |
#lint(sync: invoked_sync?('lint')) ⇒ Object
538 539 540 |
# File 'lib/squared/workspace/project/base.rb', line 538 def lint(*, sync: invoked_sync?('lint'), **) run_b(@lint, sync: sync, from: :lint) end |
#lint? ⇒ Boolean
890 891 892 |
# File 'lib/squared/workspace/project/base.rb', line 890 def lint? !!@lint end |
#localname ⇒ Object
952 953 954 |
# File 'lib/squared/workspace/project/base.rb', line 952 def localname workspace.task_localname(name) end |
#log ⇒ Object
926 927 928 929 930 |
# File 'lib/squared/workspace/project/base.rb', line 926 def log return @log unless @log.is_a?(Array) @log = Logger.new(enabled? ? @log[0] : nil, **@log[1]) end |
#populate(keys) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 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 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/project/base.rb', line 288 def populate(keys, **) task_build keys return unless ref?(Base.ref) namespace name do Base.subtasks do |action, flags| next if @pass.include?(action) namespace action do flags.each do |flag| case action when 'graph' next unless graph? format_desc action, flag, '(-)project*' task flag do |_, args| args = args.to_a.reject { |val| name == val.to_s } if flag == :run graph args else out, done = graph(args, out: []) out.map! do |val| done.each_with_index do |proj, i| next unless val.match?(/ #{Regexp.escape(proj.name)}(?:@\d|\z)/) val += " (#{i.succ})" break end val end emphasize(out, title: path, right: true, border: borderstyle, sub: [ { pat: /\A(#{Regexp.escape(path.to_s)})(.*)\z/, styles: theme[:header] }, { pat: /\A(#{Regexp.escape(name)})(.*)\z/, styles: theme[:active] }, { pat: /\A(.+ )(\()(\d+)(\))(.*)\z/, styles: theme[:inline], index: 3 } ]) end end when 'unpack' format_desc(action, flag, 'tag/url,dir,digest?,f|force?', before: flag == :ext ? 'ext' : nil) params = %i[tag dir digest force] params.unshift(:ext) if flag == :ext task flag, params do |_, args| ext = flag == :ext ? param_guard(action, flag, args: args, key: :ext) : flag.to_s tag = param_guard(action, flag, args: args, key: :tag) dir = param_guard(action, flag, args: args, key: :dir) unless tag.match?(URI_SCHEME) if flag == :gem tag = "https://rubygems.org/downloads/#{File.basename(tag, '.gem')}.gem" elsif @release tag = "%s.#{ext}" % [@release.include?('??') ? @release.sub('??', tag) : @release + tag] else raise_error("no base uri: #{tag}", hint: ext) end end case (digest = args.digest) when 'f', 'force' digest = nil force = true else force = args.fetch(:force, false) end unpack(path + dir, uri: tag, digest: digest, ext: ext, force: force) end end end end end end end |
#prereqs(sync: invoked_sync?('prereqs')) ⇒ Object
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/squared/workspace/project/base.rb', line 499 def prereqs(*, sync: invoked_sync?('prereqs'), **) on :first, :prereqs graph_deps.flatten(1).sort.each do |proj| next if @@graph[:_].include?(proj) if (val = ENV["PREREQS_#{proj.instance_variable_get(:@envname)}"] || ENV["PREREQS_#{proj.ref.upcase}"]) val.split(/\s*,\s*/).each do |meth| if proj.respond_to?(meth.to_sym) begin proj.__send__(meth, sync: sync) rescue StandardError => e ret = on(:error, :prereqs, e) raise unless ret == true else next end end if warning? warn (Logger::WARN, name, 'method not found', subject: 'prereqs', hint: meth, pass: true) end end elsif proj.build? proj.build(sync: sync) end @@graph[:_] << proj end on :last, :prereqs end |
#prereqs? ⇒ Boolean
873 874 875 876 877 878 879 880 |
# File 'lib/squared/workspace/project/base.rb', line 873 def prereqs? target = self loop do return true if target.graph? break unless (target = target.parent) end false end |
#prod? ⇒ Boolean
906 907 908 |
# File 'lib/squared/workspace/project/base.rb', line 906 def prod? @prod != false && workspace.prod?(pat: @prod, **scriptargs) end |
#ref ⇒ Object
284 285 286 |
# File 'lib/squared/workspace/project/base.rb', line 284 def ref Base.ref end |
#ref?(val) ⇒ Boolean
849 850 851 |
# File 'lib/squared/workspace/project/base.rb', line 849 def ref?(val) @ref.include?(val) end |
#rootpath(*args, ascend: nil) ⇒ Object
940 941 942 943 944 945 946 947 948 949 950 |
# File 'lib/squared/workspace/project/base.rb', line 940 def rootpath(*args, ascend: nil) ret = basepath(*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 |
#script? ⇒ Boolean
857 858 859 |
# File 'lib/squared/workspace/project/base.rb', line 857 def script? @output[0].nil? && !!@output[1] && respond_to?(:compose) end |
#series(key, override: false, &blk) ⇒ Object
779 780 781 782 783 784 785 786 787 788 789 790 |
# File 'lib/squared/workspace/project/base.rb', line 779 def series(key, override: false, &blk) if blocks.include?(key.to_sym) && block_given? if !override && series?(target = instance_variable_get(:"@#{key}")) target << blk else instance_variable_set :"@#{key}", [blk] end else log&.warn "series: @#{key} (invalid)" end self end |
#task_include?(key, ref = nil) ⇒ Boolean
914 915 916 |
# File 'lib/squared/workspace/project/base.rb', line 914 def task_include?(key, ref = nil) workspace.task_include?(self, key, ref) && !@pass.include?(key.to_s) end |
#test(sync: invoked_sync?('test')) ⇒ Object
542 543 544 |
# File 'lib/squared/workspace/project/base.rb', line 542 def test(*, sync: invoked_sync?('test'), **) run_b(@test, sync: sync, from: :test) end |
#test? ⇒ Boolean
894 895 896 |
# File 'lib/squared/workspace/project/base.rb', line 894 def test? !!@test end |
#to_s ⇒ Object
960 961 962 |
# File 'lib/squared/workspace/project/base.rb', line 960 def to_s path.to_s end |
#to_sym ⇒ Object
964 965 966 |
# File 'lib/squared/workspace/project/base.rb', line 964 def to_sym name.to_sym end |
#unpack(target, uri:, sync: true, digest: nil, ext: nil, force: false, depth: 1, headers: {}, from: :unpack) ⇒ Object
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 |
# File 'lib/squared/workspace/project/base.rb', line 629 def unpack(target, uri:, sync: true, digest: nil, ext: nil, force: false, depth: 1, headers: {}, from: :unpack) if !target.exist? target.mkpath elsif !target.directory? raise_error('invalid location', hint: target) elsif !target.empty? raise_error('directory not empty', hint: target) unless force || env('UNPACK_FORCE') create = true end if digest require 'digest' digest, type = digest.split(':', 2).reverse algo = case type&.downcase || digest.size when 32, 'md5' Digest::MD5 when 'rmd160' Digest::RMD160 when 40, 'sha1' Digest::SHA1 when 64, 'sha256' Digest::SHA256 when 96, 'sha384' Digest::SHA384 when 128, 'sha512' Digest::SHA512 else raise_error("invalid checksum: #{digest}", hint: name) end end if (val = env('HEADERS')) && (val = parse_json(val, hint: "HEADERS_#{@envname}")) headers = headers.is_a?(Hash) ? headers.merge(val) : val end data = nil (uri = as_a(uri)).each_with_index do |url, index| fetch_uri(url, headers) do |f| data = f.read if algo && algo.hexdigest(data) != digest data = nil raise_error("checksum failed: #{digest}", hint: url) if index == uri.size - 1 end next if ext && index == 0 case f.content_type when 'application/zip' ext = 'zip' when 'application/x-gzip' ext = 'tgz' when 'application/x-xz' ext = 'txz' end end break uri = url if data end unless data && (ext ||= URI.parse(uri).path[/\.(\w+)(?:\?|\z)/, 1]) raise_error("no content#{data ? ' type' : ''}", hint: uri) end ext = ext.downcase if (val = env("#{%w[zip 7z gem].include?(ext) ? ext.upcase : 'TAR'}_DEPTH", ignore: false)) depth = val.to_i end begin if ext == 'gem' dir = Dir.mktmpdir file = File.new(File.join(dir, File.basename(uri)), 'w') else require 'tempfile' file = Tempfile.new("#{name}-") end file.write(data) file.close if create warn (Logger::WARN, 'force remove', subject: name, hint: target, pass: true) target.rmtree target.mkpath end case ext when 'zip', 'aar' session 'unzip', shell_quote(file.path), quote_option('d', target) when 'tar', 'tgz', 'tar.gz', 'tar.xz', 'gz', 'xz' flags = +(verbose ? 'v' : '') if ext.end_with?('gz') flags += 'z' elsif ext.end_with?('xz') flags += 'J' end session 'tar', "-x#{flags}", basic_option('strip-components', depth), quote_option('f', file.path), quote_option('C', target) depth = 0 when '7z' session '7z', 'x', shell_quote(file.path), "-o#{shell_quote(target)}" when 'gem' session 'gem', 'unpack', shell_quote(file.path), quote_option('target', target) depth = 0 unless val else raise_error("unsupported format: #{ext}", hint: uri) end run(sync: sync, from: from) while depth > 0 && target.children.size == 1 entry = target.children.first break unless entry.directory? i = 0 while (dest = target + "#{File.basename(file.path)}-#{i}").exist? i += 1 end FileUtils.mv(entry, dest) dest.children.each { |child| FileUtils.mv(child, target) } dest.rmdir target = entry depth -= 1 end ensure if dir remove_entry dir else file&.unlink end end end |
#variable_set(key, *args, **kwargs, &blk) ⇒ Object
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 |
# File 'lib/squared/workspace/project/base.rb', line 792 def variable_set(key, *args, **kwargs, &blk) if variables.include?(key) || blocks.include?(key) val = case args.size when 0 nil when 1 args.first else args end case key when :index index_set val when :graph graph_set val when :pass pass_set val when :exclude exclude_set val when :parent parent_set val when :run run_set(*args, **kwargs) when :script script_set(*args, **kwargs) when :env run_set(output[0], *args, **kwargs) when :dependfile @dependfile = basepath(*args) else if block_given? if blocks.include?(key) series key, &blk return self end val = block_args val, &blk end instance_variable_set(:"@#{key}", val.first) end else log&.warn "variable_set: @#{key} (private)" end self end |
#version ⇒ Object
918 919 920 |
# File 'lib/squared/workspace/project/base.rb', line 918 def version(*) @version end |
#with(**kwargs, &blk) ⇒ Object
362 363 364 365 366 367 368 369 |
# File 'lib/squared/workspace/project/base.rb', line 362 def with(**kwargs, &blk) @withargs = kwargs.empty? ? nil : kwargs if block_given? instance_eval(&blk) @withargs = nil end self end |