def set_gem_home ENV['GEM_HOME'] ||= Gem.path.find { |f| File.writable?(f) } end
Unfortunately this doesn't work as a reliable sudo detector. I tried it before only to realize it works for some gems, but not others: buildr, rake, rubyforge all install executables, typically in /usr/bin, and so require sudo to install properly. also, sudo_needed? (question mark = no side effects) calling set_gem_home (set = side effects) ? :-) assaf On Wed, Sep 24, 2008 at 12:17 PM, <[EMAIL PROTECTED]> wrote: > Author: vborja > Date: Wed Sep 24 12:17:27 2008 > New Revision: 698692 > > URL: http://svn.apache.org/viewvc?rev=698692&view=rev > Log: > Modified setup.rake:install_gem to fetch gem cache only once. > Use sudo if no Gem.path is writable. > > Modified: > incubator/buildr/trunk/rakelib/setup.rake > > Modified: incubator/buildr/trunk/rakelib/setup.rake > URL: > http://svn.apache.org/viewvc/incubator/buildr/trunk/rakelib/setup.rake?rev=698692&r1=698691&r2=698692&view=diff > ============================================================================== > --- incubator/buildr/trunk/rakelib/setup.rake (original) > +++ incubator/buildr/trunk/rakelib/setup.rake Wed Sep 24 12:17:27 2008 > @@ -16,7 +16,7 @@ > > require 'rubygems/source_info_cache' > require 'stringio' # for Gem::RemoteFetcher > - > +require 'jruby' if RUBY_PLATFORM[/java/] > > # True if running on the Windows operating sytem. Different from > Gem.win_platform? > # which returns true if running on the Windows platform of MRI, false when > using JRuby. > @@ -24,11 +24,20 @@ > Config::CONFIG['host_os'] =~ > /windows|cygwin|bccwin|cygwin|djgpp|mingw|mswin|wince/i > end > > +def set_java_home > + if !ENV['JAVA_HOME'] && RUBY_PLATFORM[/java/] > + ENV['JAVA_HOME'] = java.lang.System.getProperty('java.home') > + end > + fail "Please set JAVA_HOME first #{'(no need to run as sudo)' if > ENV['USER'] == 'root'}" unless ENV['JAVA_HOME'] > +end > > -def sudo_needed? > - !windows? && (ENV['GEM_HOME'].nil?) > +def set_gem_home > + ENV['GEM_HOME'] ||= Gem.path.find { |f| File.writable?(f) } > end > > +def sudo_needed? > + !( windows? || set_gem_home ) > +end > > # Finds and returns path to executable. Consults PATH environment variable. > # Returns nil if executable not found. > @@ -41,18 +50,26 @@ > FileList[path].existing.first > end > > +# Execute a GemRunner command > +def gem_run(*args) > + rb_bin = File.join(Config::CONFIG['bindir'], > Config::CONFIG['ruby_install_name']) > + args.unshift rb_bin, '-S', 'gem' > + args.unshift 'sudo', 'env', 'JAVA_HOME=' + ENV['JAVA_HOME'] if sudo_needed? > + sh *args.map{ |a| a.inspect }.join(' ') > +end > > def install_gem(name, ver_requirement = ['> 0']) > dep = Gem::Dependency.new(name, ver_requirement) > - rb_bin = File.join(Config::CONFIG['bindir'], > Config::CONFIG['ruby_install_name']) > + @load_cache = true > if Gem::SourceIndex.from_installed_gems.search(dep).empty? > - spec = Gem::SourceInfoCache.search(dep, true, true).last > + spec = Gem::SourceInfoCache.search(dep, true, @load_cache).last > fail "#{dep} not found in local or remote repository!" unless spec > puts "Installing #{spec.full_name} ..." > - args = [rb_bin, '-S', 'gem', 'install', spec.name, '-v', > spec.version.to_s] > - fail "Please set JAVA_HOME first #{'(no need to run as sudo)' if > ENV['USER'] == 'root'}" unless ENV['JAVA_HOME'] > - args.unshift('sudo', 'env', 'JAVA_HOME=' + ENV['JAVA_HOME']) if > sudo_needed? > - sh *args.map{ |a| a.inspect }.join(' ') > + args = ['install'] > + args.push '--install-dir', ENV['GEM_HOME'] if ENV['GEM_HOME'] > + args.push spec.name, '-v', spec.version.to_s > + gem_run *args > + @load_cache = false # Just update the Gem cache once > end > end > > @@ -60,6 +77,8 @@ > desc "If you're building from sources, run this task first to setup the > necessary dependencies." > missing = spec.dependencies.select { |dep| > Gem::SourceIndex.from_installed_gems.search(dep).empty? } > task 'setup' do > + set_java_home > + set_gem_home > missing.each do |dep| > install_gem dep.name, dep.version_requirements > end > > >