public
Description: Virtualize Ruby installations
Homepage:
Clone URL: git://github.com/chneukirchen/virtualrb.git
virtualrb / naive-install
100644 68 lines (56 sloc) 2.104 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby -s
# = naive-install, install a Ruby library in a naive way
#
# Handles bin/, sbin/, etc/ and lib/. This should be enough for >80%
# of the libraries out there. If your project has other "common"
# directories (data, doc, ext, include, info, localst, man) or
# extensions, you really should provide your own setup.rb.
#
# naive-install does not keep track of installed files!
# naive-install always installs into the prefix of the calling Ruby!
# naive-install does not ask! Use -n if you are unsure if it will do
# the right thing!
#
# Best used with virtualrb.
 
require 'rbconfig'
require 'fileutils'
 
if $h || !ARGV.empty?
  puts "Usage: naive-install [-n]"
  puts " -n dry run"
  puts " -h this help"
  exit
end
 
$dry_run = $n
 
RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
 
def do_install(src, destdir, mode, options={})
  destdir.gsub!(/\$(\w+)/) { Config::CONFIG[$1] }
 
  begin
    Dir.chdir(src) {
      Dir["**/*"].each { |file|
        dest = File.join(destdir, file)
        
        next unless File.file?(file)
        next if File.dirname(file) =~ %r{\.svn|CVS}
        next if File.basename(file) =~ %r{\A(#.*|.*~|.*\.(orig|rej|bak))\z}
        next if options[:no_overwrite] && File.exist?(dest)
        
        # The times where we had to copy files in 512k blocks is long over.
        contents = File.open(file, "rb") { |f| f.read }
        
        # We want to overwrite "#!/usr/bin/env ruby" here as well.
        contents.gsub!(/\A\#!.*ruby\S*/, '#!' + RUBY) if options[:fix_shebang]
        
        print "#{src}/#{file} -> #{dest}"
        unless $dry_run
          FileUtils.mkdir_p(File.dirname(dest))
          File.open(dest, "wb") { |out|
            out.write contents
            out.chmod mode
          }
        end
        puts
      }
    }
  rescue Errno::ENOENT
  end
end
 
do_install("bin", "$prefix/bin", 0755, :fix_shebang => true)
do_install("sbin", "$prefix/sbin", 0755)
do_install("etc", "$sysconfdir", 0644, :no_overwrite => true)
do_install("lib", "$sitelibdir", 0644)