Skip to content

Commit

Permalink
[Internal] Maker draw-package
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Apr 11, 2024
1 parent 4fff0a3 commit 4b178ad
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,34 @@ jobs:
name: After Split Testing of ${{ matrix.package_name }}

steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2

- name: Checkout
uses: 'actions/checkout@v2'

- name: PHP Setup
uses: 'shivammathur/setup-php@v2'
with:
php-version: '8.1'
coverage: none

- name: Setup MySQL
uses: shogo82148/actions-setup-mysql@v1
uses: 'shogo82148/actions-setup-mysql@v1'
with:
mysql-version: 8.0
distribution: mysql
auto-start: true

- name: Create Database
run: |
mysql -h 127.0.0.1 -uroot -e "CREATE DATABASE draw"
- name: 'Composer Setup'
run: |
composer install --no-progress
vendor-bin/monorepo/vendor/bin/monorepo-builder localize-composer-paths
cd packages/${{ matrix.package_name }}
composer update --no-progress
- name: 'Automation Test'
run: |
cd packages/${{ matrix.package_name }}
Expand Down
157 changes: 157 additions & 0 deletions app/src/Maker/MakeDrawPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace App\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Twig\Environment;

class MakeDrawPackage extends AbstractMaker
{
public function __construct(
#[Autowire('%kernel.project_dir%')]
private string $kernelProjectDir,
private Environment $environment
) {
}

public static function getCommandName(): string
{
return 'make:draw-package';
}

public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('type', InputArgument::REQUIRED, 'The type of the package:: (component|bundle)')
->addArgument('name', InputArgument::REQUIRED, 'The name of the package: Example: my-package')
->addArgument('description', InputArgument::REQUIRED, 'The description of the package');
}

public function configureDependencies(DependencyBuilder $dependencies): void
{
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$type = $input->getArgument('type');
$packageName = $input->getArgument('name');

if (!\in_array($type, ['component', 'bundle'])) {
$io->error('Invalid package type');

return;
}

if (empty($packageName)) {
$io->error('Invalid package name');

return;
}

if ('bundle' === $type) {
if (!str_ends_with($packageName, '-bundle')) {
$io->error('Package name should end with -bundle');

return;
}
} else {
if (str_ends_with($packageName, '-bundle')) {
$io->error('Package name should not end with -bundle');

return;
}
}

$namespace = $this->getNamespace($packageName, $type);

$generator
->dumpFile(
'packages/'.$packageName.'/composer.json',
$this->environment->render(
$this->environment->createTemplate(file_get_contents(__DIR__.'/../Resources/skeleton/draw-package/composer.json.twig')),
[
'packageName' => $packageName,
'packageDescription' => $input->getArgument('description'),
'namespace' => $namespace,
'type' => $type,
]
)
);

$generator
->dumpFile(
'packages/'.$packageName.'/phpunit.xml.dist',
file_get_contents(__DIR__.'/../Resources/skeleton/draw-package/phpunit.xml.dist')
);

$generator
->dumpFile(
$this->kernelProjectDir.'/composer.json',
$this->getNewComposerContents($packageName, $namespace)
);

$generator
->dumpFile(
$this->kernelProjectDir.'/.github/workflows/after_splitting_test.yaml',
$this->getNewGithubWorkflowsAfterSplittingTestContents($packageName)
);

$generator->writeChanges();
}

private function getNewComposerContents(string $packageName, string $namespace): string
{
$composer = json_decode(
file_get_contents($this->kernelProjectDir.'/composer.json'),
true
);

$composer['autoload']['psr-4'][$namespace.'\\'] = 'packages/'.$packageName.'/';

ksort($composer['autoload']['psr-4']);

$composer['replace']['draw/'.$packageName] = 'self.version';

ksort($composer['replace']);

return json_encode($composer, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES);
}

private function getNewGithubWorkflowsAfterSplittingTestContents(string $packageName): string
{
$yamlSourceManipulator = new YamlSourceManipulator(
file_get_contents($this->kernelProjectDir.'/.github/workflows/after_splitting_test.yaml')
);

$data = $yamlSourceManipulator->getData();

// Sorting doesn't work with the current implementation of YamlSourceManipulator
$data['jobs']['after_split_testing']['strategy']['matrix']['package_name'][] = $packageName;

$yamlSourceManipulator->setData($data);

return $yamlSourceManipulator->getContents();
}

private function getNamespace(string $packageName, string $type): string
{
$namespace = str_replace('-', ' ', $packageName);
$namespace = ucwords($namespace);
$namespace = str_replace(' ', '', $namespace);

return sprintf(
'Draw\%s\%s',
'component' === $type ? 'Component' : 'Bundle',
$namespace
);
}
}
31 changes: 31 additions & 0 deletions app/src/Resources/skeleton/draw-package/composer.json.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "draw/{{ packageName }}",
"description": "{{ packageDescription }}",
"license": "MIT",
"type": "library",
"keywords": ["draw", "{{ type }}", "symfony"],
"authors": [
{
"name": "Martin Poirier Theoret",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"{{ namespace|replace({'\\':'\\\\'}) }}\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "0.11-dev"
}
}
}
9 changes: 9 additions & 0 deletions app/src/Resources/skeleton/draw-package/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<testsuites>
<testsuite name="Main">
<directory>./Tests</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,4 @@
"vendor/bin/phpstan analyse --generate-baseline"
]
}
}
}
94 changes: 93 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
Draw\Bundle\SonataIntegrationBundle\DrawSonataIntegrationBundle::class => ['all' => true],
Draw\Bundle\FrameworkExtraBundle\DrawFrameworkExtraBundle::class => ['all' => true],
Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];
Loading

0 comments on commit 4b178ad

Please sign in to comment.