/rb/lib/template_resolver.rb
Ruby | 218 lines | 175 code | 16 blank | 27 comment | 18 complexity | 56373b9aa92d0b0983dac12f77d698d5 MD5 | raw file
- module AsProject
- #######################################
- # TemplateResolver
- class TemplateResolver < Hash
- attr_accessor :replace_all, :ignore_all
- @@ASPROJECT_FILE_NAME = 'AsProject'
- @@RENDER_IGNORE_FILES = ['asclass_config.rb', 'SWFMillTemplate.erb']
- @@BINARY_EXTENSIONS = ['.jpg', '.png', '.gif', '.doc', '.xls', '.exe', '.swf', 'fla', '.psd']
-
- def initialize
- @replace_all = false
- @ignore_all = false
- end
-
- def copy_files(from, to, render=false)
- created_files = Array.new
- if(!File.exists? from)
- raise ProjectError.new('TemplateResolver attempted to copy files from (' + from + ') but it does not exist...')
- end
- if(File.directory? from)
- Dir.open(from).each do |filename|
- if(!AsProject.ignore_file? filename)
- fullname = File.join(from, filename)
- new_fullname = File.join(to, filename)
- cleaned_filename = clean_file_name(filename)
- cleaned_fullname = File.join(to, cleaned_filename)
- if(File.directory? fullname)
- Dir.mkdir(new_fullname) unless File.exists? new_fullname
- copy_files(fullname, new_fullname, render).each do |file|
- created_files << file
- end
- else
- file = copy_file(fullname, cleaned_fullname, render)
- if(file)
- created_files << file
- end
- end
- end
- end
- else
- raise ProjectError.new("copy_files called with a file (" + from + ") instead of a directory!")
- end
- return created_files
- end
-
- def b(path)
- (is_binary?(path)) ? 'b' : ''
- end
-
- def copy_file(from, to, render=false)
- if(write_file?(to))
- content = nil
- File.open(from, 'r' + b(from)) do |f|
- content = f.read
- end
- if(render && should_render?(from))
- begin
- content = ERB.new(content, nil, '>').result(binding)
- rescue NameError => e
- puts '>> Template ' + from + ' references a value that is not defined'
- raise e
- end
- end
- FileUtils.makedirs(File.dirname(to))
- File.open(to, 'w' + b(to)) do |f|
- f.write(content)
- end
- return to
- end
- return nil
- end
- =begin
- content = nil
- # content_mode = nil
- File.open(from, 'r') do |f|
- # content_mode = f.stat.mode
- content = f.read
- end
- parts = to.split(File::SEPARATOR)
- parts.pop
- if(render && should_render?(from))
- begin
- content = ERB.new(content, nil, '>').result(binding)
- rescue NameError => e
- puts '>> Template ' + from + ' references a value that is not defined'
- raise e
- end
- end
- File.makedirs(parts.join(File::SEPARATOR))
- File.open(to, 'w') do |f|
- f.write(content)
- end
- # FileUtils.chmod(content_mode, to)
- return to
- return nil
- =end
-
- def should_render?(file)
- if(is_binary?(file) || @@RENDER_IGNORE_FILES.index(File.basename(file)))
- return false
- end
- return true
- end
-
- def write_file?(file)
- if(!File.exists?(file))
- return true
- elsif(@replace_all)
- File.delete(file)
- return true
- elsif(@ignore_all)
- return false
- end
- relative = file.gsub(Dir.pwd, '')
- msg = <<EOF
- AsProject Encountered an existing file at [#{relative}], what would you like to do?
- (r)eplace, (i)gnore, (R)eplace all or (I)gnore all?
- EOF
- puts msg
- answer = gets.chomp!
- if(answer == 'r')
- return true
- elsif(answer == 'i')
- return false
- elsif(answer == 'R')
- msg = <<EOF
-
- Are you sure you want to replace ALL duplicate files?
- (y)es or (n)o
- EOF
- puts msg
- answer = gets.chomp!
- if(answer == 'y')
- @replace_all = true
- else
- write_file?(file)
- end
- elsif(answer == 'I')
- @ignore_all = true
- return false
- else
- puts "I didn't understand that response... Please choose from the following choices:\n\n"
- write_file?(file)
- end
- end
-
- def render_file filename
- file = File.open(filename, 'r')
- resolved = ERB.new(file.read, nil, '>').result(binding)
- file.close
- file = File.open(filename, 'w')
- file.write(resolved)
- file.close
- end
-
- def clean_file_name name
- return name.gsub(@@ASPROJECT_FILE_NAME, project_name)
- end
-
- # Override in subclasses!
- def project_name
- return @@ASPROJECT_FILE_NAME
- end
-
- #TODO: Figure out if the file is plain text or not... Possible?
- def is_binary? file
- file_extension = File.extname(file).downcase
- @@BINARY_EXTENSIONS.each do |ext|
- if(file_extension == ext)
- return true
- end
- end
- return false
- end
- =begin
- Found this code for binary inspection here:
- http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/44940
- it's not 100%, but better than what I'm doing with extensions.
- This should be tested and inserted above
- if it works.
- NON_ASCII_PRINTABLE = /[^\x20-\x7e\s]/
- def nonbinary?(io, forbidden, size = 1024)
- while buf = io.read(size)
- return false if forbidden =~ buf
- end
- true
- end
- # usage: ruby this_script.rb filename ...
- ARGV.each do |fn|
- begin
- open(fn) do |f|
- if nonbinary?(f, NON_ASCII_PRINTABLE)
- puts "#{fn}: ascii printable"
- else
- puts "#{fn}: binary"
- end
- end
- rescue
- puts "#$0: #$!"
- end
- end
- =end
- end
- end