squared 0.6.9 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,45 +6,54 @@ module Squared
6
6
  REPO_URL = 'https://storage.googleapis.com/git-repo-downloads/repo'
7
7
  private_constant :REPO_URL
8
8
 
9
- attr_reader :manifest_url, :manifest
10
-
11
- def repo(url, manifest = 'latest', install: nil, run: nil, script: nil, args: nil, dev: nil, prod: nil,
12
- ref: @ref, group: @group)
13
- @home = if (val = env('REPO_HOME'))
14
- path = Pathname.new(val)
15
- if main == path.basename.to_s
16
- @root = path.parent
17
- if path.exist?
18
- @root = nil unless path.directory?
19
- elsif !@root.exist?
20
- @root.mkpath
21
- elsif !repo_install? && !repo_confirm
22
- @root = nil
23
- end
24
- raise_error Errno::EEXIST, path.cleanpath, hint: 'REPO_HOME' unless @root
25
- end
26
- path.realdirpath
27
- elsif (val = env('REPO_ROOT'))
28
- @root = Pathname.new(val).realdirpath
29
- if !@root.exist?
30
- @root.mkpath
31
- elsif !repo_install?(parent: true) && !repo_confirm
32
- raise_error Errno::EEXIST, @root, hint: 'REPO_ROOT'
33
- end
34
- @root.join(main).realdirpath
35
- elsif repo_install?(parent: true) && (!@home.exist? || @root + main == @home)
36
- @home
37
- elsif repo_install?(@home)
38
- @home + main
39
- else
40
- (path = pwd) == @home || !repo_install?(path) ? @home : path + main
41
- end
42
- @root = @home.parent
43
- @manifest_url = url
44
- @manifest = manifest
9
+ def repo(url, manifest = 'latest', run: nil, script: nil, args: nil, dev: nil, prod: nil, doc: false, test: false,
10
+ lint: false, install: nil, name: nil, ref: @ref, group: @group)
11
+ if @repo_bin.nil?
12
+ self.home = if (val = env('REPO_HOME'))
13
+ path = Pathname.new(val)
14
+ if main == path.basename.to_s
15
+ @root = path.parent
16
+ if path.exist?
17
+ @root = nil unless path.directory?
18
+ elsif !@root.exist?
19
+ @root.mkpath
20
+ elsif !repo_install? && !repo_confirm
21
+ @root = nil
22
+ end
23
+ raise_error Errno::EEXIST, path.cleanpath, hint: 'REPO_HOME' unless @root
24
+ end
25
+ path.realdirpath
26
+ elsif (val = env('REPO_ROOT'))
27
+ @root = Pathname.new(val).realdirpath
28
+ if !@root.exist?
29
+ @root.mkpath
30
+ elsif !repo_install?(parent: true) && !repo_confirm
31
+ raise_error Errno::EEXIST, @root, hint: 'REPO_ROOT'
32
+ end
33
+ @root.join(main).realdirpath
34
+ elsif repo_install?(parent: true) && (!home.exist? || @root + main == home)
35
+ home
36
+ elsif repo_install?(home)
37
+ home + main
38
+ else
39
+ pwd == home || !repo_install?(pwd) ? home : pwd + main
40
+ end
41
+ @repo_bin = if install
42
+ install.is_a?(String) ? @root + install : @root
43
+ else
44
+ false
45
+ end
46
+ end
47
+ envargs = name ? { suffix: name.upcase, strict: true } : {}
45
48
  data = scriptobj
46
49
  if repo?
47
- sc, ru = env('REPO_BUILD', '').split(',', 2).map!(&:strip)
50
+ group = env('REPO_GROUP', **envargs) { |val| split_escape(val) }
51
+ ref = env('REPO_REF', **envargs) { |val| split_escape(val) }
52
+ data[:dev] = env_match('REPO_DEV', dev, **envargs)
53
+ data[:prod] = env_match('REPO_PROD', prod, **envargs)
54
+ @warning = env_match('REPO_WARN', @warning && !root?(@root, pass: ['.repo'])) != false unless name
55
+ sc, ru = split_escape(env('REPO_BUILD', '', **envargs), limit: 2)
56
+ global = data[:global]
48
57
  if script
49
58
  data[:script] = if sc.to_s.empty?
50
59
  script
@@ -67,8 +76,8 @@ module Squared
67
76
  sc
68
77
  end
69
78
  end
70
- data[:args] = (val = env('REPO_SCRIPT')) ? shell_split(val, join: true) : args
71
- data[:global][:script] = true
79
+ data[:args] = env('REPO_SCRIPT', **envargs) { |val| shell_split(val, join: true) } || args
80
+ global[:script] = true
72
81
  else
73
82
  ru ||= sc
74
83
  end
@@ -79,20 +88,12 @@ module Squared
79
88
  data[:env][:run] = true
80
89
  ru
81
90
  end
82
- data[:global][:run] = true
83
- end
84
- data[:dev] = env_match 'REPO_DEV', dev
85
- data[:prod] = env_match 'REPO_PROD', prod
86
- if (val = env('REPO_GROUP'))
87
- script_set(data, group: val.split(','))
88
- found = true
89
- end
90
- if (val = env('REPO_REF'))
91
- script_set(data, ref: val.split(','))
92
- found = true
91
+ global[:run] = true
93
92
  end
94
- script_set(data, group: group, ref: ref) unless found
95
- @warning = env_match('REPO_WARN', @warning && !root?(@root, pass: ['.repo'])) != false
93
+ global[:doc] = doc
94
+ global[:lint] = lint
95
+ global[:test] = test
96
+ script_set('repo', data, name: name, group: group, ref: ref, extras: { url: url, manifest: manifest })
96
97
  @extensions << :__repo__
97
98
  elsif script || run
98
99
  if script
@@ -102,126 +103,150 @@ module Squared
102
103
  data[:run] = run if run
103
104
  data[:dev] = dev
104
105
  data[:prod] = prod
105
- script_set(data, group: group, ref: ref)
106
+ script_set('repo', data, name: name, group: group, ref: ref)
106
107
  end
107
- @repo_bin = install.is_a?(String) ? @root + install : @root if install
108
108
  self
109
109
  end
110
110
 
111
+ def repo?(active = false)
112
+ active ? @extensions.include?(:__repo__) : !windows? && (repo_install? || @repo_override == true)
113
+ end
114
+
111
115
  private
112
116
 
113
117
  def __repo__(**kwargs)
118
+ self.closed = 'repo:begin'
114
119
  kwargs.delete(:parallel) if env('REPO_SYNC', ignore: '0')
115
-
116
- namespace task_name('repo') do |ns|
117
- path = ns.scope.path
118
- branch = env('REPO_MANIFEST') || repo_manifest
119
- target = branch || manifest
120
- stage = nil
121
- opts = %w[force rebase detach submodules fail no-update gc]
122
- desc = lambda do |val, alt = nil|
123
- if (ver = branch || alt)
124
- val = val.sub('{0}', "opts*=#{opts.join(',')}")
125
- task_desc(path, val, ver)
126
- else
127
- task_desc 'inactive'
120
+ script_each 'repo' do |name, data|
121
+ namespace task_name(name == '_' ? 'repo' : "repo:#{name}") do |ns|
122
+ path = ns.scope.path
123
+ envargs = name == '_' ? {} : { suffix: name.upcase, strict: true }
124
+ branch = env('REPO_MANIFEST', **envargs) || repo_manifest
125
+ target = branch || data[:extras][:manifest]
126
+ stage = nil
127
+ opts = %w[force rebase detach submodules fail no-update gc]
128
+ desc = lambda do |val, alt = nil|
129
+ if (ver = branch || alt)
130
+ val = val.sub('{0}', "opts*=#{opts.join(',')}")
131
+ task_desc(path, val, ver)
132
+ else
133
+ task_desc 'inactive'
134
+ end
128
135
  end
129
- end
130
136
 
131
- desc.call('all[{0}]')
132
- task 'all' do |_, args|
133
- stage ||= 'all'
134
- ns['sync'].invoke(*args.to_a)
135
- next if (n = env('REPO_STAGE')) == '1'
137
+ desc.call('all[{0}]')
138
+ task 'all' do |_, args|
139
+ stage ||= 'all'
140
+ ns['sync'].invoke(*args.to_a)
141
+ next if (n = env('REPO_STAGE', **envargs).to_i).anybits?(1)
136
142
 
137
- select do |proj|
138
- next unless proj.enabled?(proj.workspace.baseref) && proj.global
143
+ if (val = env('REPO_SERIES', **envargs))
144
+ split_escape(val) do |meth|
145
+ meth, sync = meth.split(':', 2)
146
+ repo_workspace(name).each do |proj|
147
+ repo_call(proj, meth, sync: (sync != 'parallel' unless sync == 'detect'))
148
+ end
149
+ end
150
+ else
151
+ repo_workspace(name).map do |proj|
152
+ proj.depend(sync: true) if proj.depend?
153
+ next [] if n.anybits?(2)
154
+
155
+ run, script, doc, lint, test = proj.scriptdata(:global, name: name)
156
+ .fetch_values(:run, :script, :doc, :lint, :test)
157
+ [proj, proj.build? && (script || run), doc && proj.doc?, lint && proj.lint?, test && proj.test?]
158
+ end
159
+ .select do |proj, run, doc, lint, test|
160
+ next unless proj
161
+
162
+ proj.doc(sync: true) if doc
163
+ proj.build(sync: true) if run
164
+ next if n.anybits?(4)
165
+
166
+ repo_call proj, 'copy' if run && proj.copy? && (proj.dev? || n.anybits?(512))
167
+ next if n.anybits?(8)
139
168
 
140
- proj.depend(sync: true) if proj.depend?
141
- next if n == '2'
169
+ proj.lint(sync: true) if lint
170
+ next if n.anybits?(16)
142
171
 
143
- proj.build?
172
+ test
173
+ end
174
+ .each { |proj| proj.test(sync: true) }
175
+ end
144
176
  end
145
- .each do |proj|
146
- proj.build(sync: true)
147
- next if n == '3'
148
- next unless proj.copy? && (proj.dev? || n == '4')
149
177
 
150
- if (ws = proj.workspace).task_defined?(target = task_join(proj.name, 'copy'))
151
- task_invoke(target, **ws.invokeargs)
152
- else
153
- proj.copy
178
+ desc.call("init[manifest?=#{target},groups?,{0}]", target)
179
+ task 'init' do |_, args|
180
+ args = args.to_a
181
+ u = env('REPO_GIT', **envargs) || data[:extras][:url]
182
+ check = -> { args.first && !opts.include?(args.first) }
183
+ if check.call
184
+ m = args.shift
185
+ g = args.shift if check.call
154
186
  end
187
+ env('REPO_GROUPS', **envargs) do |val|
188
+ g = case val
189
+ when '0', 'false'
190
+ nil
191
+ else
192
+ val
193
+ end
194
+ end
195
+ stage = 'init'
196
+ opts = repo_opts "-u #{u}", "-m #{m || target}.xml"
197
+ opts << "-g #{g}" if g
198
+ opts << '--submodules' if repo_submodules?(args.include?('submodules'), **envargs)
199
+ repo_run 'init', opts
200
+ next if env('REPO_STAGE', equals: '0', **envargs)
201
+
202
+ ns['all'].invoke(*args)
155
203
  end
156
- end
157
204
 
