Select Git revision
rollup.config.js
-
Steinwender, Tamara authoredSteinwender, Tamara authored
bundle-generator 4.64 KiB
#!/usr/bin/env php
<?php
/**
* This script generates DBP Symfony bundles
* Example parameters to show generated names:
* --unique-name=greenlight --friendly-name="Electronic Covid Access Permits" --example-entity=Permit
*
* Use "--generate-bundle" if you also want to generate the bundle in a subdirectory of the current directory
*/
declare(strict_types=1);
$longOpts = [
'vendor::',
'category::',
'unique-name:',
'friendly-name:',
'example-entity::',
'generate-bundle',
];
$options = getopt('', $longOpts);
if (!isset($options['unique-name'])) {
echo "Please set a --unique-name!\n";
exit(1);
}
if (!isset($options['friendly-name'])) {
echo "Please set a --friendly-name!\n";
exit(1);
}
$vendor = $options['vendor'] ?? 'dbp';
$category = $options['category'] ?? 'relay';
$uniqueName = $options['unique-name'];
$friendlyName = $options['friendly-name'];
$exampleEntity = $options['example-entity'] ?? 'Place';
$generateBundle = isset($options['generate-bundle']);
function plural(string $in): string
{
return $in.'s';
}
function normalize(string $in): string
{
return strtolower(str_replace(['-', '_'], ' ', $in));
}
function pascal(string $in): string
{
return str_replace(' ', '', ucwords(normalize($in)));
}
function kebap(string $in): string
{
return str_replace(' ', '-', normalize($in));
}
function snake(string $in): string
{
return str_replace(' ', '_', normalize($in));
}
function color(string $in): string
{
return "\033[32m".$in." \033[0m";
}
/**
* Lists all files in a folder by a pattern recursively
*/
function recursiveFileList($folder, $pattern): array
{
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
if (is_file($file[0])) {
$fileList[] = $file[0];
}
}
return $fileList;
}
/**
* Replaces text in files recursively
*/
function recursiveFileTextReplace($folder, $pattern, $replace)
{
foreach(recursiveFileList($folder, "/.+/") as $file) {
$content = file_get_contents($file);
$content = str_replace($pattern, $replace, $content);
file_put_contents($file, $content);
}
}
$directoryName = kebap($category).'-'.kebap($uniqueName).'-bundle';
$composerPackageName = kebap($vendor).'/'.kebap($category).'-'.kebap($uniqueName).'-bundle';
printf("%25s: %s\n", 'Composer Package Name', color($composerPackageName));
$phpNamespace = pascal($vendor).'\\'.pascal($category).'\\'.pascal($uniqueName).'Bundle';
printf("%25s: %s\n", 'PHP Namespace', color($phpNamespace));
$symfonyBundleBaseName = pascal($vendor).pascal($category).pascal($uniqueName);
$symfonyBundleName = $symfonyBundleBaseName.'Bundle';
printf("%25s: %s\n", 'Symfony Bundle Name', color($symfonyBundleName));
$bundleConfigKey = snake($vendor.' '.$category.' '.$uniqueName);
printf("%25s: %s\n", 'Bundle Config Key', color($bundleConfigKey));
$phpClassName = pascal($exampleEntity);
$variableName = lcfirst($phpClassName);
printf("%25s: %s\n", 'PHP Class Name', color($phpClassName));
$apiPlatformShortName = pascal($uniqueName).pascal($exampleEntity);
printf("%25s: %s\n", 'API-Platform Short Name', color($apiPlatformShortName));
$resourcePath = '/'.kebap($uniqueName).'/'.kebap(plural($exampleEntity));
printf("%25s: %s\n", 'Resource Path', color($resourcePath));
$serializationGroup = pascal($uniqueName).pascal($exampleEntity).':some-group';
printf("%25s: %s\n", 'Serialization Group', color($serializationGroup));
$openAPITag = $friendlyName;
printf("%25s: %s\n", 'Open API Tag', color($openAPITag));
$gitRepositoryName = kebap($vendor).'-'.kebap($category).'-'.kebap($uniqueName).'-bundle';
printf("%25s: %s\n", 'GIT Repository Name', color($gitRepositoryName));
echo "\n";
if (!$generateBundle) {
exit;
}
exec("git clone --depth=1 https://gitlab.tugraz.at/dbp/relay/dbp-relay-template-bundle.git \"$directoryName\"", $output, $resultCode);
if ($resultCode !== 0) {
echo "Could not clone template bundle into directory '$directoryName'!\n";
exit -1;
}
if (!chdir($directoryName)) {
echo "Could not change to directory '$directoryName'!\n";
exit -1;
}
// Remove git folder
exec('rm -rf .git');
$args = [
'./.bundle-rename',
'--vendor='.escapeshellarg($vendor),
'--category='.escapeshellarg($category),
'--unique-name='.escapeshellarg($uniqueName),
'--friendly-name='.escapeshellarg($friendlyName),
'--example-entity='.escapeshellarg($exampleEntity),
];
$cmd = implode(' ', $args);
$res = exec($cmd);
if ($res === false) {
echo "failed";
exit -1;
}
exec('rm -rf .bundle-rename');