Skip to content

Commit 5b5eee2

Browse files
authored
[Feature] Config Copier (#20)
* Add Config Copier * clean * Update version * clean * Add base command * fix test
1 parent b8629b6 commit 5b5eee2

File tree

20 files changed

+211
-51
lines changed

20 files changed

+211
-51
lines changed

README.MD

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ You must have implementation of `psr/http-client-implementation`
3232

3333
## Available Commands
3434

35-
| Command | Description |
36-
|--------------------------------|---------------------------------------------------------------|
37-
| ./vendor/bin/mr-linter install | Install this tool (copy configuration file to work directory) |
38-
| ./vendor/bin/mr-linter dump | Print current rules |
39-
| ./vendor/bin/mr-linter lint | Run lint to current merge request |
40-
| ./vendor/bin/mr-linter info | Print info about MR Linter |
35+
| Command | Description |
36+
|----------------------------------------------|---------------------------------------------------------------|
37+
| ./vendor/bin/mr-linter install | Install this tool (copy configuration file to work directory) |
38+
| ./vendor/bin/mr-linter install --format=yaml | Install this tool (copy configuration file to work directory) |
39+
| ./vendor/bin/mr-linter dump | Print current rules |
40+
| ./vendor/bin/mr-linter lint | Run lint to current merge request |
41+
| ./vendor/bin/mr-linter info | Print info about MR Linter |
4142

4243
## Usage
4344

src/Configuration/Copier.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Configuration;
4+
5+
use ArtARTs36\MergeRequestLinter\Support\File\Directory;
6+
use ArtARTs36\MergeRequestLinter\Support\File\File;
7+
8+
class Copier
9+
{
10+
public function __construct(
11+
private readonly Directory $stubsDir,
12+
) {
13+
//
14+
}
15+
16+
public function copy(ConfigFormat $format, Directory $targetDir): File
17+
{
18+
$stubsPath = $this->stubsDir->pathTo(sprintf('.mr-linter.%s', $format->value));
19+
$targetPath = $targetDir->pathTo(sprintf('.mr-linter.%s', $format->value));
20+
21+
copy($stubsPath, $targetPath);
22+
23+
return new File($targetPath);
24+
}
25+
}

src/Configuration/Loader/Loaders/CompositeLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ArtARTs36\MergeRequestLinter\Configuration\Config;
66
use ArtARTs36\MergeRequestLinter\Contracts\Config\ConfigLoader;
77
use ArtARTs36\MergeRequestLinter\Exception\ConfigInvalidException;
8-
use ArtARTs36\MergeRequestLinter\Support\File;
8+
use ArtARTs36\MergeRequestLinter\Support\File\File;
99

1010
class CompositeLoader implements ConfigLoader
1111
{

src/Console/Application/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class Application extends \Symfony\Component\Console\Application
1414
{
15-
public const VERSION = '0.7.3';
15+
public const VERSION = '0.7.4';
1616

1717
public function __construct(
1818
private readonly MetricManager $metrics,

src/Console/Application/ApplicationFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArtARTs36\FileSystem\Local\LocalFileSystem;
66
use ArtARTs36\MergeRequestLinter\CI\System\DefaultSystems;
77
use ArtARTs36\MergeRequestLinter\Configuration\ConfigFormat;
8+
use ArtARTs36\MergeRequestLinter\Configuration\Copier;
89
use ArtARTs36\MergeRequestLinter\Configuration\Loader\ArrayConfigLoaderFactory;
910
use ArtARTs36\MergeRequestLinter\Configuration\Loader\Loaders\CompositeLoader;
1011
use ArtARTs36\MergeRequestLinter\Configuration\Loader\Loaders\Proxy;
@@ -20,6 +21,7 @@
2021
use ArtARTs36\MergeRequestLinter\IO\Console\ConsoleLoggerFactory;
2122
use ArtARTs36\MergeRequestLinter\Linter\Runner\RunnerFactory as LinterRunnerFactory;
2223
use ArtARTs36\MergeRequestLinter\Report\Metrics\Manager\MemoryMetricManager;
24+
use ArtARTs36\MergeRequestLinter\Support\File\Directory;
2325
use ArtARTs36\MergeRequestLinter\Support\ToolInfo\ToolInfoFactory;
2426
use Symfony\Component\Console\Output\OutputInterface;
2527

@@ -53,7 +55,7 @@ public function create(OutputInterface $output): Application
5355
);
5456

5557
$application->add(new LintCommand($configResolver, $runnerFactory, $metrics));
56-
$application->add(new InstallCommand());
58+
$application->add(new InstallCommand(new Copier(new Directory(__DIR__ . '/../../../stubs'))));
5759
$application->add(new DumpCommand($configResolver));
5860
$application->add(new InfoCommand(new ToolInfoFactory()));
5961

src/Console/Command/Command.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Console\Command;
4+
5+
use ArtARTs36\MergeRequestLinter\Console\Interaction\WorkDirResolver;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
9+
abstract class Command extends \Symfony\Component\Console\Command\Command
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct();
14+
15+
$this->addWorkDirOption();
16+
}
17+
18+
final protected function getWorkDir(InputInterface $input): string
19+
{
20+
return (new WorkDirResolver())->resolve($input);
21+
}
22+
23+
private function addWorkDirOption(): void
24+
{
25+
$this
26+
->getDefinition()
27+
->addOption(new InputOption(WorkDirResolver::OPTION_NAME, mode: InputOption::VALUE_OPTIONAL));
28+
}
29+
}

src/Console/Command/DumpCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace ArtARTs36\MergeRequestLinter\Console\Command;
44

55
use ArtARTs36\MergeRequestLinter\Contracts\Config\ConfigResolver;
6-
use Symfony\Component\Console\Command\Command;
76
use Symfony\Component\Console\Input\InputInterface;
87
use Symfony\Component\Console\Output\OutputInterface;
98
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -16,9 +15,9 @@ class DumpCommand extends Command
1615

1716
protected static $defaultDescription = 'Print current rules';
1817

19-
public function __construct(protected ConfigResolver $config, string $name = null)
18+
public function __construct(protected ConfigResolver $config)
2019
{
21-
parent::__construct($name);
20+
parent::__construct();
2221
}
2322

2423
protected function configure(): void

src/Console/Command/HasConfigFileOption.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@
44

55
use ArtARTs36\MergeRequestLinter\Configuration\Resolver\ResolvedConfig;
66
use ArtARTs36\MergeRequestLinter\Configuration\User;
7-
use ArtARTs36\MergeRequestLinter\Console\Interaction\WorkDirResolver;
87
use Symfony\Component\Console\Input\InputInterface;
98
use Symfony\Component\Console\Input\InputOption;
109

1110
trait HasConfigFileOption
1211
{
12+
abstract protected function getWorkDir(InputInterface $input): string;
13+
1314
private function addConfigFileOption(): void
1415
{
1516
$this
1617
->getDefinition()
1718
->addOption(new InputOption('config', mode: InputOption::VALUE_OPTIONAL));
18-
19-
$this
20-
->getDefinition()
21-
->addOption(new InputOption(WorkDirResolver::OPTION_NAME, mode: InputOption::VALUE_OPTIONAL));
2219
}
2320

2421
/**
2522
* @throws \Exception
2623
*/
2724
private function resolveConfig(InputInterface $input): ResolvedConfig
2825
{
29-
$customPath = $input->getOption('config');
30-
31-
$currentDir = (new WorkDirResolver())->resolve($input);
32-
33-
return $this->config->resolve(new User($currentDir, $customPath));
26+
return $this
27+
->config
28+
->resolve(
29+
new User(
30+
$this->getWorkDir($input),
31+
$input->getOption('config'),
32+
)
33+
);
3434
}
3535
}

src/Console/Command/InfoCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use ArtARTs36\MergeRequestLinter\Rule\DefaultRules;
99
use ArtARTs36\MergeRequestLinter\Support\ToolInfo\ToolInfo;
1010
use ArtARTs36\MergeRequestLinter\Support\ToolInfo\ToolInfoFactory;
11-
use Symfony\Component\Console\Command\Command;
1211
use Symfony\Component\Console\Input\InputInterface;
1312
use Symfony\Component\Console\Output\OutputInterface;
1413

src/Console/Command/InstallCommand.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Console\Command;
44

5-
use Symfony\Component\Console\Command\Command;
5+
use ArtARTs36\MergeRequestLinter\Configuration\ConfigFormat;
6+
use ArtARTs36\MergeRequestLinter\Configuration\Copier;
7+
use ArtARTs36\MergeRequestLinter\Support\File\Directory;
68
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
710
use Symfony\Component\Console\Output\OutputInterface;
811
use Symfony\Component\Console\Style\SymfonyStyle;
912

@@ -13,21 +16,39 @@ class InstallCommand extends Command
1316

1417
protected static $defaultDescription = 'Install this tool';
1518

16-
public function __construct(string $name = null)
19+
public function __construct(
20+
private readonly Copier $configCopier,
21+
) {
22+
parent::__construct();
23+
}
24+
25+
protected function configure(): void
1726
{
18-
parent::__construct($name);
27+
$this
28+
->getDefinition()
29+
->addOption(new InputOption('format', mode: InputOption::VALUE_OPTIONAL));
1930
}
2031

2132
protected function execute(InputInterface $input, OutputInterface $output)
2233
{
23-
$dir = getcwd();
34+
$style = new SymfonyStyle($input, $output);
2435

25-
copy(__DIR__ . '/../../../stubs/.mr-linter.php', $pathTo = $dir . '/.mr-linter.php');
36+
$dir = $this->getWorkDir($input);
37+
$format = $this->resolveConfigFormat($input);
2638

27-
$style = new SymfonyStyle($input, $output);
39+
$createdFile = $this->configCopier->copy($format, new Directory($dir));
2840

29-
$style->info('Was copied configuration file to: '. $pathTo);
41+
$style->info(sprintf('Was copied configuration file to: %s [%s]', $createdFile, $createdFile->getSizeString()));
3042

3143
return self::SUCCESS;
3244
}
45+
46+
private function resolveConfigFormat(InputInterface $input): ConfigFormat
47+
{
48+
if ($input->getOption('format') !== null) {
49+
return ConfigFormat::from($input->getOption('format'));
50+
}
51+
52+
return ConfigFormat::PHP;
53+
}
3354
}

0 commit comments

Comments
 (0)