-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodify-formula
executable file
·169 lines (143 loc) · 4.7 KB
/
modify-formula
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
#!/usr/bin/env ruby
# encoding: utf-8
require 'optparse'
require 'digest/sha2'
SELFDIR = File.expand_path(File.dirname(__FILE__))
REQUIRED_OPTIONS = [:passenger_dir, :tarball, :formula]
# Files and directories that should be excluded from the Homebrew installation.
HOMEBREW_EXCLUDE = [
'INSTALL.md'
]
def main
options = parse_options
load_passenger_lib(options)
version = PhusionPassenger::VERSION_STRING
branch_name = calculate_branch_name(version)
sha256 = calculate_tarball_sha256(options)
formula = File.open(options[:formula], 'r:utf-8') { |f| f.read }
if formula =~ /class PassengerEnterprise/
formula.gsub!(/^ version \".*/, " version \"#{version}\"") ||
abort('Unable to substitute Homebrew formula version number')
else
formula.gsub!(/release-.+?\//, "release-#{version}/") ||
abort('Unable to substitute Homebrew formula tarball URL')
formula.gsub!(/passenger-.+?\.tar\.gz/, "passenger-#{version}.tar.gz") ||
abort('Unable to substitute Homebrew formula tarball filename')
formula.gsub!(/branch: .+/, "branch: #{branch_name.inspect}") ||
abort('Unable to substitute Homebrew formula branch name')
end
formula.gsub!(/^ sha256 .*/, " sha256 \"#{sha256}\"") ||
abort('Unable to substitute Homebrew formula SHA-256')
formula.sub!(/.*depends_on.*apache.*\n/,"")
formula.sub!(/.*uses_from_macos "ruby".*\n/,"")
formula.sub!(/.*revision.*\n/,"")
orig_tarball_files = list_passenger_tarball_files(options)
prefix = ' necessary_files = %w['
necessary_dirs = orig_tarball_files.map { |filename| filename.split('/').first }.uniq
necessary_dirs -= HOMEBREW_EXCLUDE
necessary_dirs << 'buildout'
necessary_dirs_str = word_wrap(necessary_dirs.join(' '), 80 - prefix.size)
necessary_dirs_str = necessary_dirs_str.split("\n").join("\n" + ' ' * prefix.size)
formula.sub!(/necessary_files = .*?\]/m, "necessary_files = %w[#{necessary_dirs_str}]") ||
abort('Unable to substitute Homebrew formula file whitelist')
write_output(options, formula)
end
def help
puts build_option_parser({})
end
def build_option_parser(options)
OptionParser.new do |opts|
opts.banner = 'Usage: ./modify-formula OPTIONS...'
opts.separator 'Modifies the Homebrew formula for the given Passenger' \
' installation and tarball: tarball filename, SHA256 and more.'
opts.separator ''
opts.separator 'Required options:'
opts.on('-p', '--passenger-dir PATH', String, 'Path to Passenger source directory') do |v|
options[:passenger_dir] = v
end
opts.on('-t', '--tarball PATH', String, 'Path to Passenger source tarball') do |v|
options[:tarball] = v
end
opts.on('-f', '--formula PATH', String, 'Formula file to modify, e.g. ./Formula/passenger.rb') do |v|
options[:formula] = v
end
opts.separator ''
opts.separator 'Optional options:'
opts.on('-o', '--output PATH', String, 'File to output to. Default: standard output') do |v|
options[:output] = v
end
end
end
def parse_options
options = {}
parser = build_option_parser(options)
parser.parse!
REQUIRED_OPTIONS.each do |option|
if !options.key?(option)
STDERR.puts 'ERROR: One or more required options are missing!'
help
abort
end
end
options
end
def load_passenger_lib(options)
require "#{options[:passenger_dir]}/src/ruby_supportlib/phusion_passenger.rb"
PhusionPassenger.locate_directories
PhusionPassenger.require_passenger_lib 'constants'
PhusionPassenger.require_passenger_lib 'packaging'
end
def calculate_branch_name(version)
'stable-' + version.split('.')[0..1].join('.')
end
def calculate_tarball_sha256(options)
File.open(options[:tarball], 'rb') do |f|
Digest::SHA256.hexdigest(f.read)
end
end
def list_passenger_tarball_files(options)
Dir.chdir(options[:passenger_dir]) do
PhusionPassenger::Packaging.files
end
end
def word_wrap(text, width)
output = []
text.lines do |line|
line.chomp! "\n"
if line.length > width
new_lines = split_line(line, width)
while new_lines.length > 1 && new_lines[1].length > width
output.push new_lines[0]
new_lines = split_line new_lines[1], width
end
output += new_lines
else
output.push line
end
end
output.map { |s| s.rstrip! }
output.join("\n") + "\n"
end
def split_line(line, width)
at = line.index(/\s/)
last_at = at
while at != nil && at < width
last_at = at
at = line.index(/\s/, last_at + 1)
end
if last_at == nil
[line]
else
[line[0,last_at], line[last_at+1, line.length]]
end
end
def write_output(options, formula)
if options[:output]
File.open(options[:output], 'w:utf-8') do |f|
f.write(formula)
end
else
STDOUT.write(formula)
end
end
main