Module: Templates

Defined in:
lib/conan/templates.rb

Class Method Summary collapse

Class Method Details

.evaluate(templates_dir, output_dir, context) ⇒ Object

Returns the number of files changed/created



7
8
9
10
11
12
13
14
15
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
# File 'lib/conan/templates.rb', line 7

def self.evaluate(templates_dir, output_dir, context)   
  no_changes = 0
  FileUtils.mkdir_p File.dirname(output_dir)    
  Dir.glob(File.join(templates_dir, '*.erb')) do |template|      
    target_file = File.join(output_dir, File.basename(template)[0..-5])
    eruby = Erubis::Eruby.new(File.read(template))
    if (File.exist? target_file)        
      Tempfile.open(File.basename template) { |t|          
        begin
          t.write(eruby.evaluate(context))
          t.rewind            
        rescue Exception => e
          abort "Failed to evaluate #{template} template: #{e}"
        end
        if (FileUtils.identical? t.path, target_file)
          puts "-- No change from: #{File.basename template} => #{target_file}"
        else            
          puts ">> Replace file from template: #{File.basename template} => #{target_file}"
          puts "------------------------------"
          puts `diff -y -W 120 #{t.path} #{target_file}`
          puts "------------------------------"
          FileUtils.cp t.path, target_file
          no_changes += 1
        end
      }
    else
      File.open(target_file, 'w') { |f|
        puts "++ New file from template: #{File.basename template} => #{target_file}"
        f.write(eruby.evaluate(context))
        no_changes += 1
      }
    end
  end    
  no_changes
end