Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Feature] Blueprint generation from CLI #17

Merged
merged 6 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions classes/DevToolsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Grav\Common\Grav;
use Grav\Common\Data;
use Grav\Common\Theme;
use Grav\Common\Filesystem\Folder;
use Grav\Common\GPM\GPM;
use Grav\Common\Inflector;
Expand Down Expand Up @@ -66,6 +67,11 @@ protected function init()

//Add `theme://` to prevent fail
$this->locator->addPath('theme', '', []);
$this->locator->addPath('plugin', '', []);
$this->locator->addPath('blueprint', '', []);
// $this->config->set('theme', $config->get('themes.' . $name));


}

/**
Expand All @@ -74,12 +80,31 @@ protected function init()
protected function createComponent()
{
$name = $this->component['name'];
$bpname = $this->component['bpname'];
$folderName = strtolower($this->inflector->hyphenize($name));
$type = $this->component['type'];
$grav = Grav::instance();
$config = $grav['config'];
$name = $config->get('system.pages.theme');
$activetheme = $grav['themes']->get($name)->blueprints()->get('name');
//$themes->configure();
$activethemefolder = 'themes' . DS . strtolower($this->inflector->hyphenize($activetheme));
//$myvar = $myvarold->$name;
// dump($lowervar);



$template = $this->component['template'];
$templateFolder = __DIR__ . '/../components/' . $type . DS . $template;
$componentFolder = $this->locator->findResource($type . 's://') . DS . $folderName;
// $componentFolder = $this->locator->findResource($type . 's://') . DS . '../../user/themes/' . $lowervar . '/blueprints';
if ($type == 'blueprint') {
$componentFolder = USER_DIR . $activethemefolder . '/blueprints';
dump($componentFolder);
} else {
$componentFolder = $this->locator->findResource($type . 's://') . DS . $folderName;
dump($componentFolder);
}


//Copy All files to component folder
try {
Expand Down Expand Up @@ -117,9 +142,14 @@ protected function createComponent()
$this->output->writeln($type . "creation failed!");
return false;
}

if ($type != 'blueprint') {
rename($componentFolder . DS . $type . '.php', $componentFolder . DS . $this->inflector->hyphenize($name) . '.php');
rename($componentFolder . DS . $type . '.yaml', $componentFolder . DS . $this->inflector->hyphenize($name) . '.yaml');
} else {
rename($componentFolder . DS . $type . '.yaml', $componentFolder . DS . $this->inflector->hyphenize($bpname) . '.yaml');
}



$this->output->writeln('');
$this->output->writeln('<green>SUCCESS</green> ' . $type . ' <magenta>' . $name . '</magenta> -> Created Successfully');
Expand Down Expand Up @@ -165,7 +195,12 @@ protected function validate($type, $value, $extra = '')
}

break;
case 'themename':
if($value === null || trim($value) === '') {
throw new \RuntimeException('Theme Name cannot be empty');
}

break;
case 'developer':
if ($value === null || trim($value) === '') {
throw new \RuntimeException('Developer\'s Name cannot be empty');
Expand Down
104 changes: 104 additions & 0 deletions cli/NewBlueprintCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
namespace Grav\Plugin\Console;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;

require_once(__DIR__ . '/../classes/DevToolsCommand.php');

/**
* Class NewThemeCommand
* @package Grav\Console\Cli\DevTools
*/
class NewBlueprintCommand extends DevToolsCommand
{

/**
* @var array
*/
protected $options = [];

/**
*
*/
protected function configure()
{
$this
->setName('new-blueprint')
->setAliases(['newblueprint','blueprint'])
->addOption(
'bpname',
'bp',
InputOption::VALUE_OPTIONAL,
'The name of your new Grav theme'
)
->addOption(
'name',
'bn',
InputOption::VALUE_OPTIONAL,
'The name of your new Grav theme'
)
->addOption(
'template',
'tp',
InputOption::VALUE_OPTIONAL,
'The name/username of the developer'
)
->setDescription('Create a blueprint that extend the default.yaml blueprint files')
->setHelp('The <info>new-blueprint</info> command creates a new blueprint file.');
}

/**
* @return int|null|void
*/
protected function serve()
{
$this->init();

/**
* @var array DevToolsCommand $component
*/
$this->component['type'] = 'blueprint';
$this->component['template'] = 'modular';
// $this->component['name'] = 'blueprints';
$this->component['version'] = '0.1.0';
$this->component['themename'] = 'bonjour';


$this->options = [
'name' => $this->input->getOption('name'),
'bpname' => $this->input->getOption('bpname'),
'template' => $this->input->getOption('template'),

];

$this->validateOptions();

$this->component = array_replace($this->component, $this->options);

$helper = $this->getHelper('question');

if (!$this->options['template']) {
$question = new ChoiceQuestion(
'Please choose a template type',
array('newtab', 'append')
);

$this->component['template'] = $helper->ask($this->input, $this->output, $question);
}
if (!$this->options['bpname']) {
$question = new Question('Enter <yellow>Blueprint Name</yellow>: ');


$this->component['bpname'] = $helper->ask($this->input, $this->output, $question);
}
// $this->component['template'] = $helper->ask($this->input, $this->output, $question);

$this->createComponent();
}

}
21 changes: 21 additions & 0 deletions components/blueprint/append/blueprint.yaml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
title: {{ component.bpname }}
'@extends':
type: default
context: blueprints://pages

form:
fields:
tabs:
type: tabs
active: 1

fields:
content:
fields:
header.an_example_text_field:
type: text
label: Add a number
default: 5
validate:
required: true
type: int
15 changes: 15 additions & 0 deletions components/blueprint/newtab/blueprint.yaml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
title: Item
'@extends':
type: default
context: blueprints://pages

form:
fields:
tabs:
fields:
blog:
type: tab
title: {{ component.bpname }}

fields:
header.mytextfield: