Class: Regenerate::SiteRegenerator

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

Overview

The main object representing the static website source and output directories where generation or regeneration will occur. There are three types of generation/regeneration, depending on how it is invoked:

  1. Re-generate source file or files in-place

  2. Generate output file or files from source file or files

  3. Re-generate source file or files from output file or files (this is for when output files have been directly edited,

    and when it is possible to re-generate the source)
    

Constant Summary collapse

REGENERATE_EXTENSIONS =

Extensions for types of files to be generated/regenerated

[".htm", ".html", ".xml"]
SOURCE_EXTENSIONS =
{".css" => [".scss", ".sass"]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#ensureDirectoryExists, #makeBackupFile

Constructor Details

#initialize(baseDir, sourceSubDir, outputSubDir) ⇒ SiteRegenerator

Initialise giving base directory of project, and sub-directories for source and output e.g. “/home/me/myproject”, “src” and “output”



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

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 initialized, @baseDir = #{@baseDir.inspect}"
end

Instance Attribute Details

#checkNoChangesObject

an option to check for changes and throw an error before an existing output file is changed (use this option to test that certain changes in your code _don’t_ change the result for your website)



38
39
40
# File 'lib/regenerate/site-regenerator.rb', line 38

def checkNoChanges
  @checkNoChanges
end

Instance Method Details

#checkNotSourceOnly(pathComponents) ⇒ Object

files & directories starting with “_” are not output files (they are other helper files)



55
56
57
58
59
60
61
# File 'lib/regenerate/site-regenerator.rb', line 55

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

Copy a source file directly to an output file



69
70
71
72
# File 'lib/regenerate/site-regenerator.rb', line 69

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

#regenerateFile(srcFile, pathComponents, sourceType) ⇒ Object

Regenerate (or generate) a file, either from source file or from output file



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

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

Generate an output file from a source file (pathComponents represent the path from the root source directory to the actual file)



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

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

Regenerate (or generate) from specified source file (according to whether the path is within the source or output root directory).



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/regenerate/site-regenerator.rb', line 161

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

Generate a source file from an output file (if that can be done)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/regenerate/site-regenerator.rb', line 92

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
    okToCopyBack = true
    if SOURCE_EXTENSIONS.has_key? extension
      srcExtensions = SOURCE_EXTENSIONS[extension]
      srcNameWithoutExtension = srcFile.chomp(extension)
      possibleSrcFiles = srcExtensions.map{|srcExtension| srcNameWithoutExtension + srcExtension}
      for possibleSrcFile in possibleSrcFiles
        if File.exist? possibleSrcFile
          puts "NOT COPYING #{outFile} back to source because source file #{possibleSrcFile} exists"
          okToCopyBack = false
        end
      end
    end
    if okToCopyBack
      copySrcToOutputFile(outFile, srcFile)
    end
  end
end

#regenerateSubPath(pathComponents, sourceType) ⇒ Object

Regenerate (or generated) specified sub-directory or file in sub-directory of source or output root directory (according to sourceType)



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/regenerate/site-regenerator.rb', line 142

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