Skip to content

Instantly share code, notes, and snippets.

@voxik
Created August 30, 2010 08:56
Show Gist options
  • Save voxik/557190 to your computer and use it in GitHub Desktop.
Save voxik/557190 to your computer and use it in GitHub Desktop.
Gem.pre_install do |gem_installer|
unless gem_installer.spec.extensions.empty?
require 'rubygems/defaults/which'
unless Which::Gcc.exe
raise Gem::InstallError,
"#{gem_installer.spec.name} requires installed build tools to build native extension." +
"Please update your path to include them or download DevKit from " +
"http://github.com/downloads/oneclick/rubyinstaller/DevKit-4.5.0-20100819-1536-sfx.exe " +
"and follow the instructions http://wiki.github.com/oneclick/rubyinstaller/development-kit"
end
end
end
class Pathname
def to_s
@path.tr('/','\\').sub(/^(.):/){|s| s.upcase }
end
alias to_windows to_s
def to_ruby
@path.tr('\\','/').sub(/^(.):/){|s| s.upcase }
end
def to_bash
@path.tr('\\','/').sub(/^(.):/){|s| "/#{s[0,1].downcase}"}
end
def ruby
Pathname(self.to_ruby)
end
def windows
Pathname(self.to_s)
end
def ==(other)
return false unless Pathname === other
self_ = self.dup.to_ruby.downcase
other = other.dup.to_ruby.downcase
self_ == other
end
end
class SearchPath
include Enumerable
def initialize(path)
@path = path.to_s.split(';').reject{|i| i.empty? }
end
def remove(old_path)
old = Pathname(old_path).expand_path
@path = @path.reject{|dir| Pathname(dir) == old }
@path = @path.uniq
self
end
def replace(old_path, new_path)
old_path = Pathname(old_path)
new_path = Pathname(new_path)
@path.map!{|dir|
if Pathname(dir) == old_path
new_path.to_windows.to_s
else
dir
end
}
@path = @path.uniq.compact
self
end
def each
@path.each{|path| yield path }
end
def add(new_path)
new_path = Pathname(new_path)
@path.unshift new_path.to_windows.to_s
self
end
def push(new_path)
new_path = Pathname(new_path)
@path.push new_path.to_windows.to_s
self
end
def replace_or_add(old_path, new_path)
if old_path.nil?
add(new_path)
return self
end
old_path = Pathname(old_path)
new_path = Pathname(new_path)
return self if old_path == new_path
old_search_path = @path.dup
replace(old_path, new_path)
add(new_path) if @path == old_search_path
self
end
def join
@path.join(';')
end
alias :to_s :join
def to_bash
@path.map{|i| Pathname.new(i).to_bash }.join(':')
end
def regex(string)
Regexp.new(Regexp.escape(string.to_s), true)
end
end
require 'pathname'
require 'rubygems/defaults/pathname'
require 'rubygems/defaults/search_path'
module Which
class Base
class << self
def find(search_path=ENV['PATH'])
path = SearchPath.new(search_path)
path = path.find{|dir| exist?(dir)}
Pathname(path) rescue nil
end
def find_all(search_path=ENV['PATH'])
path = SearchPath.new(search_path)
path = path.find_all{|dir| exist?(dir)}
path.map{|i| Pathname(i) }
end
def exist?(path)
!!exe(path)
end
def exe(path=find)
glob(path).first
end
alias :bat :exe
def glob(path)
return [] if path.nil?
glob = "#{Pathname.new(path).to_ruby}/{#{executables.join(',')}}"
Pathname.glob(glob)
end
end
end
class Gcc < Base
def self.executables
['gcc.bat', 'gcc.exe']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment