Change the way the commands are declare, separate "generate" and "serve"

This commit is contained in:
Stéphane Goetz 2016-07-29 07:58:48 +02:00
parent 64edc31122
commit 341ec96cf8
5 changed files with 113 additions and 109 deletions

View File

@ -5,45 +5,11 @@ use Symfony\Component\Console\Input\InputInterface;
class Application extends SymfonyApplication class Application extends SymfonyApplication
{ {
/** public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') {
* Gets the name of the command based on input. parent::__construct($name, $version);
*
* @param InputInterface $input The input interface
*
* @return string The command name
*/
protected function getCommandName(InputInterface $input)
{
// This should return the name of your command.
return 'generate';
}
/** $this->add(new Generate());
* Gets the default commands that should always be available. $this->add(new Serve());
* $this->setDefaultCommand('generate');
* @return array An array of default Command instances
*/
protected function getDefaultCommands()
{
// Keep the core default commands to have the HelpCommand
// which is used when using the --help option
$defaultCommands = parent::getDefaultCommands();
$defaultCommands[] = new Generate();
return $defaultCommands;
}
/**
* Overridden so that the application doesn't expect the command
* name to be the first argument.
*/
public function getDefinition()
{
$inputDefinition = parent::getDefinition();
// clear out the normal first argument, which is the command name
$inputDefinition->setArguments();
return $inputDefinition;
} }
} }

View File

@ -0,0 +1,31 @@
<?php namespace Todaymade\Daux\Console;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Todaymade\Daux\Daux;
class DauxCommand extends SymfonyCommand
{
protected function prepareDaux(InputInterface $input)
{
$daux = new Daux(Daux::STATIC_MODE);
// Set the format if requested
if ($input->hasOption('format') && $input->getOption('format')) {
$daux->getParams()->setFormat($input->getOption('format'));
}
// Set the source directory
if ($input->getOption('source')) {
$daux->getParams()->setDocumentationDirectory($input->getOption('source'));
}
$daux->initializeConfiguration($input->getOption('configuration'));
if ($input->hasOption('delete') && $input->getOption('delete')) {
$daux->getParams()['confluence']['delete'] = true;
}
return $daux;
}
}

View File

