View source
<?php
define('ADDTOHOMESCREEN_DOWNLOAD_URI', 'https://github.com/cubiq/add-to-homescreen/archive/master.zip');
define('ADDTOHOMESCREEN_DOWNLOAD_PREFIX', 'add-to-homescreen-');
function addtohomescreen_drush_command() {
$items = [];
$items['addtohomescreen'] = [
'callback' => 'drush_addtohomescreen',
'description' => dt('Download and install the Add to homescreen library.'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'arguments' => [
'path' => dt('Optional. A path where to install the Add to homescreen library. If omitted Drush will use the default location.'),
],
'aliases' => [
'addtohomescreen-library',
],
];
return $items;
}
function addtohomescreen_drush_help($section) {
switch ($section) {
case 'drushaddtohomescreen':
return dt('Download and install the Add to homescreen library from https://github.com/cubiq/add-to-homescreen, default location is sites/all/libraries.');
}
}
function drush_addtohomescreen() {
$args = func_get_args();
if (!empty($args[0])) {
$path = $args[0];
}
else {
$path = 'libraries';
}
if (!is_dir($path)) {
drush_op('mkdir', $path);
drush_log(dt('Directory @path was created', [
'@path' => $path,
]), 'notice');
}
$olddir = getcwd();
chdir($path);
if ($filepath = drush_download_file(ADDTOHOMESCREEN_DOWNLOAD_URI)) {
$filename = basename($filepath);
$dirname = ADDTOHOMESCREEN_DOWNLOAD_PREFIX . basename($filepath, '.zip');
if (is_dir($dirname) || is_dir('addtohomescreen')) {
drush_delete_dir($dirname, TRUE);
drush_delete_dir('addtohomescreen', TRUE);
drush_log(dt('A existing addtohomescreen plugin was deleted from @path', [
'@path' => $path,
]), 'notice');
}
drush_tarball_extract($filename);
if ($dirname != 'addtohomescreen') {
drush_move_dir($dirname, 'addtohomescreen', TRUE);
$dirname = 'addtohomescreen';
}
}
if (is_dir($dirname)) {
drush_log(dt('addtohomescreen plugin has been installed in @path', [
'@path' => $path,
]), 'success');
}
else {
drush_log(dt('Drush was unable to install the addtohomescreen plugin to @path', [
'@path' => $path,
]), 'error');
}
chdir($olddir);
}