Skip to content

Commit

Permalink
Update init command (#88)
Browse files Browse the repository at this point in the history
* [init] Update command.

* [init] Ser default destination if no drupal site.

* [init] Remove destination output.
  • Loading branch information
jmolivas authored Nov 13, 2016
1 parent 8edde0b commit 436119d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 72 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/Bootstrap/DrupalConsoleCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public function boot()
$this->appRoot?$this->appRoot:$this->root
);

if (stripos($this->root, '/bin/') <= 0) {
$container->set(
'console.root',
$this->root
);
}

$container->get('console.renderer')
->setSkeletonDirs(
[
Expand Down
111 changes: 71 additions & 40 deletions src/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class InitCommand extends Command
*/
protected $configurationManager;

/**
* @var string
*/
protected $appRoot;

/**
* @var string
*/
protected $consoleRoot;

/**
* @var InitGenerator
*/
Expand All @@ -50,25 +60,26 @@ class InitCommand extends Command
'generate_chain' => false
];

private $webRootDirectories = [
'web',
'docroot'
];

/**
* InitCommand constructor.
* @param ShowFile $showFile
* @param ConfigurationManager $configurationManager
* @param InitGenerator $generator
* @param string $appRoot
* @param string $consoleRoot
*/
public function __construct(
ShowFile $showFile,
ConfigurationManager $configurationManager,
InitGenerator $generator
InitGenerator $generator,
$appRoot,
$consoleRoot = null
) {
$this->showFile = $showFile;
$this->configurationManager = $configurationManager;
$this->generator = $generator;
$this->appRoot = $appRoot;
$this->consoleRoot = $consoleRoot;
parent::__construct();
}

Expand All @@ -80,17 +91,23 @@ protected function configure()
$this
->setName('init')
->setDescription($this->trans('commands.init.description'))
->addOption(
'destination',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.init.options.destination')
)
->addOption(
'override',
null,
InputOption::VALUE_NONE,
$this->trans('commands.init.options.override')
)
->addOption(
'local',
'autocomplete',
null,
InputOption::VALUE_NONE,
$this->trans('commands.init.options.local')
$this->trans('commands.init.options.autocomplete')
);
}

Expand All @@ -100,27 +117,26 @@ protected function configure()
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$local = $input->getOption('local');
$destination = $input->getOption('destination');
$autocomplete = $input->getOption('autocomplete');
$configuration = $this->configurationManager->getConfiguration();

if (!$local) {
$local = $io->confirm(
$this->trans('commands.init.questions.local'),
true
);
$input->setOption('local', $local);
}

