-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
421 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
app/src/Resources/skeleton/draw-package/composer.json.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,4 +286,4 @@ | |
"vendor/bin/phpstan analyse --generate-baseline" | ||
] | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.