install.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. require 'rbconfig'
  2. require 'find'
  3. require 'fileutils'
  4. include RbConfig
  5. $ruby = CONFIG['ruby_install_name']
  6. ##
  7. # Install a binary file. We patch in on the way through to
  8. # insert a #! line. If this is a Unix install, we name
  9. # the command (for example) 'rake' and let the shebang line
  10. # handle running it. Under windows, we add a '.rb' extension
  11. # and let file associations to their stuff
  12. #
  13. def installBIN(from, opfile)
  14. tmp_dir = nil
  15. for t in [".", "/tmp", "c:/temp", $bindir]
  16. stat = File.stat(t) rescue next
  17. if stat.directory? and stat.writable?
  18. tmp_dir = t
  19. break
  20. end
  21. end
  22. fail "Cannot find a temporary directory" unless tmp_dir
  23. tmp_file = File.join(tmp_dir, "_tmp")
  24. File.open(from) do |ip|
  25. File.open(tmp_file, "w") do |op|
  26. ruby = File.join($realbindir, $ruby)
  27. op.puts "#!#{ruby} -w"
  28. op.write ip.read
  29. end
  30. end
  31. opfile += ".rb" if CONFIG["target_os"] =~ /mswin/i
  32. FileUtils.install(tmp_file, File.join($bindir, opfile),
  33. {:mode => 0755, :verbose => true})
  34. File.unlink(tmp_file)
  35. end
  36. $sitedir = CONFIG["sitelibdir"]
  37. unless $sitedir
  38. version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
  39. $libdir = File.join(CONFIG["libdir"], "ruby", version)
  40. $sitedir = $:.find {|x| x =~ /site_ruby/}
  41. if !$sitedir
  42. $sitedir = File.join($libdir, "site_ruby")
  43. elsif $sitedir !~ Regexp.quote(version)
  44. $sitedir = File.join($sitedir, version)
  45. end
  46. end
  47. $bindir = CONFIG["bindir"]
  48. $realbindir = $bindir
  49. bindir = CONFIG["bindir"]
  50. if (destdir = ENV['DESTDIR'])
  51. $bindir = destdir + $bindir
  52. $sitedir = destdir + $sitedir
  53. FileUtils.mkdir_p($bindir)
  54. FileUtils.mkdir_p($sitedir)
  55. end
  56. rake_dest = File.join($sitedir, "rake")
  57. FileUtils.mkdir_p(rake_dest, {:verbose => true})
  58. File.chmod(0755, rake_dest)
  59. # The library files
  60. files = Dir.chdir('lib') { Dir['**/*.rb'] }
  61. for fn in files
  62. fn_dir = File.dirname(fn)
  63. target_dir = File.join($sitedir, fn_dir)
  64. if ! File.exist?(target_dir)
  65. FileUtils.mkdir_p(target_dir)
  66. end
  67. FileUtils.install(File.join('lib', fn), File.join($sitedir, fn),
  68. {:mode => 0644, :verbose => true})
  69. end
  70. # and the executable
  71. installBIN("bin/rake", "rake")