From 81652abd8ba72c4c0ed9b2fabbd38bb7e019c6b7 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Wed, 9 Mar 2016 20:54:10 +0000 Subject: [PATCH] Improve grunt precommit task Instead of running all tasks, all the time, let's run tasks based on the files changed. PHPUNIT is now a precommit task for all php file changes. This adds a new dependency. Please run `npm install`. Fixes #35557 Props ericlewis, netweb, jorbin git-svn-id: https://develop.svn.wordpress.org/trunk@36906 602fd350-edb4-49c9-b593-d223f7449a82 --- Gruntfile.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++--- package.json | 1 + 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 564b1eba9c..001df84499 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,6 +1,7 @@ /* jshint node:true */ module.exports = function(grunt) { var path = require('path'), + gitorsvn = require('git-or-svn'), SOURCE_DIR = 'src/', BUILD_DIR = 'build/', autoprefixer = require('autoprefixer'), @@ -658,15 +659,79 @@ module.exports = function(grunt) { grunt.task.run( '_' + this.nameArgs ); } ); - grunt.registerTask( 'precommit', 'Runs front-end dev/test tasks in preparation for a commit.', [ - 'postcss:core', - 'imagemin:core', + grunt.registerTask( 'precommit:core', [ + 'imagemin:core' + ] ); + + grunt.registerTask( 'precommit:js', [ 'browserify', 'jshint:corejs', 'uglify:bookmarklet', 'qunit:compiled' ] ); + grunt.registerTask( 'precommit:css', [ + 'postcss:core' + ] ); + + grunt.registerTask( 'precommit:php', [ + 'phpunit' + ] ); + + grunt.registerTask( 'precommit', 'Runs test and build tasks in preparation for a commit', function() { + var done = this.async(); + + // Figure out what tasks to run based on what files have been modified. + function enqueueTestingTasksForModifiedFiles( filesModified ) { + var taskList = ['precommit:core']; + if ( /.*\.js/.test( filesModified ) ) { + grunt.log.write( 'JavaScript source files modified, will run JavaScript tests.\n'); + taskList = taskList.concat( ['precommit:js'] ); + } + if ( /src.*\.css/.test( filesModified ) ) { + grunt.log.write( 'CSS source files modified, will run CSS tests.\n'); + taskList = taskList.concat( ['postcss:core'] ); + } + if ( /.*\.php/.test( filesModified ) ) { + grunt.log.write( 'PHP source files modified, will run PHP tests.\n'); + taskList = taskList.concat( ['precommit:php'] ); + } + grunt.task.run( taskList ); + done(); + } + gitorsvn( __dirname, function(gitorsvn) { + if ( gitorsvn === 'svn' ) { + grunt.util.spawn( + { + cmd: 'svn', + args: ['status'] + }, + function(error, result, code) { + if ( code !== 0 ) { + grunt.fail.warn( 'The `svn status` command returned a non-zero exit code.', code ); + } + enqueueTestingTasksForModifiedFiles( result.stdout ); + } + ); + } else if ( gitorsvn === 'git' ) { + grunt.util.spawn( + { + cmd: 'git', + args: ['diff', '--name-only'] + }, + function(error, result, code) { + if ( code !== 0 ) { + grunt.fail.warn( 'The `git diff --name-only` command returned a non-zero exit code.', code ); + } + enqueueTestingTasksForModifiedFiles( result.stdout ); + } + ); + } else { + grunt.log.write( 'This WordPress install is not under version control, no tests will be run.' ); + } + }); + }); + grunt.registerTask( 'copy:all', [ 'copy:files', 'copy:wp-admin-css-compat-rtl', diff --git a/package.json b/package.json index 69fc47cff5..f0293fb5d8 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "GPL-2.0+", "devDependencies": { "autoprefixer": "~6.3.3", + "git-or-svn": "~0.1.0", "grunt": "~0.4.5", "grunt-browserify": "~4.0.1", "grunt-contrib-clean": "~1.0.0",