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