Class: AsProject::TemplateResolver
- Inherits:
-
Hash
- Object
- Hash
- AsProject::TemplateResolver
- Defined in:
- lib/template_resolver.rb
Overview
TemplateResolver
Direct Known Subclasses
Constant Summary collapse
- @@ASPROJECT_FILE_NAME =
'AsProject'
- @@RENDER_IGNORE_FILES =
['asclass_config.rb', 'SWFMillTemplate.erb']
- @@BINARY_EXTENSIONS =
['.jpg', '.png', '.gif', '.doc', '.xls', '.exe', '.swf', 'fla', '.psd']
Instance Attribute Summary collapse
-
#ignore_all ⇒ Object
Returns the value of attribute ignore_all.
-
#replace_all ⇒ Object
Returns the value of attribute replace_all.
Instance Method Summary collapse
- #b(path) ⇒ Object
- #clean_file_name(name) ⇒ Object
- #copy_file(from, to, render = false) ⇒ Object
- #copy_files(from, to, render = false) ⇒ Object
-
#initialize ⇒ TemplateResolver
constructor
A new instance of TemplateResolver.
-
#is_binary?(file) ⇒ Boolean
TODO: Figure out if the file is plain text or not…
-
#project_name ⇒ Object
Override in subclasses!.
- #render_file(filename) ⇒ Object
-
#should_render?(file) ⇒ Boolean
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.
- #write_file?(file) ⇒ Boolean
Constructor Details
#initialize ⇒ TemplateResolver
Returns a new instance of TemplateResolver.
11 12 13 14 |
# File 'lib/template_resolver.rb', line 11 def initialize @replace_all = false @ignore_all = false end |
Instance Attribute Details
#ignore_all ⇒ Object
Returns the value of attribute ignore_all.
6 7 8 |
# File 'lib/template_resolver.rb', line 6 def ignore_all @ignore_all end |
#replace_all ⇒ Object
Returns the value of attribute replace_all.
6 7 8 |
# File 'lib/template_resolver.rb', line 6 def replace_all @replace_all end |
Instance Method Details
#b(path) ⇒ Object
47 48 49 |
# File 'lib/template_resolver.rb', line 47 def b(path) (is_binary?(path)) ? 'b' : '' end |
#clean_file_name(name) ⇒ Object
165 166 167 |
# File 'lib/template_resolver.rb', line 165 def clean_file_name name return name.gsub(@@ASPROJECT_FILE_NAME, project_name) end |
#copy_file(from, to, render = false) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/template_resolver.rb', line 51 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 |
#copy_files(from, to, render = false) ⇒ Object
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 |
# File 'lib/template_resolver.rb', line 16 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 |
#is_binary?(file) ⇒ Boolean
TODO: Figure out if the file is plain text or not… Possible?
175 176 177 178 179 180 181 182 183 |
# File 'lib/template_resolver.rb', line 175 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 |
#project_name ⇒ Object
Override in subclasses!
170 171 172 |
# File 'lib/template_resolver.rb', line 170 def project_name return @@ASPROJECT_FILE_NAME end |
#render_file(filename) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/template_resolver.rb', line 156 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 |
#should_render?(file) ⇒ Boolean
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
103 104 105 106 107 108 |
# File 'lib/template_resolver.rb', line 103 def should_render?(file) if(is_binary?(file) || @@RENDER_IGNORE_FILES.index(File.basename(file))) return false end return true end |
#write_file?(file) ⇒ Boolean
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/template_resolver.rb', line 110 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 |