Commit e9484266 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract MD5 renaming logic into separate class.

Now it can be also reused inside JSDuck itself.
parent 7847c4f3
Loading
Loading
Loading
Loading
+4 −19
Original line number Diff line number Diff line
require 'rubygems'
require 'rake'
require 'digest/md5'

$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)

require 'jsduck/util/md5'

def os_is_windows?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
end
@@ -41,22 +42,6 @@ def yui_compress(fname)
  system "java -jar $(dirname $(which sencha))/bin/yuicompressor.jar -o #{fname} #{fname}"
end

# Calculates MD5 hash of a file and renames the file to contain the
# hash inside the filename.
def md5_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

# 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

# Reads in all CSS files referenced between BEGIN CSS and END CSS markers.
# Deletes those input CSS files and writes out concatenated CSS to
# resources/css/app.css
@@ -76,7 +61,7 @@ def combine_css(html, dir, opts = :write)
  if opts == :write
    File.open(fname, 'w') {|f| f.write(css.join("\n")) }
    yui_compress(fname)
    fname = md5_rename(fname)
    fname = JsDuck::Util::MD5.rename(fname)
  end
  html.sub(css_section_re, '<link rel="stylesheet" href="resources/css/' + File.basename(fname) + '" type="text/css" />')
end
@@ -101,7 +86,7 @@ def combine_js(html, dir)

  File.open(fname, 'w') {|f| f.write(js.join("\n")) }
  yui_compress(fname)
  fname = md5_rename(fname)
  fname = JsDuck::Util::MD5.rename(fname)
  html.sub(js_section_re, '<script type="text/javascript" src="' + File.basename(fname) + '"></script>')
end

lib/jsduck/util/md5.rb

0 → 100644
+32 −0
Original line number Diff line number Diff line
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