-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRakefile
More file actions
96 lines (80 loc) · 3.32 KB
/
Rakefile
File metadata and controls
96 lines (80 loc) · 3.32 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
require 'digest'
require 'erb'
require 'net/http'
require 'tmpdir'
# URL where our MacOS builds are located
MACOS_BASE_URL = 'https://downloads.voxpupuli.org/mac'
# MacOS architectures we can support (symbolized)
SUPPORTED_ARCHITECTURES = %i[arm64 x86_64]
# Minimal supported MacOS version
MINIMAL_MACOS_VERSION_SUPPORTED = 'ventura'
# RE to decompose installer filename in a href
INSTALLER_HREF_RE = /href="([\w-]+)-(\d+\.\d+\.\d+(?:\.\d+)?)-\d\.macos\.all\.(#{SUPPORTED_ARCHITECTURES.join('|')})\.dmg"/
# Opposite to the above. Recompose installer filename from parts
def pinfo_to_filename(pinfo)
"#{pinfo[:name]}-#{pinfo[:version]}-1.macos.all.#{pinfo[:arch]}.dmg"
end
def get_with_redirs(http, path, limit: 10)
raise 'too many HTTP redirects' if limit == 0
response = http.get(path)
return response unless response == Net::HTTPRedirection
get_with_redirs(http, response['location'], limit: limit - 1) if response == Net::HTTPRedirection
end
# Crawl the collection and gather supported OSes/packages/versions/etc.
# Returns array of hashes [{ pkg: ..., version: ..., arch: ... }, {...}]
def gather_downloads(collection)
uri = URI("#{MACOS_BASE_URL}/#{collection}")
puts "Crawling #{uri}/ ..."
Net::HTTP.start(uri.hostname) do |http|
resp = get_with_redirs(http, "#{uri.path}/") # Keep the trailing slash
resp.body.scan(INSTALLER_HREF_RE).map do |x|
{ name: x[0], version: x[1], arch: x[2] }
end
end
end
# Get checksum of a package
def get_checksum(filename, collection)
uri = URI("#{MACOS_BASE_URL}/#{collection}")
puts "Fetching and calculating checksum for #{filename} ..."
Net::HTTP.start(uri.hostname) do |http|
resp = get_with_redirs(http, "#{uri.path}/#{filename}")
return nil unless resp.is_a? Net::HTTPSuccess
Digest::SHA256.hexdigest(resp.body)
end
end
namespace :brew do
desc 'Render cask file for a specific package: rake brew:cask[openbolt,8] or rake brew:cask[agent,8]'
task :cask, [:pkg, :collection] do |task, args|
pkg = args[:pkg]
collection = "openvox#{args[:collection]}"
cask = "#{collection}-#{pkg}"
# Map pkg to a remote file name on the download server
remote_name = {
'agent' => 'openvox-agent'
}.fetch(pkg, pkg)
all_packages = gather_downloads(collection)
my_packages = all_packages.filter { |x| x[:name] == remote_name }
max_version_by_arch = my_packages.group_by { |x| x[:arch] }.map do |arch, pkgs|
[arch, pkgs.max_by { |x| Gem::Version.new(x[:version]) }]
end.to_h
package_data = max_version_by_arch.map do |arch, pinfo|
filename = pinfo_to_filename(pinfo)
[arch, pinfo.merge(
sha256: get_checksum(filename, collection),
filename: filename
)]
end.to_h
source_stanza_erb = ERB.new(File.read(File.join(__dir__, 'templates', 'source_stanza.erb')), trim_mode: '-')
source_stanza_content = source_stanza_erb.result_with_hash(
base_url: "#{MACOS_BASE_URL}/#{collection}",
remote_name: remote_name,
minimal_macos_version_supported: MINIMAL_MACOS_VERSION_SUPPORTED,
package_data: package_data
)
cask_erb = ERB.new(File.read(File.join(__dir__, 'templates', "#{cask}.rb.erb")), trim_mode: '-')
cask_content = cask_erb.result_with_hash(
source_stanza: source_stanza_content
)
File.write(File.join(__dir__, 'Casks', "#{cask}.rb"), cask_content)
end
end