Skip to content

Commit

Permalink
Refactors the whole task
Browse files Browse the repository at this point in the history
This refactors the whole task by applying the DRY paradigma. Also it
removes the override of options through the command line because of
possible conflicts with custom options and those from other tasks.
  • Loading branch information
Artus Kolanowski committed Mar 14, 2015
1 parent 81e1565 commit f56603e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 179 deletions.
172 changes: 0 additions & 172 deletions tasks/lib/phpcs.js

This file was deleted.

69 changes: 62 additions & 7 deletions tasks/phpcs.js
@@ -1,6 +1,6 @@
/*
* grunt-phpunit
* https://github.com/SaschaGalley/grunt-phpunit
* grunt-phpcs
* https://github.com/SaschaGalley/grunt-phpcs
*
* Copyright (c) 2013 Sascha Galley
* http://xash.at
Expand All @@ -10,11 +10,66 @@

module.exports = function(grunt) {

// Internal lib.
var phpcs = require('./lib/phpcs').init(grunt);
var path = require('path'),
exec = require('child_process').exec;

var command = {
flags: {
verbose: 'v',
showSniffCodes: 's'
},
options: {
errorSeverity: 'error-severity',
extensions: 'extensions',
ignore: 'ignore',
report: 'report',
reportFile: 'report-file',
severity: 'severity',
standard: 'standard',
warningSeverity: 'warning-severity',
tabWidth: 'tab-width'
}
},
defaults = {
bin: 'phpcs',
report: 'full',
maxBuffer: 200*1024
},
done = null;

grunt.registerMultiTask('phpcs', 'Run phpunit', function() {
phpcs.setup(this);
phpcs.run();
grunt.registerMultiTask('phpcs', 'Run PHP Code Sniffer', function() {
var done = null,
options = this.options(defaults),
execute = path.normalize(options.bin),
files = [].concat.apply([], this.files.map(function(mapping) { return mapping.src; })).sort();

for (var flag in command.flags) {
if (options[flag] === true) {
execute += ' -' + command.flags[flag];
}
}

for (var option in command.options) {
if (options[option] !== undefined) {
execute += ' --' + command.options[option] + '=' + options[option];
}
}

files = files.filter(function(file, position) { return !position || file !== files[position - 1]; });

execute += ' ' + '"' + files.join('" "') + '"';

grunt.log.writeln('Checking ' + files.length + ' file' + (files.length === 1 ? '' : 's') + '...');
grunt.verbose.writeln('Executing: ' + execute);

done = this.async();

exec(execute, {maxBuffer: options.maxBuffer}, function(err, stdout, stderr) {
/* jshint -W030 */
typeof config.callback === 'function' && config.callback.call(this, err, stdout, stderr, done);
stdout && grunt.log.write(stdout);
err && !options.ignoreExitCode && grunt.fatal(err);
done();
});
});
};

0 comments on commit f56603e

Please sign in to comment.