1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/BuildException.php';
require_once 'phing/Project.php';
require_once 'phing/Task.php';
require_once 'phing/tasks/ext/sonar/SonarConfigurationFileParser.php';
require_once 'phing/tasks/ext/sonar/SonarProperty.php';
/**
* Runs SonarQube Scanner.
*
* @author Bernhard Mendl <mail@bernhard-mendl.de>
* @package phing.tasks.ext.sonar
* @see http://www.sonarqube.org
*/
class SonarTask extends Task
{
const EXIT_SUCCESS = 0;
/**
*
* @var string|null
*/
private $executable = null;
/**
*
* @var string
*/
private $errors = 'false';
/**
*
* @var string
*/
private $debug = 'false';
/**
*
* @var string|null
*/
private $configuration = null;
/**
*
* @var array Nested *Property* elements.
* @see Property
*/
private $propertyElements = array();
/**
* The command-line options passed to the SonarQube Scanner executable.
*
* @var array
*/
private $commandLineOptions = array();
/**
* Map containing SonarQube's "analysis parameters".
*
* Map keys are SonarQube parameter names. Map values are parameter values.
* See {@link http://docs.sonarqube.org/display/SONAR/Analysis+Parameters}.
*
* @var array
*/
private $properties = array();
/**
* Sets the path of the SonarQube Scanner executable.
*
* If the SonarQube Scanner is included in the PATH environment variable,
* the file name is sufficient.
*
* @param string $executable
* @return void
*/
public function setExecutable($executable)
{
$this->executable = (string) $executable;
$message = sprintf("Set executable to [%s].", $this->executable);
$this->log($message, Project::MSG_DEBUG);
}
/**
* Sets or unsets the "--errors" flag of SonarQube Scanner.
*
* @param string $errors
* Allowed values are "true"/"false", "yes"/"no", or "1"/"0".
* @return void
*/
public function setErrors($errors)
{
$this->errors = strtolower((string) $errors);
$message = sprintf("Set errors flag to [%s].", $this->errors);
$this->log($message, Project::MSG_DEBUG);
}
/**
* Sets or unsets the "--debug" flag of SonarQube Scanner.
*
* @param string $debug
* Allowed values are "true"/"false", "yes"/"no", or "1"/"0".
* @return void
*/
public function setDebug($debug)
{
$this->debug = strtolower((string) $debug);
$message = sprintf("Set debug flag to [%s].", $this->debug);
$this->log($message, Project::MSG_DEBUG);
}
/**
* Sets the path of a configuration file for SonarQube Scanner.
*
* @param string $configuration
* @return void
*/
public function setConfiguration($configuration)
{
$this->configuration = (string) $configuration;
$message = sprintf("Set configuration to [%s].", $this->configuration);
$this->log($message, Project::MSG_DEBUG);
}
/**
* Adds a nested Property element.
*
* @param SonarProperty $property
* @return void
*/
public function addProperty(SonarProperty $property)
{
$this->propertyElements[] = $property;
$message = sprintf("Added property: [%s] = [%s].", $property->getName(), $property->getValue());
$this->log($message, Project::MSG_DEBUG);
}
/**
*
* {@inheritdoc}
*
* @see Task::init()
*/
public function init()
{
$this->checkExecAllowed();
}
/**
*
* {@inheritdoc}
*
* @see Task::main()
*/
public function main()
{
$this->validateErrors();
$this->validateDebug();
$this->validateConfiguration();
$this->validateProperties();
$this->validateExecutable();
$command = sprintf('%s %s', escapeshellcmd($this->executable), $this->constructOptionsString());
$message = sprintf('Executing: [%s]', $command);
$this->log($message, Project::MSG_VERBOSE);
exec($command, $output, $returnCode);
foreach ($output as $line) {
$this->log($line);
}
if ($returnCode !== self::EXIT_SUCCESS) {
throw new BuildException('Execution of SonarQube Scanner failed.');
}
}
/**
* Constructs command-line options string for SonarQube Scanner.
*
* @return string
*/
private function constructOptionsString()
{
$options = implode(' ', $this->commandLineOptions);
foreach ($this->properties as $name => $value) {
$arg = sprintf('%s=%s', $name, $value);
$options .= ' -D ' . escapeshellarg($arg);
}
return $options;
}
/**
* Check whether PHP function 'exec()' is available.
*
* @throws BuildException
* @return void
*/
private function checkExecAllowed()
{
if (! function_exists('exec') || ! is_callable('exec')) {
$message = 'Cannot execute SonarQube Scanner because calling PHP function exec() is not permitted by PHP configuration.';
throw new BuildException($message);
}
}
/**
*
* @throws BuildException
* @return void
*/
private function validateExecutable()
{
if (($this->executable === null) || ($this->executable === '')) {
$message = 'You must specify the path of the SonarQube Scanner using the "executable" attribute.';
throw new BuildException($message);
}
// Note that executable is used as argument here.
$escapedExecutable = escapeshellarg($this->executable);
if ($this->isWindows()) {
$message = 'Assuming a Windows system. Looking for SonarQube Scanner ...';
$command = 'where ' . $escapedExecutable;
} else {
$message = 'Assuming a Linux or Mac system. Looking for SonarQube Scanner ...';
$command = 'which ' . $escapedExecutable;
}
$this->log($message, Project::MSG_VERBOSE);
unset($output);
exec($command, $output, $returnCode);
if ($returnCode !== self::EXIT_SUCCESS) {
$message = sprintf('Cannot find SonarQube Scanner: [%s].', $this->executable);
throw new BuildException($message);
}
// Verify that executable is indeed SonarQube Scanner ...
$escapedExecutable = escapeshellcmd($this->executable);
unset($output);
exec($escapedExecutable . ' --version', $output, $returnCode);
if ($returnCode !== self::EXIT_SUCCESS) {
$message = sprintf('Could not check version string. Executable appears not to be SonarQube Scanner: [%s].', $this->executable);
throw new BuildException($message);
}
$isOk = false;
foreach ($output as $line) {
if (preg_match('/SonarQube Scanner [0-9]+\\.[0-9]+/', $line) === 1) {
$isOk = true;
break;
}
}
if ($isOk) {
$message = sprintf('Found SonarQube Scanner: [%s].', $this->executable);
$this->log($message, Project::MSG_VERBOSE);
} else {
$message = sprintf('Could not find name of SonarQube Scanner in version string. Executable appears not to be SonarQube Scanner: [%s].', $this->executable);
throw new BuildException($message);
}
}
/**
*
* @throws BuildException
* @return void
*/
private function validateErrors()
{
if (($this->errors === '1') || ($this->errors === 'true') || ($this->errors === 'yes')) {
$errors = true;
} elseif (($this->errors === '0') || ($this->errors === 'false') || ($this->errors === 'no')) {
$errors = false;
} else {
throw new BuildException('Expected a boolean value.');
}
if ($errors) {
$this->commandLineOptions[] = '--errors';
}
}
/**
*
* @throws BuildException
* @return void
*/
private function validateDebug()
{
if (($this->debug === '1') || ($this->debug === 'true') || ($this->debug === 'yes')) {
$debug = true;
} elseif (($this->debug === '0') || ($this->debug === 'false') || ($this->debug === 'no')) {
$debug = false;
} else {
throw new BuildException('Expected a boolean value.');
}
if ($debug) {
$this->commandLineOptions[] = '--debug';
}
}
/**
*
* @throws BuildException
* @return void
*/
private function validateConfiguration()
{
if (($this->configuration === null) || ($this->configuration === '')) {
// NOTE: Ignore an empty configuration. This allows for
// using Phing properties as attribute values, e.g.
// <sonar ... configuration="{sonar.config.file}">.
return;
}
if (! @file_exists($this->configuration)) {
$message = sprintf('Cannot find configuration file [%s].', $this->configuration);
throw new BuildException($message);
}
if (! @is_readable($this->configuration)) {
$message = sprintf('Cannot read configuration file [%s].', $this->configuration);
throw new BuildException($message);
}
// TODO: Maybe check file type?
}
/**
*
* @throws BuildException
* @return void
*/
private function validateProperties()
{
$this->properties = $this->parseConfigurationFile();
foreach ($this->propertyElements as $property) {
$name = $property->getName();
$value = $property->getValue();
if ($name === null || $name === '') {
throw new BuildException('Property name must not be null or empty.');
}
if (array_key_exists($name, $this->properties)) {
$message = sprintf('Property [%s] overwritten: old value [%s], new value [%s].',
$name,
$this->properties[$name],
$value);
$this->log($message, Project::MSG_WARN);
}
$this->properties[$name] = $value;
}
// Check if all properties required by SonarQube Scanner are set ...
$requiredProperties = array(
'sonar.projectKey',
'sonar.projectName',
'sonar.projectVersion',
'sonar.sources'
);
$intersection = array_intersect($requiredProperties, array_keys($this->properties));
if (count($intersection) < count($requiredProperties)) {
$message = 'SonarQube Scanner misses some parameters. The following properties are mandatory: ' . implode(', ', $requiredProperties) . '.';
throw new BuildException($message);
}
}
/**
*
* @return array
*/
private function parseConfigurationFile() {
if (($this->configuration === null) || ($this->configuration === '')) {
return array();
}
$parser = new SonarConfigurationFileParser($this->configuration, $this->project);
return $parser->parse();
}
/**
*
* @return boolean
*/
private function isWindows()
{
$operatingSystemName = php_uname('s');
return strtoupper(substr($operatingSystemName, 0, 3)) === 'WIN';
}
}
|