Class: Regenerate::SiteRegenerator

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/regenerate/site-regenerator.rb

Constant Summary collapse

REGENERATE_EXTENSIONS =
[".htm", ".html", ".xml"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#ensureDirectoryExists, #makeBackupFile

Constructor Details

#initialize(baseDir, sourceSubDir, outputSubDir) ⇒ SiteRegenerator

Returns a new instance of SiteRegenerator.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/regenerate/site-regenerator.rb', line 30

def initialize(baseDir, sourceSubDir, outputSubDir)
  
  @baseDir = File.expand_path(baseDir)
  @sourceSubDir = sourceSubDir
  @outputSubDir = outputSubDir
  @sourceTypeSubDirs = {:source => @sourceSubDir, :output => @outputSubDir}
  @sourceTypeDirs = {:source => File.join(@baseDir, @sourceSubDir), 
    :output => File.join(@baseDir, @outputSubDir)}
  @oppositeSourceType = {:source => :output, :output => :source}
  @checkNoChanges = false
  puts "SiteRegenerator, @baseDir = #{@baseDir.inspect}"
end

Instance Attribute Details

#checkNoChangesObject

Returns the value of attribute checkNoChanges.



28
29
30
# File 'lib/regenerate/site-regenerator.rb', line 28

def checkNoChanges
  @checkNoChanges
end

Instance Method Details

#checkNotSourceOnly(pathComponents) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/regenerate/site-regenerator.rb', line 43

def checkNotSourceOnly(pathComponents)
  for component in pathComponents do
    if component.start_with?("_")
      raise "Cannot regenerate source-only component #{pathComponents.join("/")}"
    end
  end
end

#copySrcToOutputFile(srcFile, outFile) ⇒ Object



53
54
55
56
# File 'lib/regenerate/site-regenerator.rb', line 53

def copySrcToOutputFile(srcFile, outFile)
  makeBackupFile(outFile)
  FileUtils.cp(srcFile, outFile, :verbose => true)
end

#regenerateFile(srcFile, pathComponents, sourceType) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/regenerate/site-regenerator.rb', line 88

def regenerateFile(srcFile, pathComponents, sourceType)
  puts "regenerateFile, srcFile = #{srcFile}, sourceType = #{sourceType.inspect}"
  outFile = File.join(@sourceTypeDirs[@oppositeSourceType[sourceType]], File.join(pathComponents))
  puts " outFile = #{outFile}"
  outFileDir = File.dirname(outFile)
  if !File.exists?(outFileDir)
    if sourceType == :output
      raise "Cannot create missing source directory #{outFileDir} - please do so manually if required"
    end
    ensureDirectoryExists(outFileDir)
  end
  if sourceType == :output
    regenerateSourceFromOutput(srcFile, pathComponents)
  elsif sourceType == :source
    regenerateFileFromSource(srcFile, pathComponents)
  end
end

#regenerateFileFromSource(srcFile, pathComponents) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/regenerate/site-regenerator.rb', line 58

def regenerateFileFromSource(srcFile, pathComponents)
  puts "regenerateFileFromSource, srcFile = #{srcFile}, pathComponents = #{pathComponents.inspect}"
  subPath = pathComponents.join("/")
  outFile = File.join(@sourceTypeDirs[:output], subPath)
  puts "  outFile = #{outFile}"
  ensureDirectoryExists(File.dirname(outFile))
  extension = File.extname(srcFile).downcase
  puts "  extension = #{extension}"
  if REGENERATE_EXTENSIONS.include? extension
    WebPage.new(srcFile).regenerateToOutputFile(outFile, checkNoChanges)
  else
    copySrcToOutputFile(srcFile, outFile)
  end
end

#regeneratePath(path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/regenerate/site-regenerator.rb', line 123

def regeneratePath(path)
  path = File.expand_path(path)
  puts "SiteRegenerator.regeneratePath, path = #{path}"
  relativePath = Pathname.new(path).relative_path_from(Pathname.new(@baseDir))
  puts " relativePath = #{relativePath}"
  relativePathComponents = relativePath.to_s.split("/")
  puts " relativePathComponents = #{relativePathComponents.inspect}"
  subDir = relativePathComponents[0]
  relativeSubDirPathComponents = relativePathComponents[1..-1]
  checkNotSourceOnly(relativeSubDirPathComponents)
  if subDir == @sourceSubDir
    regenerateSubPath(relativeSubDirPathComponents, :source)
  elsif subDir == @outputSubDir
    regenerateSubPath(relativeSubDirPathComponents, :output)
  else
    raise "Path #{path} to regenerate is not contained in #{@sourceSubDir} (source) or #{@outputSubDir} (output) sub-directory of base dir #{@baseDir}"
  end
end

#regenerateSourceFromOutput(outFile, pathComponents) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/regenerate/site-regenerator.rb', line 73

def regenerateSourceFromOutput(outFile, pathComponents)
  puts "regenerateSourceFromOutput, outFile = #{outFile}, pathComponents = #{pathComponents.inspect}"
  subPath = pathComponents.join("/")
  srcFile = File.join(@sourceTypeDirs[:source], subPath)
  puts "  srcFile = #{srcFile}"
  ensureDirectoryExists(File.dirname(srcFile))
  extension = File.extname(outFile).downcase
  puts "  extension = #{extension}"
  if REGENERATE_EXTENSIONS.include? extension
    raise "Regeneration from output not yet implemented."
  else
    copySrcToOutputFile(outFile, srcFile)
  end
end

#regenerateSubPath(pathComponents, sourceType) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/regenerate/site-regenerator.rb', line 106

def regenerateSubPath(pathComponents, sourceType)
  puts "regenerateSubPath, pathComponents = #{pathComponents.inspect}, sourceType = #{sourceType.inspect}"
  srcPath = File.join(@sourceTypeDirs[sourceType], File.join(pathComponents))
  puts " srcPath = #{srcPath}"
  if File.directory? (srcPath)
    for entry in Dir.entries(srcPath) do
      if ![".", ".."].include? entry
        if !entry.start_with?("_")
          regenerateSubPath(pathComponents + [entry], sourceType)
        end
      end
    end
  elsif File.file? (srcPath)
    regenerateFile(srcPath, pathComponents, sourceType)
  end
end