Skip to content
Snippets Groups Projects
Commit 7847c4f3 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Inject MD5 hash into JSDuck JS and CSS file names.

This way when CSS or JS changes, the URL-s of those files will change
too, ensuring that that those files won't be read from cache when
docs are generated with new JSDuck version.
parent 73dc3116
No related branches found
No related tags found
No related merge requests found
require 'rubygems' require 'rubygems'
require 'rake' require 'rake'
require 'digest/md5'
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
...@@ -40,6 +41,22 @@ def yui_compress(fname) ...@@ -40,6 +41,22 @@ def yui_compress(fname)
system "java -jar $(dirname $(which sencha))/bin/yuicompressor.jar -o #{fname} #{fname}" system "java -jar $(dirname $(which sencha))/bin/yuicompressor.jar -o #{fname} #{fname}"
end 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. # Reads in all CSS files referenced between BEGIN CSS and END CSS markers.
# Deletes those input CSS files and writes out concatenated CSS to # Deletes those input CSS files and writes out concatenated CSS to
# resources/css/app.css # resources/css/app.css
...@@ -55,12 +72,13 @@ def combine_css(html, dir, opts = :write) ...@@ -55,12 +72,13 @@ def combine_css(html, dir, opts = :write)
end end
end end
fname = "#{dir}/resources/css/app.css"
if opts == :write if opts == :write
fname = "#{dir}/resources/css/app.css"
File.open(fname, 'w') {|f| f.write(css.join("\n")) } File.open(fname, 'w') {|f| f.write(css.join("\n")) }
yui_compress(fname) yui_compress(fname)
fname = md5_rename(fname)
end end
html.sub(css_section_re, '<link rel="stylesheet" href="resources/css/app.css" type="text/css" />') html.sub(css_section_re, '<link rel="stylesheet" href="resources/css/' + File.basename(fname) + '" type="text/css" />')
end end
# Same thing for JavaScript, result is written to: app.js # Same thing for JavaScript, result is written to: app.js
...@@ -80,9 +98,11 @@ def combine_js(html, dir) ...@@ -80,9 +98,11 @@ def combine_js(html, dir)
end end
fname = "#{dir}/app.js" fname = "#{dir}/app.js"
File.open(fname, 'w') {|f| f.write(js.join("\n")) } File.open(fname, 'w') {|f| f.write(js.join("\n")) }
yui_compress(fname) yui_compress(fname)
html.sub(js_section_re, '<script type="text/javascript" src="app.js"></script>') fname = md5_rename(fname)
html.sub(js_section_re, '<script type="text/javascript" src="' + File.basename(fname) + '"></script>')
end end
# Modifies HTML to link app.css. # Modifies HTML to link app.css.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment