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

Include aegir/provision 3.x and load commands when running bin/drush #5

Merged
merged 10 commits into from Apr 19, 2018
Merged
@@ -150,3 +150,5 @@ script:

- provision -n -v
- provision status -n
- bin/drush sa
- bin/drush --filter=provision
@@ -6,6 +6,7 @@
"type": "project",
"license": "GPL-2.0+",
"require": {
"aegir/provision": "dev-7.x-3.x",
"consolidation/Robo": "dev-block-output",
"consolidation/annotated-command": "~2",
"drupal/console-core": "1.0.2",
@@ -48,5 +49,10 @@
"prefer-stable": true,
"autoload": {
"psr-4": {"Aegir\\Provision\\": "src/Provision"}
},
"scripts": {
"post-install-cmd": [
"Aegir\\Provision\\Console\\ComposerScripts::writeDrushRcForVendorDrushDrush"
]
}
}

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

@@ -3,6 +3,7 @@
namespace Aegir\Provision;

use Aegir\Provision\Command\CdCommand;
use Aegir\Provision\Command\DrushCommand;
use Aegir\Provision\Command\SaveCommand;
use Aegir\Provision\Command\ServicesCommand;
use Aegir\Provision\Command\SetupCommand;
@@ -145,6 +146,7 @@ public function configureIO(InputInterface $input, OutputInterface $output)
protected function getDefaultCommands()
{
$commands[] = new CdCommand();
$commands[] = new DrushCommand();
$commands[] = new HelpCommand();
$commands[] = new ListCommand();
$commands[] = new SaveCommand();
@@ -0,0 +1,76 @@
<?php

namespace Aegir\Provision\Command;

use Aegir\Provision\Command;
use Aegir\Provision\Provision;
use Psy\Shell;
use Psy\Configuration;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class DrushCommand
*
* @package Aegir\Provision\Command
*/
class DrushCommand extends Command
{
const CONTEXT_REQUIRED = TRUE;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('drush')
->setDescription($this->trans('commands.drush.description'))
->setHelp($this->trans('commands.drush.help'))
->setDefinition($this->getCommandDefinition());
;
}

/**
* Generate the list of options derived from ProvisionContextType classes.
*
* @return \Symfony\Component\Console\Input\InputDefinition
*/
protected function getCommandDefinition() {
$inputDefinition[] = new InputArgument(
'drush_command_string',
InputArgument::OPTIONAL,
'The full drush command to run including options. Pass the entire command including options in a string:
Examples:
$ provision drush "status --fields=root,uri,drupal-version"

'
);
return $inputDefinition;
}


/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int|null|void
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

// Generate the full path and command. (Currently using built in drush (v8)
// Provision's built in drush acts as a wrapper for site local drush when run in that directory.
$root = $this->context->getProperty('root');
$uri = $this->context->getProperty('uri');

$command = dirname(dirname(dirname(dirname(__FILE__)))) . '/bin/drush ' . $input->getArgument('drush_command_string') . " --root=$root --uri=$uri ";

$this->getProvision()->getLogger()->debug("Running $command");

$this->context->process_exec($command, $this->context->getWorkingDir());
}
}
@@ -0,0 +1,54 @@
<?php

/**
* @file ComposerScripts.php
*
* This file is not meant to be loaded by provision as it requires Composer classes.
*
* Currently it only contains one "script": writeDrushRcForVendorDrushDrush()
*
* This is used on composer install to copy a drushrc.php file into vendor/drush/drush.
*
* This drushrc.php file is used to include the provision 3.x drush commands.
*
*/

namespace Aegir\Provision\Console;

use Composer\Script\Event;
use Composer\Installer\PackageEvent;

class ComposerScripts {

/**
* @param \Composer\Script\Event $event
*
* @throws \Exception
*/
public static function writeDrushRcForVendorDrushDrush(Event $event)
{
$root_dir = dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/drush/drush';
if (file_exists($root_dir) && is_writable($root_dir)) {
$drushrc = <<<PHP
<?php

// Includes the Aegir Provision 3.x drush commands when running bin/drush.
\$options['include'] = array(
dirname(dirname(dirname(__FILE__))) . '/aegir/provision',
);

// Dynamically loads Provision4 context data into aegir/provision 7.x-3.x drush aliases!
\$options['alias-path'] = array(
dirname(dirname(dirname(dirname(__FILE__)))) . '/src/Provision/Console/Provision3Aliases',
);

PHP;
if (file_put_contents($root_dir . '/drushrc.php', $drushrc)) {
print "Wrote drushrc.php to $root_dir \n";
}
}
else {
throw new \Exception("Directory $root_dir does not exist or is not writable. Provision cannot load it's commands.");
}
}
}
@@ -0,0 +1,40 @@
<?php

/**
* @file aliases.drushrc.php
*
* This file is used as a shim between Provision4 and Aegir Provision 7.x-3.x.
*
* It is used to generate "drush aliases" from the Provision 4 contexts.
*
* When you run bin/drush in the provision folder, it will load all of these aliases.
*
* This only works if drushrc.php is configured to look in this folder for aliases.
*
* This is done on `composer install` using Aegir\Provision\Console\ComposerScripts:
*
* We copy a small drushrc.php file into the vendor/drush/drush folder so we can
* force configuration on every bin/drush command.
*
* The reason we need a shim is that Provision 3.x has a lot of drush commands
* that will take some time to migrate to Symfony Console commands.
*
* For now we will run both side by side.
*
* When all Provision commands are converted, we will bump to 4.1.x
*
*/


# Load Provision4 Autoloader
include dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/autoload.php';

# Loop through all Provision4 Contexts
foreach (Aegir\Provision\Provision::getProvision()->getAllContexts() as $context) {

# Load all context properties as an alias.
$aliases[$context->name] = $context->getProperties();

# @TODO: Refactor the alias data so it is compatible with Provision 3.x API.

}
@@ -129,6 +129,18 @@ function __construct(
}
}

/**
* @return string Working directory for the context. Either the site or platform root, or the server config path.
*/
public function getWorkingDir() {
if ($this->hasProperty('root')) {
return $this->getProperty('root');
}
elseif ($this->hasProperty('server_config_path')) {
return $this->getProperty('server_config_path');
}
}

/**
* Load and process the Config object for this context.
*