diff --git a/libraries.api.php b/libraries.api.php index ac09743..b65bae2 100644 --- a/libraries.api.php +++ b/libraries.api.php @@ -183,7 +183,19 @@ function hook_libraries_info() { // Only used in administrative UI of Libraries API. 'name' => 'Example library', 'vendor url' => 'http://example.com', + // Optional: A website link to the Library's download page. 'download url' => 'http://example.com/download', + // Optional: Information for automated downloading of the Library using + // Drush Make. Visit Drush Make's documentation to read more regarding + // the "download" property: http://bit.ly/jWzHPa + 'download' => array( + // The type of download (file, git, svn, or cvs). + 'type' => 'git', + // The URL or repository to download the Library. + 'url' => 'http://github.com/tinymce/tinymce.git', + // The revision information to check out. + 'revision' => '3.4.6', + ), // Optional: If, after extraction, the actual library files are contained in // 'sites/all/libraries/example/lib', specify the relative path here. 'path' => 'lib', @@ -312,6 +324,10 @@ function hook_libraries_info() { 'name' => 'Simple library', 'vendor url' => 'http://example.com/simple', 'download url' => 'http://example.com/simple', + 'download' => array( + 'type' => 'file', + 'url' => 'http://example.com/simple/simple-4.3.tar.gz', + ), 'version arguments' => array( 'file' => 'readme.txt', // Best practice: Document the actual version strings for later reference. @@ -329,6 +345,11 @@ function hook_libraries_info() { 'name' => 'TinyMCE', 'vendor url' => 'http://tinymce.moxiecode.com', 'download url' => 'http://tinymce.moxiecode.com/download.php', + 'download' => array( + 'type' => 'git', + 'url' => 'http://github.com/tinymce/tinymce.git', + 'branch' => 'master', + ), 'path' => 'jscripts/tiny_mce', // The regular expression catches two parts (the major and the minor // version), which libraries_get_version() doesn't allow. diff --git a/libraries.drush.inc b/libraries.drush.inc index 26b35eb..78bb34b 100644 --- a/libraries.drush.inc +++ b/libraries.drush.inc @@ -9,18 +9,42 @@ * Implements hook_drush_command(). */ function libraries_drush_command() { + // List all registered Libraries. $items['libraries-list'] = array( 'callback' => 'libraries_drush_list', - 'description' => dt('Lists registered library information.'), + 'description' => 'Lists registered library information.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, ); - /**$items['libraries-download'] = array( - 'callback' => 'libraries_drush_download', - 'description' => dt('Downloads a registered library into the libraries directory for the active site.'), - 'arguments' => array( - 'name' => dt('The internal name of the registered library.'), - ), - );*/ + + // Make sure Drush Make is available (4.x or 5.x) to allow automated + // downloading of libraries. + $make = function_exists('drush_make_download_factory') || function_exists('make_download_factory'); + if ($make) { + $items['libraries-make'] = array( + 'callback' => 'libraries_drush_make', + 'description' => 'Downloads a registered library into the libraries directory for the active site.', + 'arguments' => array( + 'name' => '(optional) The internal name of the registered library.', + ), + 'options' => array( + 'destination' => 'Destination path (from Drupal root) for the library to be installed to. Defaults to "sites/all/libraries"', + 'type' => 'The type of download (git, svn, cvs, or file). Defaults to the type defined by the module.', + 'url' => 'The URL to download from. Defaults to the url defined by the module.', + 'revision' => 'The revision of which to download. Defaults to the revision defined by the module.', + 'branch' => 'The branch of which to check out. Defaults to the brach defined by the module.', + 'tag' => 'The tag of to check out. Defaults to the tag defined by the module.', + 'all' => 'Downloads all defined libraries.', + ), + 'examples' => array( + 'drush libraries-make' => 'Downloads all defined libraries.', + 'drush libraries-make --destination="sites/example.com/libraries"' => 'Downloads all defined libraries to sites/example.com/libraries.', + 'drush libraries-make tinymce' => 'Downloads the TinyMCE library.', + 'drush libraries-make tinymce --destination="sites/example.com/libraries"' => 'Downloads TinyMCE to the "libraries" directory for example.com.', + 'drush libraries-make tinymce --revision="3.4.3"' => 'Downloads version 3.4.3 of the TinyMCE library.', + 'drush libraries-make --all' => 'Downloads all libraries defined by all modules.', + ), + ); + } return $items; } @@ -32,10 +56,8 @@ function libraries_drush_help($section) { case 'drush:libraries-list': return dt('Lists registered library information.'); - case 'drush:libraries-download': - return dt('Downloads a registered library into the libraries directory for the active site. - -See libraries-list for a list of registered libraries.'); + case 'drush:libraries-make': + return dt('Downloads a registered library into the libraries directory for the active site.'); } } @@ -50,7 +72,7 @@ function libraries_drush_list() { ksort($libraries); if (empty($libraries)) { - drush_print('There are no registered libraries.'); + return drush_log(dt('There are no registered libraries.'), 'success'); } else { @@ -88,64 +110,58 @@ function libraries_drush_list() { * @param $name * 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.')); +function libraries_drush_make($name = '') { + // Allow automated downloading of all libraries. + if (drush_get_option('all', FALSE)) { + foreach (libraries_info() as $key => $lib) { + libraries_drush_make($key); + } + return drush_log(dt('Processed all defined libraries.'), 'success'); } - // @todo Simply use current drush site. - $args = func_get_args(); - if ($args[0]) { - $path = $args[0]; - } - else { - $path = 'sites/all/libraries'; + // Make sure a Library is provided. + if (empty($name)) { + return drush_log(dt('Please specify which library to download. See "drush libraries-list" for a list of available libraries.'), 'warning'); } - // 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'); + // Check if the library is defined. + $library = libraries_info($name); + if (empty($library)) { + return drush_log(dt('The given library, "!name", was not found.', array('!name' => $name)), 'error'); } - // Set the directory to the download location. - $olddir = getcwd(); - chdir($path); - - $filename = basename(COLORBOX_DOWNLOAD_URI); - $dirname = basename(COLORBOX_DOWNLOAD_URI, '.zip'); - - // 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'); - } - // Remove any existing Colorbox plugin zip archive - if (is_file($filename)) { - drush_op('unlink', $filename); + // Check if automated downloading of this library is avialable. + if (!isset($library['download'])) { + return drush_log(dt('Automated downloading of @name is not supported.', array('@name' => $name)), 'warning'); } - // Download the zip archive - if (!drush_shell_exec('wget '. COLORBOX_DOWNLOAD_URI)) { - drush_shell_exec('curl -O '. COLORBOX_DOWNLOAD_URI); + // Allow manual overriding of any given download options. + foreach (array('type', 'url', 'revision', 'branch', 'tag') as $index => $option) { + $value = drush_get_option($option, ''); + if (!empty($value)) { + $library['download'][$option] = $value; + } } - if (is_file($filename)) { - // Decompress the zip archive - drush_shell_exec('unzip -qq -o '. $filename); - // Remove the zip archive - drush_op('unlink', $filename); - } + // Figure out where we want to save the library. + $destination = drush_get_option('destination', 'sites/all/libraries'); + $destination = DRUPAL_ROOT . '/' . $destination . '/' . $name; - // Set working directory back to the previous working directory. - chdir($olddir); + // Support for both Drush Make 4.x and 5.x. + $make = function_exists('drush_make_download_factory') ? 'drush_make_download_factory' : 'make_download_factory'; + $path = $make($name, $library['download'], $destination); - if (is_dir($path .'/'. $dirname)) { - drush_log(dt('Colorbox plugin has been downloaded to @path', array('@path' => $path)), 'success'); + // The installation path is returned if the library was downloaded properly. + if ($path) { + return drush_log(dt('The @name library was downloaded to @path.', array( + '@name' => $name, + '@path' => $path, + )), 'success'); } else { - drush_log(dt('Drush was unable to download the Colorbox plugin to @path', array('@path' => $path)), 'error'); + return drush_log(dt('There was an error downloading @name to @destination.', array( + '@name' => $name, + '@destination' => $destination, + )), 'error'); } }