Class: Squared::Repo::Workspace
- Inherits:
-
Object
- Object
- Squared::Repo::Workspace
- Includes:
- Rake::DSL, Common::Format, 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.
-
#styles ⇒ Object
Returns the value of attribute styles.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #add(name, dir = 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) ⇒ Boolean
- #env(key, equals: nil, ignore: nil) ⇒ Object
- #find_base(obj) ⇒ Object
- #home_path(*args) ⇒ Object
-
#initialize(main) ⇒ 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') ⇒ Object
- #root_path(*args) ⇒ Object
- #run(script, group: nil, ref: nil, **kwargs) ⇒ Object
- #script(group: nil, ref: nil) ⇒ Object
- #style(name, *args) ⇒ Object
- #task_defined?(obj, key) ⇒ Boolean
- #test(script, group: nil, ref: nil) ⇒ Object
- #to_s ⇒ Object
Methods included from Common::Format
#check_style, #emphasize, #enable_aixterm, #log_message, #log_title, #sub_style
Methods included from Common
#as_a, #finalize!, #get!, #message, #set!
Constructor Details
#initialize(main) ⇒ Workspace
Returns a new instance of Workspace.
57 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 |
# File 'lib/squared/repo/workspace.rb', line 57 def initialize(main) @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 @styles = {} @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 end |
Class Attribute Details
.project_kind ⇒ Object (readonly)
Returns the value of attribute project_kind.
49 50 51 |
# File 'lib/squared/repo/workspace.rb', line 49 def project_kind @project_kind end |
Instance Attribute Details
#exception ⇒ Object
Returns the value of attribute exception.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def exception @exception end |
#home ⇒ Object (readonly)
Returns the value of attribute home.
54 55 56 |
# File 'lib/squared/repo/workspace.rb', line 54 def home @home end |
#main ⇒ Object (readonly)
Returns the value of attribute main.
54 55 56 |
# File 'lib/squared/repo/workspace.rb', line 54 def main @main end |
#manifest ⇒ Object
Returns the value of attribute manifest.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def manifest @manifest end |
#manifest_url ⇒ Object
Returns the value of attribute manifest_url.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def manifest_url @manifest_url end |
#pipe ⇒ Object
Returns the value of attribute pipe.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def pipe @pipe end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
54 55 56 |
# File 'lib/squared/repo/workspace.rb', line 54 def root @root end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
54 55 56 |
# File 'lib/squared/repo/workspace.rb', line 54 def series @series end |
#styles ⇒ Object
Returns the value of attribute styles.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def styles @styles end |
#verbose ⇒ Object
Returns the value of attribute verbose.
55 56 57 |
# File 'lib/squared/repo/workspace.rb', line 55 def verbose @verbose end |
Class Method Details
.find(path: nil, ref: nil) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/squared/repo/workspace.rb', line 37 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
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/squared/repo/workspace.rb', line 25 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
45 46 47 |
# File 'lib/squared/repo/workspace.rb', line 45 def to_s /[^:]+$/.match(super.to_s)[0] end |
Instance Method Details
#add(name, dir = nil, **kwargs) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/squared/repo/workspace.rb', line 294 def add(name, dir = nil, **kwargs) path = (dir || name).to_s ref = kwargs[:ref] project = if !ref.is_a?(::Class) Workspace.find(path: root_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 self end |
#apply(&blk) ⇒ Object
313 314 315 316 |
# File 'lib/squared/repo/workspace.rb', line 313 def apply(&blk) instance_eval(&blk) self end |
#bool_match(val, pat) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/squared/repo/workspace.rb', line 364 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
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 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 |
# File 'lib/squared/repo/workspace.rb', line 107 def build(**kwargs) 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.to_s.end_with?(':build') } if manifest_url && (empty?(root) || @override) 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
282 283 284 |
# File 'lib/squared/repo/workspace.rb', line 282 def clean(script, group: nil, ref: nil) script_command :clean, script, group, ref end |
#compose(name, &blk) ⇒ Object
308 309 310 311 |
# File 'lib/squared/repo/workspace.rb', line 308 def compose(name, &blk) namespace(name, &blk) self end |
#depend(script, group: nil, ref: nil) ⇒ Object
278 279 280 |
# File 'lib/squared/repo/workspace.rb', line 278 def depend(script, group: nil, ref: nil) script_command :depend, script, group, ref end |
#dev?(script: nil, pat: nil, global: nil) ⇒ Boolean
406 407 408 |
# File 'lib/squared/repo/workspace.rb', line 406 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
286 287 288 |
# File 'lib/squared/repo/workspace.rb', line 286 def doc(script, group: nil, ref: nil) script_command :doc, script, group, ref end |
#empty?(dir) ⇒ Boolean
385 386 387 388 389 390 391 392 |
# File 'lib/squared/repo/workspace.rb', line 385 def empty?(dir) return true if dir.empty? || 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 |
#env(key, equals: nil, ignore: nil) ⇒ Object
336 337 338 339 340 341 |
# File 'lib/squared/repo/workspace.rb', line 336 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
360 361 362 |
# File 'lib/squared/repo/workspace.rb', line 360 def find_base(obj) Workspace.project_kind.find { |proj| obj.instance_of?(proj) } end |
#home_path(*args) ⇒ Object
356 357 358 |
# File 'lib/squared/repo/workspace.rb', line 356 def home_path(*args) home.join(*args) end |
#inspect ⇒ Object
381 382 383 |
# File 'lib/squared/repo/workspace.rb', line 381 def inspect "#<#{self.class}: #{main} => #{self}>" end |
#multitask? ⇒ Boolean
394 395 396 397 398 399 400 |
# File 'lib/squared/repo/workspace.rb', line 394 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
410 411 412 413 414 |
# File 'lib/squared/repo/workspace.rb', line 410 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
343 344 345 346 347 348 349 350 |
# File 'lib/squared/repo/workspace.rb', line 343 def read_manifest require 'rexml/document' file = root_path('.repo/manifest.xml') return unless file.exist? doc = REXML::Document.new(File.read(file)) doc.elements['manifest/include'].attributes['name']&.sub('.xml', '') end |
#repo(url, manifest = 'latest') ⇒ Object
253 254 255 256 257 |
# File 'lib/squared/repo/workspace.rb', line 253 def repo(url, manifest = 'latest') @manifest_url = url @manifest = manifest self end |
#root_path(*args) ⇒ Object
352 353 354 |
# File 'lib/squared/repo/workspace.rb', line 352 def root_path(*args) root.join(*args) end |
#run(script, group: nil, ref: nil, **kwargs) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/squared/repo/workspace.rb', line 259 def run(script, group: nil, ref: nil, **kwargs) if group || ref script_command :run, script, group, ref else @script[:build] = case (val = env('REPO_BUILD')) when 'verbose' "#{script}:verbose" when 'silent' @verbose = false script else val || script end @script[:dev] = bool_match(env('REPO_DEV'), kwargs[:dev]) @script[:prod] = bool_match(env('REPO_PROD'), kwargs[:prod]) self end end |
#script(group: nil, ref: nil) ⇒ Object
328 329 330 331 332 333 334 |
# File 'lib/squared/repo/workspace.rb', line 328 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) ⇒ Object
318 319 320 321 322 323 324 325 326 |
# File 'lib/squared/repo/workspace.rb', line 318 def style(name, *args) set = ->(k, v) { styles[k.to_sym] = check_style(v, empty: false) } if name.is_a?(Hash) name.each { |k, v| set.(k, v || args.flatten) } else set.(name, args.flatten) end self end |
#task_defined?(obj, key) ⇒ Boolean
402 403 404 |
# File 'lib/squared/repo/workspace.rb', line 402 def task_defined?(obj, key) obj.is_a?(TASK_BASE[key]) end |
#test(script, group: nil, ref: nil) ⇒ Object
290 291 292 |
# File 'lib/squared/repo/workspace.rb', line 290 def test(script, group: nil, ref: nil) script_command :test, script, group, ref end |
#to_s ⇒ Object
377 378 379 |
# File 'lib/squared/repo/workspace.rb', line 377 def to_s root.to_s end |