Skip to content

Commit 81652ab

Browse files
committed
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
1 parent 6adbde9 commit 81652ab

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

Gruntfile.js

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* jshint node:true */
22
module.exports = function(grunt) {
33
var path = require('path'),
4+
gitorsvn = require('git-or-svn'),
45
SOURCE_DIR = 'src/',
56
BUILD_DIR = 'build/',
67
autoprefixer = require('autoprefixer'),
@@ -658,15 +659,79 @@ module.exports = function(grunt) {
658659
grunt.task.run( '_' + this.nameArgs );
659660
} );
660661

661-
grunt.registerTask( 'precommit', 'Runs front-end dev/test tasks in preparation for a commit.', [
662-
'postcss:core',
663-
'imagemin:core',
662+
grunt.registerTask( 'precommit:core', [
663+
'imagemin:core'
664+
] );
665+
666+
grunt.registerTask( 'precommit:js', [
664667
'browserify',
665668
'jshint:corejs',
666669
'uglify:bookmarklet',
667670
'qunit:compiled'
668671
] );
669672

673+
grunt.registerTask( 'precommit:css', [
674+
'postcss:core'
675+
] );
676+
677+
grunt.registerTask( 'precommit:php', [
678+
'phpunit'
679+
] );
680+
681+
grunt.registerTask( 'precommit', 'Runs test and build tasks in preparation for a commit', function() {
682+
var done = this.async();
683+
684+
// Figure out what tasks to run based on what files have been modified.
685+
function enqueueTestingTasksForModifiedFiles( filesModified ) {
686+
var taskList = ['precommit:core'];
687+
if ( /.*\.js/.test( filesModified ) ) {
688+
grunt.log.write( 'JavaScript source files modified, will run JavaScript tests.\n');
689+
taskList = taskList.concat( ['precommit:js'] );
690+
}
691+
if ( /src.*\.css/.test( filesModified ) ) {
692+
grunt.log.write( 'CSS source files modified, will run CSS tests.\n');
693+
taskList = taskList.concat( ['postcss:core'] );
694+
}
695+
if ( /.*\.php/.test( filesModified ) ) {
696+
grunt.log.write( 'PHP source files modified, will run PHP tests.\n');
697+
taskList = taskList.concat( ['precommit:php'] );
698+
}
699+
grunt.task.run( taskList );
700+
done();
701+
}
702+
gitorsvn( __dirname, function(gitorsvn) {
703+
if ( gitorsvn === 'svn' ) {
704+
grunt.util.spawn(
705+
{
706+
cmd: 'svn',
707+
args: ['status']
708+
},
709+
function(error, result, code) {
710+
if ( code !== 0 ) {
711+
grunt.fail.warn( 'The `svn status` command returned a non-zero exit code.', code );
712+
}
713+
enqueueTestingTasksForModifiedFiles( result.stdout );
714+
}
715+
);
716+
} else if ( gitorsvn === 'git' ) {
717+
grunt.util.spawn(
718+
{
719+
cmd: 'git',
720+
args: ['diff', '--name-only']
721+
},
722+
function(error, result, code) {
723+
if ( code !== 0 ) {
724+
grunt.fail.warn( 'The `git diff --name-only` command returned a non-zero exit code.', code );
725+
}
726+
enqueueTestingTasksForModifiedFiles( result.stdout );
727+
}
728+
);
729+
} else {
730+
grunt.log.write( 'This WordPress install is not under version control, no tests will be run.' );
731+
}
732+
});
733+
});
734+
670735
grunt.registerTask( 'copy:all', [
671736
'copy:files',
672737
'copy:wp-admin-css-compat-rtl',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "GPL-2.0+",
1111
"devDependencies": {
1212
"autoprefixer": "~6.3.3",
13+
"git-or-svn": "~0.1.0",
1314
"grunt": "~0.4.5",
1415
"grunt-browserify": "~4.0.1",
1516
"grunt-contrib-clean": "~1.0.0",

0 commit comments

Comments
 (0)