if ($local) {
$root = null;
foreach ($this->webRootDirectories as $webRootDirectory) {
if (!$root && is_dir(getcwd().'/'.$webRootDirectory)) {
$root = $webRootDirectory;
}
if (!$destination) {
if ($this->appRoot && $this->consoleRoot) {
$destinationList[] = $this->configurationManager
->getConsoleDirectory();
$destinationList[] = $this->consoleRoot . '/console/';
$destination = $io->choice(
$this->trans('commands.init.questions.destination'),
$destinationList
);
}
$this->configParameters['root'] = $root?:$io->askEmpty(
$this->trans('commands.init.questions.root')
);
else {
$destination = $this->configurationManager
->getConsoleDirectory();
}

$input->setOption('destination', $destination);
}

$this->configParameters['language'] = $io->choiceNoList(
Expand All @@ -147,6 +163,14 @@ protected function interact(InputInterface $input, OutputInterface $output)
$this->trans('commands.init.questions.generate-chain'),
false
);

if (!$autocomplete) {
$autocomplete = $io->confirm(
$this->trans('commands.init.questions.autocomplete'),
false
);
$input->setOption('autocomplete', $autocomplete);
}
}

/**
Expand All @@ -156,8 +180,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$copiedFiles = [];
$destination = $input->getOption('destination');
$autocomplete = $input->getOption('autocomplete');
$override = $input->getOption('override');
$local = $input->getOption('local');
if (!$destination) {
$destination = $this->configurationManager->getConsoleDirectory();
}

$finder = new Finder();
$finder->in(
Expand All @@ -170,21 +198,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
$finder->files();

foreach ($finder as $configFile) {
$source = sprintf(
$sourceFile = sprintf(
'%s%s/config/dist/%s',
$this->configurationManager->getApplicationDirectory(),
DRUPAL_CONSOLE_CORE,
$configFile->getRelativePathname()
);

$destination = sprintf(
$destinationFile = sprintf(
'%s%s',
$this->configurationManager->getConsoleDirectory(),
$destination,
$configFile->getRelativePathname()
);

if ($this->copyFile($source, $destination, $override)) {
$copiedFiles[] = $destination;
if ($this->copyFile($sourceFile, $destinationFile, $override)) {
$copiedFiles[] = $destinationFile;
}
}

Expand All @@ -193,19 +221,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->newLine();
}

$processBuilder = new ProcessBuilder(array('bash'));
$process = $processBuilder->getProcess();
$process->setCommandLine('echo $_');
$process->run();
$fullPathExecutable = explode('/', $process->getOutput());
$executableName = trim(end($fullPathExecutable));
$process->stop();
$executableName = null;
if ($autocomplete) {
$processBuilder = new ProcessBuilder(array('bash'));
$process = $processBuilder->getProcess();
$process->setCommandLine('echo $_');
$process->run();
$fullPathExecutable = explode('/', $process->getOutput());
$executableName = trim(end($fullPathExecutable));
$process->stop();
}

$this->generator->generate(
$this->configurationManager->getConsoleDirectory(),
$executableName,
$override,
$local,
$destination,
$this->configParameters
);

Expand Down
36 changes: 19 additions & 17 deletions src/Generator/InitGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class InitGenerator extends Generator
* @param string $userHome
* @param string $executableName
* @param boolean $override
* @param boolean $local
* @param string $destination
* @param array $configParameters
*/
public function generate(
$userHome,
$executableName,
$override,
$local,
$destination,
$configParameters
) {
$configParameters = array_map(
Expand All @@ -37,8 +37,8 @@ function ($item) {
);

$configFile = $userHome . 'config.yml';
if ($local) {
$configFile = getcwd().'/console/config.yml';
if ($destination) {
$configFile = $destination.'config.yml';
}

if (file_exists($configFile) && $override) {
Expand All @@ -54,20 +54,22 @@ function ($item) {
$configParameters
);

$parameters = [
'executable' => $executableName,
];
if ($executableName) {
$parameters = [
'executable' => $executableName,
];

$this->renderFile(
'core/autocomplete/console.rc.twig',
$userHome.'console.rc',
$parameters
);
$this->renderFile(
'core/autocomplete/console.rc.twig',
$userHome . 'console.rc',
$parameters
);

$this->renderFile(
'core/autocomplete/console.fish.twig',
$userHome.'drupal.fish',
$parameters
);
$this->renderFile(
'core/autocomplete/console.fish.twig',
$userHome . 'drupal.fish',
$parameters
);
}
}
}
17 changes: 10 additions & 7 deletions src/Utils/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public function loadConfiguration($applicationDirectory)
foreach ($configurationDirectories as $configurationDirectory) {
$file = $configurationDirectory . 'config.yml';

if ( is_dir($configurationDirectory) &&
stripos($configurationDirectory, '/vendor/') <= 0 &&
stripos($configurationDirectory, '/bin/') <= 0 &&
stripos($configurationDirectory, 'console/') > 0) {;
if (is_dir($configurationDirectory)
&& stripos($configurationDirectory, '/vendor/') <= 0
&& stripos($configurationDirectory, '/bin/') <= 0
&& stripos($configurationDirectory, 'console/') > 0
) {
$this->configurationDirectories[] = str_replace('//', '/', $configurationDirectory);
}

Expand Down Expand Up @@ -226,7 +227,7 @@ public function readDrushEquivalents($commandName)
}

/**
* @deprecated
* @return string
*/
public function getConsoleDirectory()
{
Expand All @@ -236,14 +237,16 @@ public function getConsoleDirectory()
/**
* @return array
*/
public function getMissingConfigurationFiles() {
public function getMissingConfigurationFiles()
{
return $this->missingConfigurationFiles;
}

/**
* @return array
*/
public function getConfigurationDirectories() {
public function getConfigurationDirectories()
{
return $this->configurationDirectories;
}

Expand Down
3 changes: 0 additions & 3 deletions templates/core/init/config.yml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,3 @@ application:
generate-chain: {{generate_chain}}
## yes: false
composer: false
{% if root is defined and root is not empty %}
root: '{{root}}'
{% endif %}

0 comments on commit 436119d

Please sign in to comment.