#=============================================================================== # post-setup.rb - setup documentation #=============================================================================== EXAMPLES = '.examples' STD_URI = 'http:\/\/www.imagemagick.org' STD_URI_RE = /"#{STD_URI}/ DONT_RUN = ['fonts.rb'] # never run these examples ENTITY = Hash['&' => '&', '>' => '>', '<' => '<'] if defined?(Installer) && self.class == Installer RUBYPROG = get_config('ruby-prog') SRCDIR = curr_srcdir() ALLOW_EXAMPLE_ERRORS = get_config('allow-example-errors') == 'yes' BUILD_HTMLDOC = get_config('disable-htmldoc') != 'yes' else RUBYPROG = 'ruby' SRCDIR = '.' ALLOW_EXAMPLE_ERRORS = true BUILD_HTMLDOC = true end # # A set of example programs in a directory and the output image each example produces. # class ExampleSet def initialize(of) @status_quo = get_status_quo() @errs = 0 begin File.open(EXAMPLES) do |f| @targets = Marshal.load(f) end rescue @targets = Hash.new @n = 0 @of = of @first_time = true else @first_time = false end end def persist File.open(EXAMPLES, 'w') do |f| Marshal.dump(@targets, f) end end def targets(example) @targets[example] || Array.new end def get_status_quo sq = Dir["*"] sq.delete_if { |entry| File.directory?(entry) } end def update_targets(example, new) t = targets(example) + new @targets[example] = t.uniq end def update_status_quo(example) new_status_quo = get_status_quo() new = new_status_quo - @status_quo update_targets(example, new) @status_quo = new_status_quo end def build(example) cmd = "#{RUBYPROG} -I #{SRCDIR}/lib -I #{SRCDIR}/ext/RMagick #{example}" print cmd print " (example #{@n += 1} of #{@of})" if @first_time puts system cmd if $? != 0 then puts("post-setup.rb: #{example} example returned error code #{$?}") @errs += 1 unless ALLOW_EXAMPLE_ERRORS if @errs > 4 err(<<-END_EXFAIL Too many examples failed. The RMagick documentation cannot be installed successfully. Consult the README.txt file and try again, or send email to rmagick@rubyforge.org. END_EXFAIL ) end end update_status_quo(example) end def update(example) targets = targets(example) up_to_date = ! targets.empty? targets.each do |target| up_to_date &&= File.exists?(target) && (File.stat(target) >= File.stat(example)) end build(example) unless up_to_date end # # print message and exit # def err(msg) $stderr.puts "#{$0}: #{msg}" exit 1 end end # # Modify file lines via proc. If no lines changed, discard new version. # def filter(filename, backup=nil, &filter) if ! File.writable? filename then raise ArgumentError, "`#{filename}' is write-protected" end backup_name = filename + '.' + (backup || 'old') File.rename(filename, backup_name) changed = false begin File.open(filename, 'w') do |output| File.foreach(backup_name) do |line| old = line line = filter.call(line) output.puts(line) changed ||= line != old end end rescue File.rename(backup_name, filename) raise end if !changed newname = filename + '.xxx' File.rename(filename, newname) File.rename(backup_name, filename) File.unlink(newname) elsif !backup # Don't remove old copy if a backup extension was specified File.unlink(backup_name) rescue nil end end # # Copy an example to the doc directory, wrap in HTML tags # def filetoHTML(file, html) return if File.exists?(html) && File.stat(html) >= File.stat(file) File.open(file) do |src| File.open(html, 'w') do |dest| dest.puts <<-END_EXHTMLHEAD
END_EXHTMLHEAD src.each do |line| line.gsub!(/[&><]/) { |s| ENTITY[s] } dest.puts(line) end dest.puts <<-END_EXHTMLTAIL