Skip to content

Commit b7a4e43

Browse files
committed
make use of 1 week project cache
1 parent 4e0e133 commit b7a4e43

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

src/Composer/ComposerOutdatedResponseProvider.php

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44

55
namespace Rector\Jack\Composer;
66

7+
use Nette\Utils\DateTime;
78
use Nette\Utils\FileSystem;
89
use Symfony\Component\Process\Process;
910

1011
final class ComposerOutdatedResponseProvider
1112
{
1213
public function provide(): string
1314
{
14-
// load from cache, temporarily - @todo cache on json hash + week timeout
15-
$outdatedFilename = __DIR__ . '/../../dumped-outdated.json';
16-
if (is_file($outdatedFilename)) {
17-
return FileSystem::read($outdatedFilename);
15+
$composerOutdatedFilePath = $this->resolveComposerOutdatedFilePath();
16+
17+
// let's use cache
18+
if ($this->shouldLoadCacheFile($composerOutdatedFilePath)) {
19+
/** @var string $composerOutdatedFilePath */
20+
return FileSystem::read($composerOutdatedFilePath);
1821
}
1922

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

2528
$composerOutdatedProcess->mustRun();
29+
2630
$processResult = $composerOutdatedProcess->getOutput();
2731

28-
FileSystem::write($outdatedFilename, $processResult);
32+
if (is_string($composerOutdatedFilePath)) {
33+
FileSystem::write($composerOutdatedFilePath, $processResult);
34+
}
35+
2936
return $processResult;
3037
}
38+
39+
private function resolveProjectComposerHash(): ?string
40+
{
41+
if (file_exists(getcwd() . '/composer.lock')) {
42+
return sha1(getcwd() . '/composer.lock');
43+
}
44+
45+
if (file_exists(getcwd() . '/composer.json')) {
46+
return getcwd() . '/composer.json';
47+
}
48+
49+
return null;
50+
}
51+
52+
private function resolveComposerOutdatedFilePath(): ?string
53+
{
54+
$projectComposerHash = $this->resolveProjectComposerHash();
55+
if ($projectComposerHash) {
56+
// load from cache, temporarily - @todo cache on json hash + week timeout
57+
return sys_get_temp_dir() . '/jack/composer-outdated-' . $projectComposerHash . '.json';
58+
}
59+
60+
return null;
61+
}
62+
63+
private function isFileYoungerThanWeek(string $filePath): bool
64+
{
65+
$fileTime = filemtime($filePath);
66+
if ($fileTime === false) {
67+
return false;
68+
}
69+
70+
return (time() - $fileTime) < DateTime::WEEK;
71+
}
72+
73+
private function shouldLoadCacheFile(?string $cacheFilePath): bool
74+
{
75+
if (! is_string($cacheFilePath)) {
76+
return false;
77+
}
78+
79+
if (! file_exists($cacheFilePath)) {
80+
return false;
81+
}
82+
83+
return $this->isFileYoungerThanWeek($cacheFilePath);
84+
}
3185
}

src/Composer/NextVersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
/**
1212
* @see \Rector\Jack\Tests\Composer\NextVersionResolver\NextVersionResolverTest
1313
*/
14-
final class NextVersionResolver
14+
final readonly class NextVersionResolver
1515
{
1616
private const MAJOR = 'major';
1717

1818
private const MINOR = 'minor';
1919

2020
public function __construct(
21-
private readonly VersionParser $versionParser
21+
private VersionParser $versionParser
2222
) {
2323
}
2424

src/DependencyInjection/ContainerFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public function create(): Container
2424

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

2929
$commandClasses = $this->findCommandClasses();
3030

3131
// register commands
3232
foreach ($commandClasses as $commandClass) {
3333
$command = $container->make($commandClass);
34-
$application->add($command);
34+
$jackConsoleApplication->add($command);
3535
}
3636

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

40-
return $application;
40+
return $jackConsoleApplication;
4141
});
4242

4343
$container->singleton(

src/ValueObject/OutdatedComposer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ public function getPackagesShuffled(bool $onlyDev = false): array
6666
{
6767
// adds random effect, not to always update by A-Z, as would force too narrow pattern
6868
// this is also more fun :)
69-
if ($onlyDev) {
70-
$outdatedPackages = $this->getDevPackages();
71-
} else {
72-
$outdatedPackages = $this->outdatedPackages;
73-
}
69+
$outdatedPackages = $onlyDev ? $this->getDevPackages() : $this->outdatedPackages;
7470

7571
shuffle($outdatedPackages);
7672

0 commit comments

Comments
 (0)