installer.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # $Id: installer.rb,v 1.3 2003/07/21 03:46:50 icculus Exp $
  2. require 'rbconfig'
  3. require 'find'
  4. require 'ftools'
  5. include Config
  6. module Slimb
  7. class Installer
  8. def initialize target_dir = "", &user_skip
  9. @user_skip = user_skip or proc {|f| false}
  10. @version = CONFIG["MAJOR"] + "." + CONFIG["MINOR"]
  11. @libdir = File.join(CONFIG["libdir"], "ruby", @version)
  12. @sitedir = CONFIG["sitedir"] || File.join(@libdir, "site_ruby")
  13. @dest = File.join @sitedir, target_dir
  14. File::makedirs @dest
  15. File::chmod 0755, @dest, true
  16. end
  17. def skip? file
  18. @user_skip[file] or
  19. file[0] == ?. or file[-1] == ?~ or file[-1] == ?#
  20. end
  21. def install_dir dir
  22. File::makedirs(File.join(@dest, dir))
  23. File::chmod(0755, File.join(@dest, dir), true)
  24. Dir.foreach(dir) {|file|
  25. next if skip? file
  26. if File.ftype(File.join(dir, file)) == "directory"
  27. install_dir File.join(dir, file)
  28. else
  29. install_file File.join(dir, file)
  30. end
  31. }
  32. end
  33. def install_file file
  34. if file =~ /\.so$/
  35. install_so file
  36. else
  37. File::install file, File.join(@dest, file), 0644, true
  38. end
  39. end
  40. def install_so file
  41. File::install file, File.join(CONFIG["sitearchdir"], file), 0644, true
  42. end
  43. def uninstall_so file
  44. file = File.join(CONFIG["sitearchdir"], file)
  45. File::safe_unlink file
  46. end
  47. def install something
  48. case something
  49. when Array
  50. something.each {|x|
  51. install x if x.is_a? String
  52. }
  53. when String
  54. if File.ftype(something) == "directory"
  55. install_dir something
  56. else
  57. install_file something
  58. end
  59. end
  60. end
  61. def uninstall what = "*"
  62. case what
  63. when Array
  64. files = what.map {|x| File.join(@dest, x)}
  65. when String
  66. files = Dir[File.join(@dest, what)]
  67. end
  68. files.each {|x|
  69. # FIXME: recursive uninstall is a must
  70. next if FileTest.directory? x
  71. File::safe_unlink x
  72. }
  73. end
  74. def run files, argv
  75. if !argv.grep(/--uninstall/).empty?
  76. uninstall files
  77. else
  78. install files
  79. end
  80. end
  81. end
  82. end
  83. # self-installation
  84. if $0 == __FILE__
  85. $stderr.puts "Installing slimb installer..."
  86. Slimb::Installer.new("slimb").install File.basename(__FILE__)
  87. end