Created
February 27, 2015 19:29
-
-
Save takuya/5c71d4d07ee57b9a1bef 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
#!/usr/bin/env ruby | |
#coding:utf-8 | |
require 'pp' | |
require 'optparse' | |
opt = OptionParser.new | |
link=nil | |
begin | |
opt.on("-e", "--link","brew の gnuコマンドを/usr/local/bin にリンクします"){ link=true; } | |
opt.on("-d", "--unlink","brew の gnuコマンドを/usr/local/bin から削除します"){ link=false } | |
opt.parse!(ARGV) | |
if link.nil? | |
raise OptionParser::InvalidOption | |
end | |
rescue OptionParser::InvalidOption | |
ARGV << "-h" | |
retry | |
end | |
# coreutils にあるg 始まりのコマンドで/usr/local/binにあるものは消しておこう | |
Dir.glob("/usr/local/opt/coreutils/bin/*").map{|e| "/usr/local/bin/" + File.basename(e)}.each{|e| File.unlink(e) if File.exist?(e) } | |
## gnubin ディレクトリにg* prefix なしのコマンドが用意されている | |
cmds = Dir.glob("/usr/local/opt/coreutils/libexec/gnubin/*").map{|e| e} | |
cmds.each{|e| | |
case link | |
when true | |
puts "link #{e} --> /usr/local/bin/#{File.basename e}\n" if link == true | |
File.unlink "/usr/local/bin/#{File.basename e}" if File.exist?("/usr/local/bin/#{File.basename e}") | |
File.symlink e, "/usr/local/bin/#{File.basename e}" | |
when false | |
puts "unlink /usr/local/bin/#{File.basename e}\n" | |
File.unlink "/usr/local/bin/#{File.basename e}" if File.exist?("/usr/local/bin/#{File.basename e}") | |
end | |
} | |
## その他 gプレフィックスが付いているもの | |
other_cmds = [ | |
"/usr/local/bin/ggrep", | |
"/usr/local/bin/gtar", | |
"/usr/local/bin/gsed", | |
] | |
other_cmds += Dir.glob("/usr/local/opt/binutils/bin/*") | |
other_cmds += Dir.glob("/usr/local/opt/gnu-indent/bin/*") | |
other_cmds += Dir.glob("/usr/local/opt/gnu-tar/bin/*") | |
other_cmds += Dir.glob("/usr/local/opt/gnu-sed/bin/*") | |
other_cmds.each{|e| | |
name = File.basename(e).gsub(/^g/,"") | |
case link | |
when true | |
File.unlink "/usr/local/bin/#{name}" if File.exists? "/usr/local/bin/#{name}" | |
puts "link #{e} --> /usr/local/bin/#{name}" | |
File.link "#{e}" ,"/usr/local/bin/#{name}" | |
when false | |
puts "unlink /usr/local/bin/#{name}\n" | |
File.unlink "/usr/local/bin/#{name}" if File.exists?( "/usr/local/bin/#{name}" ) | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment