Skip to content

Commit

Permalink
Set default properties dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
rogervila authored Feb 13, 2019
1 parent 60de490 commit 133b46d
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 127 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
15 changes: 12 additions & 3 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
<p align="center"><img src="https://i.imgur.com/xcIhGwP.png" alt="Run SonarQube Scanner with composer" /></p>
<p align="center"><img width="250" src="https://i.imgur.com/xcIhGwP.png" alt="Run SonarQube Scanner with composer" /></p>

[![Build Status](https://travis-ci.org/rogervila/php-sonarqube-scanner.svg?branch=master)](https://travis-ci.org/rogervila/php-sonarqube-scanner)
[![Build status](https://ci.appveyor.com/api/projects/status/weidwo98jcdrtkxm?svg=true)](https://ci.appveyor.com/project/roger-vila/php-sonarqube-scanner)


# Run SonarQube Scanner with composer

## Usage

**Install the package as a dev requirement**

```
composer install rogervila/php-sonarqube-scanner --dev
```

> Make sure you have a `sonar-project.properties` on your project root!

**Run with composer**

```
vendor/bin/sonar-scanner
```

## Defaults

In some cases, if the package finds missing properties, it will provide them automatically.

| Property | Source | Example
|----|---|---|
| sonar.projectKey | adapted `composer.json` name property | `-Dsonar.projectKey=rogervila_php-sonarqube-scanner`
| sonar.projectName | adapted `composer.json` name property | `-Dsonar.projectName=php-sonarqube-scanner`

## License

This project is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
4 changes: 2 additions & 2 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
},
"autoload": {
"psr-4": {
"SonarScanner\\": "src/"
"Sonar\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\SonarScanner\\": "tests/"
"Tests\\Sonar\\": "tests/"
}
},
"config": {
Expand Down
Empty file modified phpunit.xml
100644 → 100755
Empty file.
52 changes: 36 additions & 16 deletions sonar-scanner
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'UTC');
}

$autoloads = [
$paths = [
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'autoload.php',
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php',
];

foreach ($autoloads as $file) {
foreach ($paths as $file) {
if (file_exists($file)) {
define('COMPOSER_AUTOLOAD_FILE', $file);
break;
}
}

$paths = [
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'composer.json',
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'composer.json',
__DIR__ . DIRECTORY_SEPARATOR . 'composer.json',
];

foreach ($paths as $file) {
if (file_exists($file)) {
define('COMPOSER_CONFIG_FILE', $file);
break;
}
}

unset($file);
unset($autoloads);
unset($paths);

if (!defined('COMPOSER_AUTOLOAD_FILE')) {
fwrite(
Expand All @@ -35,21 +47,29 @@ if (!defined('COMPOSER_AUTOLOAD_FILE')) {

require COMPOSER_AUTOLOAD_FILE;

$app = new \SonarScanner\App(
new \SonarScanner\Device\Detector()
);
$app = new \Sonar\Scanner();

try {
$app->run(getcwd());
} catch (\SonarScanner\Exceptions\PropertiesFileNotFoundException $e) {
fwrite(
STDERR,
'You need to set up a sonar-project.properties file:' . PHP_EOL . PHP_EOL .
' touch sonar-project.properties' . PHP_EOL . PHP_EOL .
'You can learn all about Sonar properties on https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner.' . PHP_EOL
);
$options = new \Sonar\Options(getcwd());

die(1);
if (defined('COMPOSER_CONFIG_FILE') && file_exists(COMPOSER_CONFIG_FILE)) {
$content = json_decode(file_get_contents(COMPOSER_CONFIG_FILE), true);

if (is_array($content)) {
$options->setComposerConfiguration($content);
} else {
fwrite(
STDERR,
'WARNING! project\'s composer.json file could not be parsed' . PHP_EOL
);
}

unset($content);
}

$options->parse($argv);

try {
$app->run($options);
} catch (\Exception $e) {
fwrite(
STDERR,
Expand Down
8 changes: 0 additions & 8 deletions src/Contracts/DeviceDetectorInterface.php

This file was deleted.

11 changes: 5 additions & 6 deletions src/Device/Detector.php → src/Device.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace SonarScanner\Device;
namespace Sonar;

use SonarScanner\Contracts\DeviceDetectorInterface;
use SonarScanner\Exceptions\InvalidOperatingSystemException;
use SonarScanner\Values\OperatingSystem;
use Sonar\Exceptions\InvalidOperatingSystemException;
use Sonar\Values\OperatingSystem;

class Detector implements DeviceDetectorInterface
class Device
{
private $service;

Expand All @@ -19,7 +18,7 @@ public function __construct()
* @return OperatingSystem
* @throws InvalidOperatingSystemException
*/
public function getOperatingSystem()
public function detect()
{
if ($this->service->isOSX()) {
return new OperatingSystem(OperatingSystem::OSX);
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidOperatingSystemException.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SonarScanner\Exceptions;
namespace Sonar\Exceptions;

class InvalidOperatingSystemException extends \Exception
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidVersionException.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SonarScanner\Exceptions;
namespace Sonar\Exceptions;

class InvalidVersionException extends \Exception
{
Expand Down
8 changes: 0 additions & 8 deletions src/Exceptions/PropertiesFileNotFoundException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/UnzipFailureException.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SonarScanner\Exceptions;
namespace Sonar\Exceptions;

class UnzipFailureException extends \Exception
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ZipFileNotFoundException.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SonarScanner\Exceptions;
namespace Sonar\Exceptions;

class ZipFileNotFoundException extends \Exception
{
Expand Down
121 changes: 121 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Sonar;

class Options
{
const PROPERTIES_FILE_NAME = 'sonar-project.properties';
const INLINE_PREFIX = '-D';
const LAUNCHER = 'sonar-scanner';
const PROJECT_KEY = 'sonar.projectKey';
const PROJECT_NAME = 'sonar.projectName';

/**
* @var string
*/
private $basePath;

/**
* @var string
*/
private $propertiesFile;

/**
* @var array
*/
private $arguments;

/**
* @var array
*/
private $composer = [];

/**
* @param string $basePath
*/
public function __construct(string $basePath)
{
$this->basePath = trim($basePath);

$this->propertiesFile = $this->basePath . DIRECTORY_SEPARATOR . self::PROPERTIES_FILE_NAME;
}

/**
* @return void
*/
public function setComposerConfiguration(array $composer)
{
$this->composer = $composer;
}

/**
* @return void
*/
public function parse(array $arguments)
{
if (self::LAUNCHER == substr(self::LAUNCHER, -strlen(self::LAUNCHER))) {
array_shift($arguments);
}

$this->arguments = $arguments;

if (!$this->hasArgument(self::INLINE_PREFIX . self::PROJECT_KEY)
&& !$this->propertiesFileHasOption(self::PROJECT_KEY)
&& isset($this->composer['name'])) {
$this->setProjectKeyFromComposer();
}

if (!$this->hasArgument(self::INLINE_PREFIX . self::PROJECT_NAME)
&& !$this->propertiesFileHasOption(self::PROJECT_NAME)
&& isset($this->composer['name'])) {
$this->setProjectNameFromComposer();
}
}

/**
* @return string
*/
public function cli()
{
return implode(' ', $this->arguments);
}

private function hasArgument(string $argument)
{
return count(preg_grep('/^' . str_replace('.', '\.', $argument) . '/m', $this->arguments)) > 0;
}

/**
* @return bool
*/
private function propertiesFileHasOption($option)
{
if (!file_exists($this->propertiesFile)) {
return false;
}

preg_match('/^' . str_replace('.', '\.', $option) . '/m', file_get_contents($this->propertiesFile), $matches);

return count($matches) > 0;
}

/**
* @return void
*/
private function setProjectKeyFromComposer()
{
array_push($this->arguments, self::INLINE_PREFIX . self::PROJECT_KEY . '=' . str_replace('/', '_', $this->composer['name']));
}

/**
* @return void
*/
private function setProjectNameFromComposer()
{
$result = explode('/', $this->composer['name']);

if (isset($result[1])) {
array_push($this->arguments, self::INLINE_PREFIX . self::PROJECT_NAME . '=' . $result[1]);
}
}
}
Loading

0 comments on commit 133b46d

Please sign in to comment.