Skip to content

Instantly share code, notes, and snippets.

@serundeputy
Created December 1, 2016 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serundeputy/b3d8a8c41e7c3dfd5b2534d7dd7897e5 to your computer and use it in GitHub Desktop.
Save serundeputy/b3d8a8c41e7c3dfd5b2534d7dd7897e5 to your computer and use it in GitHub Desktop.
custom drush command
<?php
/**
* @file
* Download Backdrop CMS with drush.
*/
/**
* Implements hook_drush_command().
*/
function backdrop_download_backdrop_drush_command() {
$items = array();
$items['backdrop-download'] = array(
'description' => 'Download Backdrop CMS contrib modules.',
'callback' => 'backdrop_download_backdrop',
'hidden' => TRUE,
'arguments' => array(
'module-name' => array('The name of the module(s) you would like to download.'),
),
'options' => array(
'select' => 'Select the version of the module.',
'path' => "Specify a path to download backdrop source code to.",
),
'required-arguments' => TRUE,
'help' => "Download Backdrop CMS source code.",
'aliases' => array('dlb', 'dl-backdrop'),
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_SITE,
);
return $items;
}
/**
* Command callback. Download a Backdrop CMS.
*
* @param $projects
* Array of Backdrop CMS contrib projects to download.
*/
function backdrop_download_backdrop() {
$package_handler = backdrop_download_backdrop_validate();
if (!$package_handler) {
return;
}
$projects = func_get_args();
$options = drush_get_option('select', '');
$download_location = drush_get_option('path', '.');
if (!empty($options)) {
drush_print_r("\t\033[32mChoose one of the available releases for $projects[0] \n");
// get available releases.
$tags = backdrop_command_get_tags(
"https://updates.backdropcms.org/release-history/$projects[0]/1.x"
);
$project_path = backdrop_download_backdrop_get_path($download_location);
for($i = 0; $i < count($tags->releases->release); $i++) {
$version = (string) $tags->releases->release[$i]->version;
drush_print_r("\t\t [$i] \t:\t " . $version);
}
// get users's menu selection
$handle = fopen ("php://stdin","r");
print("\n\tSelect one [0]: ");
print("\033[0m");
$selection = fgets($handle);
$selection = trim($selection);
// default to latest release if user just hits enter.
if (empty($selection)) {
$selection = 0;
}
$sel_url = $tags->releases->release[(int) $selection]->download_link;
// download verssion of the project user selected.
exec(
"wget --quiet --O $project_path/$projects[0].zip $sel_url"
);
// Extract the zip file.
exec(
"unzip $project_path/$projects[0].zip -d $project_path"
);
// Remove the zip file.
exec(
"rm $project_path/$projects[0].zip"
);
backdrop_download_backdrop_dl_outcome($project_path, $projects[0]);
}
else {
foreach ($projects as $project) {
// Downloading backdrop itself is a special case.
// TODO: Downloading backdrop itself does not currently work since
// the backdrop drush command hooks are only picked up once a valid
// backdrop installation is detected, so we have a chicken and the egg
// problem here.
$html = backdrop_download_backdrop_from_github(
"https://github.com/backdrop/backdrop/releases/latest"
);
$html = explode("\"", $html);
$url = $html[1];
$latest = explode('/', $url);
$latest = array_reverse($latest);
// Get the core zip file.
exec(
"wget --quiet -O . https://github.com/$project/$project/releases/download/$latest[0]/backdrop.zip"
);
// Extract the zip file.
exec(
"unzip backdrop.zip"
);
// Remove the zip file.
exec(
"rm backdrop.zip"
);
backdrop_download_backdrop_dl_outcome($download_location, 'Backdrop CMS');
}
}
}
/**
* Helper function for backdrop_command_pm_download().
*
* Gets the url for the github repo of the contrib module.
*/
function backdrop_download_backdrop_from_github($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
/**
* Helper function for --select option for download command.
* Gets the tags for a project.
*/
function backdrop_download_backdrop_get_tags($url) {
$xml = simplexml_load_file($url);
return $xml;
}
/**
* Get the path to where to store backdrop.
* Defaults to current directory.
*
* @param
* string $path
* @return
* string path to download backdrop source to.
*/
function backdrop_download_backdrop_get_path($path = '.') {
$project_path = BACKDROP_ROOT . '/layouts';
return $project_path;
}
/**
* Determine success or failure of drush dl command and give the user some
* relevent feedback.
* @param string $module_install_location
* String directory path where module should end up.
*
* @param string $project
* Project name.
*/
function backdrop_download_backdrop_dl_outcome($module_install_location, $project) {
if (file_exists($module_install_location)) {
print "\n\t\033[32m Success: \033[0m Project $project downloaded to $module_install_location.\n\n";
}
else {
print "\n\t\033[31m Error: \033[0m Project $project could not be found.\n\n";
}
}
/**
* Command backdrop_download_backdrop validate function.
*/
function backdrop_download_backdrop_validate() {
// Check that wget command exists. Disable possible output.
$debug = drush_get_context('DRUSH_DEBUG');
drush_set_context('DRUSH_DEBUG', FALSE);
$success = drush_shell_exec('wget --version');
drush_set_context('DRUSH_DEBUG', $debug);
if (!$success) {
return drush_set_error(
'DRUSH_SHELL_COMMAND_NOT_FOUND',
dt("\n\t\033[1mwget\033[0m executable not found.
Downloading projects with drush depends on wget consider installing wget on your system.\n")
);
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment