-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifacts.rb
More file actions
executable file
·499 lines (401 loc) · 12.6 KB
/
Copy pathartifacts.rb
File metadata and controls
executable file
·499 lines (401 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
106
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
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
283
284
285
286
287
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
494
495
496
497
498
499
#!/usr/bin/env ruby
# encoding: UTF-8
# frozen_string_literal: true
###
# Show usage:
# ./scripts/artifacts.rb
#
# Example usage:
# # Use `-n` to only perform a dry-run.
# # Use `-c <channel>` to filter which artifacts/channels to use (fuzzy search).
#
# # Download GitHub artifacts to `pkgs/`.
# ./scripts/artifacts.rb -g
#
# # Extract (decompress) artifacts to `pkgs/`.
# ./scripts/artifacts.rb -x
#
# # Validate artifact folders for itch.io.
# ./scripts/artifacts.rb -v -c lin
# ./scripts/artifacts.rb -v -c mac
# ./scripts/artifacts.rb -v -c win
# ./scripts/artifacts.rb -v -c web
#
# # Publish artifact folders to itch.io.
# ./scripts/artifacts.rb -I -c lin
# ./scripts/artifacts.rb -I -c mac
# ./scripts/artifacts.rb -I -c win
# ./scripts/artifacts.rb -I -c web
#
# # Check status of itch.io builds.
# ./scripts/artifacts.rb -s
#
# @author Bradley Whited
###
require 'digest'
require 'fileutils'
require 'optparse'
require 'set'
require 'shellwords'
def main
ArtifactsMan.new.run
end
class ArtifactsMan
VERSION = '0.1.11'
DEST_DIR = File.join('pkgs')
USER_GAME = 'esotericpig/ekoscape'
ARTIFACTS = [
{
channel: 'linux-x64',
name: 'linux-appimage-x64',
file: 'EkoScape-linux-x64.tar.gz',
},
{
channel: 'macos-universal',
name: 'macos-uni',
file: 'EkoScape-macos-universal.tar.gz',
},
{
channel: 'windows-x64',
name: 'windows-x64',
file: 'EkoScape-windows-x64.zip',
},
{
channel: 'web',
dir: 'bin_web/Release',
ignores: %w[ekoscape.html], # Copied as `index.html`. See `CMakeLists.txt` for explanation.
},
].freeze
SLEEP_SECS = 0.500
CHECKSUM_BUFFER_SIZE = 16 * 1024
GH_CMD = %w[gh].freeze
TAR_CMD = %w[tar].freeze
UNZIP_CMD = %w[unzip].freeze
BUTLER_CMD = %w[butler].freeze
# Order matters! Because user can specify all actions.
ACTIONS = [
[:C,"[clean] delete artifacts (& their folders) in '#{DEST_DIR}/'",:clean],
[:g,"[get] download artifacts to '#{DEST_DIR}/'",:fetch],
[:k,'[checksum] verify artifact checksums',:check],
[:x,'[extract] extract artifacts',:extract],
[:v,'[validate] validate artifacts for itch.io',:validate_for_itch],
[:I,'[itch] publish extracted artifacts to itch.io',:publish_to_itch],
[:s,'[status] check status of itch.io builds',:stat_itch_builds],
].map do |action|
{opt: action[0],desc: action[1],method: action[2]}
end.freeze
def initialize
@max_prop_lens = {}
prop_keys = %i[channel file dest_dir platform arch]
@artifacts = ARTIFACTS.map do |props|
props[:file] = File.join(DEST_DIR,props[:file]) unless props[:file].nil?
props[:dest_dir] = File.join(DEST_DIR,props[:dest_dir]) unless props[:dest_dir].nil?
artifact = Artifact.new(**props)
prop_keys.each do |key|
if artifact.respond_to?(key)
len = artifact.__send__(key).to_s.length
@max_prop_lens[key] = len if len > @max_prop_lens.fetch(key,0)
end
end
artifact
end
@dry_run = true
@extra_args = []
end
def run
opts = {}
op = build_opt_parser(opts)
args,@extra_args = parse_extra_args
op.parse!(args,into: opts)
if opts.empty? || ACTIONS.none? { |action| opts[action[:opt]] }
puts op.help
exit
end
@dry_run = opts[:n]
channels = opts[:channel]
if !channels.nil? && !channels.empty?
@artifacts.filter! do |artifact|
channels.any? { |channel| artifact.channel.downcase.include?(channel.strip.downcase) }
end
end
ACTIONS.each do |action|
method(action[:method]).call if opts[action[:opt]]
end
end
def build_opt_parser(opts)
return OptionParser.new do |op|
op.program_name = File.basename($PROGRAM_NAME)
op.version = VERSION
op.summary_width = 16
si = op.summary_indent
op.separator ''
op.separator "v#{op.version}"
max_chan = @max_prop_lens.fetch(:channel,0)
max_file = @max_prop_lens.fetch(:file,0)
max_dest = @max_prop_lens.fetch(:dest_dir,0)
max_plat = @max_prop_lens.fetch(:platform,0)
max_arch = @max_prop_lens.fetch(:arch,0)
op.separator ''
op.separator 'Channels'
@artifacts.each do |artifact|
op.separator format(
"%s%-#{max_chan}s %-#{max_file}s %-#{max_dest}s %-#{max_plat}s %-#{max_arch}s [%s]",
si,artifact.channel,artifact.file,artifact.dest_dir,artifact.platform,artifact.arch,
artifact.ignores.join(','),
)
end
op.separator ''
op.separator 'Options'
op.on('-c <channel>','filter which artifacts to use (fuzzy search)') do |channel|
c = opts.fetch(:channel) { |key| opts[key] = Set.new }
c << channel
c
end
op.separator ''
op.separator 'Actions'
ACTIONS.each { |action| op.on("-#{action[:opt]}",nil,action[:desc]) }
op.separator ''
op.separator 'Basic Options'
op.on('-n',nil,'no-clobber dry run')
op.separator ''
op.separator 'Notes'
op.separator "#{si}# Any trailing options/args after '--' will be passed to the command directly:"
op.separator "#{si}#{op.program_name} -v -c lin -- --context-timeout=110"
end
end
def parse_extra_args(args = ARGV)
dash_i = args.find_index('--')
if dash_i.nil?
extra_args = []
else
extra_args = args[(dash_i + 1)..]
args = args[0...dash_i]
end
return [args,extra_args]
end
def clean
each_artifact(pauses: false,show_result: false) do |artifact|
next :skip if artifact.file.nil? || artifact.dest_dir.nil?
if File.file?(artifact.file)
FileUtils.rm(artifact.file,noop: @dry_run,verbose: true)
else
puts "[gone] '#{artifact.file}'"
end
if File.directory?(artifact.dest_dir)
FileUtils.rm_r(artifact.dest_dir,noop: @dry_run,verbose: true)
else
puts "[gone] '#{artifact.dest_dir}'"
end
true
end
end
def fetch
FileUtils.mkdir_p(DEST_DIR,noop: @dry_run,verbose: true) unless File.directory?(DEST_DIR)
# NOTE: Must download each one separately so that it doesn't create subdirs.
each_artifact do |artifact|
next :skip if artifact.name.nil?
run_cmd(GH_CMD,'run','download','--dir',DEST_DIR,'--name',artifact.name)
end
check
end
def check
each_artifact(pauses: false,newlines: false) do |artifact|
sum_file = "#{artifact.file}.sha256"
next :skip unless File.file?(sum_file)
verify_checksum_file(sum_file)
end
end
def extract
each_artifact(show_result: true) do |artifact|
next :skip if artifact.file.nil?
extract_file(artifact.file,dest_dir: artifact.dest_dir)
end
end
def validate_for_itch
each_artifact(show_result: true) do |artifact|
cmd = [BUTLER_CMD,'validate']
cmd.push('--platform',artifact.platform) unless artifact.platform.nil?
cmd.push('--arch',artifact.arch) unless artifact.arch.nil?
cmd.push(artifact.dest_dir)
run_cmd(cmd)
end
end
def publish_to_itch
each_artifact do |artifact|
cmd = [BUTLER_CMD,%w[push --fix-permissions --dereference --if-changed]]
artifact.ignores.each { |ignore| cmd.push('--ignore',ignore) }
cmd.push('--dry-run') if @dry_run
cmd.push(artifact.dest_dir,"#{USER_GAME}:#{artifact.channel}")
run_cmd(cmd,dry_run: false) # Butler has its own dry-run.
end
end
def stat_itch_builds
if @artifacts.length == ARTIFACTS.length
run_cmd(BUTLER_CMD,'status',USER_GAME)
puts
return
end
each_artifact do |artifact|
run_cmd(BUTLER_CMD,'status',"#{USER_GAME}:#{artifact.channel}")
end
end
def each_artifact(pauses: true,newlines: true,show_result: false)
if @dry_run
pauses = false
show_result = false
end
@artifacts.each do |artifact|
result = yield artifact
next if result == :skip
if show_result
puts if newlines
puts "=> Channel [#{artifact.channel}] succeeded!" if result
end
abort "=> Channel [#{artifact.channel}] failed!" unless result
sleep(SLEEP_SECS) if pauses
puts if newlines
end
puts "=> All channels succeeded! [#{@artifacts.map(&:channel).join(',')}]" if show_result
end
def verify_checksum_file(sum_file)
raise "Invalid checksum file [#{sum_file}]." if sum_file.empty? || !File.file?(sum_file)
result = true
File.foreach(sum_file,mode: 'rt',encoding: 'BOM|UTF-8:UTF-8') do |line|
parts = line.strip.split(/\s+\*?/,2)
next if parts.length < 2
hex = parts[0].strip
file = parts[1].strip
next if hex.empty? || file.empty?
file = File.join(File.dirname(sum_file),file)
result &&= verify_checksum(file,hex)
end
return result
end
def verify_checksum(file,hex)
hex = hex.strip
result = false
summary = ''
details = nil
if !file.empty? && File.file?(file)
dig = Digest::SHA256.new
if @dry_run
actual_hex = hex
else
File.open(file,'rb') do |f|
buffer = ''.dup
dig.update(buffer) while f.read(CHECKSUM_BUFFER_SIZE,buffer)
end
actual_hex = dig.hexdigest
end
if actual_hex == hex
result = true
summary = '[ok]'
else
result = false
summary = '[BAD hex]'
diff = Array.new((hex.length >= actual_hex.length) ? hex.length : actual_hex.length)
(0..diff.length).each do |i|
diff[i] = (actual_hex[i] == hex[i]) ? ' ' : '^'
end
details = [
"expected: #{hex}",
"actual: #{actual_hex}",
"diff: #{diff.join}",
]
end
else
result = false
summary = '[NO file]'
end
fmt = '%-9s %s'
puts format(fmt,summary,file)
puts details.map { |d| format(fmt,'',d) }.join("\n") unless details.nil?
return result
end
def extract_file(file,dest_dir: nil)
raise 'Empty file.' if (file = file.strip).empty?
dest_dir = nil if !dest_dir.nil? && (dest_dir = dest_dir.strip).empty?
cmd = []
# Can't use File.extname() because of `.tar.gz` (double).
case file
when /.tar.gz$/
cmd.concat(TAR_CMD)
cmd.push('-xzf',file,'--keep-old-files')
cmd.push('-C',dest_dir) unless dest_dir.nil?
when /.zip$/
cmd.concat(UNZIP_CMD)
cmd.push('-n',file)
cmd.push('-d',dest_dir) unless dest_dir.nil?
else
raise "Invalid file type to extract: #{file}."
end
FileUtils.mkdir(dest_dir,noop: @dry_run,verbose: true) if !dest_dir.nil? && !File.directory?(dest_dir)
return run_cmd(cmd)
end
def run_cmd(*cmd,dry_run: @dry_run)
cmd += @extra_args
cmd = cmd.flatten.compact.map(&:to_s)
puts cmd.map { |a| Shellwords.escape(a) }.join(' ')
return true if dry_run
return system(*cmd)
end
end
class Artifact
attr_reader :channel
attr_reader :name
attr_reader :file
attr_reader :dest_dir
attr_reader :ignores
attr_reader :platform
attr_reader :arch
def initialize(channel:,name: nil,file: nil,dir: nil,dest_dir: :parse,ignores: [],platform: :parse,
arch: :parse)
file = nil if (file = file&.strip)&.empty?
dir = nil if (dir = dir&.strip)&.empty?
raise 'Must specify either file or dir.' if file.nil? && dir.nil?
dest_dir = dir if file.nil?
if dest_dir == :parse
dest_dir = file.nil? ? '' : file.sub(/([^.])\..*$/,'\1').strip
raise "Invalid file/ext: #{file}." if dest_dir.empty?
end
if platform == :parse
# Channel can have multiple platforms.
platforms = []
# See: https://itch.io/docs/butler/pushing.html#channel-names
platforms << 'windows' if channel.match?(/win|windows/i)
platforms << 'linux' if channel.match?(/linux/i)
platforms << 'osx' if channel.match?(/mac|osx/i)
platform = (platforms.size == 1) ? platforms[0] : nil
end
if arch == :parse
# Channel can have multiple architectures.
arches = []
arches << '386' if channel.match?(/386|686|x86[^_-]|32/i)
arches << 'amd64' if channel.match?(/amd64|x86[_-]64|64/i)
arch = (arches.size == 1) ? arches[0] : nil
end
@channel = channel.strip
@name = name&.strip
@file = file
@dest_dir = dest_dir
@ignores = ignores.map(&:strip).reject(&:empty?)
@platform = platform&.strip
@arch = arch&.strip
end
def inspect
s = ''.dup
s << @channel.inspect << ': {'
s << @name.inspect
s << ', ' << @file.inspect
s << ', ' << @dest_dir.inspect
s << ', ' << @ignores.inspect
s << ', ' << @platform.inspect
s << ', ' << @arch.inspect
s << '}'
return s
end
def to_s
return inspect
end
end
main if __FILE__ == $PROGRAM_NAME