Skip to content

Commit

Permalink
Switch updater to new update method
Browse files Browse the repository at this point in the history
  • Loading branch information
pingevt committed Oct 15, 2023
1 parent b6f79e6 commit 7fbd69e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 103 deletions.
115 changes: 14 additions & 101 deletions bc_api_base.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,121 +5,34 @@
* Module File.
*/

use Bluecadet\DrupalPackageManager\Checker;
use Drupal\update\UpdateManagerInterface;
use Melbahja\Semver\Semver;


/**
* Implements hook_update_status_alter().
*/
function bc_api_base_update_status_alter(&$projects) {
$moduleHandler = \Drupal::service('module_handler');
$modules = [
$modules['bluecadet'] = [
'bc_api_base',
];

foreach ($modules as $module_name) {
try {
if ($moduleHandler->moduleExists($module_name)) {

// Init vars.
$also = [];
$releases = [];
$lates_version = "";
$recomended = "";
$package_name = 'bluecadet/' . $module_name;
$packagist_base = "https://packagist.org/packages/bluecadet/" . $module_name;
$url = "https://repo.packagist.org/p2/bluecadet/$module_name.json";

// Initiate curl and get info from Packagist.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);

$data = json_decode($result, TRUE);

try {

$exisiting_version = Semver::parse($projects[$module_name]['existing_version']);
$packages = $data['packages'][$package_name];
// Sort pakcages from packagist lowest to highest.
uasort($packages, 'bc_api_base__order_packages');

$projects[$module_name]['status'] = UpdateManagerInterface::CURRENT;
$filtered_projects = [];

foreach ($packages as $package_data) {
try {
$release_version = Semver::parse($package_data['version']);
if ($exisiting_version->compare($release_version, "<")) {

// Create release data.
$release_data = [
'name' => $projects[$module_name]['name'],
'version' => $package_data['version'],
'tag' => $package_data['version'],
'status' => "published",
'release_link' => $packagist_base . "#" . $package_data['version'],
'download_link' => $packagist_base . "#" . $package_data['version'],
'date' => strtotime($package_data['time']),
'files' => "",
'terms' => [],
'security' => "",
];

$releases[$package_data['version']] = $release_data;

// Is is also?
if ($exisiting_version->getMajor() < $release_version->getMajor()) {
$projects[$module_name]['status'] = UpdateManagerInterface::NOT_CURRENT;
$also[$release_version->getMajor() . "." . $release_version->getMinor()] = $package_data['version'];
}
elseif ($exisiting_version->getMajor() == $release_version->getMajor() && $exisiting_version->getMinor() < $release_version->getMinor()) {
$also[$release_version->getMajor() . "." . $release_version->getMinor()] = $package_data['version'];
}

// Is it latest?
// Is it recommended?
if ($exisiting_version->getMajor() == $release_version->getMajor()) {
$projects[$module_name]['latest_version'] = $package_data['version'];
$projects[$module_name]['recommended'] = $package_data['version'];

if ($exisiting_version != $release_version) {
$projects[$module_name]['status'] = UpdateManagerInterface::NOT_CURRENT;
}
}
}
}
catch (Exception $e) {
// phpcs:ignore
// \Drupal::messenger()->addWarning(t('Caught exception while checking release: %error', ['%error' => $e->getMessage()]));
\Drupal::logger('bc_api_base')->warning('Caught exception while checking release: ' . $e->getMessage());
}
}
foreach ($modules as $user => $user_data) {
$filtered_projects += array_filter($projects, function($v) use ($user_data) {
return in_array($v, $user_data);
}, ARRAY_FILTER_USE_KEY);
}

// Update attributes.
$projects[$module_name]['link'] = $packagist_base;
$projects[$module_name]['title'] = $projects[$module_name]['info']['name'];
$checker = new Checker($modules, $filtered_projects);

if (!empty($releases)) {
$projects[$module_name]['releases'] = $releases;
}
if (!empty($also)) {
$projects[$module_name]['also'] = $also;
}
}
catch (\Exception $e) {
// phpcs:ignore
// \Drupal::messenger()->addError(t('Caught exception while setting release data: %error', ['%error' => $e->getMessage()]));
\Drupal::logger('bc_api_base')->error('Caught exception while setting release data: ' . $e->getMessage());
}
foreach ($modules as $user => $user_data) {
foreach ($user_data as $module_name) {
if (in_array($module_name, array_keys($projects))) {
$projects[$module_name] = $checker->updateDrupalModulePackage($projects[$module_name], $user, $module_name);
}
}
catch (\Exception $e) {
// phpcs:ignore
// \Drupal::messenger()->addError(t('Caught exception while checking release data: %error', ['%error' => $e->getMessage()]));
\Drupal::logger('bc_api_base')->error('Caught exception while checking release data: ' . $e->getMessage());
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
"minimum-stability": "dev",
"require": {
"php": "^7.4||^8.0",
"bluecadet/bc_drupal_package_manager": "^1.0",
"composer/installers": "^1.9||^2.0",
"drupal/core": "^9||^10",
"drupal/key_auth": "^1.0||^2.0",
"oomphinc/composer-installers-extender": "^2.0",
"melbahja/semver": "^1.0.0"
"oomphinc/composer-installers-extender": "^2.0"
}
}

0 comments on commit 7fbd69e

Please sign in to comment.