Skip to content

Commit

Permalink
[Bard] Various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Sep 25, 2024
1 parent b67565d commit 018afa5
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 164 deletions.
264 changes: 140 additions & 124 deletions composer.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/SonsOfPHP/Bard/dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.phar
29 changes: 11 additions & 18 deletions src/SonsOfPHP/Bard/src/Console/Command/MergeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use SonsOfPHP\Bard\Worker\File\Composer\Package\BranchAlias;
use SonsOfPHP\Bard\Worker\File\Composer\Package\Funding;
use SonsOfPHP\Bard\Worker\File\Composer\Package\Support;
use SonsOfPHP\Bard\Worker\File\Composer\Root\ClearSection;
use SonsOfPHP\Bard\Worker\File\Composer\Root\UpdateAutoloadDevSection;
use SonsOfPHP\Bard\Worker\File\Composer\Root\UpdateAutoloadSection;
use SonsOfPHP\Bard\Worker\File\Composer\Root\UpdateProvideSection;
use SonsOfPHP\Bard\Worker\File\Composer\Root\UpdateReplaceSection;
use SonsOfPHP\Bard\Worker\File\Composer\Root\UpdateRequireDevSection;
use SonsOfPHP\Bard\Worker\File\Composer\Root\UpdateRequireSection;
use SonsOfPHP\Component\Json\Json;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -31,21 +31,12 @@
*/
final class MergeCommand extends AbstractCommand
{
private readonly Json $json;

private array $bardConfig;
private JsonFile $bardConfig;

private string $mainComposerFile;

private ?HelperInterface $formatter = null;

public function __construct()
{
$this->json = new Json();

parent::__construct();
}

protected function configure(): void
{
$this
Expand All @@ -63,9 +54,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
throw new RuntimeException(sprintf('"%s" file does not exist', $bardConfigFile));
}

$this->bardConfig = $this->json->getDecoder()
->objectAsArray()
->decode(file_get_contents($bardConfigFile));
$this->bardConfig = new JsonFile($bardConfigFile);

$this->mainComposerFile = $input->getOption('working-dir') . '/composer.json';
if (!file_exists($this->mainComposerFile)) {
Expand All @@ -83,10 +72,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$rootComposerJsonFile = new JsonFile($input->getOption('working-dir') . '/composer.json');

// Clean out a few of the sections in root composer.json file
$rootComposerJsonFile = $rootComposerJsonFile->setSection('autoload', []);
$rootComposerJsonFile = $rootComposerJsonFile->setSection('autoload-dev', []);

foreach ($this->bardConfig['packages'] as $pkg) {
$rootComposerJsonFile = $rootComposerJsonFile->with(new ClearSection('autoload'));
$rootComposerJsonFile = $rootComposerJsonFile->with(new ClearSection('autoload-dev'));
$rootComposerJsonFile = $rootComposerJsonFile->with(new ClearSection('require'));
$rootComposerJsonFile = $rootComposerJsonFile->with(new ClearSection('require-dev'));
$rootComposerJsonFile = $rootComposerJsonFile->with(new ClearSection('replace'));
$rootComposerJsonFile = $rootComposerJsonFile->with(new ClearSection('provide'));

foreach ($this->bardConfig->getSection('packages') as $pkg) {
$pkgComposerFile = realpath($input->getOption('working-dir') . '/' . $pkg['path'] . '/composer.json');
if (!file_exists($pkgComposerFile)) {
$output->writeln(sprintf('No "%s" found, skipping', $packageComposerFile));
Expand Down
26 changes: 5 additions & 21 deletions src/SonsOfPHP/Bard/src/JsonFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace SonsOfPHP\Bard;

use SonsOfPHP\Component\Json\Json;

/**
* Used to manage bard.json and composer.json files
*
Expand All @@ -15,20 +13,13 @@ final class JsonFile
{
private array $config = [];

private Json $json;

public function __construct(
private string $filename,
) {
$this->json = new Json();
$this->load();
}
) {}

private function load(): void
{
$this->config = $this->json->getDecoder()
->objectAsArray()
->decode(file_get_contents($this->filename));
$this->config = json_decode(file_get_contents($this->filename), true);
}

public function getFilename(): string
Expand All @@ -43,7 +34,7 @@ public function getFilename(): string
*/
public function getSection(string $section): mixed
{
if (!isset($this->config)) {
if ([] === $this->config) {
$this->load();
}

Expand All @@ -54,7 +45,7 @@ public function getSection(string $section): mixed
*/
public function setSection(string $section, $value): self
{
if (!isset($this->config)) {
if ([] === $this->config) {
$this->load();
}

Expand All @@ -64,19 +55,12 @@ public function setSection(string $section, $value): self
return $clone;
}

/**
*/
public function toJson(): string
{
return $this->json->getEncoder()
->prettyPrint()
->unescapedUnicode()
->unescapedSlashes()
->encode($this->config);
return json_encode($this->config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

/**
* $operator = new Operator();
* $jsonFile->with(new ExampleOperator());
*/
public function with($operator): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function __construct(private JsonFile $rootComposerJsonFile) {}
public function apply(JsonFile $pkgComposerJsonFile): JsonFile
{
$rootSupportSection = $this->rootComposerJsonFile->getSection('support');
$pkgSupportSection = $pkgComposerJsonFile->getSection('support');

// Docs may be different for package so we do not want to overwrite
// that value
$rootSupportSection['docs'] = $pkgSupportSection['docs'];

return $pkgComposerJsonFile->setSection('support', $rootSupportSection);
}
Expand Down
21 changes: 21 additions & 0 deletions src/SonsOfPHP/Bard/src/Worker/File/Composer/Root/ClearSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Bard\Worker\File\Composer\Root;

use SonsOfPHP\Bard\JsonFile;
use SonsOfPHP\Bard\Worker\WorkerInterface;

/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
final readonly class ClearSection implements WorkerInterface
{
public function __construct(private string $section) {}

public function apply(JsonFile $rootComposerJsonFile): JsonFile
{
return $rootComposerJsonFile->setSection($this->section, []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ public function apply(JsonFile $rootComposerJsonFile): JsonFile

if (isset($rootAutoloadSection['psr-4'])) {
$rootAutoloadSection['psr-4'] = array_unique($rootAutoloadSection['psr-4']);
ksort($rootAutoloadSection['psr-4']);
}

if (isset($rootAutoloadSection['exclude-from-classmap'])) {
$rootAutoloadSection['exclude-from-classmap'] = array_unique($rootAutoloadSection['exclude-from-classmap']);
ksort($rootAutoloadSection['exclude-from-classmap']);
}

ksort($rootAutoloadSection);

return $rootComposerJsonFile->setSection('autoload-dev', $rootAutoloadSection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ public function apply(JsonFile $rootComposerJsonFile): JsonFile

if (isset($rootAutoloadSection['psr-4'])) {
$rootAutoloadSection['psr-4'] = array_unique($rootAutoloadSection['psr-4']);
ksort($rootAutoloadSection['psr-4']);
}

if (isset($rootAutoloadSection['exclude-from-classmap'])) {
$rootAutoloadSection['exclude-from-classmap'] = array_unique($rootAutoloadSection['exclude-from-classmap']);
ksort($rootAutoloadSection['exclude-from-classmap']);
}

ksort($rootAutoloadSection);

return $rootComposerJsonFile->setSection('autoload', $rootAutoloadSection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function apply(JsonFile $rootComposerJsonFile): JsonFile
$rootProvideSection[$pkg] = $version;
}

ksort($rootProvideSection);

return $rootComposerJsonFile->setSection('provide', $rootProvideSection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function apply(JsonFile $rootComposerJsonFile): JsonFile
$pkgName = $this->pkgComposerJsonFile->getSection('name');

$rootReplace[$pkgName] = 'self.version';
ksort($rootReplace);

return $rootComposerJsonFile->setSection('replace', $rootReplace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function apply(JsonFile $rootComposerJsonFile): JsonFile
$rootRequireDev[$package] = $version;
}

ksort($rootRequireDev);

return $rootComposerJsonFile->setSection('require-dev', $rootRequireDev);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function apply(JsonFile $rootComposerJsonFile): JsonFile
$rootRequire[$package] = $version;
}

ksort($rootRequire);

return $rootComposerJsonFile->setSection('require', $rootRequire);
}
}
2 changes: 1 addition & 1 deletion src/SonsOfPHP/Component/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@
"url": "https://tidelift.com/subscription/pkg/packagist-sonsofphp-sonsofphp"
}
]
}
}

0 comments on commit 018afa5

Please sign in to comment.