Sha256: d95318af77d4e6a41b6ac77ba48cbe90c713eb6fb8f907cb7f62a736691dfe34
Contents?: true
Size: 1.26 KB
Versions: 1
Compression:
Stored size: 1.26 KB
Contents
require 'fileutils' require 'digest/sha1' module Inochi module Generate extend self ## # Notify the user about some action being performed. # def notify action, message printf "%16s %s\n", action, message end ## # 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. # 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 end end unless File.respond_to? :write ## # Writes the given content to the given file. # def File.write path, content open(path, 'wb') {|f| f.write content } end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
inochi-6.0.0 | lib/inochi/generate.rb |