From ddac8a29f43b6b74caa7094bf3432136e1bf6b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Tue, 12 Mar 2019 17:26:02 +0100 Subject: [PATCH] More default options Road to v1.0 --- composer.json | 1 + sonar-scanner | 8 ++++ src/Options.php | 119 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 120 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index dff166e..3195e6e 100755 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "require": { "ext-zip": "*", "crysalead/dir": "^2.0", + "norwichtech/php-git-branch": "^1.1", "tivie/php-os-detector": "^1.1" }, "require-dev": { diff --git a/sonar-scanner b/sonar-scanner index 6ad659c..9318c33 100755 --- a/sonar-scanner +++ b/sonar-scanner @@ -36,6 +36,14 @@ $app = new \Sonar\Scanner(); $options = new \Sonar\Options(getcwd()); +$git = new \NorwichTech\PHPGitBranch\GitBranch(getcwd(), new \NorwichTech\FileWrapper\FileWrapper); + +if (is_string($git->branch)) { + $options->setSourceManagerBranch($git->branch); +} + +unset($git); + if (defined('COMPOSER_CONFIG_FILE') && file_exists(COMPOSER_CONFIG_FILE)) { $content = json_decode(file_get_contents(COMPOSER_CONFIG_FILE), true); diff --git a/src/Options.php b/src/Options.php index ab5a8d9..4133c36 100755 --- a/src/Options.php +++ b/src/Options.php @@ -4,11 +4,22 @@ class Options { + const EDITION_COMMUNITY = 0; + const EDITION_DEVELOPER = 1; + const EDITION_ENTERPRISE = 2; + const EDITION_DATA_CENTER = 3; + 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'; + const PROJECT_DESCRIPTION = 'sonar.projectDescription'; + const SOURCES = 'sonar.sources'; + const EXCLUSIONS = 'sonar.exclusions'; + const BRANCH_NAME = 'sonar.branch.name'; + const BRANCH_TARGET = 'sonar.branch.target'; /** * @var string @@ -30,6 +41,16 @@ class Options */ private $composer = []; + /** + * @var string + */ + private $branch = ''; + + /** + * @var int + */ + private $edition = self::EDITION_COMMUNITY; + /** * @param string $basePath */ @@ -41,6 +62,7 @@ public function __construct(string $basePath) } /** + * @param array $composer * @return void */ public function setComposerConfiguration(array $composer) @@ -49,6 +71,25 @@ public function setComposerConfiguration(array $composer) } /** + * @param string $composer + * @return void + */ + public function setSourceManagerBranch(string $branch) + { + $this->branch = $branch; + } + + /** + * @param int $edition + * @return void + */ + public function setEdition(int $edition) + { + $this->edition = $edition; + } + + /** + * @param array $arguments * @return void */ public function parse(array $arguments) @@ -59,16 +100,34 @@ public function parse(array $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 (isset($this->composer['name'])) { + $this->loadDefault(self::PROJECT_KEY, 'setProjectKeyFromComposer'); + $this->loadDefault(self::PROJECT_NAME, 'setProjectNameFromComposer'); + } + + if (isset($this->composer['description'])) { + $this->loadDefault(self::PROJECT_DESCRIPTION, 'setProjectDescriptionFromComposer'); + } + + if (strlen($this->branch) > 0 && $this->edition > self::EDITION_COMMUNITY) { + $this->loadDefault(self::BRANCH_NAME, 'setProjectBranchName'); + $this->loadDefault(self::BRANCH_TARGET, 'setProjectBranchTarget'); } - if (!$this->hasArgument(self::INLINE_PREFIX . self::PROJECT_NAME) - && !$this->propertiesFileHasOption(self::PROJECT_NAME) - && isset($this->composer['name'])) { - $this->setProjectNameFromComposer(); + $this->loadDefault(self::SOURCES, 'setSourcesProperty'); + $this->loadDefault(self::EXCLUSIONS, 'setExclusionsProperty'); + } + + /** + * @param string $option + * @param string $method + * @return void + */ + private function loadDefault(string $option, string $method) + { + if (!$this->hasArgument(self::INLINE_PREFIX . $option) + && !$this->propertiesFileHasOption($option)) { + call_user_func_array([$this, $method], []); } } @@ -80,6 +139,10 @@ public function cli() return implode(' ', $this->arguments); } + /** + * @param string $argument + * @return boolean + */ private function hasArgument(string $argument) { return count(preg_grep('/^' . str_replace('.', '\.', $argument) . '/m', $this->arguments)) > 0; @@ -118,4 +181,44 @@ private function setProjectNameFromComposer() array_push($this->arguments, self::INLINE_PREFIX . self::PROJECT_NAME . '=' . $result[1]); } } + + /** + * @return void + */ + private function setProjectDescriptionFromComposer() + { + array_push($this->arguments, self::INLINE_PREFIX . self::PROJECT_DESCRIPTION . '="'. $this->composer['description'] .'"'); + } + + /** + * @return void + */ + private function setProjectBranchName() + { + array_push($this->arguments, self::INLINE_PREFIX . self::BRANCH_NAME . '=' . $this->branch); + } + + /** + * @return void + */ + private function setProjectBranchTarget() + { + array_push($this->arguments, self::INLINE_PREFIX . self::BRANCH_TARGET . '=' . $this->branch); + } + + /** + * @return void + */ + private function setSourcesProperty() + { + array_push($this->arguments, self::INLINE_PREFIX . self::SOURCES . '=' . $this->basePath); + } + + /** + * @return void + */ + private function setExclusionsProperty() + { + array_push($this->arguments, self::INLINE_PREFIX . self::EXCLUSIONS . '="vendor/**, node_modules/**, .scannerwork/**"'); + } }