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

69 lines
2.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace OliverDaviesLtd\BuildConfiguration\Console\Command;
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',
name: 'build-configuration'
)]
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
->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'] === true) {
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-19 18:53:42 +00:00
}
2023-01-19 18:37:13 +00:00
return Command::SUCCESS;
}
}