Skip to content

Commit

Permalink
ci: split repo validation command
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Sep 4, 2024
1 parent 1074f8c commit ed8e98f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
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());
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/',
],
];

0 comments on commit ed8e98f

Please sign in to comment.