Skip to content

Commit

Permalink
Support fs:template with data uris
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfaassen committed May 7, 2016
1 parent ccb4662 commit e554f4f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
1 change: 1 addition & 0 deletions bin/droid-fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ $application->add(new Droid\Plugin\Fs\Command\FsCopyCommand());
$application->add(new Droid\Plugin\Fs\Command\FsMkdirCommand());
$application->add(new Droid\Plugin\Fs\Command\FsMountCommand());
$application->add(new Droid\Plugin\Fs\Command\FsRenameCommand());
$application->add(new Droid\Plugin\Fs\Command\FsTemplateCommand());
$application->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require": {
"php": ">=5.3.0",
"symfony/console": "~2.7",
"symfony/process": "~2.7"
"symfony/process": "~2.7",
"zordius/lightncandy": "~0.93"
},
"require-dev": {
"linkorb/autotune": "~1.0"
Expand Down
86 changes: 86 additions & 0 deletions src/Command/FsTemplateCommand.php
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);
}
}
2 changes: 1 addition & 1 deletion src/DroidPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getCommands()
$commands[] = new \Droid\Plugin\Fs\Command\FsMkdirCommand();
$commands[] = new \Droid\Plugin\Fs\Command\FsMountCommand();
$commands[] = new \Droid\Plugin\Fs\Command\FsRenameCommand();
//$commands[] = new \Droid\Plugin\Fs\Command\FsTemplateCommand();
$commands[] = new \Droid\Plugin\Fs\Command\FsTemplateCommand();
return $commands;
}
}
17 changes: 17 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Droid\Plugin\Fs;

use RuntimeException;

class Utils
{
public static function normalizePath($path)
Expand All @@ -19,4 +21,19 @@ public static function normalizePath($path)
}
return $path;
}

public static function getContents($filename)
{
if (substr($filename, 0, 5) == 'data:') {
// parse as data-uri
$content = file_get_contents($filename);
} else {
$filename = Utils::normalizePath($filename);
if (!file_exists($filename)) {
throw new RuntimeException("File not found: " . $filename);
}
$content = file_get_contents($filename);
}
return $content;
}
}

0 comments on commit e554f4f

Please sign in to comment.