require 'erb' module Bivouac module Template 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 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 def copyTemplate( file, dest ) if File.exist?( dest + "/" + File.basename( file ) ) puts "\tskip #{File.expand_path( dest + "/" + File.basename( file ) )}: already exist" else puts "\tcreate #{File.expand_path( dest ) + "/" + File.basename( file )}" src = File.dirname(__FILE__) + "/template/" + file FileUtils::cp( src, dest ) end end def template( template_name, b = nil ) template_file = File.dirname(__FILE__) + "/template/" + template_name + ".rb" 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 end end