cloudformation-plus-plus: cfn template preprocessor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
1.9 KiB

<?php
declare(strict_types=1);
namespace App\Commands\Stack;
use LaravelZero\Framework\Commands\Command;
/**
* Read a file as input and perform all necessary compilation steps before
* writing it to an output file.
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class Compile extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'stack:compile {in_file} {out_file} {--format=Yaml}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Read an input file, process, output result to a file';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$engine = new \App\Engine\Engine();
$serializer = $this->getSerializer();
$unserializer = $this->getUnserializer();
$engine->setSerializer($serializer)->setUnserializer($unserializer)->setCompiler(new \App\Engine\Cfnpp());
$output = $engine->process($this->argument('in_file'), new \App\Engine\CfnppOptions());
file_put_contents($this->argument('out_file'), $output);
}
/**
* Retrieve a serializer instance based on the format specified in the command options.
*
* @return \App\Serialize\ISerialize
*/
protected function getSerializer(): \App\Serialize\ISerialize
{
$format = $this->option('format');
$class = '\\App\\Serialize\\'.$format;
if (!class_exists($class))
{
throw new \Exception('Unknown formatter: '.$format);
}
return new $class();
}
/**
* Retrieve a unserializer instance based on the format specified in the command options.
*
* @return \App\Serialize\IUnserialize
*/
protected function getUnserializer(): \App\Serialize\IUnserialize
{
$format = $this->option('format');
$class = '\\App\\Serialize\\'.$format;
if (!class_exists($class))
{
throw new \Exception('Unknown formatter: '.$format);
}
return new $class();
}
}