Make WordPress Core

Changeset 36906


Ignore:
Timestamp:
03/09/2016 08:54:10 PM (11 years ago)
Author:
jorbin
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Gruntfile.js

    r36780 r36906  
    22module.exports = function(grunt) {
    33        var path = require('path'),
     4                gitorsvn = require('git-or-svn'),
    45                SOURCE_DIR = 'src/',
    56                BUILD_DIR = 'build/',
    … …  
    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',
    … …  
    667670                'qunit:compiled'
    668671        ] );
     672
     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        });
    669734
    670735        grunt.registerTask( 'copy:all', [
  • trunk/package.json

    r36865 r36906  
    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",
Note: See TracChangeset for help on using the changeset viewer.