Skip to content

Commit

Permalink
chore(composer): update composer-fixer script
Browse files Browse the repository at this point in the history
- 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
guanguans committed Jan 11, 2024
1 parent 6aaec44 commit 954ae35
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 63 deletions.
186 changes: 186 additions & 0 deletions composer-fixer
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));
}
})();
62 changes: 0 additions & 62 deletions composer-fixer.php

This file was deleted.

5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@
"checks-parallel": "@composer-parallel composer-validate md-lint lint style-lint test psalm",
"composer-bin-all-update": "@composer bin all update --ansi -v",
"composer-check-platform-reqs": "@composer check-platform-reqs --lock --ansi -v",
"composer-fixer": "@php ./composer-fixer.php",
"composer-fixer": [
"Composer\\Config::disableProcessTimeout",
"@php ./composer-fixer"
],
"composer-normalize": "@composer normalize --dry-run --diff --ansi -v",
"composer-parallel": "@composer parallel --ansi -v",
"composer-unused": "@php ./vendor/bin/composer-unused --ansi -v",
Expand Down
6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;

return static function (RectorConfig $rectorConfig): void {
define('MHASH_XXH3', 2 << 0);
define('MHASH_XXH32', 2 << 1);
define('MHASH_XXH64', 2 << 2);
define('MHASH_XXH128', 2 << 3);
$rectorConfig->importNames(false, false);
$rectorConfig->importShortClasses(false);
// $rectorConfig->disableParallel();
Expand Down Expand Up @@ -152,6 +157,7 @@
]);

$rectorConfig->sets([
DowngradeLevelSetList::DOWN_TO_PHP_74,
LevelSetList::UP_TO_PHP_74,
SetList::PHP_74,
SetList::CODE_QUALITY,
Expand Down

0 comments on commit 954ae35

Please sign in to comment.