Skip to content

Commit

Permalink
More default options
Browse files Browse the repository at this point in the history
Road to v1.0
  • Loading branch information
rogervila authored Mar 12, 2019
1 parent e5c07c5 commit ddac8a2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 8 additions & 0 deletions sonar-scanner
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
119 changes: 111 additions & 8 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +41,16 @@ class Options
*/
private $composer = [];

/**
* @var string
*/
private $branch = '';

/**
* @var int
*/
private $edition = self::EDITION_COMMUNITY;

/**
* @param string $basePath
*/
Expand All @@ -41,6 +62,7 @@ public function __construct(string $basePath)
}

/**
* @param array $composer
* @return void
*/
public function setComposerConfiguration(array $composer)
Expand All @@ -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)
Expand All @@ -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], []);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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/**"');
}
}

0 comments on commit ddac8a2

Please sign in to comment.