chosen_lib.drush.inc in Chosen 3.0.x
Drush integration for chosen.
File
modules/chosen_lib/drush/chosen_lib.drush.inc
View source
<?php
define('CHOSEN_DOWNLOAD_URI', 'https://github.com/harvesthq/chosen/releases/download/v1.8.7/chosen_v1.8.7.zip');
function chosen_lib_drush_command() {
$items = [];
$items['chosen-plugin'] = [
'callback' => 'drush_chosen_lib_plugin',
'description' => dt('Download and install the Chosen plugin.'),
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'arguments' => [
'path' => dt('Optional. A path where to install the Chosen plugin. If omitted Drush will use the default location.'),
],
'aliases' => [
'chosenplugin',
],
];
return $items;
}
function chosen_lib_drush_help($section) {
switch ($section) {
case 'drush:chosen-plugin':
return dt('Download and install the Chosen plugin from https://harvesthq.github.com/chosen, default location is /libraries.');
}
}
function drush_chosen_lib_plugin() {
$args = func_get_args();
if (!empty($args[0])) {
$path = $args[0];
}
else {
$path = 'libraries';
}
if (!is_dir($path)) {
drush_op('mkdir', $path);
\Drupal::logger(dt('Directory @path was created', [
'@path' => $path,
]))
->notice('notice');
}
$olddir = getcwd();
chdir($path);
if ($filepath = drush_download_file(CHOSEN_DOWNLOAD_URI)) {
$filename = basename($filepath);
$dirname = basename($filepath, '.zip');
if (is_dir($dirname) || is_dir('chosen')) {
$fileservice = \Drupal::service('file_system');
$fileservice
->deleteRecursive($dirname);
$fileservice
->deleteRecursive('chosen');
\Drupal::logger(dt('A existing Chosen plugin was deleted from @path', [
'@path' => $path,
]))
->notice('notice');
}
drush_tarball_extract($filename, $dirname);
if ($dirname != 'chosen') {
drush_move_dir($dirname, 'chosen', TRUE);
$dirname = 'chosen';
}
}
if (is_dir($dirname)) {
\Drupal::logger(dt('Chosen plugin has been installed in @path', [
'@path' => $path,
]))
->info('success');
}
else {
\Drupal::logger(dt('Drush was unable to install the Chosen plugin to @path', [
'@path' => $path,
]))
->error('error');
}
chdir($olddir);
}