This repository was archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-spoons
More file actions
executable file
·117 lines (104 loc) · 3.54 KB
/
build-spoons
File metadata and controls
executable file
·117 lines (104 loc) · 3.54 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
#!/usr/bin/env ruby
require 'yaml'
require 'digest/sha2'
require 'open-uri'
require 'fileutils'
require 'pathname'
require 'benchmark'
FILE_MANIFEST_PATH = 'files.yml'
PLATFORM_LAYOUT_PATH = 'layout.yml'
CACHE_PATH = 'cache'
USB_MASTER_PATH = 'usb-image'
DEFAULT_USB_PATH = '/Volumes/RBB*'
MAC_CRUFT = %w[.Spotlight-V100 .Trashes .fseventsd]
class SpoonBuilder
def initialize(paths)
@file_manifest = YAML.load_file(FILE_MANIFEST_PATH)
@platform_layout = YAML.load_file(PLATFORM_LAYOUT_PATH)
@usb_paths = paths.empty? ? Dir.glob(DEFAULT_USB_PATH) : paths
end
def run
cleanup_cache
download_files
build_usb_master
sync_usb_drives
end
def cleanup_cache
puts "Looking for old files to remove"
Dir.glob("#{CACHE_PATH}/*").each do |path|
file = File.basename(path)
unless @file_manifest.keys.include?(file)
print "I don't see #{file} in files.yml; it's probably from an older workshop. Delete it? "
answer = STDIN.gets.strip
FileUtils.rm(path) if answer.downcase.start_with?('y')
end
end
end
def download_files
puts "Checking and updating files in cache"
@file_manifest.each do |file, info|
file_cache_path = "#{CACHE_PATH}/#{file}"
if checksum_matches?(file_cache_path, info['checksum'])
puts "- up to date: #{file_cache_path}"
else
puts "- need to download (this may take a minute, it's working...): #{file_cache_path}"
IO.copy_stream(open(info['url']), file_cache_path)
end
end
end
def checksum_matches?(path, checksum)
File.exist?(path) && Digest::SHA256.hexdigest(File.read(path)) == checksum
end
def build_usb_master
return if ENV['CONCURRENT_SPOONS'] && File.exist?(USB_MASTER_PATH)
puts "Deleting and recreating #{USB_MASTER_PATH} directory"
FileUtils.rm_rf USB_MASTER_PATH
FileUtils.mkdir_p USB_MASTER_PATH
MAC_CRUFT.each {|c| FileUtils.touch "#{USB_MASTER_PATH}/#{c}" }
@platform_layout.each do |p_name, p_dir|
puts "Linking files for #{p_name}"
p_path = "#{USB_MASTER_PATH}/#{p_dir}"
FileUtils.mkdir_p(p_path)
@file_manifest.each do |file, info|
if info['platforms'].include?(p_name)
relative_symlink("#{CACHE_PATH}/#{file}", "#{p_path}/#{file}")
end
end
end
end
def relative_symlink(target, symlink)
unless File.exist?(symlink)
relative_target = Pathname.new(target).relative_path_from(Pathname.new(File.dirname(symlink)))
puts "- creating link: #{symlink} -> #{relative_target}"
FileUtils.ln_s(relative_target, symlink)
end
end
def sync_usb_drives
if @usb_paths.empty?
puts "Can't find any usb drives at #{DEFAULT_USB_PATH} -- are they plugged in?"
puts "If they are somewhere else on your computer (i.e. Linux), please specify them by path."
else
log_time 'synchronizing all RailsBridge USB drives' do
@usb_paths.each do |usb_path|
puts "-" * 80
puts "Synchronizing #{usb_path}"
puts "-" * 80
log_time usb_path do
system 'rsync', '-rtPh', '--del', '--modify-window=5', '--copy-links', "#{USB_MASTER_PATH}/", "#{usb_path}/"
end
end
end
end
end
def log_time(message)
elapsed = Benchmark.measure { yield }.real.round
minutes = elapsed / 60
seconds = elapsed % 60
end_message = "Finished #{message} in #{minutes} minutes #{seconds} seconds"
puts "#{end_message}!"
if system 'command -v say >/dev/null 2>&1'
system 'say', end_message
end
end
end
SpoonBuilder.new(ARGV).run