You are here

function drush_chosen_lib_plugin in Chosen 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/chosen_lib/drush/chosen_lib.drush.inc \drush_chosen_lib_plugin()

Command to download the Chosen plugin.

1 string reference to 'drush_chosen_lib_plugin'
chosen_lib_drush_command in modules/chosen_lib/drush/chosen_lib.drush.inc
Implements hook_drush_command().

File

modules/chosen_lib/drush/chosen_lib.drush.inc, line 47
Drush integration for chosen.

Code

function drush_chosen_lib_plugin() {
  $args = func_get_args();
  if (!empty($args[0])) {
    $path = $args[0];
  }
  else {
    $path = 'libraries';
  }

  // Create the path if it does not exist.
  if (!is_dir($path)) {
    drush_op('mkdir', $path);
    \Drupal::logger(dt('Directory @path was created', [
      '@path' => $path,
    ]))
      ->notice('notice');
  }

  // Set the directory to the download location.
  $olddir = getcwd();
  chdir($path);

  // Download the zip archive.
  if ($filepath = drush_download_file(CHOSEN_DOWNLOAD_URI)) {
    $filename = basename($filepath);
    $dirname = basename($filepath, '.zip');

    // Remove any existing Chosen plugin directory.
    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');
    }

    // Decompress the zip archive.
    drush_tarball_extract($filename, $dirname);

    // Change the directory name to "chosen" if needed.
    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');
  }

  // Set working directory back to the previous working directory.
  chdir($olddir);
}