2023-01-19 18:11:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-01-21 20:42:52 +00:00
|
|
|
namespace OliverDaviesLtd\BuildConfigs\Console\Command;
|
2023-01-19 18:11:24 +00:00
|
|
|
|
|
|
|
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;
|
2023-01-19 18:11:24 +00:00
|
|
|
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;
|
2023-01-19 18:11:24 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2023-01-19 18:37:13 +00:00
|
|
|
use Twig\Environment;
|
2023-01-19 18:11:24 +00:00
|
|
|
|
|
|
|
#[AsCommand(
|
|
|
|
description: 'Build configuration files',
|
2023-01-21 20:42:52 +00:00
|
|
|
name: 'build-configs'
|
2023-01-19 18:11:24 +00:00
|
|
|
)]
|
|
|
|
final class BuildConfigurationCommand extends Command
|
|
|
|
{
|
2023-01-19 18:53:42 +00:00
|
|
|
private const LANGUAGE_PHP = 'php';
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
private Environment $twig,
|
|
|
|
private Filesystem $filesystem,
|
|
|
|
) {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:38:10 +00:00
|
|
|
protected function configure(): void
|
|
|
|
{
|
|
|
|
$this
|
2023-01-19 20:45:02 +00:00
|
|
|
->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
|
2023-01-19 18:11:24 +00:00
|
|
|
{
|
2023-01-19 20:45:02 +00:00
|
|
|
$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);
|
|
|
|
|
2023-01-19 20:45:02 +00:00
|
|
|
$configurationData = Yaml::parseFile($configFile);
|
2023-01-19 19:47:15 +00:00
|
|
|
$configurationData['dockerCompose'] = $configurationData['docker-compose'];
|
|
|
|
$configurationData['docker-compose'] = null;
|
2023-01-19 18:11:24 +00:00
|
|
|
|
2023-01-19 19:47:15 +00:00
|
|
|
$io->info("Building configuration for {$configurationData['name']}.");
|
2023-01-19 18:11:24 +00:00
|
|
|
|
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
|
|
|
|
2023-01-21 12:28:02 +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
|
|
|
}
|
|
|
|
|
|
|
|
if ($configurationData['language'] === self::LANGUAGE_PHP) {
|
2023-01-19 20:38:10 +00:00
|
|
|
$this->filesystem->dumpFile("{$outputDir}/phpcs.xml.dist", $this->twig->render('phpcs.xml.twig', $configurationData));
|
|
|
|
$this->filesystem->dumpFile("{$outputDir}/phpstan.neon.dist", $this->twig->render('phpstan.neon.twig', $configurationData));
|
|
|
|
$this->filesystem->dumpFile("{$outputDir}/phpunit.xml.dist", $this->twig->render('phpunit.xml.twig', $configurationData));
|
2023-01-21 19:52:58 +00:00
|
|
|
|
|
|
|
$this->filesystem->mkdir("{$outputDir}/tools/docker/images/php/root/usr/local/bin");
|
|
|
|
$this->filesystem->dumpFile("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php", $this->twig->render('docker-entrypoint-php.twig', $configurationData));
|
|
|
|
}
|
2023-01-21 19:53:25 +00:00
|
|
|
|
|
|
|
if ($configurationData['web']['type'] === 'nginx') {
|
|
|
|
$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
|
|
|
|
2023-01-19 18:11:24 +00:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|