refactor: extract functions

This commit is contained in:
Oliver Davies 2023-04-20 22:42:39 +01:00
parent ac1e0dc2c8
commit 4bf72b59e3

View file

@ -81,80 +81,14 @@ $app->command(
$configurationData['docker-compose'] = null;
}
$configurationData['managedText'] = 'Do not edit this file. It is automatically generated by \'build-configs\'.';
$filesToGenerate = getFiles(configurationData: $configurationData);
$io->info("Building configuration for {$configurationData['name']}.");
/** @var Collection<int, TemplateFile> */
$filesToGenerate = collect([
new TemplateFile(data: 'common/.dockerignore', name: '.dockerignore'),
new TemplateFile(data: 'common/.hadolint.yaml', name: '.hadolint.yaml'),
new TemplateFile(data: 'env.example', name: '.env.example'),
]);
$extraDatabases = Arr::get($configurationData, 'database.extra_databases', []);
if (count($extraDatabases) > 0) {
$filesToGenerate[] = new TemplateFile(
data: 'extra-databases.sql',
name: 'extra-databases.sql',
path: 'tools/docker/images/database/root/docker-entrypoint-initdb.d',
);
}
if (false !== Arr::get($configurationData, "justfile", true)) {
$filesToGenerate[] = new TemplateFile(data: 'justfile', name: 'justfile');
}
if (isset($configurationData['dockerCompose']) && $configurationData['dockerCompose'] !== null) {
$filesToGenerate[] = new TemplateFile(data: 'docker-compose.yaml', name: 'docker-compose.yaml');
}
if (isPhp(Arr::get($configurationData, 'language'))) {
$filesToGenerate[] = new TemplateFile(data: 'php/Dockerfile', name: 'Dockerfile');
$filesToGenerate[] = new TemplateFile(data: 'php/phpcs.xml', name: 'phpcs.xml.dist');
$filesToGenerate[] = new TemplateFile(data: 'php/phpstan.neon', name: 'phpstan.neon.dist');
$filesToGenerate[] = new TemplateFile(data: 'php/phpunit.xml', name: 'phpunit.xml.dist');
$filesToGenerate[] = new TemplateFile(
data: 'php/docker-entrypoint-php',
name: 'docker-entrypoint-php',
path: 'tools/docker/images/php/root/usr/local/bin',
);
}
if (isNode(Arr::get($configurationData, 'language'))) {
$filesToGenerate[] = new TemplateFile(data: 'node/.yarnrc', name: '.yarnrc');
$filesToGenerate[] = new TemplateFile(data: 'node/Dockerfile', name: 'Dockerfile');
}
if (isCaddy(Arr::get($configurationData, 'web.type'))) {
$filesToGenerate[] = new TemplateFile(
data: 'web/caddy/Caddyfile',
name: 'Caddyfile',
path: 'tools/docker/images/web/root/etc/caddy',
);
}
if (isNginx(Arr::get($configurationData, 'web.type'))) {
$filesToGenerate[] = new TemplateFile(
data: 'web/nginx/default.conf',
name: 'default.conf',
path: 'tools/docker/images/web/root/etc/nginx/conf.d',
);
}
if ('drupal-project' === Arr::get($configurationData, 'type')) {
// Add a Drupal version of phpunit.xml.dist.
$filesToGenerate[] = new TemplateFile(data: 'drupal-project/phpunit.xml.dist', name: 'phpunit.xml.dist');
}
// Display a list of generated files.
$io->write('Generated files:');
$io->listing($filesToGenerate
->map(fn (TemplateFile $templateFile): string =>
collect([$templateFile->path, $templateFile->name])->filter()->implode('/'))
->unique()
->sort()
->toArray());
$configurationData['managedText'] = 'Do not edit this file. It is automatically generated by \'build-configs\'.';
$io->listing(getListOfFiles(filesToGenerate: $filesToGenerate)->toArray());
generateFiles(
configurationData: $configurationData,
@ -205,6 +139,82 @@ function generateFiles(
}
}
function getFiles(array $configurationData): Collection
{
/** @var Collection<int, TemplateFile> */
$filesToGenerate = collect([
new TemplateFile(data: 'common/.dockerignore', name: '.dockerignore'),
new TemplateFile(data: 'common/.hadolint.yaml', name: '.hadolint.yaml'),
new TemplateFile(data: 'env.example', name: '.env.example'),
]);
$extraDatabases = Arr::get($configurationData, 'database.extra_databases', []);
if (count($extraDatabases) > 0) {
$filesToGenerate[] = new TemplateFile(
data: 'extra-databases.sql',
name: 'extra-databases.sql',
path: 'tools/docker/images/database/root/docker-entrypoint-initdb.d',
);
}
if (false !== Arr::get($configurationData, "justfile", true)) {
$filesToGenerate[] = new TemplateFile(data: 'justfile', name: 'justfile');
}
if (isset($configurationData['dockerCompose']) && $configurationData['dockerCompose'] !== null) {
$filesToGenerate[] = new TemplateFile(data: 'docker-compose.yaml', name: 'docker-compose.yaml');
}
if (isPhp(Arr::get($configurationData, 'language'))) {
$filesToGenerate[] = new TemplateFile(data: 'php/Dockerfile', name: 'Dockerfile');
$filesToGenerate[] = new TemplateFile(data: 'php/phpcs.xml', name: 'phpcs.xml.dist');
$filesToGenerate[] = new TemplateFile(data: 'php/phpstan.neon', name: 'phpstan.neon.dist');
$filesToGenerate[] = new TemplateFile(data: 'php/phpunit.xml', name: 'phpunit.xml.dist');
$filesToGenerate[] = new TemplateFile(
data: 'php/docker-entrypoint-php',
name: 'docker-entrypoint-php',
path: 'tools/docker/images/php/root/usr/local/bin',
);
}
if (isNode(Arr::get($configurationData, 'language'))) {
$filesToGenerate[] = new TemplateFile(data: 'node/.yarnrc', name: '.yarnrc');
$filesToGenerate[] = new TemplateFile(data: 'node/Dockerfile', name: 'Dockerfile');
}
if (isCaddy(Arr::get($configurationData, 'web.type'))) {
$filesToGenerate[] = new TemplateFile(
data: 'web/caddy/Caddyfile',
name: 'Caddyfile',
path: 'tools/docker/images/web/root/etc/caddy',
);
}
if (isNginx(Arr::get($configurationData, 'web.type'))) {
$filesToGenerate[] = new TemplateFile(
data: 'web/nginx/default.conf',
name: 'default.conf',
path: 'tools/docker/images/web/root/etc/nginx/conf.d',
);
}
if ('drupal-project' === Arr::get($configurationData, 'type')) {
// Add a Drupal version of phpunit.xml.dist.
$filesToGenerate[] = new TemplateFile(data: 'drupal-project/phpunit.xml.dist', name: 'phpunit.xml.dist');
}
return $filesToGenerate;
}
function getListOfFiles(Collection $filesToGenerate): Collection
{
return $filesToGenerate
->map(fn (TemplateFile $templateFile): string =>
collect([$templateFile->path, $templateFile->name])->filter()->implode('/'))
->unique()
->sort();
}
function isCaddy(?string $webServer): bool
{
if (is_null($webServer)) {