Skip to content

Commit

Permalink
Issue drush-ops#4: Add support for drush pm-download my_module.
Browse files Browse the repository at this point in the history
  • Loading branch information
serundeputy committed Jan 1, 2016
1 parent 21c8394 commit 7ab1c8c
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions backdrop.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ function backdrop_drush_command() {
),
);

$items['backdrop-download'] = array(
'description' => 'Download Backdrop CMS contrib modules.',
'hidden' => TRUE,
'arguments' => array(
'module-name' => 'The name of the module you would like to download.',
'backdrop-root' => 'Root of the Backdrop installation.'
),
'required-arguments' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
);

$items['backdrop-unsupported'] = array(
'description' => 'Fallback command if the provided command is not supported in Backdrop.',
'hidden' => TRUE,
Expand Down Expand Up @@ -130,6 +141,9 @@ function backdrop_drush_command_alter(&$command) {
case 'updatedb-status':
$backdrop_command = 'backdrop-updatedb-status';
break;
case 'pm-download':
$backdrop_command = 'backdrop-download';
break;
}

// Commands that work with Backdrop with no adjustments.
Expand Down Expand Up @@ -243,6 +257,67 @@ function drush_backdrop_updatedb_status() {
return $pending;
}

/**
* Command callback. Download a Backdrop CMS contrib module.
*/
function drush_backdrop_download($project) {
if ($project != 'backdrop') {
$html = get_content_from_github(
"https://github.com/backdrop-contrib/$project/releases/latest"
);

$html = explode("\"", $html);
$url = $html[1];
$latest = explode('/', $url);
$latest = array_reverse($latest);
if (file_exists(BACKDROP_ROOT . '/modules/contrib')) {
$module_path = BACKDROP_ROOT . '/modules/contrib';
}
else {
$module_path = BACKDROP_ROOT . '/modules';
}

exec(
"wget --directory-prefix $module_path https://github.com/backdrop-contrib/$project/releases/download/$latest[0]/$project.zip"
);
// extract the zip file
exec(
"unzip $module_path/$project.zip -d $module_path"
);
// remove the zip file
exec(
"rm $module_path/$project.zip"
);
}
// Downloading backdrop itself is a special case.
elseif ($project == 'backdrop') {
$html = get_content_from_github(
"https://github.com/backdrop/backdrop/releases/latest"
);

$html = explode("\"", $html);
print_r($html);
$url = $html[1];
$latest = explode('/', $url);
$latest = array_reverse($latest);

// get the module
exec(
"wget 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"
);
}

print "Successfully downloaded.\n";
}

/**
* Command callback. Informs the user that the given command is not supported.
*/
Expand All @@ -251,3 +326,16 @@ function drush_backdrop_unsupported() {
return FALSE;
}

/**
* Helper function for backdrop_download()
* gets the url for the github repo of the contrib module.
*/
function get_content_from_github($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}

0 comments on commit 7ab1c8c

Please sign in to comment.