Close #842 - Move version task to a grunt plugin

This commit is contained in:
Ben Word
2013-09-08 18:05:25 -05:00
parent 3a79ebfb9f
commit c66c20b7f2
3 changed files with 12 additions and 44 deletions

View File

@@ -48,6 +48,15 @@ module.exports = function(grunt) {
}
}
},
version: {
options: {
file: 'lib/scripts.php',
css: 'assets/css/main.min.css',
cssHandle: 'roots_main',
js: 'assets/js/scripts.min.js',
jsHandle: 'roots_scripts'
}
},
watch: {
less: {
files: [
@@ -85,12 +94,12 @@ module.exports = function(grunt) {
});
// Load tasks
grunt.loadTasks('tasks');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-recess');
grunt.loadNpmTasks('grunt-wp-version');
// Register tasks
grunt.registerTask('default', [

View File

@@ -23,6 +23,7 @@
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-uglify": "~0.2.4",
"grunt-contrib-watch": "~0.5.3",
"grunt-recess": "~0.4.0"
"grunt-recess": "~0.4.0",
"grunt-wp-version": "~0.1.0"
}
}

View File

@@ -1,42 +0,0 @@
/**
* Task: version
* Set the versions in scripts.php for CSS/JS.
*/
'use strict';
var fs = require('fs'),
path = require('path'),
crypto = require('crypto');
module.exports = function(grunt) {
grunt.registerTask('version', 'Set the versions in scripts.php for CSS/JS', function() {
var scriptsPhp = 'lib/scripts.php';
// Hash the CSS
var hashCss = md5('assets/css/main.min.css');
// Hash the JS
var hashJs = md5('assets/js/scripts.min.js');
// Update scripts.php to reference the new versions
var regexCss = /(wp_enqueue_style\('roots_main',(\s*[^,]+,){2})\s*[^\)]+\);/;
var regexJs = /(wp_register_script\('roots_scripts',(\s*[^,]+,){2})\s*[^,]+,\s*([^\)]+)\);/;
var content = grunt.file.read(scriptsPhp);
content = content.replace(regexCss, "\$1 '" + hashCss + "');");
content = content.replace(regexJs, "\$1 '" + hashJs + "', " + "\$3);");
grunt.file.write(scriptsPhp, content);
grunt.log.writeln('"' + scriptsPhp + '" updated with new CSS/JS versions.');
});
/**
* 'md5' is a basic wrapper around crypto.createHash
*/
var md5 = function(filepath) {
var hash = crypto.createHash('md5');
hash.update(fs.readFileSync(filepath));
grunt.log.write('Versioning ' + filepath + '...').ok();
return hash.digest('hex');
};
};