Sha256: 0accfba940ed0bc046c7873efd9182b403530552402a81396712323c95b23591

Contents?: true

Size: 884 Bytes

Versions: 45

Compression:

Stored size: 884 Bytes

Contents

require 'digest/md5'
require 'jsduck/util/singleton'

module JsDuck
  module Util

    # Helper to rename files so that the MD5 hash of their contents is
    # placed into their name.
    class MD5
      include Util::Singleton

      # Calculates MD5 hash of a file and renames the file to contain the
      # hash inside the filename.  Returns the new name of the file.
      def rename(fname)
        hash = Digest::MD5.file(fname).hexdigest
        hashed_name = inject_hash_to_filename(fname, hash)
        File.rename(fname, hashed_name)
        return hashed_name
      end

      private

      # Given filename "foo/bar.js" and hash "HASH" produces "foo/bar-HASH.js"
      def inject_hash_to_filename(fname, hash)
        parts = File.basename(fname).split(/\./)
        parts[0] += "-" + hash
        File.dirname(fname) + "/" + parts.join(".")
      end
    end

  end
end

Version data entries

45 entries across 45 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/util/md5.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/util/md5.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/util/md5.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/util/md5.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/util/md5.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/util/md5.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.10 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.9 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.8 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.7 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.5 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.4 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.3 lib/jsduck/util/md5.rb
jsduck-troopjs-0.0.1 lib/jsduck/util/md5.rb
jsduck-6.0.0beta lib/jsduck/util/md5.rb
jsduck-5.3.4 lib/jsduck/util/md5.rb
jsduck-5.3.3 lib/jsduck/util/md5.rb
jsduck-5.3.2 lib/jsduck/util/md5.rb
jsduck-5.3.1 lib/jsduck/util/md5.rb