Skip to content

Commit e3abfa0

Browse files
committed
[#17] Add configuration file support
[#15] Read php-semver-checker-git.yaml configuration file by default Add a BaseCommand which all commands may extend in order to setup LevelMapping overrides from a configuration file (as well as any other CLI arguments/options).
1 parent 0fe4b98 commit e3abfa0

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PHPSemVerCheckerGit\Console\Command;
4+
5+
use PHPSemVerChecker\Configuration\Configuration;
6+
use PHPSemVerChecker\Configuration\LevelMapping;
7+
use PHPSemVerChecker\Console\InputMerger;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class BaseCommand extends Command
13+
{
14+
/**
15+
* @var \PHPSemVerChecker\Configuration\Configuration
16+
*/
17+
protected $config;
18+
19+
/**
20+
* @param \Symfony\Component\Console\Input\InputInterface $input
21+
* @param \Symfony\Component\Console\Output\OutputInterface $output
22+
*/
23+
protected function initialize(InputInterface $input, OutputInterface $output)
24+
{
25+
parent::initialize($input, $output);
26+
$configPath = $input->getOption('config');
27+
$this->config = $configPath ? Configuration::fromFile($configPath) : Configuration::defaults('php-semver-checker-git');
28+
$inputMerger = new InputMerger();
29+
$inputMerger->merge($input, $this->config);
30+
31+
// Set overrides
32+
LevelMapping::setOverrides($this->config->getLevelMapping());
33+
}
34+
}

src/PHPSemVerCheckerGit/Console/Command/CompareCommand.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
use Gitter\Client;
66
use PHPSemVerChecker\Analyzer\Analyzer;
7+
use PHPSemVerChecker\Configuration\LevelMapping;
78
use PHPSemVerChecker\Finder\Finder;
89
use PHPSemVerChecker\Reporter\Reporter;
910
use PHPSemVerChecker\Scanner\Scanner;
1011
use PHPSemVerCheckerGit\Filter\SourceFilter;
1112
use PHPSemVerCheckerGit\Reporter\JsonReporter;
12-
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Helper\ProgressBar;
1414
use Symfony\Component\Console\Input\InputArgument;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Output\OutputInterface;
1818

19-
class CompareCommand extends Command {
19+
class CompareCommand extends BaseCommand {
2020
protected function configure()
2121
{
2222
$this
@@ -29,6 +29,7 @@ protected function configure()
2929
new InputOption('include-after', null, InputOption::VALUE_OPTIONAL, 'List of paths to include <info>(comma separated)</info>'),
3030
new InputOption('exclude-before', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
3131
new InputOption('exclude-after', null, InputOption::VALUE_REQUIRED, 'List of paths to exclude <info>(comma separated)</info>'),
32+
new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker-git'),
3233
new InputOption('to-json', null, InputOption::VALUE_REQUIRED, 'Output the result to a JSON file')
3334
]);
3435
}
@@ -38,14 +39,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
3839
$startTime = microtime(true);
3940

4041
$targetDirectory = getcwd();
41-
$commitBefore = $input->getArgument('before');
42-
$commitAfter = $input->getArgument('after');
42+
$commitBefore = $this->config->get('before');
43+
$commitAfter = $this->config->get('after');
4344

44-
$includeBefore = $input->getOption('include-before');
45-
$excludeBefore = $input->getOption('exclude-before');
45+
$includeBefore = $this->config->get('include-before');
46+
$excludeBefore = $this->config->get('exclude-before');
4647

47-
$includeAfter = $input->getOption('include-after');
48-
$excludeAfter = $input->getOption('exclude-after');
48+
$includeAfter = $this->config->get('include-after');
49+
$excludeAfter = $this->config->get('exclude-after');
4950

5051
$finder = new Finder();
5152
$sourceFilter = new SourceFilter();
@@ -105,7 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
105106
$reporter->setFullPath(true);
106107
$reporter->output($output);
107108

108-
$toJson = $input->getOption('to-json');
109+
$toJson = $this->config->get('to-json');
109110
if ($toJson) {
110111
$commitBeforeHash = $repository->getCommit($commitBefore)->getHash();
111112
$commitAfterHash = $repository->getCommit($commitAfter)->getHash();

src/PHPSemVerCheckerGit/Console/Command/SuggestCommand.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPSemVerChecker\SemanticVersioning\Level;
1313
use PHPSemVerCheckerGit\Filter\SourceFilter;
1414
use RuntimeException;
15-
use Symfony\Component\Console\Command\Command;
1615
use Symfony\Component\Console\Helper\ProgressBar;
1716
use Symfony\Component\Console\Input\InputArgument;
1817
use Symfony\Component\Console\Input\InputInterface;
@@ -21,7 +20,7 @@
2120
use vierbergenlars\SemVer\expression as SemanticExpression;
2221
use vierbergenlars\SemVer\version as SemanticVersion;
2322

24-
class SuggestCommand extends Command
23+
class SuggestCommand extends BaseCommand
2524
{
2625
protected function configure()
2726
{
@@ -34,6 +33,7 @@ protected function configure()
3433
new InputOption('against', 'a', InputOption::VALUE_REQUIRED, 'What to test against the tag (HEAD by default)'),
3534
new InputOption('allow-detached', 'd', InputOption::VALUE_NONE, 'Allow suggest to start from a detached HEAD'),
3635
new InputOption('details', null, InputOption::VALUE_NONE, 'Report the changes on which the suggestion is based'),
36+
new InputOption('config', null, InputOption::VALUE_REQUIRED, 'A configuration file to configure php-semver-checker-git'),
3737
]);
3838
}
3939

@@ -42,14 +42,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
4242
$startTime = microtime(true);
4343

4444
$targetDirectory = getcwd();
45-
$tag = $input->getOption('tag');
46-
$against = $input->getOption('against') ?: 'HEAD';
45+
$tag = $this->config->get('tag');
46+
$against = $this->config->get('against') ?: 'HEAD';
4747

48-
$includeBefore = $input->getOption('include-before');
49-
$excludeBefore = $input->getOption('exclude-before');
48+
$includeBefore = $this->config->get('include-before');
49+
$excludeBefore = $this->config->get('exclude-before');
5050

51-
$includeAfter = $input->getOption('include-after');
52-
$excludeAfter = $input->getOption('exclude-after');
51+
$includeAfter = $this->config->get('include-after');
52+
$excludeAfter = $this->config->get('exclude-after');
5353

5454
$client = new Client();
5555

@@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8080

8181
$initialBranch = $repository->getCurrentBranch();
8282

83-
if ( ! $input->getOption('allow-detached') && ! $initialBranch) {
83+
if ( ! $this->config->get('allow-detached') && ! $initialBranch) {
8484
$output->writeln('<error>You are on a detached HEAD, aborting.</error>');
8585
$output->writeln('<info>If you still wish to run against a detached HEAD, use --allow-detached.</info>');
8686
return -1;
@@ -146,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
146146
$output->writeln('<info>Initial semantic version: ' . $tag . '</info>');
147147
$output->writeln('<info>Suggested semantic version: ' . $newTag . '</info>');
148148

149-
if ($input->getOption('details')) {
149+
if ($this->config->get('details')) {
150150
$reporter = new Reporter($report);
151151
$reporter->output($output);
152152
}

0 commit comments

Comments
 (0)