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

20 files changed

+211
-51
lines changed

README.MD

+7-6
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

+25
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

+1-1
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

+1-1
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

+3-1
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

+29
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

+2-3
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

+10-10
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

-1
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

+28-7
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
}

src/Console/Command/LintCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use ArtARTs36\MergeRequestLinter\Note\Notes;
1616
use ArtARTs36\MergeRequestLinter\Note\NoteSeverity;
1717
use ArtARTs36\MergeRequestLinter\Support\Bytes;
18-
use Symfony\Component\Console\Command\Command;
1918
use Symfony\Component\Console\Helper\ProgressBar;
2019
use Symfony\Component\Console\Helper\TableCell;
2120
use Symfony\Component\Console\Helper\TableCellStyle;

src/Support/File.php

-11
This file was deleted.

src/Support/File/Directory.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Support\File;
4+
5+
class Directory
6+
{
7+
public function __construct(
8+
private readonly string $directory,
9+
) {
10+
//
11+
}
12+
13+
public function pathTo(string $filename): string
14+
{
15+
return $this->directory . DIRECTORY_SEPARATOR . $filename;
16+
}
17+
18+
public function __toString(): string
19+
{
20+
return $this->directory;
21+
}
22+
}

src/Support/File/File.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Support\File;
4+
5+
use ArtARTs36\MergeRequestLinter\Support\Bytes;
6+
7+
class File extends \SplFileInfo
8+
{
9+
public function getSizeString(): string
10+
{
11+
$size = $this->getSize();
12+
13+
if ($size === false) {
14+
return '';
15+
}
16+
17+
return Bytes::toString($size);
18+
}
19+
20+
public static function extension(string $path): string
21+
{
22+
return mb_strtolower(pathinfo($path, PATHINFO_EXTENSION));
23+
}
24+
}

src/Support/ToolInfo/ToolInfo.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ToolInfo
1313
public const REPO_NAME = 'php-merge-request-linter';
1414
public const REPO_URI = 'https://github.com/ArtARTs36/php-merge-request-linter';
1515

16+
private const PHAR_URI_PREFIX = 'phar://';
17+
1618
public function __construct(
1719
private readonly GithubClient $github,
1820
) {
@@ -21,13 +23,15 @@ public function __construct(
2123

2224
public function getLatestVersion(): ?Tag
2325
{
24-
$versions = $this->github->getTags(new TagsInput(self::REPO_OWNER, self::REPO_NAME));
25-
26-
return $versions->sortByMajority()->first();
26+
return $this
27+
->github
28+
->getTags(new TagsInput(self::REPO_OWNER, self::REPO_NAME))
29+
->sortByMajority()
30+
->first();
2731
}
2832

2933
public function usedAsPhar(): bool
3034
{
31-
return Str::startsWith(__DIR__, 'phar://');
35+
return Str::startsWith(__DIR__, self::PHAR_URI_PREFIX);
3236
}
3337
}

stubs/.mr-linter.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"rules": {
3+
"@mr-linter/has_any_labels_of": {
4+
"labels": [
5+
"Feature",
6+
"Bug",
7+
"Docs",
8+
"Tests"
9+
]
10+
},
11+
"@mr-linter/title_must_starts_with_any_prefix": {
12+
"prefixes": [
13+
"[Feature]",
14+
"[Bug]",
15+
"[Docs]",
16+
"[Tests]"
17+
]
18+
},
19+
"@mr-linter/description_not_empty": {}
20+
},
21+
"credentials": {
22+
"github_actions": "env(MR_LINTER_GITHUB_HTTP_TOKEN)"
23+
}
24+
}

stubs/.mr-linter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
use ArtARTs36\MergeRequestLinter\Rule\DescriptionNotEmptyRule;
77
use ArtARTs36\MergeRequestLinter\Rule\HasAnyLabelsOfRule;
88
use ArtARTs36\MergeRequestLinter\Rule\TitleStartsWithAnyPrefixRule;
9+
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Set;
910

1011
return [
1112
'rules' => [
1213
new DescriptionNotEmptyRule(),
13-
HasAnyLabelsOfRule::make([
14+
new HasAnyLabelsOfRule(Set::fromList([
1415
'Feature',
1516
'Bug',
1617
'Docs',
1718
'Tests',
18-
]),
19+
])),
1920
new TitleStartsWithAnyPrefixRule([
2021
'[Feature]',
2122
'[Bug]',

0 commit comments

Comments
 (0)