View source
<?php
define('ADDRESSFIELD_AUTOCOMPLETE_GEOCOMPLETE_DOWNLOAD_URI', 'https://github.com/ubilabs/geocomplete/archive/');
function addressfield_autocomplete_drush_command() {
$items = array();
$items['geocomplete-plugin'] = array(
'callback' => 'drush_addressfield_autocomplete_geocomplete_plugin',
'description' => dt('Download and install the Geocomplete plugin.'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'options' => array(
'version' => dt('The version of the geocomplete plugin to download e.g. 1.6.3'),
'path' => dt('A path where to install the Geocomplete plugin. If omitted Drush will use the default location.'),
),
'aliases' => array(
'geocompleteplugin',
'addressfieldautocomplete-plugin',
),
);
return $items;
}
function addressfield_autocomplete_drush_help($section) {
switch ($section) {
case 'drush:geocomplete-plugin':
return dt('Download and install the Geocomplete plugin from ubilabs.github.io/geocomplete/, default location is sites/all/libraries.');
}
}
function drush_addressfield_autocomplete_geocomplete_plugin() {
$version = drush_get_option('version');
$path = drush_get_option('path', 'sites/all/libraries');
if (!preg_match('/v?(\\d\\.?){2,3}/', $version)) {
$version = 'master';
}
$olddir = getcwd();
try {
_addressfield_autocomplete_drush_plugin_process($version, $path);
drush_log(dt('Geocomplete plugin has been installed in @path', array(
'@path' => $path,
)), 'success');
} catch (\Exception $e) {
drush_log(dt($e
->getMessage()), 'error');
}
chdir($olddir);
}
function _addressfield_autocomplete_drush_plugin_process($version, $path) {
if (!is_dir($path)) {
if (!drush_op('mkdir', $path)) {
throw new \Exception(sprintf('Directory %s can not be created', $path));
}
drush_log(dt('Directory @path was created', array(
'@path' => $path,
)), 'notice');
}
if (!is_writable($path)) {
throw new \Exception(sprintf('%s is not writable', $path));
}
chdir($path);
$filepath = _addressfield_autocomplete_drush_plugin_download($version);
if (!$filepath) {
$filepath = _addressfield_autocomplete_drush_plugin_download('v' . $version);
if (!$filepath) {
throw new \Exception('Geocomplete plugin could not be downloaded.');
}
}
if (is_dir('geocomplete')) {
drush_delete_dir('geocomplete', TRUE);
drush_log(dt('An existing Geocomplete plugin was deleted from @path', array(
'@path' => $path,
)), 'notice');
}
$filename = basename($filepath);
$listing = drush_tarball_extract($filename, FALSE, TRUE);
if (!$listing || empty($listing[1])) {
throw new \Exception('Geocomplete plugin could not be extracted');
}
$dirname = rtrim($listing[1], '/');
if ($dirname != 'geocomplete' && !drush_move_dir($dirname, 'geocomplete', TRUE)) {
throw new \Exception('Geocomplete plugin could not be moved');
}
}
function _addressfield_autocomplete_drush_plugin_download($version) {
$url = ADDRESSFIELD_AUTOCOMPLETE_GEOCOMPLETE_DOWNLOAD_URI . $version . '.zip';
return drush_download_file($url);
}