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

Site-set / use functioning again #2989

Merged
merged 5 commits into from
Sep 30, 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
26 changes: 0 additions & 26 deletions includes/sitealias.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,13 @@ use Drush\Log\LogLevel;
use Drush\SiteAlias\AliasRecord;
use Webmozart\PathUtil\Path;



/**
* Option keys used for site selection.
*/
function drush_sitealias_site_selection_keys() {
return array('remote-host', 'remote-user', 'ssh-options', '#name', 'os');
}

/**
* Returns the filename for the file that stores the DRUPAL_SITE variable.
*
* @param string $filename_prefix
* An arbitrary string to prefix the filename with.
*
* @return string|false
* Returns the full path to temp file if possible, or FALSE if not.
*/
function drush_sitealias_get_envar_filename($filename_prefix = 'drush-drupal-site-') {
$shell_pid = getenv('DRUSH_SHELL_PID');
if (!$shell_pid && function_exists('posix_getppid')) {
$shell_pid = posix_getppid();
}
if (!$shell_pid) {
return FALSE;
}

$tmp = getenv('TMPDIR') ? getenv('TMPDIR') : '/tmp';
$username = drush_get_username();

return "{$tmp}/drush-env-{$username}/{$filename_prefix}" . $shell_pid;
}

/**
* Get a site alias record given an alias name or site specification.
*
Expand Down
56 changes: 33 additions & 23 deletions src/Commands/core/SiteCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
namespace Drush\Commands\core;

use Drush\Commands\DrushCommands;

use Drush\Drush;
use Drush\SiteAlias\AliasRecord;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\SiteAlias\SiteAliasManagerAwareTrait;
use Drush\SiteAlias\SiteAliasName;
use Consolidation\OutputFormatters\StructuredData\ListDataFromKeys;
use Robo\Common\ConfigAwareTrait;
use Robo\Contract\ConfigAwareInterface;

class SiteCommands extends DrushCommands implements SiteAliasManagerAwareInterface
class SiteCommands extends DrushCommands implements SiteAliasManagerAwareInterface, ConfigAwareInterface
{
use SiteAliasManagerAwareTrait;
use ConfigAwareTrait;

/**
* Set a site alias to work on that will persist for the current session.
* Set a site alias that will persist for the current session.
*
* Stores the site alias being used in the current session in a temporary
* file.
*
* @command site:set
* @param $site Site specification to use, or "-" for previous site. Omit this argument to unset.
*
* @param string $site Site specification to use, or "-" for previous site. Omit this argument to unset.
*
* @throws \Exception
* @handle-remote-commands
* @validate-php-extension posix
* @usage drush site:set @dev
Expand All @@ -32,14 +37,12 @@ class SiteCommands extends DrushCommands implements SiteAliasManagerAwareInterfa
* @usage drush site:set
* Without an argument, any existing site becomes unset.
* @aliases use,site-set
* @hidden
*/
public function siteSet($site = '@none', $options = ['a' =>'b'])
public function siteSet($site = '@none')
{
// @todo Needs modernizing to get it functional, so @hidden for now.

if ($filename = drush_sitealias_get_envar_filename()) {
$last_site_filename = drush_sitealias_get_envar_filename('drush-drupal-prev-site-');
$filename = $this->getConfig()->get('drush.site-file-current');
if ($filename) {
$last_site_filename = $this->getConfig()->get('drush.site-file-previous');
if ($site == '-') {
if (file_exists($last_site_filename)) {
$site = file_get_contents($last_site_filename);
Expand All @@ -48,37 +51,40 @@ public function siteSet($site = '@none', $options = ['a' =>'b'])
}
}
if ($site == '@self') {
$path = drush_cwd();
$site_record = []; // drush_sitealias_lookup_alias_by_path($path, true);
// $path = drush_cwd();
// $site_record = drush_sitealias_lookup_alias_by_path($path, true);
$site_record = [];
if (isset($site_record['#name'])) {
$site = '@' . $site_record['#name'];
} else {
$site = '@none';
}
// Using 'site-set @self' is quiet if there is no change.
// Using 'site:set @self' is quiet if there is no change.
$current = is_file($filename) ? trim(file_get_contents($filename)) : "@none";
if ($current == $site) {
return;
}
}
if (false && _drush_sitealias_set_context_by_name($site)) {
// Alias record lookup exists.
$aliasRecord = $this->siteAliasManager()->get($site);
if ($aliasRecord) {
if (file_exists($filename)) {
@unlink($last_site_filename);
@rename($filename, $last_site_filename);
}
$success_message = dt("Site set to !site", array('!site' => $site));
if ($site == '@none') {
$success_message = dt('Site set to @site', array('@site' => $site));
if ($site == '@none' || $site == '') {
if (drush_delete_dir($filename)) {
$this->logger()->success(dt('Site unset.'));
}
} elseif (drush_mkdir(dirname($filename), true)) {
if (file_put_contents($filename, $site)) {
$this->logger()->success($success_message);
$this->logger()->info(dt("Site information stored in !file", array('!file' => $filename)));
$this->logger()->info(dt('Site information stored in @file', array('@file' => $filename)));
}
}
} else {
throw new \Exception(dt("Could not find a site definition for !site.", array('!site' => $site)));
throw new \Exception(dt('Could not find a site definition for @site.', array('@site' => $site)));
}
}
}
Expand All @@ -87,7 +93,12 @@ public function siteSet($site = '@none', $options = ['a' =>'b'])
* Show site alias details, or a list of available site aliases.
*
* @command site:alias
* @param $site Site alias or site specification.
*
* @param string $site Site alias or site specification.
* @param array $options
*
* @return \Consolidation\OutputFormatters\StructuredData\ListDataFromKeys
* @throws \Exception
* @aliases sa
* @usage drush site:alias
* List all alias records known to drush.
Expand All @@ -97,7 +108,6 @@ public function siteSet($site = '@none', $options = ['a' =>'b'])
* Print only actual aliases; omit multisites from the local Drupal installation.
* @topics docs:aliases
*
* @return \Consolidation\OutputFormatters\StructuredData\ListDataFromKeys
*/
public function siteAlias($site = null, $options = ['format' => 'yaml'])
{
Expand Down
65 changes: 65 additions & 0 deletions src/Config/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public function loadSiteAutoloader($root)
return $this->siteLoader;
}

/**
* Return the name of the user running drush.
*/
protected function getUsername()
{
$name = null;
if (!$name = getenv("username")) { // Windows
if (!$name = getenv("USER")) {
// If USER not defined, use posix
if (function_exists('posix_getpwuid')) {
$processUser = posix_getpwuid(posix_geteuid());
$name = $processUser['name'];
}
}
}
return $name;
}

/**
* Convert the environment object into an exported configuration
* array. This will be fed though the EnvironmentConfigLoader to
Expand Down Expand Up @@ -111,7 +129,12 @@ public function exportConfigData()
'user-dir' => $this->userConfigPath(),
'system-dir' => $this->systemConfigPath(),
'system-command-dir' => $this->systemCommandFilePath(),
'site-file-previous' => $this->getSiteSetAliasFilePath('drush-drupal-prev-site-'),
'site-file-current' => $this->getSiteSetAliasFilePath(),
],
'runtime' => [
'user' => $this->getUsername(),
]
];
}

Expand All @@ -126,6 +149,23 @@ public function drushBasePath()
return $this->drushBasePath;
}

/**
* Get the site:set alias from the current site:set file path.
*
* @return bool|string
*/
public function getSiteSetAliasName()
{
$site_filename = $this->getSiteSetAliasFilePath();
if (file_exists($site_filename)) {
$site = file_get_contents($site_filename);
if ($site) {
return $site;
}
}
return false;
}

/**
* User's home directory
*
Expand Down Expand Up @@ -374,4 +414,29 @@ public function calculateColumns()
// TODO: should we deal with reserve-margin here, or adjust it later?
return $columns;
}

/**
* Returns the filename for the file that stores the DRUPAL_SITE variable.
*
* @param string $filename_prefix
* An arbitrary string to prefix the filename with.
*
* @return string|false
* Returns the full path to temp file if possible, or FALSE if not.
*/
protected function getSiteSetAliasFilePath($filename_prefix = 'drush-drupal-site-')
{
$shell_pid = getenv('DRUSH_SHELL_PID');
if (!$shell_pid && function_exists('posix_getppid')) {
$shell_pid = posix_getppid();
}
if (!$shell_pid) {
return false;
}

$tmp = getenv('TMPDIR') ? getenv('TMPDIR') : '/tmp';
$username = $this->getUsername();

return "{$tmp}/drush-env-{$username}/{$filename_prefix}" . $shell_pid;
}
}
12 changes: 1 addition & 11 deletions src/Preflight/Preflight.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
<?php
namespace Drush\Preflight;

use Composer\Autoload\ClassLoader;
use Drush\Drush;
use Drush\Config\Environment;
use Drush\Config\ConfigLocator;
use Drush\Config\EnvironmentConfigLoader;
use Drush\SiteAlias\SiteAliasManager;
use DrupalFinder\DrupalFinder;

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;

use Webmozart\PathUtil\Path;

/**
* Prepare to bootstrap Drupal
*
Expand Down Expand Up @@ -212,7 +202,7 @@ protected function doRun($argv)

// Configure alias manager.
$aliasManager = (new SiteAliasManager())->addSearchLocations($paths);
$selfAliasRecord = $aliasManager->findSelf($preflightArgs->alias(), $root, $preflightArgs->uri());
$selfAliasRecord = $aliasManager->findSelf($preflightArgs, $this->environment, $root);
$aliasConfig = $selfAliasRecord->exportConfig();
$configLocator->addAliasConfig($aliasConfig);

Expand Down
42 changes: 33 additions & 9 deletions src/SiteAlias/SiteAliasManager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Drush\SiteAlias;

use Drush\Config\Environment;
use Drush\Preflight\PreflightArgsInterface;

/**
* Site Alias manager
*/
Expand Down Expand Up @@ -109,15 +112,17 @@ public function setSelf(AliasRecord $selfAliasRecord)
* During bootstrap, finds the currently selected site from the parameters
* provided on the commandline.
*
* @param string $aliasName An alias name or site specification
* @param string $root The default Drupal root (from --root or cwd)
* @param string $uri The selected multisite
* @param string $cwd The cwd at the time Drush was first called
* @return type
* @param PreflightArgsInterface $preflightArgs An alias name or site specification
* @param \Drush\Config\Environment $environment
* @param string $root The default Drupal root (from site:set, --root or cwd)
*
* @return \Drush\SiteAlias\type
* @throws \Exception
*/
public function findSelf($aliasName, $root, $uri)
public function findSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
{
$selfAliasRecord = $this->buildSelf($aliasName, $root, $uri);
$aliasName = $preflightArgs->alias();
$selfAliasRecord = $this->buildSelf($preflightArgs, $environment, $root);
if (!$selfAliasRecord) {
throw new \Exception("The alias $aliasName could not be found.");
}
Expand Down Expand Up @@ -192,9 +197,17 @@ public function listAllFilePaths()
* Either look up the specified alias name / site spec,
* or, if those are invalid, then generate one from
* the provided root and URI.
*
* @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
* @param \Drush\Config\Environment $environment
* @param $root
*
* @return \Drush\SiteAlias\AliasRecord
*/
protected function buildSelf($aliasName, $root, $uri)
protected function buildSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
{
$aliasName = $preflightArgs->alias();

// If the user specified an @alias, that takes precidence.
if (SiteAliasName::isAliasName($aliasName)) {
return $this->getAlias($aliasName);
Expand All @@ -206,6 +219,17 @@ protected function buildSelf($aliasName, $root, $uri)
return new AliasRecord($specParser->parse($aliasName, $root), $aliasName);
}

// If the user provides the --root parameter then we don't want to use
// the site alias.
$selectedRoot = $preflightArgs->selectedSite();
if (!$selectedRoot) {
$aliasName = $environment->getSiteSetAliasName();
$alias = $this->getAlias($aliasName);
if ($alias) {
return $alias;
}
}

// If there is no root, then return '@none'
if (!$root) {
return new AliasRecord([], '@none');
Expand All @@ -224,7 +248,7 @@ protected function buildSelf($aliasName, $root, $uri)
return new AliasRecord(
[
'root' => $root,
'uri' => $uri,
'uri' => $preflightArgs->uri(),
],
'@self'
);
Expand Down
Loading