Module: Bivouac::Template

Defined in:
lib/bivouac/template.rb

Instance Method Summary collapse

Instance Method Details

#copyTemplate(file, dest, opts = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bivouac/template.rb', line 58

def copyTemplate( file, dest, opts = {} )
  force = opts[:force] || false
  from = opts[:from] || File.dirname(__FILE__)
  
  if File.exist?( dest + "/" + File.basename( file ) ) and force == false
    puts "\tskip #{File.expand_path( dest + "/" + File.basename( file ) )}: already exist"
  else
    if force
      puts "\tforce #{File.expand_path( dest ) + "/" + File.basename( file )}"
    else
      puts "\tcreate #{File.expand_path( dest ) + "/" + File.basename( file )}"
    end
    src = from + "/template/" + file
    FileUtils::cp( src, dest )
  end
end

#createDir(dirname) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/bivouac/template.rb', line 6

def createDir( dirname )
  if File.exist?( dirname )
    puts "\tskip #{File.expand_path( dirname )}: already exist"
  else
    puts "\tcreate #{File.expand_path( dirname )}"
    FileUtils::mkdir_p( "#{dirname}" )
  end
end

#createFile(filename, chmod_x = false, force_create_if_existe = nil, &block) ⇒ Object



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bivouac/template.rb', line 15

def createFile( filename, chmod_x = false, force_create_if_existe = nil, &block )
  create_ok = false
  
  if force_create_if_existe.nil?
    if File.exist?( filename )
      print "\toverwrite #{File.expand_path( filename )}? [Yn] "
      r = $stdin.gets.chomp
      if r == "Y" or r == "y" or r == ""
        puts "\tforce #{File.expand_path( filename )}"
        create_ok = true
      else
        puts "\tskip #{File.expand_path( filename )}: already exist"
        create_ok = false
      end
    else
      puts "\tcreate #{File.expand_path( filename )}"
      create_ok = true
    end
  elsif force_create_if_existe == false 
    if File.exist?( filename )
      puts "\tskip #{File.expand_path( filename )}: already exist"
      create_ok = false
    else
      puts "\tcreate #{File.expand_path( filename )}"
      create_ok = true
    end
  elsif force_create_if_existe == true
    if File.exist?( filename )
      puts "\tforce #{File.expand_path( filename )}"
    else
      puts "\tcreate #{File.expand_path( filename )}"
    end
    create_ok = true
  end
  
  if create_ok == true
    open( "#{filename}", "w" ) { |io| 
      yield( io )
    }
    FileUtils::chmod 0755, filename if chmod_x
  end
end

#plugin_template(template_file, b = nil) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/bivouac/template.rb', line 75

def plugin_template( template_file, b = nil )
  result = if b.nil? 
    ERB.new(File.readlines(template_file).join, nil, '-').result( )
  else
    ERB.new(File.readlines(template_file).join, nil, '-').result( b )
  end
  return( result )
end

#template(template_name, b = nil, from = File.dirname(__FILE__)) ⇒ Object



84
85
86
87
88
# File 'lib/bivouac/template.rb', line 84

def template( template_name, b = nil, from = File.dirname(__FILE__) )
  template_file = from + "/template/" + template_name + ".rb"
  
  return plugin_template( template_file, b )
end