Last active
January 5, 2016 22:57
-
-
Save sandro/3da00b02ed41fedc694a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main_bin = ARGV[0] | |
REPLACES = { | |
"@@HOMEBREW_PREFIX@@" => `brew --prefix`.chomp, | |
"@@HOMEBREW_CELLAR@@" => `brew --cellar`.chomp | |
} | |
VALID_REPLACE = Regexp.union(REPLACES.keys) | |
OTOOL = "otool" | |
CHANGE_TOOL = "install_name_tool" | |
def get_depends(bin) | |
dependencies = [] | |
result = cmd([OTOOL, "-L", bin], true) | |
result.readline | |
result.each_line do |line| | |
if line =~ VALID_REPLACE | |
line.match(/^\s([^\s]+)/) do |matches| | |
dependencies << matches[1] | |
end | |
end | |
end | |
dependencies | |
end | |
def replace(dependency, bin) | |
new = dependency.dup | |
REPLACES.each do |k,v| | |
new.sub!(k, v) | |
end | |
cmd(["sudo", CHANGE_TOOL, "-change", dependency, new, bin], true) | |
new | |
end | |
def run_replacement(bin) | |
puts "Working with #{bin}" | |
new_dependencies = [] | |
dependencies = get_depends(bin) | |
dependencies.each do |dependency| | |
puts "have dependency #{dependency.inspect}" | |
new_dependencies << replace(dependency, bin) | |
end | |
new_dependencies | |
end | |
def cmd(cmd, not_dry_run) | |
puts "running #{cmd}" | |
if not_dry_run | |
io = IO.popen(cmd) | |
select [io] | |
io | |
end | |
end | |
if main_bin | |
dependencies = run_replacement(main_bin) | |
dependencies.each do |d| | |
run_replacement(d) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment