Sha256: 3c6c0b046be6baf8e120e257782cb58c5c57fd47cf64b71cf7f36a4f3d4f6ddd

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 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, :preserve => true
          File.write new, content

          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

12 entries across 12 versions & 1 rubygems

Version Path
inochi-6.1.0 lib/inochi/generate.rb
inochi-6.0.2 lib/inochi/generate.rb
inochi-6.0.1 lib/inochi/generate.rb
inochi-5.1.0 lib/inochi/generate.rb
inochi-5.0.2 lib/inochi/generate.rb
inochi-5.0.1 lib/inochi/generate.rb
inochi-5.0.0 lib/inochi/generate.rb
inochi-4.0.1 lib/inochi/generate.rb
inochi-4.0.0 lib/inochi/generate.rb
inochi-3.0.0 lib/inochi/generate.rb
inochi-2.0.1 lib/inochi/generate.rb
inochi-2.0.0 lib/inochi/generate.rb