Class: Gravitext::DevTools::HeaderProcessor

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

Constant Summary collapse

CMAP =
{}
GOOD =
mk_tag( "GOOD ",  :green )
NONE =
mk_tag( "NONE ",  :red )
DATE =
mk_tag( "DATE ",  :cyan )
EMPTY =
mk_tag( "EMPTY", :yellow )
WROTE =
mk_tag( "WROTE", :yellow )

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname, do_write, writer) ⇒ HeaderProcessor

Returns a new instance of HeaderProcessor.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gravitext-devtools/header_writer.rb', line 144

def initialize( fname, do_write, writer )
  @cpos = 0
  @do_write = do_write
  @writer = writer
  @fname = fname
  @format = case fname
            when /\.(java|c|h)$/
              :java
            when /\.(xml|htm(l?))$/
              :xml
            when /\.rb$/
              :rb
            else
              :txt
            end
  @state = :first
end

Class Method Details

.mk_tag(name, color) ⇒ Object



132
133
134
135
136
# File 'lib/gravitext-devtools/header_writer.rb', line 132

def self.mk_tag( name, color )
  tag = name.foreground( color )
  CMAP[ tag ] = color
  tag
end

Instance Method Details



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/gravitext-devtools/header_writer.rb', line 254

def check_copyright
  passes =
    if @lines[@cline] =~ /Copyright \(c\) (\d{4})(-(\d{4}))? (\S.*)\s*$/
      ldate = $3 || $1 #last date
      ldate && ( ldate == Time.now.year.to_s ) && ( $4 == @writer.holder )
    else
      false
    end

  unless passes
    start = @lines[@cline].match( /^.*Copyright/ )
    @lines[@cline] = [ start, "(c)",
                       @writer.years, @writer.holder ].join( ' ' )
  end

  passes
end


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gravitext-devtools/header_writer.rb', line 221

def find_copyright
  @cline = nil
  @lines.each_index do |i|
    line = @lines[i]
    case @format
    when :rb
      case line
      when /^#\s+Copyright/
        @cline = i
        break
      when /^\s*$/
      when /^\s*[^#]/
        break
      end
    when :java
      case line
      when /^\s*\*\s+Copyright/
        @cline = i
        break
      when /^\s*$/
      when /^\s*[^\/\*]/
        break
      end
    else
      if line =~ /Copyright \([cC]\)/
        @cline = i
        break
      end
    end
  end
  @cline
end

#insert_headerObject



272
273
274
275
276
277
278
279
280
# File 'lib/gravitext-devtools/header_writer.rb', line 272

def insert_header
  header = @writer.cached_header( @format )
  @lines.insert( @cpos, *header )
  @cpos += header.length
  # Insert an extra line break if needed.
  unless @lines[ @cpos ] =~ /^\s*$/ || @format == :xml
    @lines.insert( @cpos, "" )
  end
end

#processObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gravitext-devtools/header_writer.rb', line 162

def process
  state = GOOD
  @lines = IO.readlines( @fname )
  if @lines.empty?
    state = EMPTY
  else
    scan_prolog
    if find_copyright
      if !check_copyright
        if @do_write
          rewrite_file
          state = WROTE
        else
          state = DATE
        end
      end
    else
      if @do_write
        insert_header
        rewrite_file
        state = WROTE
      else
        state = NONE
      end
    end
  end
  puts( "%s %s" %
        [ state, @fname.dup.foreground( CMAP[ state ] ) ] )
end

#rewrite_fileObject



192
193
194
# File 'lib/gravitext-devtools/header_writer.rb', line 192

def rewrite_file
  open( @fname, "w" ) { |fout| fout.puts( @lines ) }
end

#scan_prologObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gravitext-devtools/header_writer.rb', line 196

def scan_prolog

  if @lines[0] =~ /^#\!([^\s]+)/
    @format = :rb if $1 =~ /ruby$/
    @cpos = 1
  end

  if @lines[0] =~ /^<\?xml/
    @format = :xml
    @cpos = 1
  end

  @lines.each_index do |i|
    line = @lines[i]
    if line =~ /^#.*-\*-\s*ruby\s*-\*-/
      @format = :rb
      @cpos = i+1
      break
    else
      break if line !~ /^\s*#/
    end
  end

end