From c66c20b7f28ddee3e06a691b4a13bcd9fd1a65fd Mon Sep 17 00:00:00 2001 From: Ben Word Date: Sun, 8 Sep 2013 18:05:25 -0500 Subject: [PATCH] Close #842 - Move version task to a grunt plugin --- Gruntfile.js | 11 ++++++++++- package.json | 3 ++- tasks/version.js | 42 ------------------------------------------ 3 files changed, 12 insertions(+), 44 deletions(-) delete mode 100644 tasks/version.js diff --git a/Gruntfile.js b/Gruntfile.js index d09a127..8f4fad1 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', [ diff --git a/package.json b/package.json index 26b835b..8ec0ca0 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/tasks/version.js b/tasks/version.js deleted file mode 100644 index 2dc5c64..0000000 --- a/tasks/version.js +++ /dev/null @@ -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'); - }; -};