hyperf/bin/composer-json-fixer

116 lines
3.5 KiB
Plaintext
Raw Normal View History

2019-08-02 10:09:33 +08:00
#!/usr/bin/env php
<?php
declare(strict_types=1);
2023-06-07 14:33:04 +08:00
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
2019-08-02 10:09:33 +08:00
use Symfony\Component\Finder\Finder;
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}
$options = $argv;
// ./bin/composer-json-fixer --version=~3.1.0
$version = '';
foreach ($options as $option) {
if (str_starts_with($option, '--') && $option = substr($option, 2)) {
[$key, $value] = explode('=', $option, 2);
match ($key) {
'version' => $version = $value,
default => null,
};
}
}
2019-08-02 10:09:33 +08:00
$files = Finder::create()
->in(__DIR__ . '/../src')
->exclude(['helper', 'polyfill-coroutine'])
2019-08-02 10:09:33 +08:00
->name('composer.json')
->files();
$packages = [];
foreach ($files as $file) {
$packages[] = 'hyperf/' . basename(dirname($file->getRealPath()));
}
2019-08-02 10:09:33 +08:00
$require = [];
$autoload = [];
2019-08-06 19:05:22 +08:00
$autoloadFiles = [];
2019-08-02 10:09:33 +08:00
$autoloadDev = [];
$configProviders = [];
2021-06-29 13:41:42 +08:00
$replaces = [];
foreach ($files as $fileInfo) {
$file = $fileInfo->getRealPath();
2019-08-02 10:09:33 +08:00
$component = basename(dirname($file));
$composerJson = json_decode(file_get_contents($file), true);
if ($version) {
$json = json_decode(file_get_contents($file));
foreach ($json->require ?? [] as $key => $item) {
if (in_array($key, $packages)) {
$json->require->{$key} = $version;
}
}
unset($json->{"require-dev"});
file_put_contents(
$fileInfo->getRealPath(),
json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
);
}
2023-06-07 14:33:04 +08:00
if (isset($composerJson['name']) && str_starts_with($composerJson['name'], 'hyperf')) {
2021-06-29 13:41:42 +08:00
$replaces[$composerJson['name']] = '*';
}
2019-08-02 10:09:33 +08:00
foreach ($composerJson['autoload']['files'] ?? [] as $file) {
2019-10-31 10:32:07 +08:00
$autoloadFiles[] = "src/{$component}/" . preg_replace('#^./#', '', $file);
2019-08-06 19:05:22 +08:00
}
foreach ($composerJson['autoload']['psr-4'] ?? [] as $ns => $dir) {
$value = "src/{$component}/" . trim($dir, '/') . '/';
if (isset($autoload[$ns])) {
$autoload[$ns] = [$value, ...(array) $autoload[$ns]];
} else {
$autoload[$ns] = $value;
}
2019-08-02 10:09:33 +08:00
}
foreach ($composerJson['autoload-dev']['psr-4'] ?? [] as $ns => $dir) {
$value = "src/{$component}/" . trim($dir, '/') . '/';
2023-06-07 14:33:04 +08:00
if (isset($autoloadDev[$ns])) {
$autoloadDev[$ns] = [$value, ...(array) $autoloadDev[$ns]];
2023-06-07 14:33:04 +08:00
} else {
$autoloadDev[$ns] = $value;
}
2019-08-02 10:09:33 +08:00
}
if (isset($composerJson['extra']['hyperf']['config'])) {
2023-06-07 14:33:04 +08:00
$configProviders = array_merge($configProviders, (array) $composerJson['extra']['hyperf']['config']);
2019-08-02 10:09:33 +08:00
}
}
2019-08-02 10:18:04 +08:00
2019-08-02 10:09:33 +08:00
ksort($autoload);
2019-08-06 19:05:22 +08:00
sort($autoloadFiles);
2019-08-02 10:09:33 +08:00
ksort($autoloadDev);
sort($configProviders);
2021-06-29 13:41:42 +08:00
ksort($replaces);
2019-08-02 10:18:04 +08:00
2019-08-02 10:09:33 +08:00
$json = json_decode(file_get_contents(__DIR__ . '/../composer.json'));
2019-08-06 19:05:22 +08:00
$json->autoload->files = $autoloadFiles;
2019-08-02 10:09:33 +08:00
$json->autoload->{'psr-4'} = $autoload;
$json->{'autoload-dev'}->{'psr-4'} = $autoloadDev;
$json->extra->hyperf->config = $configProviders;
2021-06-29 13:41:42 +08:00
$json->replace = $replaces;
2019-08-02 10:18:04 +08:00
2019-08-02 10:09:33 +08:00
file_put_contents(
__DIR__ . '/../composer.json',
2019-10-31 10:32:07 +08:00
json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
2019-08-02 10:09:33 +08:00
);