Skip to content

Commit

Permalink
Refactoring into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cryer committed Feb 24, 2013
1 parent bd1686f commit fe58a02
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 81 deletions.
10 changes: 5 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({

jshint: {
all: [
'Gruntfile.js',
Expand All @@ -22,21 +22,21 @@ module.exports = function(grunt) {
jshintrc: '.jshintrc'
}
},

phpcs: {
application: {
dir: 'application/classes'
dir: 'php/modules'
},
options: {
bin: 'vendor/bin/phpcs',
standard: 'Zend'
standard: 'PSR2'
}
}
});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

grunt.loadNpmTasks('grunt-contrib-jshint');

// By default, lint and run all tests.
Expand Down
47 changes: 47 additions & 0 deletions php/modules/Grunt/PhpCs/World.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Sample class for tests of grunt-phpcs runner
*
* @package Grunt\PhpCs
*/
namespace Grunt\PhpCs;

class World
{

/**
*
* @var string
*/
private $name = '';

/**
* Constructor
*
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}

/**
* Sets the world's name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* Returns world's name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
110 changes: 110 additions & 0 deletions tasks/lib/phpcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* grunt-phpunit
* https://github.com/SaschaGalley/grunt-phpunit
*
* Copyright (c) 2013 Sascha Galley
* http://xash.at
* Licensed under the MIT license.
*/
'use strict';

// External libs.
var path = require('path');
var exec = require('child_process').exec;

exports.init = function(grunt) {

var exports = {},
defaults = {
// Default options
bin: 'phpcs',
debug: false,
extensions: false,
ignore: false,
severity: false,
standard: false,
verbose: false
},
cmd = null,
done = null,
config = {};

/**
* Builds phpunit command
*
* @return string
*/
var buildCommand = function(dir) {

var cmd = path.normalize(config.bin);

if (grunt.option('debug') || config.debug === true) {
// Display debbuging information during test execution.
cmd += ' --debug';
}

if (grunt.option('extensions') || config.extensions) {
// A comma separated list of file extensions to check
cmd += ' --extensions=' + config.extensions;
}

if (grunt.option('ignore') || config.ignore) {
// A comma separated list of patterns to ignore files and directories.
cmd += ' --ignore=' + config.ignore;
}

if (grunt.option('severity') || config.severity) {
// The minimum severity required to display an error or warning
cmd += ' --severity=' + config.severity;
}

if (grunt.option('verbose') || config.standard) {
// Define the code sniffer standard.
cmd += ' --standard=' + config.standard;
}

if (grunt.option('verbose') || config.verbose === true) {
// Output more verbose information.
cmd += ' -v';
}
return cmd;
};

/**
* Setup task before running it
*
* @param Object runner
*/
exports.setup = function(runner) {

var dir = path.normalize(runner.data.dir);
config = runner.options(defaults);
cmd = buildCommand(dir) + ' ' + dir;

grunt.log.writeln('Starting phpcs (target: ' + runner.target.cyan + ') in ' + dir.cyan);
grunt.verbose.writeln('Exec: ' + cmd);

done = runner.async();
};

/**
* Runs phpunit command with options
*
*/
exports.run = function() {

exec(cmd, function(err, stdout, stderr) {

if (stdout) {
grunt.log.write(stdout);
}

if (err) {
grunt.fatal(err);
}
done();
});
};

return exports;
};
84 changes: 8 additions & 76 deletions tasks/phpcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,15 @@
* http://xash.at
* Licensed under the MIT license.
*/
module.exports = function(grunt) {
'use strict';
'use strict';

var _ = grunt.util._;

var path = require('path');
var exec = require('child_process').exec;

grunt.registerMultiTask( 'phpcs', 'Run phpunit', function() {

var done = this.async();

// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
// Default options
bin: 'phpcs',
debug: false,
extensions: false,
ignore: false,
severity: false,
standard: false,
verbose: false
});

// Normalize dir and cmd.
var dir = path.normalize(this.data.dir);
var cmd = path.normalize(options.bin);

if (grunt.option('debug') || options.debug === true) {
// Display debbuging information during test execution.
cmd += ' --debug';
}

if (grunt.option('extensions') || options.extensions) {
// A comma separated list of file extensions to check
cmd += ' --extensions=' + options.extensions;
}

if (grunt.option('ignore') || options.ignore) {
// A comma separated list of patterns to ignore files and directories.
cmd += ' --ignore=' + options.ignore;
}

if (grunt.option('severity') || options.severity) {
// The minimum severity required to display an error or warning
cmd += ' --severity=' + options.severity;
}

if (grunt.option('verbose') || options.standard) {
// Define the code sniffer standard.
cmd += ' --standard=' + options.standard;
}

if (grunt.option('verbose') || options.verbose === true) {
// Output more verbose information.
cmd += ' -v';
}

// Set working directory.
cmd += ' ' + dir;

grunt.log.writeln('Starting phpcs (target: ' + this.target.cyan + ') in ' + dir.cyan);
grunt.verbose.writeln('Exec: ' + cmd);

// Execute phpunit command.
exec(cmd, function( err, stdout, stderr) {

if (stdout) {
grunt.log.write(stdout);
}
module.exports = function(grunt) {

if (err) {
grunt.fatal(err);
}
// Internal lib.
var phpcs = require('./lib/phpcs').init(grunt);

done();
});
});
grunt.registerMultiTask('phpcs', 'Run phpunit', function() {
phpcs.setup(this);
phpcs.run();
});
};

0 comments on commit fe58a02

Please sign in to comment.