You are here

function drush_jssor_download in Jssor Slider 8

Implements drush_hook_COMMAND().

File

./jssor.drush.php, line 32
Drush integration for the jssor module.

Code

function drush_jssor_download() {
  $args = func_get_args();
  if (!empty($args[0])) {
    $path = $args[0];
  }
  else {
    $path = 'libraries';
  }
  $folder = '/jssor-slider';

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

  // Check if the folder already exists.
  if (is_dir($path . $folder)) {
    return drush_log('The jssor library appears to be already installed.', 'ok');
  }

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

  // Download the zip archive
  if ($filepath = drush_download_file('https://github.com/jssor/slider/archive/master.zip')) {
    $filename = basename($filepath);
    $dirname = strtolower(basename($filepath, '.zip'));

    // Decompress the zip archive
    drush_tarball_extract($filename);
    if (!file_exists($dirname)) {
      $dirname = 'slider-master';
    }

    // Change the directory name to "jssor-slider" if needed.
    if ($dirname != $folder) {
      drush_move_dir($dirname, $folder, TRUE);
      $dirname = $folder;
    }
  }
  if (is_dir($dirname)) {
    drush_log(dt('jssor library has been installed in @path', [
      '@path' => $path,
    ]), 'success');
  }
  else {
    drush_log(dt('Drush was unable to install the jssor library to @path', [
      '@path' => $path,
    ]), 'error');
  }

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