Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: split repo validation command #375

Merged
merged 3 commits into from
Sep 4, 2024
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: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
->in(__DIR__ . DIRECTORY_SEPARATOR . 'tests')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')
->exclude(__DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'Template')
->append(['.php-cs-fixer.php', 'monorepo-builder.php', 'rector.php']);
->append(['.php-cs-fixer.php', 'monorepo-builder.php', 'rector.php', './bin/split-repo.php']);

$rules = [
'@Symfony' => true,
Expand Down
115 changes: 115 additions & 0 deletions bin/split-repo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

use System\Console\Command;
use System\Console\Style\ProgressBar;

use function System\Console\fail;
use function System\Console\ok;
use function System\Console\style;
use function System\Console\warn;

require_once __DIR__ . '../../vendor/autoload.php';

$command = new class($argv) extends Command {
public function entry(): int
{
return match (true) {
'validate' === $this->CMD => $this->validate(),
default => 0,
};
}

public function validate(): int
{
$config = $this->loadConfig();
$version = $config['tag_version'];
$split_repositorys = $config['split_repositorys'];
$paths = array_values($split_repositorys);
$packages = array_keys($split_repositorys);
$progressbar = new ProgressBar();
$progressbar->complete = static fn (): string => (string) ok('Done, your monorepo is greate!!!');
$progressbar->maks = count($paths);

foreach ($paths as $path) {
if (false === $this->validateComposerVersion($composer_path = dirname(__DIR__) . $path . 'composer.json', $packages, $version)) {
$progressbar->complete = static fn (): string => (string) fail('failed!');
$progressbar->current = $progressbar->maks + 1;
$progressbar->tick();

warn('Conflig found')->out(false);

if (file_exists($composer_path)) {
style('your version: ')->push($version)->textDim()->out();
$composer = $this->loadComposer($composer_path);
foreach ($composer['require'] as $package => $package_version) {
if (in_array($package, $packages)) {
style($package)->textYellow()->push(':')->push($package_version)->textDim()->out();
}
}
}

return 1;
}

$progressbar->current++;
$progressbar->tick();
}

return 0;
}

/**
* Validate Composer pacakge version by compire with current verstion.
*
* @param string[] $packages
*/
private function validateComposerVersion(string $path, array $packages, string $version): bool
{
if (false === file_exists($path)) {
return true;
}

$composer = $this->loadComposer($path);

if (false === array_key_exists('require', $composer)) {
return true;
}

foreach ($composer['require'] as $package => $package_version) {
$package_version = preg_replace('/[^\d\.]/', '', $package_version);
if (in_array($package, $packages)) {
if ($package_version !== $version) {
return false;
}
}
}

return true;
}

/**
* Load split configuration.
*
* @return array<string, string|array<string, string>>
*/
private function loadConfig(): array
{
return require __DIR__ . '../../split-repo.php';
}

/**
* Load composer as array.
*
* @return array<string, mixed>
*/
private function loadComposer(string $path): array
{
$composer_file = file_get_contents($path);

return json_decode($composer_file, true);
}
};

exit($command->entry());
35 changes: 19 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@
]
},
"replace": {
"sonypradana/collection": "0.35.6",
"sonypradana/console": "0.35.6",
"sonypradana/container": "0.35.6",
"sonypradana/cron": "0.35.6",
"sonypradana/database": "0.35.6",
"sonypradana/file": "0.35.6",
"sonypradana/http": "0.35.6",
"sonypradana/router": "0.35.6",
"sonypradana/security": "0.35.6",
"sonypradana/support": "0.35.6",
"sonypradana/template": "0.35.6",
"sonypradana/text": "0.35.6",
"sonypradana/time": "0.35.6",
"sonypradana/view": "0.35.6"
"sonypradana/collection": "self.version",
"sonypradana/console": "self.version",
"sonypradana/container": "self.version",
"sonypradana/cron": "self.version",
"sonypradana/database": "self.version",
"sonypradana/file": "self.version",
"sonypradana/http": "self.version",
"sonypradana/router": "self.version",
"sonypradana/security": "self.version",
"sonypradana/support": "self.version",
"sonypradana/template": "self.version",
"sonypradana/text": "self.version",
"sonypradana/time": "self.version",
"sonypradana/view": "self.version"
},
"extra": {
"branch-alias": {
Expand All @@ -75,17 +75,20 @@
"test:lint": "php-cs-fixer fix -v --dry-run --diff",
"test:types": "phpstan analyse --ansi --memory-limit=-1 --debug",
"test:unit": "phpunit --testdox --exclude-group database",
"test:release": "php .\\bin\\split-repo.php validate",
"test:dev": [
"@test:refacto",
"@test:lint",
"@test:types",
"@test:unit"
"@test:unit",
"@test:release"
],
"test": [
"rector --dry-run",
"php-cs-fixer fix -v --dry-run",
"phpstan analyse --ansi --memory-limit=-1",
"phpunit --exclude-group database"
"phpunit --exclude-group database",
"php .\\bin\\split-repo.php validate"
]
},
"minimum-stability": "dev",
Expand Down
24 changes: 24 additions & 0 deletions split-repo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

return [
'tag_version' => "0.35.6",
'split_repositorys' => [
'sonypradana/collection' => '/src/System/Collection/',
'sonypradana/console' => '/src/System/Console/',
'sonypradana/container' => '/src/System/Container/',
'sonypradana/cron' => '/src/System/Cron/',
'sonypradana/database' => '/src/System/Database/',
'sonypradana/file' => '/src/System/File/',
'sonypradana/http' => '/src/System/Http/',
'sonypradana/macroable' => '/src/System/Macroable/',
'sonypradana/router' => '/src/System/Router/',
'sonypradana/security' => '/src/System/Security/',
'sonypradana/support' => '/src/System/Support/',
'sonypradana/template' => '/src/System/Template/',
'sonypradana/text' => '/src/System/Text/',
'sonypradana/time' => '/src/System/Time/',
'sonypradana/view' => '/src/System/View/',
],
];
Loading