@ -1,13 +1,11 @@
<?php namespace Todaymade\Daux\Console; <?php namespace Todaymade\Daux\Console;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\Server\Server;
class Generate extends SymfonyCommand class Generate extends DauxCommand
{ {
protected function configure() protected function configure()
{ {
@ -22,12 +20,6 @@ class Generate extends SymfonyCommand
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format, html or confluence', 'html') ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format, html or confluence', 'html')
->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree') ->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree')
// Serve the current documentation
->addOption('serve', null, InputOption::VALUE_NONE, 'Serve the current directory')
->addOption('host', null, InputOption::VALUE_REQUIRED, 'The host to serve on', 'localhost')
->addOption('port', null, InputOption::VALUE_REQUIRED, 'The port to serve on', 8085)
// Confluence format only // Confluence format only
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete pages not linked to a documentation page (confluence)') ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete pages not linked to a documentation page (confluence)')
@ -40,12 +32,6 @@ class Generate extends SymfonyCommand
{ {
$daux = $this->prepareDaux($input); $daux = $this->prepareDaux($input);
if ($input->getOption('serve')) {
$this->serve($daux, $input);
return;
}
$width = $this->getApplication()->getTerminalDimensions()[0]; $width = $this->getApplication()->getTerminalDimensions()[0];
// Instiantiate the processor if one is defined // Instiantiate the processor if one is defined
@ -58,39 +44,6 @@ class Generate extends SymfonyCommand
$daux->getGenerator()->generateAll($input, $output, $width); $daux->getGenerator()->generateAll($input, $output, $width);
} }
protected function serve(Daux $daux, InputInterface $input)
{
// Daux can only serve HTML
$daux->getParams()->setFormat('html');
//TODO :: support configuration and processor
Server::runServer($daux->getParams(), $input->getOption('host'), $input->getOption('port'));
}
protected function prepareDaux(InputInterface $input)
{
$daux = new Daux(Daux::STATIC_MODE);
// Set the format if requested
if ($input->getOption('format')) {
$daux->getParams()->setFormat($input->getOption('format'));
}
// Set the source directory
if ($input->getOption('source')) {
$daux->getParams()->setDocumentationDirectory($input->getOption('source'));
}
$daux->initializeConfiguration($input->getOption('configuration'));
if ($input->getOption('delete')) {
$daux->getParams()['confluence']['delete'] = true;
}
return $daux;
}
protected function prepareProcessor(Daux $daux, InputInterface $input, OutputInterface $output, $width) protected function prepareProcessor(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{ {
if ($input->getOption('processor')) { if ($input->getOption('processor')) {

76
libs/Console/Serve.php Executable file
View File

@ -0,0 +1,76 @@
<?php namespace Todaymade\Daux\Console;
use Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessUtils;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Server\Server;
class Serve extends DauxCommand
{
protected function configure()
{
$this
->setName('serve')
->setDescription('Serve documentation')
->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Configuration file')
->addOption('source', 's', InputOption::VALUE_REQUIRED, 'Where to take the documentation from')
->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree')
// Serve the current documentation
->addOption('serve', null, InputOption::VALUE_NONE, 'Serve the current directory')
->addOption('host', null, InputOption::VALUE_REQUIRED, 'The host to serve on', 'localhost')
->addOption('port', null, InputOption::VALUE_REQUIRED, 'The port to serve on', 8085);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$host = $input->getOption('host');
$port = $input->getOption('port');
$daux = $this->prepareDaux($input);
// Daux can only serve HTML
$daux->getParams()->setFormat('html');
chdir(__DIR__ . '/../../');
putenv('DAUX_SOURCE=' . $daux->getParams()->getDocumentationDirectory());
//TODO :: support configuration and processor
//putenv('DAUX_CONFIGURATION=' . $daux->getParams()->getConfigurationFile());
//putenv('DAUX_PROCESSOR=' . $daux->getParams()->getProcessorFile());
$base = ProcessUtils::escapeArgument(__DIR__ . '/../../');
$binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
echo "Daux development server started on http://{$host}:{$port}/\n";
if (defined('HHVM_VERSION')) {
if (version_compare(HHVM_VERSION, '3.8.0') >= 0) {
passthru("{$binary} -m server -v Server.Type=proxygen -v Server.SourceRoot={$base}/ -v Server.IP={$host} -v Server.Port={$port} -v Server.DefaultDocument=server.php -v Server.ErrorDocument404=server.php");
} else {
throw new Exception("HHVM's built-in server requires HHVM >= 3.8.0.");
}
} else {
passthru("{$binary} -S {$host}:{$port} {$base}/index.php");
}
}
protected function prepareProcessor(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{
if ($input->getOption('processor')) {
$daux->getParams()['processor'] = $input->getOption('processor');
}
$class = $daux->getProcessorClass();
if (!empty($class)) {
$daux->setProcessor(new $class($daux, $output, $width));
}
}
}

View File

@ -66,28 +66,6 @@ class Server
echo $page->getContent(); echo $page->getContent();
} }
public static function runServer(Config $config, $host, $port)
{
chdir(__DIR__ . '/../../');
putenv('DAUX_SOURCE=' . $config->getDocumentationDirectory());
$base = ProcessUtils::escapeArgument(__DIR__ . '/../../');
$binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
echo "Daux development server started on http://{$host}:{$port}/\n";
if (defined('HHVM_VERSION')) {
if (version_compare(HHVM_VERSION, '3.8.0') >= 0) {
passthru("{$binary} -m server -v Server.Type=proxygen -v Server.SourceRoot={$base}/ -v Server.IP={$host} -v Server.Port={$port} -v Server.DefaultDocument=server.php -v Server.ErrorDocument404=server.php");
} else {
throw new Exception("HHVM's built-in server requires HHVM >= 3.8.0.");
}
} else {
passthru("{$binary} -S {$host}:{$port} {$base}/index.php");
}
}
public function __construct(Daux $daux) public function __construct(Daux $daux)
{ {
$this->daux = $daux; $this->daux = $daux;