Skip to content

Instantly share code, notes, and snippets.

@illmatix
Last active April 7, 2016 23:47
Show Gist options
  • Select an option

  • Save illmatix/fd8bc392c0e17b44e59fa5231005648d to your computer and use it in GitHub Desktop.

Select an option

Save illmatix/fd8bc392c0e17b44e59fa5231005648d to your computer and use it in GitHub Desktop.
Revised Drush aliases for devboxes. Checks all the aliaes and determines if they're local or remote aliases.
<?php
/**
* @file aliases.drushrc.php
* Author: Chad Payne <chadpayne@gmail.com>
* Date: Apr 7th 2016
*
* Automatic alias Generation for drush sa.
* Updated to allow for local site alias and to ignore any gitlab remotes
*/
// Get list of Remotes for this repo.
exec('git remote -v 2> /dev/null', $remotes);
// Get systems current hostname.
exec('hostname 2> /dev/null', $hostname);
// Filter out any gitlab remotes
$remotes = array_filter($remotes, function($remote) {
return (strpos($remote, 'git@gitlab.com') == FALSE);
});
foreach ($remotes as $remote_raw) {
// If this is a remote set up aliases for it.
if (strpos($remote_raw, '@') !== FALSE) {
preg_match('{(?<label>.*?)\s(?<host>.*?):(?<path>(?<basename>.*?)(?:/public_html)?)(?:/.git)}', $remote_raw, $git_remote);
$label = $git_remote['label'];
$host = $git_remote['host'];
$host = explode('@', $host);
$root = $git_remote['path'];
$site_name = basename($git_remote['basename']);
$uri = basename($site_name) . '.' . $host[1];
$alias_key = $host[1] . '-' . $site_name;
$remote = array(
'remote-host' => $host[1],
'root' => $root,
'uri' => $uri,
'path-aliases' => array(
'%files' => 'sites/default/files',
),
);
$aliases['remote-' . $label] = $remote;
} else {
preg_match('{(?<label>.*?)\s(?<path>(?<basename>.*?)(?:/public_html)?)(?:/.git)}', $remote_raw, $git_remote);
$label = $git_remote['label'];
$host = $hostname[0];
$host = explode('@', $host);
$root = $git_remote['path'];
$site_name = basename($git_remote['basename']);
$uri = basename($site_name) . '.' . $host[1];
$local = array(
'root' => $root,
'uri' => $uri,
'path-aliases' => array(
'%files' => 'sites/default/files',
),
);
$aliases['local-' . $label] = $local;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment