Class: Squared::Repo::Workspace
- Inherits:
-
Object
- Object
- Squared::Repo::Workspace
- Includes:
- Format, Rake::DSL, Common, System, Task
- Defined in:
- lib/squared/repo/workspace.rb
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.
-
#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.
-
#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, **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, init: true) ⇒ Boolean
- #enabled? ⇒ Boolean
- #env(key, equals: nil, ignore: nil) ⇒ Object
- #find_base(obj) ⇒ Object
- #home_path(*args) ⇒ Object
-
#initialize(main, common: true) ⇒ Workspace
constructor
A new instance of Workspace.
- #inspect ⇒ Object
- #multitask? ⇒ 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
- #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.
58 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 |
# File 'lib/squared/repo/workspace.rb', line 58 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 @theme = common ? __get__(:theme)[:workspace] : {} @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, init: false) end |
Class Attribute Details
.project_kind ⇒ Object (readonly)
Returns the value of attribute project_kind.
50 51 52 |
# File 'lib/squared/repo/workspace.rb', line 50 def project_kind @project_kind end |
Instance Attribute Details
#exception ⇒ Object
Returns the value of attribute exception.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def exception @exception end |
#home ⇒ Object (readonly)
Returns the value of attribute home.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def home @home end |
#main ⇒ Object (readonly)
Returns the value of attribute main.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def main @main end |
#manifest ⇒ Object
Returns the value of attribute manifest.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def manifest @manifest end |
#manifest_url ⇒ Object
Returns the value of attribute manifest_url.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def manifest_url @manifest_url end |
#pipe ⇒ Object
Returns the value of attribute pipe.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def pipe @pipe end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def root @root end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def series @series end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def theme @theme end |
#verbose ⇒ Object
Returns the value of attribute verbose.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def verbose @verbose end |
#warning ⇒ Object
Returns the value of attribute warning.
56 57 58 |
# File 'lib/squared/repo/workspace.rb', line 56 def warning @warning end |
Class Method Details
.find(path: nil, ref: nil) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/squared/repo/workspace.rb', line 38 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
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/squared/repo/workspace.rb', line 26 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
46 47 48 |
# File 'lib/squared/repo/workspace.rb', line 46 def to_s /[^:]+$/.match(super.to_s)[0] end |
Instance Method Details
#add(name, path = nil, **kwargs) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/squared/repo/workspace.rb', line 306 def add(name, path = nil, **kwargs) path = root_path((path || name).to_s) ref = kwargs[:ref] 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
325 326 327 328 |
# File 'lib/squared/repo/workspace.rb', line 325 def apply(&blk) instance_eval(&blk) self end |
#bool_match(val, pat) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/squared/repo/workspace.rb', line 382 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
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 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 |
# File 'lib/squared/repo/workspace.rb', line 109 def build(**kwargs) return unless enabled? default = kwargs[:default] parallel = env('REPO_SYNC', ignore: '0') ? [] : (kwargs[: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 # rubocop:disable Style/CombinableLoops @project.each_value { |proj| proj.depend if proj.enabled? } @project.each_value do |proj| next unless proj.enabled? && proj.build? proj.has?(:dev) ? proj.refresh : proj.build end # rubocop:enable Style/CombinableLoops 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? unless (valid = parallel.include?(key)) group = key.to_s valid = parallel.include?(group.split(':').first.to_sym) if group.include?(':') end if valid && items.size > 1 desc "#{key} (thread)" multitask key => items desc "#{key} (sync)" task "#{key}:sync": items else desc key.to_s task key => items end end yield self if block_given? end |
#clean(script, group: nil, ref: nil) ⇒ Object
294 295 296 |
# File 'lib/squared/repo/workspace.rb', line 294 def clean(script, group: nil, ref: nil) script_command :clean, script, group, ref end |
#compose(name, &blk) ⇒ Object
320 321 322 323 |
# File 'lib/squared/repo/workspace.rb', line 320 def compose(name, &blk) namespace(name, &blk) self end |
#depend(script, group: nil, ref: nil) ⇒ Object
290 291 292 |
# File 'lib/squared/repo/workspace.rb', line 290 def depend(script, group: nil, ref: nil) script_command :depend, script, group, ref end |
#dev?(script: nil, pat: nil, global: nil) ⇒ Boolean
432 433 434 |
# File 'lib/squared/repo/workspace.rb', line 432 def dev?(script: nil, pat: nil, global: nil) with?(:dev, script: script, pat: pat, global: (pat.nil? && global.nil?) || global) end |
#doc(script, group: nil, ref: nil) ⇒ Object
298 299 300 |
# File 'lib/squared/repo/workspace.rb', line 298 def doc(script, group: nil, ref: nil) script_command :doc, script, group, ref end |
#empty?(dir, init: true) ⇒ Boolean
419 420 421 422 423 424 425 426 |
# File 'lib/squared/repo/workspace.rb', line 419 def empty?(dir, init: true) return true if dir.empty? || (init && dir.join('.repo').directory?) return true if dir.children.size == 1 && dir.join(dir.children.first).to_s == __FILE__ dir == root && !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
403 404 405 |
# File 'lib/squared/repo/workspace.rb', line 403 def enabled? repo? || @project.any? { |_, proj| proj.enabled? } end |
#env(key, equals: nil, ignore: nil) ⇒ Object
354 355 356 357 358 359 |
# File 'lib/squared/repo/workspace.rb', line 354 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
378 379 380 |
# File 'lib/squared/repo/workspace.rb', line 378 def find_base(obj) Workspace.project_kind.find { |proj| obj.instance_of?(proj) } end |
#home_path(*args) ⇒ Object
374 375 376 |
# File 'lib/squared/repo/workspace.rb', line 374 def home_path(*args) home.join(*args) end |
#inspect ⇒ Object
399 400 401 |
# File 'lib/squared/repo/workspace.rb', line 399 def inspect "#<#{self.class}: #{main} => #{self}>" end |
#multitask? ⇒ Boolean
411 412 413 414 415 416 417 |
# File 'lib/squared/repo/workspace.rb', line 411 def multitask? ::Rake::Task.tasks.any? do |item| next unless item.already_invoked item.name.end_with?(':sync') || (!item.name.include?(':') && !item.name.start_with?('all', 'init')) end end |
#prod?(script: nil, pat: nil, global: false) ⇒ Boolean
436 437 438 439 440 |
# File 'lib/squared/repo/workspace.rb', line 436 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
361 362 363 364 365 366 367 368 |
# File 'lib/squared/repo/workspace.rb', line 361 def read_manifest(*) require 'rexml/document' file = root_path('.repo/manifest.xml') return unless file.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
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/squared/repo/workspace.rb', line 257 def repo(url, manifest = 'latest', run: nil, dev: nil, prod: nil) @manifest_url = url @manifest = manifest script = case (val = env('REPO_BUILD')) when 'verbose' 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 script ? run(script, dev: bool_match(env('REPO_DEV'), dev), prod: bool_match(env('REPO_DEV'), prod)) : self end |
#repo? ⇒ Boolean
407 408 409 |
# File 'lib/squared/repo/workspace.rb', line 407 def repo? !manifest_url.nil? && (empty?(root) || @override) end |
#root_path(*args) ⇒ Object
370 371 372 |
# File 'lib/squared/repo/workspace.rb', line 370 def root_path(*args) root.join(*args) end |
#run(script, group: nil, ref: nil, **kwargs) ⇒ Object
279 280 281 282 283 284 285 286 287 288 |
# File 'lib/squared/repo/workspace.rb', line 279 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
346 347 348 349 350 351 352 |
# File 'lib/squared/repo/workspace.rb', line 346 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
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/squared/repo/workspace.rb', line 330 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 |
#task_defined?(obj, key) ⇒ Boolean
428 429 430 |
# File 'lib/squared/repo/workspace.rb', line 428 def task_defined?(obj, key) obj.is_a?(TASK_BASE[key]) end |
#test(script, group: nil, ref: nil) ⇒ Object
302 303 304 |
# File 'lib/squared/repo/workspace.rb', line 302 def test(script, group: nil, ref: nil) script_command :test, script, group, ref end |
#to_s ⇒ Object
395 396 397 |
# File 'lib/squared/repo/workspace.rb', line 395 def to_s root.to_s end |