summaryrefslogtreecommitdiffstats
path: root/skeletons.rb
blob: f88ae4955c0bf453e05b2d1282cf1d11da5f7329 (plain)
1
2
3
4
5
6
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
42
43
44
45
46
47
#!/usr/bin/env ruby
#
# vmirc => au BufNewFile * :exe("0r! ~/.vim/skeletons.rb %:p " . &filetype)
#
require 'erb'
require 'date'

SKELETON_DIR = File.expand_path("~/.vim/skeletons");
EXTENSION  = "erb"

filepath, filetype = ARGV
filename = File.basename( filepath )
extension = File.extname( filename ).split( '.' )[1]

if filename =~ /\.h$/ then filetype='c_header'; end # otherwise filetype is cpp
if filetype.nil? then filetype = extension; end

skeleton  = [ filename, extension, filetype ].map { |s| File.join( SKELETON_DIR, "#{s}.#{EXTENSION}" ) }.find { |s| File.exist?(s) && File.readable?(s) }

exit if skeleton.nil?

@username = ENV['USERNAME'] || 'John Doe'
@email = ENV['EMAIL'] || 'john.doe@nope.com'
@website = ENV['WEBSITE'] || 'http://john.doe.org'
@now = Date.today.strftime("%d/%m/%y")
@year = Date.today.strftime("%Y")
@license_file = ENV['LICENSE']
@filename = filename
@filename_base = filename.split('.')[0]
@class_name = filename.split('.')[0].capitalize
@project = File.split( File.dirname( filepath ) ).last

def license comment=nil
    return unless File.exist? @license_file
    return File.new( @license_file ).read if comment.nil?
    license = ''
    File.new( @license_file ).each_line { |l| license+=comment+l }
    license
end

def header fn
    fp = File.join( SKELETON_DIR, fn )
    return unless File.exists? fp
    File.open( fp ) { |f| puts ERB.new( f.read, nil, '<>' ).result( binding ) }
end

File.open( skeleton ) do |f| puts ERB.new( f.read, nil, '<>' ).result( binding ) end