158
- desc.call("init[manifest?=#{target},groups?,{0}]", target)
159
- task 'init' do |_, args|
160
- args = args.to_a
161
- u = env('REPO_GIT') || manifest_url
162
- m = args.first && !opts.include?(args.first) ? args.shift : target
163
- g = (args.shift if args.first && !opts.include?(args.first))
164
- g = case (val = env('REPO_GROUPS'))
165
- when '', NilClass
166
- g
167
- when '0', 'false'
168
- nil
169
- else
170
- val
205
+ desc.call('sync[{0}]')
206
+ task 'sync' do |t, args|
207
+ opts = if stage == 'init'
208
+ []
209
+ else
210
+ raise_error 'repo not initialized' unless branch
211
+ repo_opts
212
+ end
213
+ args.to_a.each do |val|
214
+ case val
215
+ when /^force(-checkout)?$/
216
+ opts << '--force-checkout'
217
+ when 'rebase', 'detach'
218
+ opts << "--#{val}"
219
+ when /^(fetch-)?submodules$/
220
+ opts << '--fetch-submodules' if repo_submodules?(true, **envargs)
221
+ when /^(fail-)?fail$/
222
+ opts << '--fail-fast'
223
+ when /^no-(manifest-)?update$/
224
+ opts << '--no-manifest-update'
225
+ when /^(auto-)?gc$/
226
+ opts << '--auto-gc'
171
227
  end
172
- stage = 'init'
173
- opts = repo_opts "-u #{u}", "-m #{m}.xml"
174
- opts << "-g #{g}" if g
175
- opts << '--submodules' if repo_submodules?(args.include?('submodules'))
176
- repo_run "#{repo_bin} init #{opts.uniq.join(' ')}"
177
- next if env('REPO_STAGE', equals: '0')
178
-
179
- ns['all'].invoke(*args)
180
- end
181
-
182
- desc.call('sync[{0}]')
183
- task 'sync' do |t, args|
184
- opts = if stage == 'init'
185
- []
186
- else
187
- raise_error 'repo not initialized' unless branch
188
- repo_opts
189
- end
190
- args.to_a.each do |val|
191
- case val
192
- when 'force'
193
- opts << '--force-checkout'
194
- when 'rebase', 'detach'
195
- opts << "--#{val}"
196
- when 'submodules'
197
- opts << '--fetch-submodules' if repo_submodules?(true)
198
- when 'fail'
199
- opts << '--fail-fast'
200
- when 'no-update'
201
- opts << '--no-manifest-update'
202
- when 'gc'
203
- opts << '--auto-gc'
228
+ end
229
+ opts << "-j#{ENV.fetch('REPO_JOBS', Rake::CpuCounter.count)}" unless opts.any?(/^--?j(obs)?$/)
230
+ opts << '--fetch-submodules' if repo_submodules?(**envargs)
231
+ begin
232
+ repo_run('sync', opts, exception: opts.include?('--fail-fast'), options: stage != 'init')
233
+ rescue Errno::ENOENT => e
234
+ emphasize(e, title: root)
235
+ raise
236
+ rescue => e
237
+ emphasize(e, title: "rake stash #{t.name}")
238
+ raise
204
239
  end
205
240
  end
206
- opts << "-j#{ENV.fetch('REPO_JOBS', Rake::CpuCounter.count)}" unless opts.any?(/^--?j(?:obs)?/)
207
- opts << '--fetch-submodules' if repo_submodules?
208
- begin
209
- repo_run("#{repo_bin} sync #{opts.uniq.join(' ')}", exception: opts.include?('--fail-fast'))
210
- rescue Errno::ENOENT => e
211
- emphasize(e, title: root)
212
- raise
213
- rescue StandardError => e
214
- emphasize(e, title: "rake stash #{t.name}")
215
- raise
216
- end
217
- end
218
241
 
219
- series.sync.push(
220
- task_join(path, 'all'),
221
- task_join(path, 'init'),
222
- task_join(path, 'sync')
223
- )
242
+ series.sync.push(
243
+ task_join(path, 'all'),
244
+ task_join(path, 'init'),
245
+ task_join(path, 'sync')
246
+ )
247
+ end
224
248
  end
249
+ self.closed = 'repo:end'
225
250
  end
226
251
 
227
252
  def repo_manifest(path = root)
@@ -244,12 +269,16 @@ module Squared
244
269
  else
245
270
  Common::Prompt.confirm(
246
271
  "#{log_title(:warn)} \"#{path}\" is not empty. Continue with installation?", 'N',
247
- force: true, timeout: env('REPO_TIMEOUT').to_i.yield_self { |n| n > 0 ? n : 15 }
272
+ force: true, timeout: env('REPO_TIMEOUT') { |n| n if (n = n.to_i) > 0 } || 15
248
273
  )
