From 355ca421b4ddb52be98f13ab475fd2caf298d055 Mon Sep 17 00:00:00 2001 From: Andreas Lappe Date: Thu, 11 Jul 2013 20:51:30 +0200 Subject: [PATCH] Initial commit --- README.md | 104 +++++++++++++++++++++++++++++++++++++ package.json | 25 +++++++++ src/tasks/lib/phpmd.coffee | 55 ++++++++++++++++++++ src/tasks/phpmd.coffee | 14 +++++ tasks/lib/phpmd.js | 80 ++++++++++++++++++++++++++++ tasks/phpmd.js | 17 ++++++ 6 files changed, 295 insertions(+) create mode 100644 README.md create mode 100644 package.json create mode 100644 src/tasks/lib/phpmd.coffee create mode 100644 src/tasks/phpmd.coffee create mode 100644 tasks/lib/phpmd.js create mode 100644 tasks/phpmd.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..e7f7e15 --- /dev/null +++ b/README.md @@ -0,0 +1,104 @@ +# grunt-phpmd + +> Grunt plugin for running [PHP Mess Detector](http://phpmd.org). + +_This plugin is developed for Grunt `0.4.0` and is not tested for backward compatibility with Grunt `0.3.x`._ + +## Getting Started + +1. Install this grunt plugin with the follwing command: + +```shell +npm install grunt-phpmd --save-dev +``` + +2. [Install PHPMD](http://phpmd.org/download/index.html) + +3. Add this to your project's `Gruntfile.js`: + +```js +grunt.loadNpmTasks('grunt-phpmd'); +``` + +## PHPMD task + +_Run this task with the `grunt phpmd` command._ + +_This task is a [multi task][] so any targets, files and options should be specified according to the [multi task][] documentation._ + +[multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks + +### Usage Example + +```js +phpmd: { + application: { + dir: 'application' + } + options: { + rulesets: 'codesize' + } +} +``` + +### Target Properties + +#### dir +Type: `String` + +The file or directory where phpmd should search for files. + +### Options +#### bin +Type: `String` +Default: `'phpmd'` + +The binary name if it is in your PATH or the full path if not. + +#### reportFormat +Type: `String` +Default: `'xml'` + +The type of report to generate. PHPMD supports `xml`, `text` and `html`. + +#### reportFile +Type: `String` +Default: `false` + +Set a path and filename here to write to a file, otherwise it will write to stdout. + +#### suffixes +Type: `String` +Default: `false` + +Set a comma-separated string of valid source code filename extensions. + +#### exclude +Type: `String` +Default: `false` + +Set a comma-separated string of patterns that are used to ignore directories. + +#### minimumPriority +Type: `Number` +Default: `false` + +Rule priority threshold: rules with lower priority than this will not be used. + +#### strict +Type: `Boolean` +Default: `false` + +Also report those nodes with a @SuppressWarnings annotation. + +#### rulesets +Type: `String` +Default: `'codesize,unusedcode,naming'` + +A ruleset filename or a comma-separated string of rulesetfilenames. Make sure to have no spaces between the items! + +#### maxBuffer +Type: `Number` +Default: `200*1024` + +Override the maxBuffer-Size of nodejs's exec() function if you expect a long output on stdout. diff --git a/package.json b/package.json new file mode 100644 index 0000000..17a2d9d --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "grunt-phpmd", + "version": "0.1.0", + "description": "Grunt plugin for running PHP Mess Detector.", + "main": "tasks/phpmd.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/alappe/grunt-phpmd.git" + }, + "keywords": [ + "gruntplugin", + "phpmd", + "mess-detection", + "ci", + "continous-integration" + ], + "author": "Andreas Lappe", + "license": "BSD", + "bugs": { + "url": "https://github.com/alappe/grunt-phpmd/issues" + } +} diff --git a/src/tasks/lib/phpmd.coffee b/src/tasks/lib/phpmd.coffee new file mode 100644 index 0000000..a98c0c2 --- /dev/null +++ b/src/tasks/lib/phpmd.coffee @@ -0,0 +1,55 @@ +### +grunt-phpmd + +Copyright (c) 2013 Andreas Lappe +http://kaeufli.ch +Licensed under the BSD license. +### + +path = require 'path' +exec = (require 'child_process').exec + +exports.init = (grunt) -> + + exports = config = {} + cmd = done = null + defaults = + bin: 'phpmd' + # Can be xml, text or html + reportFormat: 'xml' + # Path and filename, otherwise STDOUT is used + reportFile: false + suffixes: false + exclude: false + minimumPriority: false + strict: false + rulesets: 'codesize,unusedcode,naming' + maxBuffer: 200*1024 + + buildCommand = (dir) -> + cmd = "#{path.normalize config.bin} #{dir} #{config.reportFormat} #{config.rulesets}" + cmd += " --minimumpriority #{config.minimumPriority}" if config.minimumPriority + cmd += " --reportfile #{config.reportFile}" if config.reportFile + cmd += " --suffixes #{config.suffixes}" if config.suffixes + cmd += " --exclude #{config.exclude}" if config.exclude + cmd += " --strict" if config.strict + cmd + + exports.setup = (runner) -> + dir = path.normalize runner.data.dir + config = runner.options defaults + cmd = buildCommand dir + grunt.log.writeln "Starting phpmd (target: #{runner.target.cyan}) in #{dir.cyan}" + grunt.verbose.writeln "Execute: #{cmd}" + done = runner.async() + + exports.run = -> + cmdOptions = maxBuffer: config.maxBuffer + exec cmd, cmdOptions, (err, stdout, stderr) -> + grunt.log.write stdout if stdout + if err + # As documented on # http://phpmd.org/documentation/index.html#exit-codes + grunt.fatal err if err.code isnt 2 + done() + + exports diff --git a/src/tasks/phpmd.coffee b/src/tasks/phpmd.coffee new file mode 100644 index 0000000..92a633f --- /dev/null +++ b/src/tasks/phpmd.coffee @@ -0,0 +1,14 @@ +### +grunt-phpmd + +Copyright (c) 2013 Andreas Lappe +http://kaeufli.ch +Licensed under the BSD license. +### + +module.exports = (grunt) -> + phpmd = (require './lib/phpmd').init grunt + + grunt.registerMultiTask 'phpmd', 'Run phpmd', -> + phpmd.setup @ + phpmd.run() diff --git a/tasks/lib/phpmd.js b/tasks/lib/phpmd.js new file mode 100644 index 0000000..390e2fc --- /dev/null +++ b/tasks/lib/phpmd.js @@ -0,0 +1,80 @@ +// Generated by CoffeeScript 1.6.2 +/* +grunt-phpmd + +Copyright (c) 2013 Andreas Lappe +http://kaeufli.ch +Licensed under the BSD license. +*/ + +var exec, path; + +path = require('path'); + +exec = (require('child_process')).exec; + +exports.init = function(grunt) { + var buildCommand, cmd, config, defaults, done, exports; + + exports = config = {}; + cmd = done = null; + defaults = { + bin: 'phpmd', + reportFormat: 'xml', + reportFile: false, + suffixes: false, + exclude: false, + minimumPriority: false, + strict: false, + rulesets: 'codesize,unusedcode,naming', + maxBuffer: 200 * 1024 + }; + buildCommand = function(dir) { + cmd = "" + (path.normalize(config.bin)) + " " + dir + " " + config.reportFormat + " " + config.rulesets; + if (config.minimumPriority) { + cmd += " --minimumpriority " + config.minimumPriority; + } + if (config.reportFile) { + cmd += " --reportfile " + config.reportFile; + } + if (config.suffixes) { + cmd += " --suffixes " + config.suffixes; + } + if (config.exclude) { + cmd += " --exclude " + config.exclude; + } + if (config.strict) { + cmd += " --strict"; + } + return cmd; + }; + exports.setup = function(runner) { + var dir; + + dir = path.normalize(runner.data.dir); + config = runner.options(defaults); + cmd = buildCommand(dir); + grunt.log.writeln("Starting phpmd (target: " + runner.target.cyan + ") in " + dir.cyan); + grunt.verbose.writeln("Execute: " + cmd); + return done = runner.async(); + }; + exports.run = function() { + var cmdOptions; + + cmdOptions = { + maxBuffer: config.maxBuffer + }; + return exec(cmd, cmdOptions, function(err, stdout, stderr) { + if (stdout) { + grunt.log.write(stdout); + } + if (err) { + if (err.code !== 2) { + grunt.fatal(err); + } + } + return done(); + }); + }; + return exports; +}; diff --git a/tasks/phpmd.js b/tasks/phpmd.js new file mode 100644 index 0000000..76915b3 --- /dev/null +++ b/tasks/phpmd.js @@ -0,0 +1,17 @@ +// Generated by CoffeeScript 1.6.2 +/* +grunt-phpmd + +Copyright (c) 2013 Andreas Lappe +http://kaeufli.ch +Licensed under the BSD license. +*/ +module.exports = function(grunt) { + var phpmd; + + phpmd = (require('./lib/phpmd')).init(grunt); + return grunt.registerMultiTask('phpmd', 'Run phpmd', function() { + phpmd.setup(this); + return phpmd.run(); + }); +};