|
1 | 1 | /* jshint node:true */
|
2 | 2 | module.exports = function(grunt) {
|
3 | 3 | var path = require('path'),
|
| 4 | + gitorsvn = require('git-or-svn'), |
4 | 5 | SOURCE_DIR = 'src/',
|
5 | 6 | BUILD_DIR = 'build/',
|
6 | 7 | autoprefixer = require('autoprefixer'),
|
@@ -658,15 +659,79 @@ module.exports = function(grunt) {
|
658 | 659 | grunt.task.run( '_' + this.nameArgs );
|
659 | 660 | } );
|
660 | 661 |
|
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', [ |
664 | 667 | 'browserify',
|
665 | 668 | 'jshint:corejs',
|
666 | 669 | 'uglify:bookmarklet',
|
667 | 670 | 'qunit:compiled'
|
668 | 671 | ] );
|
669 | 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 | + }); |
| 734 | + |
670 | 735 | grunt.registerTask( 'copy:all', [
|
671 | 736 | 'copy:files',
|
672 | 737 | 'copy:wp-admin-css-compat-rtl',
|
|
0 commit comments