Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/issue-111-update-command' into…
Browse files Browse the repository at this point in the history
… issue-299-leave-shared-version-when-using-multisite
  • Loading branch information
hosef committed Sep 12, 2024
2 parents a60141b + 7bb4e46 commit 9dba670
Showing 1 changed file with 132 additions and 32 deletions.
164 changes: 132 additions & 32 deletions commands/update.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,44 @@ function update_bee_command() {
),
'optional_arguments' => array('projects'),
'multiple_argument' => 'projects',
'options' => array(
'security-only' => array(
'description' => bt('Only update modules that have security updates available.'),
'short' => 's',
),
),
'examples' => array(
'bee update' => bt('Update everything with a new release.'),
'bee update --security-only' => bt('Update everything that is a security update only'),
'bee update webform tatsu' => bt('Updates the Webform module and Tatsu theme only'),
),
),
'update-list' => array(
'description' => bt('Lists available updates for backdrop, modules, themes and layouts with new releases'),
'callback' => 'update_list_bee_callback',
'aliases' => array('ups', 'pm-updatestatus', 'update-status', 'upl'),
'bootstrap' => BEE_BOOTSTRAP_FULL,
'arguments' => array(
'projects' => bt('One or more projects to list updates for.'),
),
'optional_arguments' => array('projects'),
'multiple_argument' => 'projects',
'options' => array(
'security-only' => array(
'description' => bt('Only list updates for modules that have security updates available.'),
'short' => 's',
),
'format' => array(
'description' => bt('Format to output update information in. Options are "table" (default), "list".'),
'value' => bt('table'),
),
),
'examples' => array(
'bee update-list' => bt('List updates for everything with a new release.'),
'bee update-list --security-only' => bt('List only security updates for everything with a new release.'),
'bee update-list webform tatsu' => bt('List updates for the Webform module and Tatsu theme only'),
),
),
);
}

Expand Down Expand Up @@ -123,9 +156,60 @@ function update_db_bee_callback() {
* @todo Consider a way to download first, check the download has worked before delete.
*
*/
function update_bee_callback($arguments) {
function update_bee_callback($arguments, $options) {
global $_bee_backdrop_root;
require_once $_bee_backdrop_root . '/core/includes/file.inc';
$data = update_bee_get_available_updates($arguments, $options);
if ($data != NULL) {
update_bee_render_table_output($data);
// Prompt to continue.
if (!bee_confirm(bt('Would you like to continue?'), FALSE)) {
return;
}
foreach ($data as $item) {
$folder = download_get_project_destination($item['name'], $item['project_type']);
if (is_dir($folder)) {
bee_delete($folder);
}
download_bee_callback(array('projects' => array($item['name']),), []);
echo '';
}
} else {
bee_message(bt('No Modules or Themes to Update'));
}
}

/**
* Command callback: Show module and theme updates.
*/
function update_list_bee_callback(array $arguments, array $options) {
$data = update_bee_get_available_updates($arguments, $options);
$is_list_format = isset($options['format']) && $options['format'] === 'list';
if ($data == NULL) {
if (!$is_list_format) {
bee_message(bt('No Modules or Themes to Update'));
}
return;
}
if ($is_list_format) {
update_bee_render_list_output($data);
} else {
update_bee_render_table_output($data);
}
}

/**
* Get an array of available project updates.
*
* @param array $arguments
* A list of arguments passed to the command.
* @param array $options
* A list of options passed to the command.
*
* @return array|NULL
* An array of projects with updates available, or NULL if there are none.
*/
function update_bee_get_available_updates(array $arguments, array $options) {
$data = NULL;
if ($available = update_get_available(TRUE)) {
module_load_include('inc', 'update', 'update.compare');
Expand All @@ -135,45 +219,61 @@ function update_bee_callback($arguments) {
foreach ($data as $item) {
if ($item['name'] == 'backdrop') {
unset($data['backdrop']);
continue;
}
if (!empty($arguments['projects']) && !in_array($item['name'], $arguments['projects'])) {
unset($data[$item['name']]);
continue;
}
if (!isset($item['latest_version'])) {
unset($data[$item['name']]);
continue;
}
if ($item['existing_version'] == $item['latest_version']) {
unset($data[$item['name']]);
continue;
}
}
}
if ($data != NULL) {
foreach ($data as $item) {
$rows[] = array(
array('value' => $item['name']),
array('value' => $item['existing_version']),
array('value' => $item['latest_version']),
);
}
bee_render_text(array('value' => bt("These are the items being updated:\n")));
bee_render_table(array(
'rows' => $rows,
'header' => array(
array('value' => bt('Module')),
array('value' => bt('Existing')),
array('value' => bt('Latest')),
),
));
// Prompt to continue.
if (!bee_confirm(bt('Would you like to continue?'), FALSE)) {
return;
}
foreach ($data as $item) {
$folder = download_get_project_destination($item['name'], $item['project_type']);
if (is_dir($folder)) {
bee_delete($folder);
if (isset($options['security-only']) && $item['status'] !== UPDATE_NOT_SECURE) {
unset($data[$item['name']]);
}
download_bee_callback(array('projects' => array($item['name']),), []);
echo '';
}
} else {
bee_message(bt('No Modules or Themes to Update'));
}
return $data;
}

/**
* Render available updates as a table.
*
* @param array $data
* An array of projects with updates available.
*/
function update_bee_render_table_output(array $data) {
foreach ($data as $item) {
$rows[] = array(
array('value' => $item['name']),
array('value' => $item['existing_version']),
array('value' => $item['latest_version']),
);
}
bee_render_text(array('value' => bt("These are the items being updated:\n")));
bee_render_table(array(
'rows' => $rows,
'header' => array(
array('value' => bt('Module')),
array('value' => bt('Existing')),
array('value' => bt('Latest')),
),
));
}

/**
* Render available updates as a list.
*
* @param array $data
* An array of projects with updates available.
*/
function update_bee_render_list_output(array $data) {
foreach ($data as $item) {
bee_render_text(array('value' => $item['name']));
}
}

0 comments on commit 9dba670

Please sign in to comment.