-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(composer): update composer-fixer script
- Updated composer-fixer script to use Composer\Config::disableProcessTimeout - Added new constants for mhash functions - Added DowngradeLevelSetList::DOWN_TO_PHP_74 to Rector config
- Loading branch information
Showing
4 changed files
with
196 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the guanguans/laravel-soar. | ||
* | ||
* (c) guanguans <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled. | ||
*/ | ||
|
||
use Composer\InstalledVersions; | ||
use Symfony\Component\Console\Input\ArgvInput; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\PhpExecutableFinder; | ||
use Symfony\Component\Process\Process; | ||
|
||
require __DIR__.'/vendor/autoload.php'; | ||
|
||
(new class() { | ||
private string $composerJsonFile = __DIR__.'/composer.json'; | ||
|
||
private SymfonyStyle $symfonyStyle; | ||
|
||
public function __construct() | ||
{ | ||
$this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput()); | ||
} | ||
|
||
public function __invoke(): void | ||
{ | ||
$this | ||
->updateComposerPackages() | ||
->updateComposerJsonFile($this->updateComposerDecodedJson()) | ||
->updateComposerPackages() | ||
->normalizeComposerJsonFile() | ||
->success(); | ||
} | ||
|
||
protected function getComposerDecodedJson(): array | ||
{ | ||
/** @noinspection JsonEncodingApiUsageInspection */ | ||
return json_decode(file_get_contents($this->composerJsonFile), true); | ||
} | ||
|
||
private function updateComposerPackages(): self | ||
{ | ||
$this->createAndMustRunProcess("{$this->findComposerBinary()} update -W --ansi -v"); | ||
|
||
return $this; | ||
} | ||
|
||
private function updateComposerJsonFile(array $composerDecodedJson): self | ||
{ | ||
/** @noinspection JsonEncodingApiUsageInspection */ | ||
file_put_contents( | ||
$this->composerJsonFile, | ||
json_encode( | ||
$composerDecodedJson, | ||
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | ||
).PHP_EOL | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
private function updateComposerDecodedJson(): array | ||
{ | ||
$composerOutdatedPackages = $this->getComposerOutdatedPackages(); | ||
$composerDecodedJson = $this->getComposerDecodedJson(); | ||
|
||
foreach ($composerDecodedJson as $option => &$value) { | ||
if (! in_array($option, ['require', 'require-dev'], true)) { | ||
continue; | ||
} | ||
|
||
foreach ($value as $package => &$version) { | ||
if ( | ||
'php' === $package | ||
|| '*' === $version | ||
|| str_starts_with($package, 'ext-') | ||
|| str_contains($version, '-') | ||
|| str_contains($version, '@') | ||
|| str_contains($version, '|') | ||
) { | ||
continue; | ||
} | ||
|
||
// Update composer outdated package version | ||
if (in_array($package, $composerOutdatedPackages, true)) { | ||
$this->updateComposerOutdatedPackage($package, $option); | ||
} | ||
|
||
// Update composer package version | ||
$version = '^'.implode( | ||
'.', | ||
array_slice( | ||
explode('.', InstalledVersions::getVersion($package), 3), | ||
0, | ||
2 | ||
) | ||
); | ||
} | ||
} | ||
|
||
return $composerDecodedJson; | ||
} | ||
|
||
private function normalizeComposerJsonFile(): self | ||
{ | ||
$this->createAndMustRunProcess("{$this->findComposerBinary()} normalize --dry-run --diff --ansi -v"); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param null|array|string $message | ||
*/ | ||
private function success($message = null): void | ||
{ | ||
$this->symfonyStyle->success($message ?: 'Composer packages updated successfully!'); | ||
|
||
exit(0); | ||
} | ||
|
||
private function getComposerOutdatedPackages(): array | ||
{ | ||
/** @noinspection JsonEncodingApiUsageInspection */ | ||
return array_map( | ||
static fn (array $package): string => $package['name'], | ||
json_decode( | ||
$this | ||
->createAndMustRunProcess("{$this->findComposerBinary()} outdated --format=json --direct --ansi -v") | ||
->getOutput(), | ||
true | ||
)['installed'] | ||
); | ||
} | ||
|
||
private function updateComposerOutdatedPackage(string $composerOutdatedPackage, string $environment): void | ||
{ | ||
try { | ||
$this->createAndMustRunProcess(sprintf( | ||
"{$this->findComposerBinary()} require $composerOutdatedPackage %s --no-scripts -W --ansi -v", | ||
'require-dev' === $environment ? '--dev' : '' | ||
)); | ||
} catch (ProcessFailedException $processFailedException) { | ||
$this->symfonyStyle->error("Failed to update composer outdated [$composerOutdatedPackage] package version."); | ||
} | ||
} | ||
|
||
private function findComposerBinary(): string | ||
{ | ||
return sprintf( | ||
'%s %s', | ||
(new PhpExecutableFinder())->find(), | ||
(new Symfony\Component\Process\ExecutableFinder())->find('composer') | ||
); | ||
} | ||
|
||
/** | ||
* @param array|string $command | ||
* @param mixed $input The input as stream resource, scalar or \Traversable, or null for no input | ||
*/ | ||
private function createAndMustRunProcess( | ||
$command, | ||
?string $cwd = null, | ||
?array $env = null, | ||
$input = null, | ||
?float $timeout = 60 | ||
): Process { | ||
$process = is_string($command) | ||
? Process::fromShellCommandline($command, $cwd, $env, $input, $timeout) | ||
: new Process($command, $cwd, $env, $input, $timeout); | ||
|
||
$this->symfonyStyle->warning($process->getCommandLine()); | ||
|
||
return $process | ||
->setWorkingDirectory(dirname($this->composerJsonFile)) | ||
->setEnv(['COMPOSER_MEMORY_LIMIT' => -1]) | ||
->mustRun(fn (string $type, string $buffer) => $this->symfonyStyle->write($buffer)); | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
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