-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New command useful to limit the amount of testing in a project. (#10)
feat: add new command ChangedFiles
- Loading branch information
1 parent
0844062
commit 4318c65
Showing
5 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Supportive\Console\Command; | ||
|
||
use Qossmic\Deptrac\Supportive\Console\Symfony\Style; | ||
use Qossmic\Deptrac\Supportive\Console\Symfony\SymfonyOutput; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
class ChangedFilesCommand extends Command | ||
{ | ||
public static $defaultName = 'changed-files'; | ||
public static $defaultDescription = 'Lists layers corresponding to the changed files'; | ||
|
||
public function __construct( | ||
private readonly ChangedFilesRunner $runner, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
parent::configure(); | ||
$this->addOption('with-dependencies', null, InputOption::VALUE_NONE, 'show dependent layers'); | ||
$this->addArgument('files', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'List of changed files'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
ini_set('memory_limit', '-1'); | ||
|
||
$symfonyOutput = new SymfonyOutput($output, new Style(new SymfonyStyle($input, $output))); | ||
|
||
try { | ||
/** @var list<string> $files */ | ||
$files = $input->getArgument('files'); | ||
$this->runner->run($files, (bool) $input->getOption('with-dependencies'), $symfonyOutput); | ||
} catch (CommandRunException) { | ||
return self::FAILURE; | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Supportive\Console\Command; | ||
|
||
use Qossmic\Deptrac\Contract\OutputFormatter\OutputInterface; | ||
use Qossmic\Deptrac\Contract\Result\CoveredRuleInterface; | ||
use Qossmic\Deptrac\Contract\Result\OutputResult; | ||
use Qossmic\Deptrac\Contract\Result\RuleInterface; | ||
use Qossmic\Deptrac\Core\Analyser\AnalyserException; | ||
use Qossmic\Deptrac\Core\Analyser\DependencyLayersAnalyser; | ||
use Qossmic\Deptrac\Core\Analyser\LayerForTokenAnalyser; | ||
use Qossmic\Deptrac\Core\Analyser\TokenType; | ||
|
||
/** | ||
* @internal Should only be used by ChangedFilesCommand | ||
*/ | ||
final class ChangedFilesRunner | ||
{ | ||
public function __construct( | ||
private readonly LayerForTokenAnalyser $layerForTokenAnalyser, | ||
private readonly DependencyLayersAnalyser $dependencyLayersAnalyser | ||
) {} | ||
|
||
/** | ||
* @param list<string> $files | ||
* | ||
* @throws CommandRunException | ||
*/ | ||
public function run(array $files, bool $withDependencies, OutputInterface $output): void | ||
{ | ||
try { | ||
$layers = []; | ||
foreach ($files as $file) { | ||
$matches = $this->layerForTokenAnalyser->findLayerForToken($file, TokenType::FILE); | ||
foreach ($matches as $match) { | ||
foreach ($match as $layer) { | ||
$layers[$layer] = $layer; | ||
} | ||
} | ||
} | ||
$output->writeLineFormatted(implode(';', $layers)); | ||
} catch (AnalyserException $exception) { | ||
throw CommandRunException::analyserException($exception); | ||
} | ||
|
||
if ($withDependencies) { | ||
try { | ||
$result = OutputResult::fromAnalysisResult($this->dependencyLayersAnalyser->analyse()); | ||
$layersDependOnLayers = $this->calculateLayerDependencies($result->allRules()); | ||
|
||
$layerDependencies = []; | ||
foreach ($layers as $layer) { | ||
$layerDependencies += $layersDependOnLayers[$layer] ?? []; | ||
} | ||
do { | ||
$size = count($layerDependencies); | ||
$layerDependenciesCopy = $layerDependencies; | ||
foreach ($layerDependenciesCopy as $layerDependency) { | ||
$layerDependencies += $layersDependOnLayers[$layerDependency] ?? []; | ||
} | ||
} while ($size !== count($layerDependencies)); | ||
|
||
$output->writeLineFormatted(implode(';', $layerDependencies)); | ||
} catch (AnalyserException $exception) { | ||
throw CommandRunException::analyserException($exception); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param RuleInterface[] $rules | ||
* | ||
* @return array<string, array<string, string>> | ||
*/ | ||
private function calculateLayerDependencies(array $rules): array | ||
{ | ||
$layersDependOnLayers = []; | ||
|
||
foreach ($rules as $rule) { | ||
if (!$rule instanceof CoveredRuleInterface) { | ||
continue; | ||
} | ||
|
||
$layerA = $rule->getDependerLayer(); | ||
$layerB = $rule->getDependentLayer(); | ||
|
||
if (!isset($layersDependOnLayers[$layerB])) { | ||
$layersDependOnLayers[$layerB] = []; | ||
} | ||
|
||
if (!array_key_exists($layerA, $layersDependOnLayers[$layerB])) { | ||
$layersDependOnLayers[$layerB][$layerA] = $layerA; | ||
} | ||
} | ||
|
||
return $layersDependOnLayers; | ||
} | ||
} |