forked from caldwell/build-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensure-brew
executable file
·97 lines (86 loc) · 4.05 KB
/
ensure-brew
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
#!/usr/bin/env ruby
# Copyright © 2016 David Caldwell
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'optparse'
require 'json'
require File.join File.expand_path(File.dirname(__FILE__)),'verbose-shell.rb'
Vsh = VerboseShell
def with_env(env)
saved = {}
env.each {|k,v| saved[k] = ENV[k]; ENV[k] = v }
yield
saved.each {|k,v| ENV[k] = v }
end
def ensure_brew(dir, packages, options)
vsh = VerboseShell
vsh.verbose = options[:verbose]
vsh.system(*%W"git clone https://github.com/Homebrew/brew.git #{dir}") || abort("git failed!") unless File.exist?(dir)
get_source = options[:get_source] && File.expand_path(options[:get_source])
if get_source
vsh.mkdir_p(get_source)
vsh.cp(File.join(File.dirname(__FILE__),'brew-source.rb'), File.join(dir,'bin'))
end
FileUtils.cd(dir) do
vsh.system(*%W"git checkout #{options[:gitrev]}") || abort("git failed!") if options[:gitrev]
with_env 'PATH' => "#{Dir.pwd}/bin:#{ENV['PATH']}",
'HOMEBREW_NO_AUTO_UPDATE' => '1' do
vsh.system(*%W"brew update") unless options[:gitrev]
if options[:tapgitrev]
unless File.exist?("Library/Taps/homebrew/homebrew-core")
# This is much heavier than the normal `brew tap` (which does --depth=1), but otherwise we can't check out arbitrary revisions.
vsh.system(*%W"git clone https://github.com/Homebrew/homebrew-core Library/Taps/homebrew/homebrew-core") || abort("git clone failed!")
end
FileUtils.cd('Library/Taps/homebrew/homebrew-core') do
vsh.system(*%W"git checkout #{options[:tapgitrev]}") || abort("git checkout of tap failed!")
end
else
vsh.system(*%W"brew tap homebrew/core") # This *actually* installs brew the first time, and does nothing subsequent times.
end
packages.each do |_package|
extra_args = options[:extra_args] || []
package = _package
if _package.class == Hash
package = _package.keys.first
extra_args += _package[package]
end
info = IO.popen(%W"brew info --json=v1 #{package}") { |io| JSON.parse(io.read) }[0]
if info['linked_keg']
if info['outdated'] # brew upgrade doesn't support --build-bottle
vsh.system(*(%W"brew uninstall --ignore-dependencies #{package}"))
vsh.system(*(%W"brew install #{package}" + extra_args))
end
else
vsh.system(*(%W"brew install #{package}" + extra_args))
end
vsh.system(*%W"brew source #{package} #{get_source}") if get_source
end
end
end
end
if $0 == __FILE__
gitrev = nil
tapgitrev = nil
get_source = nil
verbose = nil
(opts=OptionParser.new do |opts|
opts.banner = "Usage:\n\t#{$0} [options] <DIRECTORY> [<PACKAGE> ...]"
opts.on("-v", "--verbose", "Turn up the verbosity") { |v| verbose = true }
opts.on( "--checkout=<GIT_REVISION>", "Checkout a particular git revision of homebrew") { |rev| gitrev = rev }
opts.on( "--checkout-tap=<GIT_REV>", "Checkout a given git rev of homebrew's core tap"){ |rev| tapgitrev = rev }
opts.on( "--get-source=<SOURCE_DIR>", "Also fetch sources into SOURCE_DIR") { |dir| get_source = dir }
opts.on_tail("-h", "--help") { puts opts; exit }
end).parse!
dir = ARGV.shift || opts.abort("Missing <DIRECTORY>\n\n"+opts.help)
ensure_brew(dir, ARGV, :gitrev => gitrev, :tapgitrev => tapgitrev, :get_source => get_source, :verbose=>verbose)
end