Skip to content

Commit

Permalink
Merge pull request #103 from RossJHagan/init-template
Browse files Browse the repository at this point in the history
adds an init:template command (fix #40)
  • Loading branch information
mnapoli committed May 11, 2015
2 parents 97482ff + db80a1f commit 706f6ea
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/Application/Cli/InitTemplateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Couscous\Application\Cli;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Initialize a new Couscous template.
*
* @author Ross J. Hagan <[email protected]>
*/
class InitTemplateCommand extends Command {

protected function configure()
{
$this
->setName('init:template')
->setDescription('Initialize a new Couscous template');

$this->addArgument(
'template_name',
InputArgument::REQUIRED,
'Template name'
)
->addArgument(
'directory',
InputArgument::OPTIONAL,
'Directory name',
'website'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{

$fileExtension = '.twig';

$dirName = $input->getArgument('directory');
$directory = getcwd() . '/' . $dirName . '/';
$templateName = $input->getArgument('template_name') . $fileExtension;

$fileLocation = $directory . $templateName;
$fileExists = file_exists($fileLocation);

if (! file_exists(getcwd() . '/' . $dirName)) {
$output->writeln('<comment>Creating directory.</comment>');
mkdir(getcwd() . '/' . $dirName);
}

if ($fileExists) {
$output->writeln('<error>That template exists at ' . $fileLocation . ', so nothing has been changed.</error>');
$output->writeln('<error>Try another name!</error>');
return;
}

if (! $fileExists) {
$output->writeln('<comment>Initialising template.</comment>');
$template = <<<HTML
<!DOCTYPE html>
<html>
<head>
<title>My project!</title>
</head>
<body>
{% block content %}
<p>
Don't forget you can add variables into your YAML front matter, or in your couscous.yml
then use them inside double curly braces!
</p>
<p>
Also, set up a baseUrl in your couscous.yml and use it
to <a href="{{ baseUrl }}/link/in/the/site">link to another page</a> in your site
</p>
{{ content|raw }}
{% endblock %}
</body>
</html>
HTML;
file_put_contents($fileLocation, $template);
}

}

}
1 change: 1 addition & 0 deletions src/Application/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
$application->add($c->get('Couscous\Application\Cli\DeployCommand'));
$application->add($c->get('Couscous\Application\Cli\ClearCommand'));
$application->add($c->get('Couscous\Application\Cli\TravisAutoDeployCommand'));
$application->add($c->get('Couscous\Application\Cli\InitTemplateCommand'));

return $application;
}),
Expand Down

0 comments on commit 706f6ea

Please sign in to comment.