Skip to content

Instantly share code, notes, and snippets.

@sandro
Last active January 5, 2016 22:57
Show Gist options
  • Save sandro/3da00b02ed41fedc694a to your computer and use it in GitHub Desktop.
Save sandro/3da00b02ed41fedc694a to your computer and use it in GitHub Desktop.
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