Class: Gravitext::DevTools::HeaderWriter

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/gravitext-devtools/header_writer.rb

Constant Summary collapse

TDIR =
File.expand_path( File.join( File.dirname( __FILE__ ),
'..', '..', 'templates' ) )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaderWriter

Returns a new instance of HeaderWriter.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gravitext-devtools/header_writer.rb', line 35

def initialize
  @verbose = false
  @do_write = false
  @license = :apache
  @inception = Time.now.year

  @git_lister = GitFileLister.new

  @inclusions = []
  @exclusions = [ '**/.gitignore',
                  '**/.gt-config',
                  '**/History.rdoc',
                  '**/History.txt',
                  '**/Manifest.static',
                  '**/Manifest.txt',
                  '**/*.jar',
                  '**/Rakefile',
                  '**/pom.xml',
                  '**/assembly.xml',
                  '*.gemspec',
                  'Gemfile*' ]

  @cached_header = {}

  Hooker.apply( [ :gt, :header ], self )
end

Instance Attribute Details

#exclusionsObject

Returns the value of attribute exclusions.



32
33
34
# File 'lib/gravitext-devtools/header_writer.rb', line 32

def exclusions
  @exclusions
end

#holderObject

Returns the value of attribute holder.



29
30
31
# File 'lib/gravitext-devtools/header_writer.rb', line 29

def holder
  @holder
end

#inceptionObject

Returns the value of attribute inception.



30
31
32
# File 'lib/gravitext-devtools/header_writer.rb', line 30

def inception
  @inception
end

#inclusionsObject

Returns the value of attribute inclusions.



33
34
35
# File 'lib/gravitext-devtools/header_writer.rb', line 33

def inclusions
  @inclusions
end

#licenseObject

Returns the value of attribute license.



31
32
33
# File 'lib/gravitext-devtools/header_writer.rb', line 31

def license
  @license
end

#verboseObject

Returns the value of attribute verbose.



28
29
30
# File 'lib/gravitext-devtools/header_writer.rb', line 28

def verbose
  @verbose
end

Instance Method Details

#cached_header(format) ⇒ Object



95
96
97
# File 'lib/gravitext-devtools/header_writer.rb', line 95

def cached_header( format )
  @cached_header[ format ] ||= gen_header( format )
end

#cached_licenseObject



105
106
107
# File 'lib/gravitext-devtools/header_writer.rb', line 105

def cached_license
  @cached_license ||= gen_license
end

#expand(template, bnd) ⇒ Object



118
119
120
121
# File 'lib/gravitext-devtools/header_writer.rb', line 118

def expand( template, bnd )
  ERB.new( template, nil, '%' ).result( bnd ).
    split( $/ ).map { |l| l.rstrip }
end

#gen_header(format) ⇒ Object



99
100
101
102
103
# File 'lib/gravitext-devtools/header_writer.rb', line 99

def gen_header( format )
  efile = File.join( TDIR, 'format', "format.#{format}" )
  license = cached_license
  expand( IO.read( efile ), binding )
end

#gen_licenseObject



109
110
111
112
113
114
115
116
# File 'lib/gravitext-devtools/header_writer.rb', line 109

def gen_license
  template = if license.is_a?( Symbol )
               IO.read( File.join( TDIR, 'license', license.to_s ) )
             else
               license.to_s
             end
  expand( template, binding )
end

#parse_options(args = ARGV) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gravitext-devtools/header_writer.rb', line 62

def parse_options( args = ARGV )

  @git_lister.parse_options( args ) do |opts|
    opts.banner = "Usage:  gt-header [dir|file] ..."
    opts.on( "-v", "--verbose",
             "Output full details" ) do
      @verbose = true
    end
    opts.on( "-w", "--write",
             "Write headers if needed" ) do
      @do_write = true
    end

  end
end

#run(args = ARGV) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gravitext-devtools/header_writer.rb', line 78

def run( args = ARGV )

  parse_options( args )

  @git_lister.inclusions = @inclusions
  @git_lister.exclusions = @exclusions

  files = @git_lister.files

  files.each do |fname|
    HeaderProcessor.new( fname, @do_write, self ).process
  end
end

#yearsObject



123
124
125
# File 'lib/gravitext-devtools/header_writer.rb', line 123

def years
  @years ||= [ inception, Time.now.year ].uniq.join( '-' )
end