build-configs/src/Console/Command/BuildConfigurationCommand.php

75 lines
3.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2023-01-21 20:42:52 +00:00
namespace OliverDaviesLtd\BuildConfigs\Console\Command;
2023-02-02 18:06:47 +00:00
use OliverDaviesLtd\BuildConfigs\Enum\Language;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
2023-01-19 20:38:10 +00:00
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
2023-01-19 18:37:13 +00:00
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
2023-01-19 18:37:13 +00:00
use Twig\Environment;
#[AsCommand(
description: 'Build configuration files',
2023-01-21 20:42:52 +00:00
name: 'build-configs'
)]
final class BuildConfigurationCommand extends Command
{
2023-01-19 18:53:42 +00:00
public function __construct(
private Environment $twig,
private Filesystem $filesystem,
) {
parent::__construct();
}
2023-01-19 20:38:10 +00:00
protected function configure(): void
{
$this
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The configuration file to use', 'build.yaml')
2023-01-19 20:38:10 +00:00
->addOption('output-dir', 'o', InputOption::VALUE_REQUIRED, 'The directory to create files in', '.');
}
2023-01-19 19:47:15 +00:00
public function execute(InputInterface $input, OutputInterface $output): int
{
$configFile = $input->getOption('config');
2023-01-19 20:38:10 +00:00
$outputDir = $input->getOption('output-dir');
2023-01-19 18:53:42 +00:00
$io = new SymfonyStyle($input, $output);
$configurationData = Yaml::parseFile($configFile);
2023-01-19 19:47:15 +00:00
$configurationData['dockerCompose'] = $configurationData['docker-compose'];
$configurationData['docker-compose'] = null;
2023-01-19 19:47:15 +00:00
$io->info("Building configuration for {$configurationData['name']}.");
2023-01-19 20:38:10 +00:00
$this->filesystem->dumpFile("{$outputDir}/.env.example", $this->twig->render('env.example.twig', $configurationData));
$this->filesystem->dumpFile("{$outputDir}/Dockerfile", $this->twig->render('Dockerfile.twig', $configurationData));
2023-01-19 19:47:15 +00:00
if ($configurationData['dockerCompose'] !== null) {
2023-01-19 20:38:10 +00:00
$this->filesystem->dumpFile("{$outputDir}/docker-compose.yaml", $this->twig->render('docker-compose.yaml.twig', $configurationData));
2023-01-19 19:47:15 +00:00
}
2023-02-02 18:06:47 +00:00
if (strtoupper($configurationData['language']) === Language::PHP) {
2023-02-02 00:40:42 +00:00
$this->filesystem->dumpFile("{$outputDir}/phpcs.xml.dist", $this->twig->render('php/phpcs.xml.twig', $configurationData));
$this->filesystem->dumpFile("{$outputDir}/phpstan.neon.dist", $this->twig->render('php/phpstan.neon.twig', $configurationData));
$this->filesystem->dumpFile("{$outputDir}/phpunit.xml.dist", $this->twig->render('php/phpunit.xml.twig', $configurationData));
2023-01-21 19:52:58 +00:00
$this->filesystem->mkdir("{$outputDir}/tools/docker/images/php/root/usr/local/bin");
2023-02-02 00:40:42 +00:00
$this->filesystem->dumpFile("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php", $this->twig->render('php/docker-entrypoint-php.twig', $configurationData));
2023-01-21 19:52:58 +00:00
}
2023-01-21 19:53:25 +00:00
2023-02-03 18:57:50 +00:00
if (isset($configurationData['web']['type']) && $configurationData['web']['type'] === 'nginx') {
2023-01-21 19:53:25 +00:00
$this->filesystem->mkdir("{$outputDir}/tools/docker/images/nginx/root/etc/nginx/conf.d");
$this->filesystem->dumpFile("{$outputDir}/tools/docker/images/nginx/root/etc/nginx/conf.d/default.conf", $this->twig->render('default.conf', $configurationData));
2023-01-19 18:53:42 +00:00
}
2023-01-19 18:37:13 +00:00
return Command::SUCCESS;
}
}