Module: Inochi::Generate

Extended by:
Generate
Included in:
Engine, Generate
Defined in:
lib/inochi/generate.rb

Instance Method Summary collapse

Instance Method Details

#generate(path, content) ⇒ Object

Writes the given contents to the file at the given path. If the given path already exists, then a backup is created before invoking the given block.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inochi/generate.rb', line 20

def generate path, content # :yields: old_file, new_file, output_file
  if File.exist? path
    old_digest = Digest::SHA1.digest(File.read(path))
    new_digest = Digest::SHA1.digest(content)

    if old_digest == new_digest
      notify :skip, path
    else
      notify :update, path
      cur, old, new = path, "#{path}.old", "#{path}.new"

      FileUtils.cp cur, old
      File.write new, content
      FileUtils.chmod 0400, [old, new]

      yield old, new, cur if block_given?
    end
  else
    notify :create, path
    FileUtils.mkdir_p File.dirname(path)
    File.write path, content
  end
end

#notify(action, message) ⇒ Object

Notify the user about some action being performed.



11
12
13
# File 'lib/inochi/generate.rb', line 11

def notify action, message
  printf "%16s  %s\n", action, message
end