Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);

use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPreparedSets(psr12: true, common: true, symplify: true)
->withRules([LineLengthFixer::class])
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withRootFiles();
64 changes: 59 additions & 5 deletions src/Composer/ComposerOutdatedResponseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

namespace Rector\Jack\Composer;

use Nette\Utils\DateTime;
use Nette\Utils\FileSystem;
use Symfony\Component\Process\Process;

final class ComposerOutdatedResponseProvider
{
public function provide(): string
{
// load from cache, temporarily - @todo cache on json hash + week timeout
$outdatedFilename = __DIR__ . '/../../dumped-outdated.json';
if (is_file($outdatedFilename)) {
return FileSystem::read($outdatedFilename);
$composerOutdatedFilePath = $this->resolveComposerOutdatedFilePath();

// let's use cache
if ($this->shouldLoadCacheFile($composerOutdatedFilePath)) {
/** @var string $composerOutdatedFilePath */
return FileSystem::read($composerOutdatedFilePath);
}

$composerOutdatedProcess = Process::fromShellCommandline(
Expand All @@ -23,9 +26,60 @@ public function provide(): string
);

$composerOutdatedProcess->mustRun();

$processResult = $composerOutdatedProcess->getOutput();

FileSystem::write($outdatedFilename, $processResult);
if (is_string($composerOutdatedFilePath)) {
FileSystem::write($composerOutdatedFilePath, $processResult);
}

return $processResult;
}

private function resolveProjectComposerHash(): ?string
{
if (file_exists(getcwd() . '/composer.lock')) {
return sha1(getcwd() . '/composer.lock');
}

if (file_exists(getcwd() . '/composer.json')) {
return getcwd() . '/composer.json';
}

return null;
}

private function resolveComposerOutdatedFilePath(): ?string
{
$projectComposerHash = $this->resolveProjectComposerHash();
if ($projectComposerHash) {
// load from cache, temporarily - @todo cache on json hash + week timeout
return sys_get_temp_dir() . '/jack/composer-outdated-' . $projectComposerHash . '.json';
}

return null;
}

private function isFileYoungerThanWeek(string $filePath): bool
{
$fileTime = filemtime($filePath);
if ($fileTime === false) {
return false;
}

return (time() - $fileTime) < DateTime::WEEK;
}

private function shouldLoadCacheFile(?string $cacheFilePath): bool
{
if (! is_string($cacheFilePath)) {
return false;
}

if (! file_exists($cacheFilePath)) {
return false;
}

return $this->isFileYoungerThanWeek($cacheFilePath);
}
}
4 changes: 2 additions & 2 deletions src/Composer/NextVersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
/**
* @see \Rector\Jack\Tests\Composer\NextVersionResolver\NextVersionResolverTest
*/
final class NextVersionResolver
final readonly class NextVersionResolver
{
private const MAJOR = 'major';

private const MINOR = 'minor';

public function __construct(
private readonly VersionParser $versionParser
private VersionParser $versionParser
) {
}

Expand Down
67 changes: 67 additions & 0 deletions src/Console/Command/CleanListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Rector\Jack\Console\Command;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Descriptor\ApplicationDescription;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;

/**
* Simple command list, without bloated options
*/
final class CleanListCommand extends ListCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
Assert::isInstanceOf($this->getApplication(), Application::class);

$output->writeln($this->getApplication()->getName());
$output->writeln('');
$output->writeln('<comment>Available commands:</>');

$applicationDescription = new ApplicationDescription($this->getApplication());
$this->describeCommands($applicationDescription, $output);

return self::SUCCESS;
}

/**
* @param non-empty-array<Command> $commands
*/
private function resolveCommandNameColumnWidth(array $commands): int
{
$commandNameLengths = [];
foreach ($commands as $command) {
$commandNameLengths[] = strlen((string) $command->getName());
}

return max($commandNameLengths) + 4;
}

private function describeCommands(ApplicationDescription $applicationDescription, OutputInterface $output): void
{
if ($applicationDescription->getCommands() === []) {
return;
}

$commands = $applicationDescription->getCommands();
$commandNameColumnWidth = $this->resolveCommandNameColumnWidth($commands);

foreach ($commands as $command) {
$spacingWidth = $commandNameColumnWidth - strlen((string) $command->getName());

$output->writeln(sprintf(
' <info>%s</>%s%s',
$command->getName(),
str_repeat(' ', $spacingWidth),
$command->getDescription()
));
}
}
}
26 changes: 26 additions & 0 deletions src/Console/JackConsoleApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\Jack\Console;

