diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 1742323..8474e97 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -

Run SonarQube Scanner with composer

+

Run SonarQube Scanner with composer

[![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) @@ -6,14 +6,14 @@ # 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** @@ -21,6 +21,15 @@ composer install rogervila/php-sonarqube-scanner --dev 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). diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 759b8ab..dff166e --- a/composer.json +++ b/composer.json @@ -23,12 +23,12 @@ }, "autoload": { "psr-4": { - "SonarScanner\\": "src/" + "Sonar\\": "src/" } }, "autoload-dev": { "psr-4": { - "Tests\\SonarScanner\\": "tests/" + "Tests\\Sonar\\": "tests/" } }, "config": { diff --git a/phpunit.xml b/phpunit.xml old mode 100644 new mode 100755 diff --git a/sonar-scanner b/sonar-scanner index 42fe34c..52a93af 100755 --- a/sonar-scanner +++ b/sonar-scanner @@ -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( @@ -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, diff --git a/src/Contracts/DeviceDetectorInterface.php b/src/Contracts/DeviceDetectorInterface.php deleted file mode 100644 index 8c8e088..0000000 --- a/src/Contracts/DeviceDetectorInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -service->isOSX()) { return new OperatingSystem(OperatingSystem::OSX); diff --git a/src/Exceptions/InvalidOperatingSystemException.php b/src/Exceptions/InvalidOperatingSystemException.php old mode 100644 new mode 100755 index 3526f24..17062bc --- a/src/Exceptions/InvalidOperatingSystemException.php +++ b/src/Exceptions/InvalidOperatingSystemException.php @@ -1,6 +1,6 @@ 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]); + } + } +} diff --git a/src/App.php b/src/Scanner.php old mode 100644 new mode 100755 similarity index 62% rename from src/App.php rename to src/Scanner.php index 0fd396e..f1f30dc --- a/src/App.php +++ b/src/Scanner.php @@ -1,33 +1,27 @@ detector = $detector; + $this->device = new Device; $this->zip = new \ZipArchive; } /** * @return void */ - public function run(string $executionPath) + public function run(Options $options) { - $this->executionPath = $executionPath; - - $this->version = new Version(Version::DEFAULT_VERSION); + $this->options = $options; - $this->os = $this->detector->getOperatingSystem(); + $this->os = $this->device->detect(); $this->setZipName(); @@ -99,7 +88,7 @@ private function setZipName() { $this->zipName = implode(self::FILE_SEPARATOR, [ self::ZIP_PREFIX, - $this->version->getValue(), + self::VERSION, $this->os->getValue(), ]) . '.zip'; } @@ -111,7 +100,7 @@ private function setFolderName() { $this->folderName = implode(self::FILE_SEPARATOR, [ self::FOLDER_PREFIX, - $this->version->getValue(), + self::VERSION, $this->os->getValue(), ]); } @@ -136,7 +125,7 @@ private function setZipFile() private function unzip() { if ($this->zip->open($this->zipFile) === true) { - $this->zip->extractTo(self::EXTRACT_PATH); + $this->zip->extractTo(__DIR__ . self::EXTRACT_ROUTE); $this->zip->close(); } else { throw new UnzipFailureException(); @@ -148,10 +137,10 @@ private function unzip() */ private function fixPermissions() { - echo "Asking for executable permissions...\n"; + echo 'Asking for executable permissions...' . PHP_EOL; $files = Dir::scan( - self::EXTRACT_PATH . $this->folderName, + __DIR__ . self::EXTRACT_ROUTE . $this->folderName, [ 'type' => 'file', 'skipDots' => true, @@ -171,17 +160,16 @@ private function fixPermissions() */ private function execute() { - if (!file_exists($this->executionPath . DIRECTORY_SEPARATOR . self::PROPERTIES_FILE)) { - throw new PropertiesFileNotFoundException(); - } - $extension = $this->os->equals(new OperatingSystem(OperatingSystem::WINDOWS)) ? '.bat' : ''; - echo "Running scanner..." . PHP_EOL; + $command = __DIR__ . self::EXTRACT_ROUTE . $this->folderName . self::EXECUTION_ROUTE . $extension . ' ' . $this->options->cli(); + + echo 'Running scanner...' . PHP_EOL; + echo 'INFO: ' . $command . PHP_EOL; - exec(self::EXTRACT_PATH . $this->folderName . self::EXECUTION_ROUTE . $extension, $output); + exec($command, $output); echo implode(PHP_EOL, $output) . PHP_EOL; } diff --git a/src/Traits/HasValue.php b/src/Traits/HasValue.php old mode 100644 new mode 100755 index 999a8d2..f393328 --- a/src/Traits/HasValue.php +++ b/src/Traits/HasValue.php @@ -1,6 +1,6 @@ versions)) { - throw new InvalidVersionException(); - } - - $this->setValue($version); - } - - /** - * @return array - */ - public function getVersions() - { - return $this->versions; - } -} diff --git a/tests/AppTest.php b/tests/AppTest.php old mode 100644 new mode 100755