-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccb4662
commit e554f4f
Showing
5 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Droid\Plugin\Fs\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Droid\Plugin\Fs\Utils; | ||
use RuntimeException; | ||
use LightnCandy\LightnCandy; | ||
|
||
class FsTemplateCommand extends Command | ||
{ | ||
public function configure() | ||
{ | ||
$this->setName('fs:template') | ||
->setDescription('Generates a file from a template') | ||
->addArgument( | ||
'src', | ||
InputArgument::REQUIRED, | ||
'Source filename to update' | ||
) | ||
->addArgument( | ||
'dest', | ||
InputArgument::REQUIRED, | ||
'Destination filename' | ||
) | ||
->addOption( | ||
'json', | ||
'j', | ||
InputOption::VALUE_REQUIRED, | ||
'JSON filename to get data for in the template' | ||
) | ||
->addOption( | ||
'mode', | ||
'm', | ||
InputOption::VALUE_REQUIRED, | ||
'Permission filemode' | ||
) | ||
->addOption( | ||
'force', | ||
'f', | ||
InputOption::VALUE_NONE, | ||
'Force' | ||
) | ||
; | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$src = $input->getArgument('src'); | ||
$content = Utils::getContents($src); | ||
|
||
$dest = $input->getArgument('dest'); | ||
$dest = Utils::normalizePath($dest); | ||
|
||
$mode = $input->getOption('mode'); | ||
if (!$mode) { | ||
$mode = 0644; | ||
} else { | ||
$mode = octdec($mode); | ||
} | ||
|
||
|
||
$output->writeLn("Creating file $dest. Mode: " . decoct($mode)); | ||
|
||
$php = LightnCandy::compile($content); | ||
$render = LightnCandy::prepare($php); | ||
|
||
$data = []; | ||
$jsonFilename = $input->getOption('json'); | ||
if ($jsonFilename) { | ||
$json = Utils::getContents($jsonFilename); | ||
$data = json_decode($json, true); | ||
if (!$data) { | ||
throw new RuntimeException("Can't parse data as JSON"); | ||
} | ||
} | ||
|
||
$content = $render($data); | ||
|
||
file_put_contents($dest, $content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters