diff -u b/libraries.drush.inc b/libraries.drush.inc --- b/libraries.drush.inc +++ b/libraries.drush.inc @@ -1,5 +1,4 @@ dt('Lists registered library information.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, - 'aliases' => array('ll'), + 'aliases' => array('libl', 'liblist'), ); $commands['libraries-download'] = array( 'description' => dt('Download all non-existent libraries from "hook_libraries_info()". You able to specify necessary libraries separating their names by spaces or download all non-existent libraries.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, - 'aliases' => array('ld'), + 'aliases' => array('libd', 'libget'), 'options' => array( - 'profile' => dt('Specify this empty option if you want to download libraries into profile installation folder.'), + 'profile' => dt('Specify this empty option if you want to download libraries into profile installation folder. In this case the "--uri" parameter will be ignored.'), + 'all' => dt('Download all defined, non-existent libraries.'), ), 'examples' => array( - 'drush ld' => dt('Download all defined and not downloaded libraries to "sites/all/libraries".'), - 'drush ld --profile' => dt('Download all defined libraries into "!dir".', array('!dir' => 'profiles//libraries')), - 'drush ld twitteroauth parsedown' => dt('Download the "Twitter OAuth" and "Parsedown" libraries to "sites/all/libraries".'), + 'drush ld [--uri|-l ]' => dt('Download defined libraries.'), ), ); @@ -110,26 +108,16 @@ */ function drush_libraries_download() { $info = libraries_info(); - // Remove the command name from arguments. - $arguments = array_slice(drush_get_arguments(), 1); - $download_all = empty($arguments); - - // Allow to download libraries specified by user. - if (!$download_all) { - foreach ($info as $name => $library) { - // If library name is not contained in the arguments array then - // remove item from list for downloading. - if (!in_array($name, $arguments)) { - unset($info[$name]); - } - } + + if (!drush_get_option('all', FALSE)) { + $info = array_intersect_key($info, array_flip(array_slice(drush_get_arguments(), 1))); } - if (empty($info) && !$download_all) { + if (empty($info)) { drush_log(dt('You are not specified any existing library, defined by "hook_libraries_info()".'), 'error'); } else { - $path = drush_get_option('profile') ? drupal_get_path('profile', drupal_get_profile()) : 'sites/all'; + $path = sprintf('%s/%s/libraries', drush_locate_root(), _drush_libraries_download_path()); $libraries = libraries_get_libraries(); $downloaded = _drush_libraries_download($path, $libraries, $info); @@ -147,7 +135,7 @@ $download[$machine_name] = $info[$machine_name]; } else { - drush_log(dt('Cannot remove the "!dir" directory.', array('!dir' => $libraries[$machine_name])), 'error'); + drush_log(dt('Cannot remove the "@dir" directory.', array('@dir' => $libraries[$machine_name])), 'error'); } } @@ -164,7 +152,7 @@ * @internal * * @param string $path - * Relative path to "libraries" folder (sites/all or profiles/). + * Destination path. * @param array $existing * An empty or returned by {@link libraries_get_libraries()} array. * @param array $download @@ -176,32 +164,50 @@ */ function _drush_libraries_download($path, array $existing, array $download) { $downloaded = array(); - $path = sprintf('%s/%s/libraries', DRUPAL_ROOT, $path); foreach ($download as $name => $library) { - $archive = $path . '/' . basename($library['download url']); + $filename = basename($library['download url']); + // Path to file that will be downloaded. + $file = "$path/$filename"; + // Temporary path needed for extracting archives content + // for future moving of contents to destination folder. + $tmp = "$file-tmp"; if (empty($existing[$name])) { // Keep going, if library does not exist and user allows to download it. - if (drush_confirm(dt('Are you want download the "!lib" library?', array('!lib' => $library['name'])))) { + if (drush_confirm(dt('Are you want download the "@lib" library?', array('@lib' => $library['name'])))) { // Download an archive into "*/libraries" folder. - if (!empty($library['download url']) && _drush_download_file($library['download url'], $archive)) { - // Extract data into "*/libraries/LIBRARY-tmp" directory and get - // the listing of the whole structure of an archive. - $listing = drush_tarball_extract($archive, "$path/$name-tmp", TRUE); + if (!empty($library['download url']) && _drush_download_file($library['download url'], $file)) { + $destination = "$path/$name"; + // Re-assign variable because we need to remove directory later. + $source = $file; + + if (drush_file_is_tarball($file)) { + // Extract data into "*/libraries/LIBRARY-tmp" directory and get + // the listing of the whole structure of an archive. + $listing = drush_tarball_extract($file, $tmp, TRUE); + + if (!empty($listing)) { + $source = $tmp . '/' . reset($listing); + } + } + // Process single file. + elseif (drush_mkdir($destination)) { + $destination .= "/$filename"; + } // Move the downloaded data into a "*/libraries/LIBRARY" directory. - if (!empty($listing) && drush_move_dir("$path/$name-tmp/" . reset($listing), "$path/$name")) { - drush_log(dt('The library was downloaded to "!dir".', array('!dir' => "$path/$name")), 'success'); + if (drush_copy_dir($source, $destination)) { + drush_log(dt('The library was downloaded to "@dir".', array('@dir' => "$path/$name")), 'success'); } else { drush_log(dt('Library could not be moved from temporary folder.'), 'error'); } - array_map('drush_delete_dir', array($archive, "$path/$name-tmp")); + array_map('drush_delete_dir', array($file, $tmp)); } else { - drush_log(dt('To download a library, the "download url" parameter shall point to an archive file.'), 'error'); + drush_log(dt('To download a library, the "download url" parameter shall point to file that can be downloaded.'), 'error'); } } } @@ -214,0 +221,23 @@ + +/** + * Determine downloading destination. + * + * @return string + * Relative path for downloading. + */ +function _drush_libraries_download_path() { + if (drush_get_option('profile', FALSE)) { + return drupal_get_path('profile', drupal_get_profile()); + } + + $uri = drush_get_option('uri'); + $path = drush_conf_path($uri); + + // If "--uri" or "-l" parameter specified, then check that configuration + // path exists or user specially chosen "default" directory. + if (!isset($path) || ('sites/default' === $path && 'default' !== $uri)) { + return drush_drupal_sitewide_directory(); + } + + return $path; +}