build-configs/src/Action/CreateListOfFilesToGenerate.php

206 lines
7.3 KiB
PHP
Raw Normal View History

2023-04-24 23:16:21 +00:00
<?php
declare(strict_types=1);
namespace App\Action;
use App\DataTransferObject\Config;
2023-04-24 23:16:21 +00:00
use App\DataTransferObject\TemplateFile;
use App\Enum\Language;
2023-09-24 17:10:24 +00:00
use App\Enum\ProjectType;
2023-04-24 23:16:21 +00:00
use App\Enum\WebServer;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
2023-04-24 23:16:21 +00:00
final class CreateListOfFilesToGenerate
{
public function handle(array $configurationDataAndDto, \Closure $next)
{
/**
* @var Config $configurationDataDto,
* @var array<string,mixed> $configurationData
*/
[$configurationData, $configurationDataDto] = $configurationDataAndDto;
2023-04-24 23:16:21 +00:00
/** @var Collection<int, TemplateFile> */
$filesToGenerate = collect();
2023-09-24 17:10:24 +00:00
switch (strtolower($configurationDataDto->type)) {
case (strtolower(ProjectType::Astro->name)):
$filesToGenerate = collect([
['astro/.envrc', '.envrc'],
2023-09-24 20:47:45 +00:00
['astro/.gitignore', '.gitignore'],
['astro/flake.nix', 'flake.nix'],
['astro/tsconfig.json', 'tsconfig.json'],
2023-09-24 17:10:24 +00:00
])->map(function (array $file) {
return new TemplateFile(
data: $file[0],
name: $file[1],
path: null,
);
});
break;
}
if ($filesToGenerate->isNotEmpty()) {
return $next([$configurationData, $configurationDataDto, $filesToGenerate]);
}
$isDocker = static::isDocker($configurationData);
$isFlake = static::isFlake($configurationData);
if ($isDocker) {
$filesToGenerate->push(new TemplateFile(data: 'common/.dockerignore', name: '.dockerignore'));
$filesToGenerate->push(new TemplateFile(data: 'common/.hadolint.yaml', name: '.hadolint.yaml'));
$filesToGenerate->push(new TemplateFile(data: 'env.example', name: '.env.example'));
}
if ($isFlake) {
$filesToGenerate->push(new TemplateFile(data: 'common/envrc', name: '.envrc'));
$filesToGenerate->push(new TemplateFile(data: 'common/flake.nix', name: 'flake.nix'));
}
2023-04-24 23:16:21 +00:00
$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 (static::isPhp(Arr::get($configurationData, 'language'))) {
if ($isDocker) {
$filesToGenerate[] = new TemplateFile(data: 'php/Dockerfile', name: 'Dockerfile');
}
2023-04-24 23:16:21 +00:00
$filesToGenerate[] = new TemplateFile(data: 'php/phpcs.xml', name: 'phpcs.xml.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',
);
$filesToGenerate[] = new TemplateFile(
data: 'php/php.ini',
name: 'php.ini',
path: 'tools/docker/images/php/root/usr/local/etc/php',
);
2023-04-24 23:16:21 +00:00
if (Arr::has(array: $configurationData, keys: 'php.phpstan')) {
$filesToGenerate[] = new TemplateFile(data: 'php/phpstan.neon', name: 'phpstan.neon.dist');
}
}
if (static::isTypeScript(Arr::get($configurationData, 'language'))) {
if ($isDocker) {
$filesToGenerate[] = new TemplateFile(data: 'typescript/.yarnrc', name: '.yarnrc');
$filesToGenerate[] = new TemplateFile(data: 'typescript/Dockerfile', name: 'Dockerfile');
}
2023-04-24 23:16:21 +00:00
}
if (static::isCaddy(Arr::get($configurationData, 'web.type'))) {
$filesToGenerate[] = new TemplateFile(
data: 'web/caddy/Caddyfile',
name: 'Caddyfile',
path: 'tools/docker/images/web/root/etc/caddy',
);
}
if (static::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',
);
}
2023-07-22 09:40:02 +00:00
if ('drupal' === Arr::get($configurationData, 'type')) {
2023-04-24 23:16:21 +00:00
// Add a Drupal version of phpunit.xml.dist.
2023-07-22 09:40:02 +00:00
$filesToGenerate[] = new TemplateFile(data: 'drupal/phpunit.xml.dist', name: 'phpunit.xml.dist');
2023-04-24 23:16:21 +00:00
}
if (Arr::get($configurationData, 'experimental.createGitHubActionsConfiguration', false) === true) {
$filesToGenerate[] = new TemplateFile(
data: 'ci/github-actions/ci.yml',
name: 'ci.yml',
path: '.github/workflows',
);
}
$filesToGenerate[] = new TemplateFile(
data: 'git-hooks/prepare-commit-msg',
name: 'prepare-commit-msg',
path: '.githooks',
);
2023-04-24 23:16:21 +00:00
if (Arr::get($configurationData, 'experimental.runGitHooksBeforePush', false) === true) {
$filesToGenerate[] = new TemplateFile(
data: 'git-hooks/pre-push',
name: 'pre-push',
path: '.githooks',
);
}
return $next([$configurationData, $configurationDataDto, $filesToGenerate]);
2023-04-24 23:16:21 +00:00
}
private static function isCaddy(?string $webServer): bool
{
if (is_null($webServer)) {
return false;
}
return strtoupper($webServer) === WebServer::CADDY->name;
}
private static function isDocker(array $configurationData): bool
{
// This should return `false` if there is no explicit `dockerfile` key
// in the build.yaml file. This is currently not the case, I assume
// because of default values being added.
// For now, if it's not a Flake, it's Docker.
return !static::isFlake($configurationData);
}
private static function isFlake(array $configurationData): bool
{
return Arr::get($configurationData, 'flake') !== null;
}
2023-04-24 23:16:21 +00:00
private static function isNginx(?string $webServer): bool
{
if (is_null($webServer)) {
return false;
}
return strtoupper($webServer) === WebServer::NGINX->name;
}
private static function isTypeScript(?string $language): bool
2023-04-24 23:16:21 +00:00
{
if (is_null($language)) {
return false;
}
return strtoupper($language) === Language::TYPESCRIPT->name;
2023-04-24 23:16:21 +00:00
}
private static function isPhp(?string $language): bool
{
if (is_null($language)) {
return false;
}
return strtoupper($language) === Language::PHP->name;
}
}