249
274
  end
250
275
  end
251
276
 
252
- def repo_run(cmd, exception: false)
277
+ def repo_run(cmd, opts = [], exception: false, options: true, **kwargs)
278
+ cmd = [repo_bin, cmd]
279
+ cmd << opts.uniq.join(' ') unless opts.empty?
280
+ env('REPO_OPTIONS', **kwargs) { |val| cmd << val } if options
281
+ cmd = cmd.join(' ')
253
282
  puts log_message(cmd, subject: main, hint: root) if verbose
254
283
  Common::System.shell(cmd, chdir: root, exception: exception)
255
284
  end
@@ -257,35 +286,71 @@ module Squared
257
286
  def repo_bin
258
287
  return Common::Shell.shell_bin('repo') unless @repo_bin
259
288
 
260
- @repo_bin.join('repo').tap do |bin|
261
- next if bin.file?
289
+ bin = @repo_bin.join('repo')
290
+ return bin if bin.file?
262
291
 
263
- require 'open-uri'
264
- puts log_message('Installing repo...', subject: main, hint: @repo_bin) if verbose
265
- URI.open(REPO_URL) do |url|
266
- @repo_bin.mkpath
267
- File.open(bin, 'wb') do |f|
268
- f.write(url.read)
269
- f.chmod(0o755)
270
- end
292
+ require 'open-uri'
293
+ puts log_message('Installing repo...', subject: main, hint: @repo_bin) if verbose
294
+ URI.open(REPO_URL) do |url|
295
+ @repo_bin.mkpath
296
+ File.open(bin, 'wb') do |f|
297
+ f.write(url.read)
298
+ f.chmod(0o755)
271
299
  end
272
300
  end
301
+ bin
273
302
  end
274
303
 
275
- def repo_opts(*args)
276
- return args unless (n = ARGV.index('--'))
304
+ def repo_workspace(name)
305
+ g, r = @global[name].fetch_values(:group, :ref)
306
+ g = g.keys
307
+ r = r.keys
308
+ pass = g.empty? && r.empty?
309
+ select do |proj|
310
+ next unless proj.enabled?(baseref)
277
311
 
278
- ARGV[n.succ..-1].concat(args)
312
+ proj.global_set(name)
313
+ i = -1
314
+ n = repo_scriptdata(proj, name).reduce(0) do |a, b|
315
+ i += 1
316
+ next a unless b
317
+
318
+ a + (1 << i)
319
+ end
320
+ proj.global(n) && (pass || g.include?(proj.group) || r.any? { |val| proj.ref?(val) })
321
+ end
322
+ end
323
+
324
+ def repo_scriptdata(proj, name)
325
+ proj.scriptdata(:global, name: name).fetch_values(:run, :script, :doc, :test)
326
+ end
327
+
328
+ def repo_call(proj, meth, sync: nil)
329
+ name = task_join proj.name, meth
330
+ detect = proj.workspace.task_defined?(name)
331
+ if proj.has?(meth) && (!detect || !sync.nil?)
332
+ if sync.nil?
333
+ proj.__send__(meth)
334
+ else
335
+ proj.__send__(meth, sync: sync)
336
+ end
337
+ elsif detect
338
+ task_invoke(name, **proj.workspace.invokeargs)
339
+ end
340
+ rescue => e
341
+ raise if exception
342
+
343
+ warn log_warn(e, pass: true) if warning
279
344
  end
280
345
 
281
- def repo?
282
- return false unless manifest_url && !windows?
346
+ def repo_opts(*args)
347
+ return args unless (n = ARGV.index('--'))
283
348
 
284
- repo_install? || @repo_override == true
349
+ ARGV[n.succ..-1].concat(args)
285
350
  end
286
351
 
287
- def repo_submodules?(val = false)
288
- case (s = env('REPO_SUBMODULES'))
352
+ def repo_submodules?(val = false, **kwargs)
353
+ case (s = env('REPO_SUBMODULES', **kwargs))
289
354
  when '0', 'false'
290
355
  false
291
356
  else