use Rector\Jack\Console\Command\CleanListCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\CompleteCommand;
use Symfony\Component\Console\Command\DumpCompletionCommand;
use Symfony\Component\Console\Command\HelpCommand;

final class JackConsoleApplication extends Application
{
protected function getDefaultCommands(): array
{
return [
new HelpCommand(),
new CompleteCommand(),
new DumpCompletionCommand(),

// clean list, without bloated options
new CleanListCommand(),
];
}
}
9 changes: 5 additions & 4 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Jack\DependencyInjection;

use Illuminate\Container\Container;
use Rector\Jack\Console\JackConsoleApplication;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand All @@ -23,20 +24,20 @@ public function create(): Container

// console
$container->singleton(Application::class, function (Container $container): Application {
$application = new Application('Rector Jack');
$jackConsoleApplication = new JackConsoleApplication('Rector Jack');

$commandClasses = $this->findCommandClasses();

// register commands
foreach ($commandClasses as $commandClass) {
$command = $container->make($commandClass);
$application->add($command);
$jackConsoleApplication->add($command);
}

// remove basic command to make output clear
$this->hideDefaultCommands($application);
$this->hideDefaultCommands($jackConsoleApplication);

return $application;
return $jackConsoleApplication;
});

$container->singleton(
Expand Down
3 changes: 3 additions & 0 deletions src/OutdatedComposerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Rector\Jack\Mapper\OutdatedPackageMapper;
use Rector\Jack\ValueObject\OutdatedComposer;

/**
* @see \Rector\Jack\Tests\OutdatedComposerFactory\OutdatedComposerFactoryTest
*/
final readonly class OutdatedComposerFactory
{
public function __construct(
Expand Down
6 changes: 1 addition & 5 deletions src/ValueObject/OutdatedComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ public function getPackagesShuffled(bool $onlyDev = false): array
{
// adds random effect, not to always update by A-Z, as would force too narrow pattern
// this is also more fun :)
if ($onlyDev) {
$outdatedPackages = $this->getDevPackages();
} else {
$outdatedPackages = $this->outdatedPackages;
}
$outdatedPackages = $onlyDev ? $this->getDevPackages() : $this->outdatedPackages;

shuffle($outdatedPackages);

Expand Down
6 changes: 6 additions & 0 deletions tests/OutdatedComposerFactory/Fixture/some-composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "some/project",
"require": {
"symfony/console": "^3.5"
}
}
39 changes: 39 additions & 0 deletions tests/OutdatedComposerFactory/OutdatedComposerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Jack\Tests\OutdatedComposerFactory;

use Rector\Jack\OutdatedComposerFactory;
use Rector\Jack\Tests\AbstractTestCase;
use Rector\Jack\ValueObject\OutdatedPackage;

final class OutdatedComposerFactoryTest extends AbstractTestCase
{
public function test(): void
{
$outdatedComposerFactory = $this->make(OutdatedComposerFactory::class);

$outdatedComposer = $outdatedComposerFactory->createOutdatedComposer([
[
'name' => 'symfony/console',
'direct-dependency' => true,
'homepage' => 'https://symfony.com',
'source' => 'https://github.com/symfony/console/tree/v6.4.20',
'version' => 'v6.4.20',
'release-age' => '1 month old',
'release-date' => '2025-03-03T17:16:38+00:00',
'latest' => 'v7.2.6',
'latest-status' => 'update-possible',
'latest-release-date' => '2025-04-07T19:09:28+00:00',
'description' => 'Eases the creation of beautiful and testable command line interfaces',
'abandoned' => false,
],
], __DIR__ . '/Fixture/some-composer.json');

$this->assertCount(1, $outdatedComposer->getProdPackages());
$this->assertContainsOnlyInstancesOf(OutdatedPackage::class, $outdatedComposer->getProdPackages());

$this->assertCount(0, $outdatedComposer->getDevPackages());
}
}