Permalink
Browse files

re-copied from escape/install.rb

git-svn-id: svn://svn.a-k-r.org/akr/truthtable/trunk@709 0ecacc03-ac1b-0410-941d-e442c79421a6
  • Loading branch information...
1 parent d1410e7 commit 4b7ffe2c041cc1eee9b477922082fb1701d4385f @akr committed Nov 8, 2007
Showing with 112 additions and 0 deletions.
  1. +112 −0 install.rb
View
@@ -0,0 +1,112 @@
+#!/usr/bin/env ruby
+
+# usage: ruby install.rb [-n] [--destdir=DESTDIR]
+# options:
+# -n : don't install
+# --destdir=DESTDIR
+#
+# Author: Tanaka Akira <akr@m17n.org>
+
+require 'optparse'
+require 'fileutils'
+
+def target_directory
+ $:.each {|loc|
+ if %r{/site_ruby/[\d.]+\z} =~ loc
+ return loc
+ end
+ }
+ raise "could not find target install directory"
+end
+
+CVS_FILES = {}
+def cvs_files(dir)
+ return CVS_FILES[dir] if CVS_FILES.include? dir
+ if File.directory? "#{dir}/CVS"
+ result = {}
+ File.foreach("#{dir}/CVS/Entries") {|line|
+ case line
+ when %r{\A/([^/]+)/} then result[$1] = true
+ when %r{\AD/([^/]+)/} then result[$1] = true
+ end
+ }
+ else
+ result = nil
+ end
+ CVS_FILES[dir] = result
+ result
+end
+
+def each_target(&block)
+ target_set = {}
+ cvs = cvs_files('.')
+ Dir.glob("*.rb") {|filename|
+ next if /\Atest-/ =~ filename
+ next if /\Ainstall/ =~ filename
+ next if cvs && !cvs.include?(filename)
+ target_set[filename] = true
+ yield filename
+ each_require(filename, target_set, &block)
+ }
+end
+
+def each_require(file, target_set, &block)
+ File.foreach(file) {|line|
+ next if /\A\s*require\s+['"]([^'"]+)['"]/ !~ line
+ feature = $1
+ filename = "#{feature}.rb"
+ next if target_set.include? filename
+ next if !File.exist?(filename)
+ target_set[filename] = true
+ yield filename
+ each_require(filename, target_set, &block)
+ }
+end
+
+def collect_target
+ result = []
+ each_target {|filename| result << filename }
+ result.sort!
+ result
+end
+
+def install_file(src, dst)
+ ignore_exc(Errno::ENOENT) { return if FileUtils.compare_file src, dst }
+ # check shadow
+ ignore_exc(Errno::ENOENT) { File.unlink dst }
+ FileUtils.mkdir_p(File.dirname(dst), :mode=>0755)
+ FileUtils.cp(src, dst, :verbose => true)
+ File.chmod(0644, dst)
+end
+
+def ignore_exc(exc)
+ begin
+ yield
+ rescue exc
+ end
+end
+
+$opt_n = false
+$opt_destdir = ""
+ARGV.options {|q|
+ q.banner = 'ruby install.rb [opts]'
+ q.def_option('--help', 'show this message') {puts q; exit(0)}
+ q.def_option('-n', "don't install") { $opt_n = true }
+ q.def_option('--destdir=DESTDIR', "specify DESTDIR") {|destdir| $opt_destdir = destdir }
+ q.parse!
+}
+
+if $opt_n
+ dir = target_directory
+ collect_target.each {|filename|
+ puts "-> #{$opt_destdir}#{dir}/#{filename}"
+ }
+ exit
+else
+ File.umask 022
+ dir = target_directory
+ collect_target.each {|filename|
+ install_file filename, "#{$opt_destdir}#{dir}/#{filename}"
+ }
+end
+

0 comments on commit 4b7ffe2

Please sign in to comment.