Class: AsProject::TemplateResolver

Inherits:
Hash
  • Object
show all
Defined in:
lib/template_resolver.rb

Overview

TemplateResolver

Direct Known Subclasses

AsProjectResolver, PathFinder

Constant Summary collapse

@@ASPROJECT_FILE_NAME =
'AsProject'
@@RENDER_IGNORE_FILES =
['asclass_config.rb', 'SWFMillTemplate.erb']
@@BINARY_EXTENSIONS =
['.jpg', '.png', '.gif', '.doc', '.xls', '.exe']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplateResolver

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_allObject

Returns the value of attribute ignore_all.



6
7
8
# File 'lib/template_resolver.rb', line 6

def ignore_all
  @ignore_all
end

#replace_allObject

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

#clean_file_name(name) ⇒ Object



161
162
163
# File 'lib/template_resolver.rb', line 161

def clean_file_name name
  return name.gsub(@@ASPROJECT_FILE_NAME, project_name)
end

#copy_file(from, to, render = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/template_resolver.rb', line 47

def copy_file(from, to, render=false)
  if(write_file?(to))
    content = nil
    File.open(from, 'r') 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') 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?

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
# File 'lib/template_resolver.rb', line 171

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_nameObject

Override in subclasses!



166
167
168
# File 'lib/template_resolver.rb', line 166

def project_name
  return @@ASPROJECT_FILE_NAME
end

#render_file(filename) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/template_resolver.rb', line 152

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

Returns:

  • (Boolean)


99
100
101
102
103
104
# File 'lib/template_resolver.rb', line 99

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

Returns:

  • (Boolean)


106
107
108
109
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
# File 'lib/template_resolver.rb', line 106

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