Class: Squared::Repo::Workspace
- Inherits:
-
Object
- Object
- Squared::Repo::Workspace
- Includes:
- Format, Rake::DSL, Common, System, Task
- Defined in:
- lib/squared/repo/workspace.rb
Constant Summary collapse
- TASK_KEYS =
TASK_NAME.keys.freeze
Class Attribute Summary collapse
-
.project_kind ⇒ Object
readonly
Returns the value of attribute project_kind.
Instance Attribute Summary collapse
-
#exception ⇒ Object
Returns the value of attribute exception.
-
#home ⇒ Object
readonly
Returns the value of attribute home.
-
#main ⇒ Object
readonly
Returns the value of attribute main.
-
#manifest ⇒ Object
Returns the value of attribute manifest.
-
#manifest_url ⇒ Object
Returns the value of attribute manifest_url.
-
#multiple ⇒ Object
readonly
Returns the value of attribute multiple.
-
#parallel ⇒ Object
readonly
Returns the value of attribute parallel.
-
#pipe ⇒ Object
Returns the value of attribute pipe.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#series ⇒ Object
readonly
Returns the value of attribute series.
-
#sync ⇒ Object
readonly
Returns the value of attribute sync.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#warning ⇒ Object
Returns the value of attribute warning.
Class Method Summary collapse
Instance Method Summary collapse
- #add(name, path = nil, ref: nil, **kwargs) ⇒ Object
- #apply(&blk) ⇒ Object
- #bool_match(val, pat) ⇒ Object
- #build(**kwargs) {|_self| ... } ⇒ Object
- #clean(script, group: nil, ref: nil) ⇒ Object
- #compose(name, &blk) ⇒ Object
- #depend(script, group: nil, ref: nil) ⇒ Object
- #dev?(script: nil, pat: nil, global: nil) ⇒ Boolean
- #doc(script, group: nil, ref: nil) ⇒ Object
- #empty?(dir, repo: true) ⇒ Boolean
- #enabled? ⇒ Boolean
- #env(key, equals: nil, ignore: nil) ⇒ Object
- #find_base(obj) ⇒ Object
- #group(name, path, **kwargs) ⇒ Object
- #home_path(*args) ⇒ Object
-
#initialize(main, common: true) ⇒ Workspace
constructor
A new instance of Workspace.
- #inspect ⇒ Object
- #multiple?(val = nil) ⇒ Boolean
- #parallel?(val = nil) ⇒ Boolean
- #prod?(script: nil, pat: nil, global: false) ⇒ Boolean
- #read_manifest ⇒ Object
- #repo(url, manifest = 'latest', run: nil, dev: nil, prod: nil) ⇒ Object
- #repo? ⇒ Boolean
- #root_path(*args) ⇒ Object
- #run(script, group: nil, ref: nil, **kwargs) ⇒ Object
- #script(group: nil, ref: nil) ⇒ Object
- #style(name, *args, target: nil, empty: false) ⇒ Object
- #sync?(val = nil) ⇒ Boolean
- #task_defined?(obj, key) ⇒ Boolean
- #test(script, group: nil, ref: nil) ⇒ Object
- #to_s ⇒ Object
Methods included from Common
#__get__, #__set__, #as_a, #finalize!, #message
Constructor Details
#initialize(main, common: true) ⇒ Workspace
Returns a new instance of Workspace.
59 60 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/squared/repo/workspace.rb', line 59 def initialize(main, *, common: true, **) @main = main.to_s @home = if (val = env('REPO_HOME')) home = Pathname.new(val) if @main == home.basename.to_s @root = home.parent if home.exist? @root = nil unless home.directory? elsif !@root.exist? @root.mkpath elsif !empty?(@root) if repo_prompt(@root) @override = true else @root = nil end end end raise ArgumentError, ('REPO_HOME', val, hint: 'invalid') unless @root home.realdirpath elsif (val = env('REPO_ROOT')) @root = Pathname.new(val).realdirpath if !@root.exist? @root.mkpath elsif !empty?(@root) raise ArgumentError, ('REPO_ROOT', val, hint: 'exist') unless repo_prompt(@root) @override = true end @root.join(@main).realdirpath else empty?(pwd = Pathname.pwd) ? pwd.join(@main) : pwd end @root ||= @home.parent @series = TASK_NAME.dup @project = {} @script = { group: {}, ref: {}, build: nil, dev: nil, prod: nil } @exception = !env('PIPE_FAIL', ignore: '0').nil? @pipe = !env('PIPE_OUT', ignore: '0').nil? @verbose = !@pipe @warning = !empty?(@root, repo: false) @sync = [] @multiple = [] @parallel = [] @theme = common && @verbose ? __get__(:theme)[:workspace] : {} end |
Class Attribute Details
.project_kind ⇒ Object (readonly)
Returns the value of attribute project_kind.
51 52 53 |
# File 'lib/squared/repo/workspace.rb', line 51 def project_kind @project_kind end |
Instance Attribute Details
#exception ⇒ Object
Returns the value of attribute exception.
57 58 59 |
# File 'lib/squared/repo/workspace.rb', line 57 def exception @exception end |
#home ⇒ Object (readonly)
Returns the value of attribute home.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def home @home end |
#main ⇒ Object (readonly)
Returns the value of attribute main.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def main @main end |
#manifest ⇒ Object
Returns the value of attribute manifest.
57 58 59 |
# File 'lib/squared/repo/workspace.rb', line 57 def manifest @manifest end |
#manifest_url ⇒ Object
Returns the value of attribute manifest_url.
57 58 59 |
# File 'lib/squared/repo/workspace.rb', line 57 def manifest_url @manifest_url end |
#multiple ⇒ Object (readonly)
Returns the value of attribute multiple.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def multiple @multiple end |
#parallel ⇒ Object (readonly)
Returns the value of attribute parallel.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def parallel @parallel end |
#pipe ⇒ Object
Returns the value of attribute pipe.
57 58 59 |
# File 'lib/squared/repo/workspace.rb', line 57 def pipe @pipe end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def root @root end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def series @series end |
#sync ⇒ Object (readonly)
Returns the value of attribute sync.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def sync @sync end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def theme @theme end |
#verbose ⇒ Object
Returns the value of attribute verbose.
57 58 59 |
# File 'lib/squared/repo/workspace.rb', line 57 def verbose @verbose end |
#warning ⇒ Object
Returns the value of attribute warning.
57 58 59 |
# File 'lib/squared/repo/workspace.rb', line 57 def warning @warning end |
Class Method Details
.find(path: nil, ref: nil) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/squared/repo/workspace.rb', line 39 def find(path: nil, ref: nil) if ref.is_a?(::Symbol) || ref.is_a?(::String) ret = project_kind.find { |proj| proj.to_sym == ref.to_sym } return ret if ret end project_kind.find { |proj| proj.is_a?(path) } if path end |
.implement(*objs) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/squared/repo/workspace.rb', line 27 def implement(*objs) objs.each do |obj| next unless obj < Project::Base project_kind.unshift(obj) obj.tasks&.each do |val| TASK_NAME[val] = [] TASK_BASE[val] = obj end end end |
.to_s ⇒ Object
47 48 49 |
# File 'lib/squared/repo/workspace.rb', line 47 def to_s super.to_s.match(/[^:]+$/)[0] end |
Instance Method Details
#add(name, path = nil, ref: nil, **kwargs) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/squared/repo/workspace.rb', line 319 def add(name, path = nil, ref: nil, **kwargs) path = root_path((path || name).to_s) project = if !ref.is_a?(::Class) Workspace.find(path: path, ref: ref) elsif ref < Project::Base ref end instance = (project || Project::Base).new(name, path, self, **kwargs) @project[n = name.to_sym] = instance __get__(:project)[n] = instance unless kwargs[:private] self end |
#apply(&blk) ⇒ Object
358 359 360 361 |
# File 'lib/squared/repo/workspace.rb', line 358 def apply(&blk) instance_eval(&blk) self end |
#bool_match(val, pat) ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/squared/repo/workspace.rb', line 414 def bool_match(val, pat) case val when nil, '' pat.is_a?(::Regexp) ? pat : nil when '0' false when '1' true else Regexp.new(val) end end |
#build(**kwargs) {|_self| ... } ⇒ Object
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 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 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 213 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/squared/repo/workspace.rb', line 113 def build(**kwargs) return unless enabled? default = kwargs[:default] multi = env('REPO_SYNC', ignore: '0') ? [] : kwargs.fetch(:parallel, []).map!(&:to_sym) group = {} parent = {} incl = [] @project.each do |name, proj| next unless proj.enabled? series.each do |key, items| target = "#{name}:#{key}" case key when :build, :refresh, :depend, :copy, :outdated, :doc, :test, :clean valid = proj.has?(key) || ::Rake::Task.task_defined?(target) next unless valid || (key == :refresh && series[:build].include?(target = "#{name}:build")) else next unless task_defined?(proj, key) end if proj.group incl << proj.group (group[:"#{key}:#{proj.group}"] ||= []).push(target) else items << target end next unless (base = find_base(proj)) unless (id = base.to_sym.to_s) == proj.group incl << id (parent[:"#{key}:#{id}"] ||= []).push(target) end end proj.populate(**kwargs) end series.merge!(parent) if incl.uniq.size > 1 series.merge!(group) series[:refresh].clear if series[:refresh].all? { |target| target.end_with?(':build') } if repo? branch = env('REPO_MANIFEST') || read_manifest target = branch || manifest stage = nil failfast = true cmd = [] newline = ARGV.index { |val| val.start_with?('repo:') }.to_i > 0 parse_opts = lambda do |args| collect_args(args, :opts).each do |val| case val when 'no-fail' failfast = false when 'force' cmd << '--force-checkout' when 'rebase' cmd << '--rebase' when 'detach' cmd << '--detach' when 'gc' cmd << '--auto-gc' when 'no-update' cmd << '--no-manifest-update' end end end status = lambda do |val, alt = nil| ver = branch || alt ver ? ('repo', val.sub('{0}', 'opts*=force,rebase,detach,gc,no-update,no-fail'), ver) : 'inactive' end namespace :repo do |repo| desc status.('all[{0}]') task :all, [:opts] do |_, args| parse_opts.(args) stage ||= :all repo[:sync].invoke @project.select do |_, proj| if proj.enabled? proj.depend proj.build? end end .each_value { |proj| proj.has?(:dev) ? proj.refresh : proj.build } end desc status.("init[manifest?=#{target},{0}]", target) task :init, [:manifest, :opts] do |_, args| parse_opts.(args) stage = :init puts if newline system("repo init -u #{manifest_url} -m #{args.manifest || target}.xml", chdir: root) repo[:all].invoke end desc status.('sync[{0}]') task :sync, [:opts] do |_, args| raise LoadError, ('repo is not initialized', 'rake repo:init') unless branch || stage == :init parse_opts.(args) cmd << "-j#{ENV.fetch('REPO_JOBS', ::Rake::CpuCounter.count)}" cmd << '--fail-fast' if failfast puts if newline && stage != :init begin shell("repo sync #{cmd.join(' ')}", chdir: root, exception: failfast) rescue StandardError => e emphasize(e, title: "rake stash repo:#{stage || 'sync'}") raise end end end end Workspace.project_kind.each { |obj| obj.populate(self, **kwargs) } if !series[:build].empty? init = [:depend, dev? && !series[:refresh].empty? ? :refresh : :build] task default: default || init[1] desc 'init' task init: init elsif default && !series[default].empty? task default: default end series.each do |key, items| next if items.empty? s = key.to_s if items.size > 1 multiple << s unless (valid = multi.include?(key)) group = key.to_s valid = multi.include?(group.split(':').first.to_sym) if group.include?(':') end if valid desc "#{key} (thread)" multitask key => items parallel << s desc "#{key} (sync)" task "#{key}:sync": items sync << "#{key}:sync" multiple << "#{key}:sync" next end end desc s task key => items end yield self if block_given? end |
#clean(script, group: nil, ref: nil) ⇒ Object
307 308 309 |
# File 'lib/squared/repo/workspace.rb', line 307 def clean(script, group: nil, ref: nil) script_command :clean, script, group, ref end |
#compose(name, &blk) ⇒ Object
353 354 355 356 |
# File 'lib/squared/repo/workspace.rb', line 353 def compose(name, &blk) namespace(name, &blk) self end |
#depend(script, group: nil, ref: nil) ⇒ Object
303 304 305 |
# File 'lib/squared/repo/workspace.rb', line 303 def depend(script, group: nil, ref: nil) script_command :depend, script, group, ref end |
#dev?(script: nil, pat: nil, global: nil) ⇒ Boolean
474 475 476 |
# File 'lib/squared/repo/workspace.rb', line 474 def dev?(script: nil, pat: nil, global: nil) with?(:dev, script: script, pat: pat, global: global || (pat.nil? && global.nil?)) end |
#doc(script, group: nil, ref: nil) ⇒ Object
311 312 313 |
# File 'lib/squared/repo/workspace.rb', line 311 def doc(script, group: nil, ref: nil) script_command :doc, script, group, ref end |
#empty?(dir, repo: true) ⇒ Boolean
461 462 463 464 465 466 467 468 |
# File 'lib/squared/repo/workspace.rb', line 461 def empty?(dir, repo: true) return true if dir.empty? || (repo && dir.join('.repo').directory?) return true if dir.children.size == 1 && dir.join(dir.children.first).to_s == __FILE__ dir.to_s == root.to_s && !env('REPO_ROOT').nil? && root.children.none? do |path| path.directory? && !path.basename.to_s.start_with?('.') && path.to_s != home.to_s end end |
#enabled? ⇒ Boolean
435 436 437 |
# File 'lib/squared/repo/workspace.rb', line 435 def enabled? repo? || @project.any? { |_, proj| proj.enabled? } end |
#env(key, equals: nil, ignore: nil) ⇒ Object
387 388 389 390 391 392 |
# File 'lib/squared/repo/workspace.rb', line 387 def env(key, equals: nil, ignore: nil) ret = ENV.fetch("#{key}_#{main.gsub(/[^\w]/, '_').upcase}", ENV[key]).to_s return ret == equals.to_s if equals ret.empty? || as_a(ignore).any? { |val| ret == val.to_s } ? nil : ret end |
#find_base(obj) ⇒ Object
410 411 412 |
# File 'lib/squared/repo/workspace.rb', line 410 def find_base(obj) Workspace.project_kind.find { |proj| obj.instance_of?(proj) } end |
#group(name, path, **kwargs) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/squared/repo/workspace.rb', line 332 def group(name, path, **kwargs) root_path(path).children.map do |ent| next unless (dir = Pathname.new(ent)).directory? index = 0 basename = dir.basename.to_s.to_sym override = kwargs.delete(basename) while @project[basename] index += 1 basename = :"#{basename}-#{index}" end [basename, dir, override] end .each do |basename, dir, override| opts = kwargs.dup opts.merge!(override) if override add(basename, dir, group: name, **opts) end self end |
#home_path(*args) ⇒ Object
406 407 408 |
# File 'lib/squared/repo/workspace.rb', line 406 def home_path(*args) home.join(*args) end |
#inspect ⇒ Object
431 432 433 |
# File 'lib/squared/repo/workspace.rb', line 431 def inspect "#<#{self.class}: #{main} => #{self}>" end |
#multiple?(val = nil) ⇒ Boolean
443 444 445 446 447 |
# File 'lib/squared/repo/workspace.rb', line 443 def multiple?(val = nil) return multiple.include?(val) && invoked?(val) if val ::Rake::Task.tasks.any? { |item| item.already_invoked && multiple.include?(item.name) } end |
#parallel?(val = nil) ⇒ Boolean
455 456 457 458 459 |
# File 'lib/squared/repo/workspace.rb', line 455 def parallel?(val = nil) return parallel.include?(val) && invoked?(val) if val ::Rake::Task.tasks.any? { |item| item.already_invoked && parallel.include?(item.name) } end |
#prod?(script: nil, pat: nil, global: false) ⇒ Boolean
478 479 480 481 482 |
# File 'lib/squared/repo/workspace.rb', line 478 def prod?(script: nil, pat: nil, global: false) return false if global && (@script[:dev] == true || !@script[:prod]) with?(:prod, script: script, pat: pat, global: global) end |
#read_manifest ⇒ Object
394 395 396 397 398 399 400 |
# File 'lib/squared/repo/workspace.rb', line 394 def read_manifest(*) require 'rexml/document' return unless (file = root_path('.repo/manifest.xml')).exist? doc = REXML::Document.new(file.read) doc.elements['manifest/include'].attributes['name']&.sub('.xml', '') end |
#repo(url, manifest = 'latest', run: nil, dev: nil, prod: nil) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/squared/repo/workspace.rb', line 267 def repo(url, manifest = 'latest', run: nil, dev: nil, prod: nil) @manifest_url = url @manifest = manifest script = case (val = env('REPO_BUILD')) when 'verbose' @verbose = true run ? "#{run}:verbose" : nil when 'silent' @verbose = false @warning = false run else val || run end case env('REPO_WARN') when '0' @warning = false when '1' @warning = true end return self unless script run(script, dev: bool_match(env('REPO_DEV'), dev), prod: bool_match(env('REPO_DEV'), prod)) end |
#repo? ⇒ Boolean
439 440 441 |
# File 'lib/squared/repo/workspace.rb', line 439 def repo? !manifest_url.nil? && (empty?(root) || @override) end |
#root_path(*args) ⇒ Object
402 403 404 |
# File 'lib/squared/repo/workspace.rb', line 402 def root_path(*args) root.join(*args) end |
#run(script, group: nil, ref: nil, **kwargs) ⇒ Object
292 293 294 295 296 297 298 299 300 301 |
# File 'lib/squared/repo/workspace.rb', line 292 def run(script, group: nil, ref: nil, **kwargs) if group || ref script_command :run, script, group, ref else @script[:build] = script @script[:dev] = kwargs[:dev] @script[:prod] = kwargs[:prod] self end end |
#script(group: nil, ref: nil) ⇒ Object
379 380 381 382 383 384 385 |
# File 'lib/squared/repo/workspace.rb', line 379 def script(group: nil, ref: nil) if group || ref (group && @script[:group][group.to_sym]) || (ref && @script[:ref][ref.to_sym]) else @script[:build] end end |
#style(name, *args, target: nil, empty: false) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/squared/repo/workspace.rb', line 363 def style(name, *args, target: nil, empty: false) data = nil if target as_a(target).each_with_index do |val, i| if i == 0 break unless (data = __get__(:theme)[val.to_sym]) else data = data[val.to_sym] ||= {} end end return unless data end apply_style(data || theme, name, *args, empty: empty) self end |
#sync?(val = nil) ⇒ Boolean
449 450 451 452 453 |
# File 'lib/squared/repo/workspace.rb', line 449 def sync?(val = nil) return sync.include?(val) && invoked?(val) if val ::Rake::Task.tasks.any? { |item| item.already_invoked && sync.include?(item.name) } end |
#task_defined?(obj, key) ⇒ Boolean
470 471 472 |
# File 'lib/squared/repo/workspace.rb', line 470 def task_defined?(obj, key) obj.is_a?(TASK_BASE[key]) end |
#test(script, group: nil, ref: nil) ⇒ Object
315 316 317 |
# File 'lib/squared/repo/workspace.rb', line 315 def test(script, group: nil, ref: nil) script_command :test, script, group, ref end |
#to_s ⇒ Object
427 428 429 |
# File 'lib/squared/repo/workspace.rb', line 427 def to_s root.to_s end |