forked from ryan-robeson/homebrew-gimp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prep-bottles
executable file
·178 lines (140 loc) · 4.7 KB
/
prep-bottles
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
#!/usr/bin/env ruby -s
require "fileutils"
require "open3"
require "shellwords"
bottle_dir = "bottles"
local_url = "http://127.0.0.1:8000"
release_url = "https://github.com/ryan-robeson/homebrew-gimp/releases/download/"
def usage
$stderr.puts <<~eos
Usage: #{$0} [-h|-help] [-skip-install] (-t=TAG|-local) -f=FORMULA
Prepares a bottle for FORMULA for release.
By default, (re)installs FORMULA with --build-bottle.
Run with -skip-install if that has already been done.
Also creates or updates the file 'release-TAG-files' - a newline
delimited list of archives that should be included in release tag.
-h, -help\tDisplay this message
-t=TAG\tThe tag this bottle will be released under (e.g. v1.1).
\t\tTAG doesn't have to exist, but won't be created by this script.
-local\tThis bottle will be served locally (testing only).
\t\t-t is ignored.
-f=FORMULA\tThe formula being bottled (e.g. resynthesizer)
-skip-install\tSkip (re)installing FORMULA with the --build-bottle
\t\toption. Saves a lot of time when this has already been
\t\tdone manually.
Example: #{$0} -t=v1.1 -f=resynthesizer
eos
exit 1
end
# Accepts a formula name (path and extension are optional)
# Makes sure the formula exists.
# Returns the formula name (minus path and extension) or
# ends the program.
def find_formula!(f)
f = f.dup
# Allow specifying formula name with extension
f.gsub!(/\.rb$/, '')
# Allow specifying path to formula
f = File.basename(f)
if !File.exist? "Formula/#{f}.rb"
$stderr.puts "Formula '#{f}' not found."
exit 1
end
f
end
usage if $h || $help
usage if $f.nil? || ($t.nil? && $local.nil?)
formula = find_formula!($f).shellescape
tag = $t.shellescape
if $local
root_url = local_url
else
root_url = release_url + tag
end
# For consistency
skip_install = $skip_install
release_file = File.expand_path("release-#{tag}-files")
# Generate bottles in `bottle_dir`
FileUtils.mkdir_p(bottle_dir)
Dir.chdir(bottle_dir)
# Reinstall formula with --build-bottle so bottle is built correctly.
unless skip_install
system("brew", "uninstall", "--ignore-dependencies", formula)
# Can't continue if the install fails
unless system("brew", "install", "--build-bottle", formula)
$stderr.puts "brew install --build-bottle #{formula} failed."
exit 1
end
end
# Generate the bottles along with json used for updating the formula
out, status = Open3.capture2("brew", "bottle", "--json", "--root-url=#{root_url}", formula)
if ! status.success?
exit 1
end
# out looks like:
# ==> Determining ryan-robeson/gimp/resynthesizer bottle rebuild...
# ==> Bottling resynthesizer--2.0.3.sierra.bottle.1.tar.gz...
# ./resynthesizer--2.0.3.sierra.bottle.1.tar.gz
# bottle do
# cellar :any
# rebuild 1
# sha256 "536e40d3d32e7b89d2d72d7295f4e8f9712aa16c5c7a8a7cdc4356238d3ad425" => :sierra
# end
bottle = out.gsub(/==>.+\n/, '').each_line.first.chomp # ./resynthesizer--2.0.3.sierra.bottle.1.tar.gz
bottle_prefix = bottle.gsub(/[^[:word:]]*([[:word:]].+)\.sierra.+/, '\1') # resynthesizer--2.0.3
# Includes bottle.tar.gz and bottle.json
bottles = Dir["#{bottle_prefix}*\.bottle\.*"]
# Remove double dashes in filenames
bottles.map! do |b|
new = b.gsub(/--/, "-")
# Always overwrite json because rebuilds output the same name.
if new.end_with?('.json') || ! File.exist?(new)
FileUtils.mv b, new
end
new
end
bottle.gsub!(/--/, "-")
bottle_prefix.gsub!(/--/, "-")
orig_os_pattern = /(?<=[^[:word:]])sierra(?=\.?)/
new_bottles = []
# Copy for High Sierra, Mojave and Catalina
["high_sierra", "mojave", "catalina"].each do |os|
bottles.each do |b|
new = b.gsub(orig_os_pattern, os)
# Never skip json files in case of rebuilds.
unless new.end_with? '.json'
next if File.exist? new
end
FileUtils.cp b, new
new_bottles << new
if new.end_with? '.json'
c = File.read new
c.gsub!(orig_os_pattern, os)
File.write new, c
end
end
end
bottles += new_bottles
# Update formula with bottle info
system("brew", "bottle", "--merge", "--write", "--no-commit", *Dir["#{bottle_prefix}*.json"])
# Post processing
if formula =~ /libgimp2.0/
# Hopefully Homebrew detecting libgimp2.0 as non-relocatable is a false positive.
# libgimpbase appears to be the culprit
c = File.read("../Formula/#{formula}.rb")
c.gsub!(/^\s*prefix .+\n/, '')
c.gsub!(/(?<=cellar )".+"/, ':any')
File.write("../Formula/#{formula}.rb", c)
end
# Output created bottles for possible automation.
# In another Ruby script, parse with "".shellsplit
archives = bottles.grep(/\.tar\.gz$/)
puts "Bottles:"
puts archives.map(&:shellescape).join(' ')
unless $local
open(release_file, 'a') do |f|
archives.each do |a|
f.puts "#{bottle_dir}/#{a}"
end
end
end