Skip to content

Commit 2c6f41e

Browse files
committed
Introduces a configuration layer
1 parent 162a561 commit 2c6f41e

8 files changed

Lines changed: 157 additions & 16 deletions

File tree

src/Commands/CreateCommand.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Stolt\LeanPackage\Analysers\NegatedExportIgnoreAnalyser;
1010
use Stolt\LeanPackage\Commands\Concerns\GeneratesGitattributesOptions;
1111
use Stolt\LeanPackage\Commands\Concerns\OutputOptions;
12+
use Stolt\LeanPackage\Configuration\Factory;
1213
use Stolt\LeanPackage\Gitattributes\FileRepository as GitattributesFileRepository;
1314
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\InputArgument;
@@ -71,14 +72,11 @@ protected function configure(): void
7172

7273
protected function execute(InputInterface $input, OutputInterface $output): int
7374
{
74-
$directory = (string) $input->getArgument('directory') ?: \getcwd();
75-
$forceCreation = (bool) $input->getOption('force');
75+
$configuration = (new Factory())->createCreateConfig($input);
7676

77-
$this->analyser->getActualExportIgnoreAnalyser()->setDirectory($directory);
77+
$this->analyser->getActualExportIgnoreAnalyser()->setDirectory($configuration->directory);
7878

79-
$isAgenticRun = $this->isAgenticRun();
80-
81-
$generationFlavour = $input->getOption('flavour') ?: ClassicExportIgnoreAnalyser::EXPORT_IGNORE_CLASSIC;
79+
$generationFlavour = $configuration->flavour ?: ClassicExportIgnoreAnalyser::EXPORT_IGNORE_CLASSIC;
8280

8381
if (!\in_array($generationFlavour, [ClassicExportIgnoreAnalyser::EXPORT_IGNORE_CLASSIC, NegatedExportIgnoreAnalyser::EXPORT_IGNORE_NEGATED], true)) {
8482
$output->writeln('<error>Invalid flavour specified. Use <info>classic</info> or <info>negated</info>.</error>');
@@ -92,9 +90,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9290

9391
$gitattributesPath = $this->analyser->getActualExportIgnoreAnalyser()->getGitattributesFilePath();
9492

95-
if (\file_exists($gitattributesPath) && $forceCreation === false && $this->isDryRun($input) !== true) {
96-
$message = 'A .gitattributes file already exists. Use the update command to modify it.';
97-
if ($isAgenticRun) {
93+
if (\file_exists($gitattributesPath) && $configuration->forceOverwrite === false && $this->isDryRun($input) !== true) {
94+
$message = 'A .gitattributes file already exists. Use the update command or --force option to modify it.';
95+
if ($configuration->isAgenticRun) {
9896
$this->writeAgenticOutput($output, $this->getName(), false, $message);
9997
} else {
10098
$output->writeln($message);
@@ -106,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
106104

107105
if ($expected === '') {
108106
$message = 'Unable to determine expected .gitattributes content for the given directory.';
109-
if ($isAgenticRun) {
107+
if ($configuration->isAgenticRun) {
110108
$this->writeAgenticOutput($output, $this->getName(), false, $message);
111109
} else {
112110
$output->writeln($message);
@@ -124,18 +122,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124122
$this->repository->createGitattributesFile($expected);
125123
} catch (Throwable $e) {
126124
$message = 'Creation of .gitattributes file failed.';
127-
if ($isAgenticRun) {
125+
if ($configuration->isAgenticRun) {
128126
$this->writeAgenticOutput($output, $this->getName(), false, $message);
129127
} else {
130128
$output->writeln($message);
131129
}
132130
return self::FAILURE;
133131
}
134132

135-
$directory = \realpath($directory);
136-
$message = "A .gitattributes file has been created in {$directory}.";
133+
$directory = \realpath($configuration->directory);
134+
$message = "A .gitattributes file has been created in $directory.";
137135

138-
if ($isAgenticRun) {
136+
if ($configuration->isAgenticRun) {
139137
$this->writeAgenticOutput($output, $this->getName(), true, $message, ['gitattributes_file_path' => $gitattributesPath]);
140138
} else {
141139
$output->writeln($message);

src/Commands/UpdateCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ protected function configure(): void
5050
\defined('WORKING_DIRECTORY') ? WORKING_DIRECTORY : \getcwd()
5151
)->addOption(
5252
'reformat-export-ignores',
53-
null,
53+
'r',
5454
InputOption::VALUE_NONE,
5555
'Only reformat the export-ignores directives in the .gitattributes file'
56+
)->addOption(
57+
'migrate-to-negated-export-ignores',
58+
'm',
59+
InputOption::VALUE_NONE,
60+
'Migrate from classic to negated-export-ignores'
5661
)->addOption(
5762
'group',
5863
null,
@@ -77,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7782
$this->exportIgnoreAnalyser->setDirectory($directory);
7883
$isAgenticRun = $this->isAgenticRun();
7984

80-
if ((bool) $input->getOption('group')) {
85+
if ($input->getOption('group')) {
8186
$this->exportIgnoreAnalyser->setGroupNonExportIgnores(true);
8287
}
8388

src/Configuration/Create.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stolt\LeanPackage\Configuration;
4+
5+
final readonly class Create
6+
{
7+
public function __construct(
8+
public string $directory,
9+
public bool $forceOverwrite,
10+
public string $flavour ,
11+
public bool $isDryRun,
12+
public bool $isAgenticRun,
13+
) {
14+
}
15+
}

src/Configuration/ExportIgnore.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stolt\LeanPackage\Configuration;
4+
5+
final readonly class ExportIgnore
6+
{
7+
public function __construct(
8+
public string $flavour,
9+
) {
10+
}
11+
}

src/Configuration/Factory.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Configuration;
6+
7+
use Laravel\AgentDetector\AgentDetector;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
10+
final class Factory
11+
{
12+
public function createValidateConfig(
13+
InputInterface $input
14+
): Validate {
15+
return new Validate(
16+
(string) $input->getArgument('directory'),
17+
(bool) $input->getOption('dry-run'),
18+
AgentDetector::detect()->isAgent === true,
19+
);
20+
}
21+
22+
public function createCreateConfig(
23+
InputInterface $input
24+
): Create {
25+
return new Create(
26+
(string) $input->getArgument('directory') ?: WORKING_DIRECTORY,
27+
(bool) $input->getOption('force'),
28+
(string) $input->getOption('flavour'),
29+
(bool) $input->getOption('dry-run'),
30+
AgentDetector::detect()->isAgent === true,
31+
);
32+
}
33+
34+
public function createReformatConfig(
35+
InputInterface $input
36+
): Reformat {
37+
return new Reformat(
38+
(string) $input->getArgument('directory'),
39+
(bool) $input->getOption('sort-alphabetically'),
40+
(bool) $input->getOption('sort-from-directories-to-files'),
41+
(bool) $input->getOption('group'),
42+
(bool) $input->getOption('dry-run'),
43+
AgentDetector::detect()->isAgent === true,
44+
);
45+
}
46+
47+
public function createUpdateConfig(
48+
InputInterface $input
49+
): Update {
50+
return new Update(
51+
(string) $input->getArgument('directory'),
52+
(bool) $input->getOption('reformat-export-ignores'),
53+
(bool) $input->getOption('migrate-to-negated-export-ignores'),
54+
(bool) $input->getOption('group'),
55+
(bool) $input->getOption('dry-run'),
56+
AgentDetector::detect()->isAgent === true,
57+
);
58+
}
59+
60+
public function createExportIgnoreConfig(
61+
InputInterface $input
62+
): ExportIgnore {
63+
return new ExportIgnore(
64+
(string) $input->getOption('flavour'),
65+
);
66+
}
67+
}

src/Configuration/Reformat.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stolt\LeanPackage\Configuration;
4+
5+
final readonly class Reformat
6+
{
7+
public function __construct(
8+
public string $directory,
9+
public bool $sortAlphabetically,
10+
public bool $sortFromDirectoriesToFiles,
11+
public bool $groupContent,
12+
public bool $dryRun,
13+
public bool $agenticRun,
14+
) {
15+
}
16+
}

src/Configuration/Update.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stolt\LeanPackage\Configuration;
4+
5+
final readonly class Update
6+
{
7+
public function __construct(
8+
public string $directory,
9+
public bool $reformatExportIgnores,
10+
public bool $migrateToNegatedExportIgnores,
11+
public bool $group,
12+
public bool $dryRun,
13+
public bool $agenticRun,
14+
) {
15+
}
16+
}

src/Configuration/Validate.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stolt\LeanPackage\Configuration;
4+
5+
final readonly class Validate
6+
{
7+
public function __construct(
8+
public string $directory,
9+
public bool $dryRun,
10+
public bool $agenticRun,
11+
) {
12+
}
13+
}

0 commit comments

Comments
 (0)