Skip to content

Commit ea3336f

Browse files
committed
Adds sort-from-directories-to-files option
1 parent 05c48c4 commit ea3336f

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bin/release-version export-ignore
1313
bin/start-watchman export-ignore
1414
box.json.dist export-ignore
1515
CHANGELOG.md export-ignore
16+
composer.json export-ignore
1617
example/ export-ignore
1718
LICENSE.md export-ignore
1819
lpv-logo.png export-ignore

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
99

1010
### Added
1111

12+
- Option to sort ignored artifacts from directories to files. Closes [#30](https://github.com/raphaelstolt/lean-package-validator/issues/30).
1213
- Further PHP preset expansion.
1314

1415
## [v4.1.1] - 2025-01-14

src/Analyser.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Stolt\LeanPackage;
44

5+
use PHPStan\Type\Php\DateIntervalDynamicReturnTypeExtension;
56
use Stolt\LeanPackage\Exceptions\InvalidGlobPattern;
67
use Stolt\LeanPackage\Exceptions\InvalidGlobPatternFile;
78
use Stolt\LeanPackage\Exceptions\NonExistentGlobPatternFile;
@@ -71,14 +72,22 @@ class Analyser
7172
private bool $staleExportIgnoresComparison = false;
7273

7374
/**
74-
* Whether to do a strict alignment comparsion of the export-ignores
75+
* Whether to do a strict alignment comparison of the export-ignores
7576
* in the .gitattributes files against the expected ones
7677
* or not.
7778
*
7879
* @var boolean
7980
*/
8081
private bool $strictAlignmentComparison = false;
8182

83+
/**
84+
* Whether to sort the export-ignores
85+
* in the .gitattributes from directories to files or not.
86+
*
87+
* @var boolean
88+
*/
89+
private bool $sortFromDirectoriesToFiles = false;
90+
8291
/**
8392
* Whether at least one export-ignore pattern has
8493
* a preceding slash or not.
@@ -317,6 +326,13 @@ public function enableStrictOrderComparison(): Analyser
317326
return $this;
318327
}
319328

329+
public function sortFromDirectoriesToFiles(): Analyser
330+
{
331+
$this->sortFromDirectoriesToFiles = true;
332+
333+
return $this;
334+
}
335+
320336
/**
321337
* Guard for strict order comparison.
322338
*
@@ -550,12 +566,18 @@ public function getExpectedGitattributesContent(array $postfixlessExportIgnores
550566
\sort($postfixlessExportIgnores, SORT_STRING | SORT_FLAG_CASE);
551567

552568
if (\count($postfixlessExportIgnores) > 0) {
553-
if ($this->isAlignExportIgnoresEnabled() || $this->isStrictAlignmentComparisonEnabled()) {
569+
if ($this->sortFromDirectoriesToFiles === false && ($this->isAlignExportIgnoresEnabled() || $this->isStrictAlignmentComparisonEnabled())) {
554570
$postfixlessExportIgnores = $this->getAlignedExportIgnoreArtifacts(
555571
$postfixlessExportIgnores
556572
);
557573
}
558574

575+
if ($this->sortFromDirectoriesToFiles) {
576+
$postfixlessExportIgnores = $this->getByDirectoriesToFilesExportIgnoreArtifacts(
577+
$postfixlessExportIgnores
578+
);
579+
}
580+
559581
$content = \implode(" export-ignore" . $this->preferredEol, $postfixlessExportIgnores)
560582
. " export-ignore" . $this->preferredEol;
561583

@@ -898,6 +920,22 @@ private function getAlignedExportIgnoreArtifacts(array $artifacts): array
898920
}, $artifacts);
899921
}
900922

923+
private function getByDirectoriesToFilesExportIgnoreArtifacts(array $artifacts): array
924+
{
925+
$directories = \array_filter($artifacts, function ($artifact) {
926+
if (\strpos($artifact, DIRECTORY_SEPARATOR)) {
927+
return $artifact;
928+
}
929+
});
930+
$files = \array_filter($artifacts, function ($artifact) {
931+
if (\strpos($artifact, DIRECTORY_SEPARATOR) === false) {
932+
return $artifact;
933+
}
934+
});
935+
936+
return \array_merge($directories, $files);
937+
}
938+
901939
/**
902940
* Is existing .gitattributes file having all export-ignore(s).
903941
*

src/Commands/ValidateCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected function configure(): void
102102
$keepLicenseDescription = 'Do not export-ignore the license file';
103103
$keepReadmeDescription = 'Do not export-ignore the README file';
104104
$keepGlobPatternDescription = 'Do not export-ignore matching glob pattern e.g. <comment>{LICENSE.*,README.*,docs*}</comment>';
105+
$sortDescription = 'Sort from directories to files';
105106

106107
$alignExportIgnoresDescription = 'Align export-ignores on create or overwrite';
107108

@@ -163,6 +164,12 @@ protected function configure(): void
163164
InputOption::VALUE_NONE,
164165
$alignExportIgnoresDescription
165166
);
167+
$this->addOption(
168+
'sort-from-directories-to-files',
169+
's',
170+
InputOption::VALUE_NONE,
171+
$sortDescription
172+
);
166173
$this->addOption(
167174
'omit-header',
168175
null,
@@ -226,14 +233,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
226233
$reportStaleExportIgnores = $input->getOption('report-stale-export-ignores');
227234

228235
$enforceStrictOrderComparison = $input->getOption('enforce-strict-order');
236+
$sortFromDirectoriesToFiles = $input->getOption('sort-from-directories-to-files');
229237

230-
if ($enforceStrictOrderComparison) {
238+
if ($enforceStrictOrderComparison && $sortFromDirectoriesToFiles === false) {
231239
$verboseOutput = '+ Enforcing strict order comparison.';
232240
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
233241

234242
$this->analyser->enableStrictOrderComparison();
235243
}
236244

245+
if ($sortFromDirectoriesToFiles) {
246+
$verboseOutput = '+ Sorting from files to directories.';
247+
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
248+
249+
$this->analyser->sortFromDirectoriesToFiles();
250+
}
251+
237252
if ($reportStaleExportIgnores) {
238253
$verboseOutput = '+ Enforcing stale export ignores comparison.';
239254
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);

tests/AnalyserTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,41 @@ public function exportIgnoresAreAligned(): void
12381238
);
12391239
}
12401240

1241+
#[Test]
1242+
public function exportIgnoresAreSortedFromDirectoriesToFiles(): void
1243+
{
1244+
$artifactFilenames = [
1245+
'LICENSE.txt',
1246+
'README.md',
1247+
'phpspec.yml.dist',
1248+
];
1249+
1250+
$this->createTemporaryFiles(
1251+
$artifactFilenames,
1252+
['specs', 'docs']
1253+
);
1254+
1255+
$expectedGitattributesContent = <<<CONTENT
1256+
* text=auto eol=lf
1257+
1258+
docs/ export-ignore
1259+
specs/ export-ignore
1260+
.gitattributes export-ignore
1261+
LICENSE.txt export-ignore
1262+
phpspec.yml.dist export-ignore
1263+
README.md export-ignore
1264+
1265+
CONTENT;
1266+
1267+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory)->sortFromDirectoriesToFiles();
1268+
$actualGitattributesContent = $analyser->getExpectedGitattributesContent();
1269+
1270+
$this->assertEquals(
1271+
$expectedGitattributesContent,
1272+
$actualGitattributesContent
1273+
);
1274+
}
1275+
12411276
#[Test]
12421277
#[Ticket('https://github.com/raphaelstolt/lean-package-validator/issues/4')]
12431278
public function precedingSlashesAreDetected(): void

0 commit comments

Comments
 (0)