Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[EasyCI] Move ValidateFileLengthCommand to easy-ci command (#2913)
Browse files Browse the repository at this point in the history
* [EasyCI] Move ValidateFileLengthCommand to easy-ci command

* fix

* fix

* fix
  • Loading branch information
samsonasik authored Feb 5, 2021
1 parent 055e5d9 commit 155e540
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
name: Check for Git Conflicts
run: packages/easy-ci/bin/easy-ci check-conflicts .

-
name: Check File Length
run: packages/easy-ci/bin/easy-ci validate-file-length packages

-
name: Composer Validate
run: composer validate --strict
Expand Down
81 changes: 81 additions & 0 deletions packages/easy-ci/src/Command/ValidateFileLengthCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCI\Command;

use Symplify\EasyCI\Finder\ProjectFilesFinder;
use Symplify\EasyCI\Resolver\TooLongFilesResolver;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\ValueObject\Option;
use Symfony\Component\Console\Input\InputArgument;

final class ValidateFileLengthCommand extends Command
{
/**
* @var SymfonyStyle
*/
private $symfonyStyle;

/**
* @var ProjectFilesFinder
*/
private $projectFilesFinder;

/**
* @var TooLongFilesResolver
*/
private $tooLongFilesResolver;

public function __construct(
ProjectFilesFinder $projectFilesFinder,
SymfonyStyle $symfonyStyle,
TooLongFilesResolver $tooLongFilesResolver
) {
$this->symfonyStyle = $symfonyStyle;

parent::__construct();

$this->projectFilesFinder = $projectFilesFinder;
$this->tooLongFilesResolver = $tooLongFilesResolver;
}

protected function configure(): void
{
$this->setDescription('[CI] Make sure the file path length are not breaking normal Windows max length');
$this->addArgument(Option::SOURCES, InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Path to project');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string[] $sources */
$sources = (array) $input->getArgument(Option::SOURCES);

$fileInfos = $this->projectFilesFinder->find($sources);
$tooLongFileInfos = $this->tooLongFilesResolver->resolve($fileInfos);

if ($tooLongFileInfos === []) {
$message = sprintf('Checked %d files - all fit max file length', count($fileInfos));
$this->symfonyStyle->success($message);

return ShellCode::SUCCESS;
}

foreach ($tooLongFileInfos as $tooLongFileInfo) {
$message = sprintf(
'Paths for file "%s" has %d chars, but must be shorter than %d.',
$tooLongFileInfo->getRealPath(),
strlen($tooLongFileInfo->getRealPath()),
TooLongFilesResolver::MAX_FILE_LENGTH
);

$this->symfonyStyle->warning($message);
}

return ShellCode::ERROR;
}
}
34 changes: 34 additions & 0 deletions packages/easy-ci/src/Finder/ProjectFilesFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCI\Finder;

use Symplify\SmartFileSystem\Finder\SmartFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ProjectFilesFinder
{
/**
* @var SmartFinder
*/
private $smartFinder;

public function __construct(SmartFinder $smartFinder)
{
$this->smartFinder = $smartFinder;
}

/**
* @return SmartFileInfo[]
*/
public function find(array $sources): array
{
$paths = [];
foreach ($sources as $source) {
$paths[] = getcwd() . DIRECTORY_SEPARATOR . $source;
}

return $this->smartFinder->find($paths, '*');
}
}
28 changes: 28 additions & 0 deletions packages/easy-ci/src/Resolver/TooLongFilesResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCI\Resolver;

use Symplify\SmartFileSystem\SmartFileInfo;

final class TooLongFilesResolver
{
/**
* In windows the max-path length is 260 chars. we give a bit room for the path up to the rector project
* @var int
*/
public const MAX_FILE_LENGTH = 200;

/**
* @param SmartFileInfo[] $fileInfos
* @return SmartFileInfo[]
*/
public function resolve(array $fileInfos): array
{
return array_filter($fileInfos, function (SmartFileInfo $fileInfo): bool {
$filePathLength = strlen($fileInfo->getRealPath());
return $filePathLength > self::MAX_FILE_LENGTH;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ protected function setUp(): void
public function testFileProvingNeedOfProperSupportOfChangesets(): void
{
$smartFileInfo = new SmartFileInfo(
__DIR__ . '/FileProcessorSource/ReferenceUsedNamesOnlySniff/FileProvingNeedOfProperSupportOfChangesets.php.inc'
__DIR__ . '/FileProcessorSource/ReferenceUsedNamesOnlySniff/FileProveNeedSupportChangesets.php.inc'
);

$changedContent = $this->sniffFileProcessor->processFile($smartFileInfo);
$this->assertStringEqualsFile(
__DIR__ . '/FileProcessorSource/ReferenceUsedNamesOnlySniff/FileProvingNeedOfProperSupportOfChangesets-fixed.php.inc',
__DIR__ . '/FileProcessorSource/ReferenceUsedNamesOnlySniff/FileProveNeedSupportChangesets-fix.php.inc',
$changedContent
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare(strict_types=1);

namespace Foo;

final class FileProvingNeedOfProperSupportOfChangesets
final class FileProveNeedSupportChangesets
{
/**
* @var \Foo\Baz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare(strict_types=1);

namespace Foo;

final class FileProvingNeedOfProperSupportOfChangesets
final class FileProveNeedSupportChangesets
{
/**
* @var \Foo\Baz
Expand Down

0 comments on commit 155e540

Please sign in to comment.