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,8 +81,66 @@ $app->command(
$configurationData['docker-compose'] = null; $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']}."); $io->info("Building configuration for {$configurationData['name']}.");
$io->write('Generated files:');
$io->listing(getListOfFiles(filesToGenerate: $filesToGenerate)->toArray());
generateFiles(
configurationData: $configurationData,
filesToGenerate: $filesToGenerate,
outputDir: $outputDir,
);
}
)->descriptions('Generate project-specific configuration files.', [
'--config-file' => 'The path to the project\'s build.yaml file',
'--output-dir' => 'The directory to create files in',
]);
$app->run();
/**
* @param array<string, string> $configurationData
* @param Collection<int, TemplateFile> $filesToGenerate
*/
function generateFiles(
Collection $filesToGenerate,
string $outputDir,
array $configurationData,
): void
{
$filesystem = new Filesystem();
$twig = new Environment(new FilesystemLoader([__DIR__ . '/../templates']));
$filesToGenerate->each(function(TemplateFile $templateFile) use ($configurationData, $filesystem, $outputDir, $twig): void {
if ($templateFile->path !== null) {
if (!$filesystem->exists($templateFile->path)) {
$filesystem->mkdir("{$outputDir}/{$templateFile->path}");
}
}
$sourceFile = "{$templateFile->data}.twig";
$outputFile = collect([
$outputDir,
$templateFile->path,
$templateFile->name,
])->filter()->implode('/');
$filesystem->dumpFile($outputFile, $twig->render($sourceFile, $configurationData));
});
// If the Docker entrypoint file is generated, ensure it is executable.
if ($filesystem->exists("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php")) {
$filesystem->chmod("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php", 0755);
}
}
function getFiles(array $configurationData): Collection
{
/** @var Collection<int, TemplateFile> */ /** @var Collection<int, TemplateFile> */
$filesToGenerate = collect([ $filesToGenerate = collect([
new TemplateFile(data: 'common/.dockerignore', name: '.dockerignore'), new TemplateFile(data: 'common/.dockerignore', name: '.dockerignore'),
@ -145,64 +203,16 @@ $app->command(
$filesToGenerate[] = new TemplateFile(data: 'drupal-project/phpunit.xml.dist', name: 'phpunit.xml.dist'); $filesToGenerate[] = new TemplateFile(data: 'drupal-project/phpunit.xml.dist', name: 'phpunit.xml.dist');
} }
// Display a list of generated files. return $filesToGenerate;
$io->write('Generated files:'); }
$io->listing($filesToGenerate
function getListOfFiles(Collection $filesToGenerate): Collection
{
return $filesToGenerate
->map(fn (TemplateFile $templateFile): string => ->map(fn (TemplateFile $templateFile): string =>
collect([$templateFile->path, $templateFile->name])->filter()->implode('/')) collect([$templateFile->path, $templateFile->name])->filter()->implode('/'))
->unique() ->unique()
->sort() ->sort();
->toArray());
$configurationData['managedText'] = 'Do not edit this file. It is automatically generated by \'build-configs\'.';
generateFiles(
configurationData: $configurationData,
filesToGenerate: $filesToGenerate,
outputDir: $outputDir,
);
}
)->descriptions('Generate project-specific configuration files.', [
'--config-file' => 'The path to the project\'s build.yaml file',
'--output-dir' => 'The directory to create files in',
]);
$app->run();
/**
* @param array<string, string> $configurationData
* @param Collection<int, TemplateFile> $filesToGenerate
*/
function generateFiles(
Collection $filesToGenerate,
string $outputDir,
array $configurationData,
): void
{
$filesystem = new Filesystem();
$twig = new Environment(new FilesystemLoader([__DIR__ . '/../templates']));
$filesToGenerate->each(function(TemplateFile $templateFile) use ($configurationData, $filesystem, $outputDir, $twig): void {
if ($templateFile->path !== null) {
if (!$filesystem->exists($templateFile->path)) {
$filesystem->mkdir("{$outputDir}/{$templateFile->path}");
}
}
$sourceFile = "{$templateFile->data}.twig";
$outputFile = collect([
$outputDir,
$templateFile->path,
$templateFile->name,
])->filter()->implode('/');
$filesystem->dumpFile($outputFile, $twig->render($sourceFile, $configurationData));
});
// If the Docker entrypoint file is generated, ensure it is executable.
if ($filesystem->exists("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php")) {
$filesystem->chmod("{$outputDir}/tools/docker/images/php/root/usr/local/bin/docker-entrypoint-php", 0755);
}
} }
function isCaddy(?string $webServer): bool function isCaddy(?string $webServer): bool