diff --git a/libraries.drush.inc b/libraries.drush.inc index 26b35eb..45fcb9b 100644 --- a/libraries.drush.inc +++ b/libraries.drush.inc @@ -14,13 +14,17 @@ function libraries_drush_command() { 'description' => dt('Lists registered library information.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, ); - /**$items['libraries-download'] = array( + $items['libraries-download'] = array( 'callback' => 'libraries_drush_download', - 'description' => dt('Downloads a registered library into the libraries directory for the active site.'), + 'description' => 'Downloads a registered library into the libraries directory for the active site.', 'arguments' => array( - 'name' => dt('The internal name of the registered library.'), + 'name' => 'The internal name of the registered library.', ), - );*/ + 'options' => array( + 'use-site-dir' => 'Force to use the site specific directory. It will create the directory if it doesn\'t exist.', + ), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, + ); return $items; } @@ -89,63 +93,122 @@ function libraries_drush_list() { * The internal name of the library to download. */ function libraries_drush_download($name) { - return; - - // @todo Looks wonky? - if (!drush_shell_exec('type unzip')) { - return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.')); + // Get necessary information about the given library. + if (!($library = libraries_info($name))) { + drush_print(dt('Given library does not exists.')); + return; } - // @todo Simply use current drush site. - $args = func_get_args(); - if ($args[0]) { - $path = $args[0]; + // Determine the destination where the library files should be downloaded to. + $destination = _libraries_drush_download_destination($library); + $destination .= DIRECTORY_SEPARATOR . $library['machine name']; + + // Generate a temporary location for the downloaded files. + $temporary_base = drush_tempdir(); + $temporary_path = $temporary_base . DIRECTORY_SEPARATOR . $library['machine name']; + + drush_log(dt('Downloading library !name to !dir ...', array('!name' => $library['machine name'], '!dir' => $temporary_base))); + + $cache_duration = 86400 * 365; + $filename = basename($library['download url']); + + $path = drush_download_file($library['download url'], $temporary_base . DIRECTORY_SEPARATOR . $filename); + if (!$path) { + return drush_set_error('libraries_drush_download_failed', 'Unable to download ' . $library['machine name'] . ' to ' . $temporary_base . ' from ' . $library['download url']); } else { - $path = 'sites/all/libraries'; + drush_log(dt('Downloading ' . $library['machine name'] . ' was successful.')); } - // Create the path if it does not exist. - if (!is_dir($path)) { - drush_op('mkdir', $path); - drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice'); + // Extract the tarball. + $file_list = drush_tarball_extract($path, $temporary_base, TRUE); + + // Move untarred directory to a directory with the same name as the library's machine name. + $library_dir = drush_trim_path($file_list[0]); + if ($library_dir !== $library['machine name']) { + drush_move_dir($temporary_base . DIRECTORY_SEPARATOR . $library_dir, $temporary_base . DIRECTORY_SEPARATOR . $library['machine name']); } - // Set the directory to the download location. - $olddir = getcwd(); - chdir($path); + // Copy the library to the install location. + if (drush_op('_drush_recursive_copy', $temporary_path, $destination)) { + drush_log(dt("Library !library downloaded to !dest.", array('!library' => $library['machine name'], '!dest' => $destination)), 'success'); + } +} + +/** + * Get the correct download destination for the given library. + * + * @param $library + * The library object. + */ +function _libraries_drush_download_destination($library) { + $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT'); + $site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT', FALSE); + $full_site_root = $drupal_root .'/'. $site_root; + $sites_all = $drupal_root . '/sites/all'; + + $in_site_directory = FALSE; + // Check if we are running within the site directory. + if (strpos(realpath(drush_cwd()), realpath($full_site_root)) !== FALSE || (drush_get_option('use-site-dir', FALSE))) { + $in_site_directory = TRUE; + } - $filename = basename(COLORBOX_DOWNLOAD_URI); - $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip'); + // Attempt 1: If we are in a specific site directory, and the destination + // directory already exists, then we use that. + $destination = ''; + if ($site_root && $in_site_directory) { + $create_dir = drush_get_option('use-site-dir', FALSE); + $destination = _libraries_drush_download_destination_lookup($drupal_root, $full_site_root, $create_dir); + } - // Remove any existing Colorbox plugin directory - if (is_dir($dirname)) { - drush_log(dt('A existing Colorbox plugin was overwritten at @path', array('@path' => $path)), 'notice'); + // Attempt 2: If the destination directory already exists for sites/all, + // then we use that. + if (empty($destination) && $drupal_root) { + $destination = _libraries_drush_download_destination_lookup($drupal_root, $sites_all); } - // Remove any existing Colorbox plugin zip archive - if (is_file($filename)) { - drush_op('unlink', $filename); + + // Attempt 3: If a specific (non default) site directory exists and + // sites/all does not exist, then we create destination in the site + // specific directory. + if (empty($destination) && $site_root && $site_root !== 'sites/default' && is_dir($full_site_root) && !is_dir($sites_all)) { + $destination = _libraries_drush_download_destination_lookup($drupal_root, $full_site_root, TRUE); } - // Download the zip archive - if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) { - drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI); + // Attempt 4: If sites/all exists, then we create destination in the + // sites/all directory. + if (empty($destination) && is_dir($sites_all)) { + $destination = _libraries_drush_download_destination_lookup($drupal_root, $sites_all, TRUE); + } + + // Attempt 5: If site directory exists (even default), then we create + // destination in the this directory. + if (empty($destination) && $site_root && is_dir($full_site_root)) { + $destination = _libraries_drush_download_destination_lookup($drupal_root, $full_site_root, TRUE); } - if (is_file($filename)) { - // Decompress the zip archive - drush_shell_exec('unzip -qq -o '. $filename); - // Remove the zip archive - drush_op('unlink', $filename); + // Attempt 6: If we didn't find a valid directory yet (or we somehow found + // one that doesn't exist) we always fall back to the current directory. + if (empty($destination) || !is_dir($destination)) { + // @todo Log error. + return FALSE; } - // Set working directory back to the previous working directory. - chdir($olddir); + return $destination; +} + +function _libraries_drush_download_destination_lookup($drupal_root, $sitepath, $create = FALSE) { + $destination = $sitepath . '/libraries'; - if (is_dir($path .'/'. $dirname)) { - drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success'); + if ($create) { + drush_log(dt('Attempting to create destination directory at !dir', array('!dir' => $destination))); + drush_mkdir($destination, TRUE); } - else { - drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error'); + + if (is_dir($destination)) { + drush_log(dt('Using destination directory !dir', array('!dir' => $destination))); + return $destination; } + + drush_log(dt('Could not find destination directory at !dir', array('!dir' => $destination))); + return FALSE; }