regenerate 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,7 @@ module Regenerate
12
12
  puts "Renaming file #{outFile} to #{backupFileName} ..."
13
13
  File.rename(outFile, backupFileName)
14
14
  end
15
+ backupFileName
15
16
  end
16
17
 
17
18
  def ensureDirectoryExists(directoryName)
@@ -25,7 +25,10 @@ module Regenerate
25
25
 
26
26
  include Regenerate::Utils
27
27
 
28
+ attr_accessor :checkNoChanges
29
+
28
30
  def initialize(baseDir, sourceSubDir, outputSubDir)
31
+
29
32
  @baseDir = File.expand_path(baseDir)
30
33
  @sourceSubDir = sourceSubDir
31
34
  @outputSubDir = outputSubDir
@@ -33,6 +36,7 @@ module Regenerate
33
36
  @sourceTypeDirs = {:source => File.join(@baseDir, @sourceSubDir),
34
37
  :output => File.join(@baseDir, @outputSubDir)}
35
38
  @oppositeSourceType = {:source => :output, :output => :source}
39
+ @checkNoChanges = false
36
40
  puts "SiteRegenerator, @baseDir = #{@baseDir.inspect}"
37
41
  end
38
42
 
@@ -60,7 +64,7 @@ module Regenerate
60
64
  extension = File.extname(srcFile).downcase
61
65
  puts " extension = #{extension}"
62
66
  if REGENERATE_EXTENSIONS.include? extension
63
- WebPage.new(srcFile).regenerateToOutputFile(outFile)
67
+ WebPage.new(srcFile).regenerateToOutputFile(outFile, checkNoChanges)
64
68
  else
65
69
  copySrcToOutputFile(srcFile, outFile)
66
70
  end
@@ -171,7 +171,7 @@ module Regenerate
171
171
  class ParseException<Exception
172
172
  end
173
173
 
174
- class ParsedRejennerCommentLine
174
+ class ParsedRegenerateCommentLine
175
175
 
176
176
  attr_reader :isInstanceVar, :hasCommentStart, :hasCommentEnd, :sectionStart, :sectionEnd
177
177
  attr_reader :isEmptySection, :line, :name, :value
@@ -229,6 +229,9 @@ module Regenerate
229
229
 
230
230
  end
231
231
 
232
+ class UnexpectedChangeError<Exception
233
+ end
234
+
232
235
  class WebPage
233
236
 
234
237
  include Regenerate::Utils
@@ -356,8 +359,37 @@ module Regenerate
356
359
  end
357
360
  end
358
361
 
359
- def writeRegeneratedFile(outFile)
360
- makeBackupFile(outFile)
362
+ def diffReport(newString, oldString)
363
+ i = 0
364
+ minLength = [newString.length, oldString.length].min
365
+ while i<minLength and newString[i] == oldString[i] do
366
+ i += 1
367
+ end
368
+ diffPos = i
369
+ newStringEndPos = [diffPos+20,newString.length].min
370
+ oldStringEndPos = [diffPos+20, newString.length].min
371
+ startPos = [0, diffPos-10].max
372
+ "Different from position #{diffPos}: \n #{newString[startPos...newStringEndPos].inspect}\n !=\n #{oldString[startPos...newStringEndPos].inspect}"
373
+ end
374
+
375
+ def checkOutputFileUnchanged(outFile, oldFile)
376
+ if File.exists? oldFile
377
+ oldFileContents = File.read(oldFile)
378
+ newFileContents = File.read(outFile)
379
+ if oldFileContents != newFileContents
380
+ newFileName = outFile + ".new"
381
+ File.rename(outFile, newFileName)
382
+ File.rename(oldFile, outFile)
383
+ raise UnexpectedChangeError.new("New file #{newFileName} is different from old file #{outFile}: #{diffReport(newFileContents,oldFileContents)}")
384
+ end
385
+ else
386
+ raise UnexpectedChangeError.new("Can't check #{outFile} against backup #{oldFile} " +
387
+ "because backup file doesn't exist")
388
+ end
389
+ end
390
+
391
+ def writeRegeneratedFile(outFile, checkNoChanges)
392
+ backupFileName = makeBackupFile(outFile)
361
393
  puts "Outputting regenerated page to #{outFile} ..."
362
394
  File.open(outFile, "w") do |f|
363
395
  for component in @components do
@@ -365,6 +397,9 @@ module Regenerate
365
397
  end
366
398
  end
367
399
  puts "Finished writing #{outFile}"
400
+ if checkNoChanges
401
+ checkOutputFileUnchanged(outFile, backupFileName)
402
+ end
368
403
  end
369
404
 
370
405
  def readFileLines
@@ -376,7 +411,7 @@ module Regenerate
376
411
  #puts "line #{lineNumber}: #{line}"
377
412
  commentLineMatch = COMMENT_LINE_REGEX.match(line)
378
413
  if commentLineMatch
379
- parsedCommandLine = ParsedRejennerCommentLine.new(line, commentLineMatch)
414
+ parsedCommandLine = ParsedRegenerateCommentLine.new(line, commentLineMatch)
380
415
  #puts "parsedCommandLine = #{parsedCommandLine}"
381
416
  if parsedCommandLine.isRejennerCommentLine
382
417
  parsedCommandLine.checkIsValid
@@ -398,9 +433,9 @@ module Regenerate
398
433
  #display
399
434
  end
400
435
 
401
- def regenerateToOutputFile(outFile)
436
+ def regenerateToOutputFile(outFile, checkNoChanges = false)
402
437
  executeRubyComponents
403
- writeRegeneratedFile(outFile)
438
+ writeRegeneratedFile(outFile, checkNoChanges)
404
439
  end
405
440
 
406
441
  def executeRubyComponents
@@ -441,6 +476,13 @@ module Regenerate
441
476
  end
442
477
  end
443
478
 
479
+ def erbFromString(templateString)
480
+ @binding = binding
481
+ template = ERB.new(templateString, nil, nil)
482
+ template.result(@binding)
483
+ end
484
+
485
+
444
486
  def relative_path(path)
445
487
  File.expand_path(File.join(@baseDir, path.to_str))
446
488
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regenerate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-27 00:00:00.000000000 Z
12
+ date: 2013-06-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Use to regenerate to write a web page with embedded instance variable
15
15
  definitions and embedded ruby code which executes to regenerate the same web page