Skip to content

Commit

Permalink
Make it possible to check for several packages that has updated (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm authored Feb 17, 2024
1 parent 189f76c commit 730662b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
90 changes: 61 additions & 29 deletions src/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class Updater
protected $shouldThrowOnUnupdated = true;

/**
* @var string
* @var array
*/
protected $packageToCheck;
protected $packagesToCheck = [];

/**
* @return bool
Expand All @@ -94,12 +94,19 @@ public function setShouldThrowOnUnupdated(bool $shouldThrowOnUnupdated)
$this->shouldThrowOnUnupdated = $shouldThrowOnUnupdated;
}

/**
* @deprecated This method should not be used, and instead we should use the
* one that accepts an array of packages. @see ::setPackagesToCheckHasUpdated
*/
public function setPackageToCheckHasUpdated($package)
{
$this->packageToCheck = $package;
$this->setPackagesToCheckHasUpdated([$package]);
}


public function setPackagesToCheckHasUpdated(array $packages)
{
$this->packagesToCheck = $packages;
}

/**
* @return bool
Expand Down Expand Up @@ -175,7 +182,7 @@ public function __construct($cwd, $package)
{
$this->cwd = $cwd;
$this->package = $package;
$this->packageToCheck = $package;
$this->setPackagesToCheckHasUpdated([$package]);
}

/**
Expand Down Expand Up @@ -264,10 +271,17 @@ public function setDevPackage($devPackage)
$this->devPackage = $devPackage;
}

public function executeRequire($new_version)
protected function getPreUpdateData() : array
{
$pre_update_lock = ComposerLockData::createFromFile($this->cwd . '/composer.lock');
$pre_update_data = $pre_update_lock->getPackageData($this->packageToCheck);
return array_map(function ($package) use ($pre_update_lock) {
return $pre_update_lock->getPackageData($package);
}, $this->packagesToCheck);
}

public function executeRequire($new_version)
{
$pre_update_data = $this->getPreUpdateData();
$commands = $this->getRequireRecipes($new_version);
$exception = null;
$success = false;
Expand Down Expand Up @@ -313,8 +327,7 @@ public function executeRequire($new_version)
*/
public function executeUpdate()
{
$pre_update_lock = ComposerLockData::createFromFile($this->cwd . '/composer.lock');
$pre_update_data = $pre_update_lock->getPackageData($this->packageToCheck);
$pre_update_data = $this->getPreUpdateData();
$commands = $this->getUpdateRecipies();
$exception = null;
$success = false;
Expand Down Expand Up @@ -354,7 +367,7 @@ public function executeUpdate()
}
}

protected function handlePostComposerCommand($pre_update_data, Process $process)
protected function handlePostComposerCommand(array $pre_update_data_array, Process $process)
{
$new_lock_data = @json_decode(@file_get_contents(sprintf('%s/composer.lock', $this->cwd)));
if (!$new_lock_data) {
Expand All @@ -366,27 +379,36 @@ protected function handlePostComposerCommand($pre_update_data, Process $process)
$this->log($process->getErrorOutput());
throw new \Exception($message);
}
$post_update_data = ComposerLockData::createFromString(json_encode($new_lock_data))->getPackageData($this->packageToCheck);
$version_to = $post_update_data->version;
$version_from = $pre_update_data->version;
if (isset($post_update_data->source) && $post_update_data->source->type == 'git' && isset($pre_update_data->source)) {
$version_from = $pre_update_data->source->reference;
$version_to = $post_update_data->source->reference;
$has_updated_at_least_one_package = false;
foreach ($this->packagesToCheck as $package) {
$pre_update_data = $this->getPreUpdataDataForPackageFromArray($pre_update_data_array, $package);
$post_update_data = ComposerLockData::createFromString(json_encode($new_lock_data))->getPackageData($package);
$version_to = $post_update_data->version;
$version_from = $pre_update_data->version;
if (isset($post_update_data->source) && $post_update_data->source->type == 'git' && isset($pre_update_data->source)) {
$version_from = $pre_update_data->source->reference;
$version_to = $post_update_data->source->reference;
}
if ($version_from === $version_to) {
// In theory though, the reference sources can be the same (the same commit), but the
// version is different. In which case it does not really matter much to update, but it
// can be frustrating to get an error. So let's not give an error.
if ($post_update_data->version === $pre_update_data->version) {
$this->log($process->getErrorOutput(), [
'package' => $this->package,
]);
} else {
$has_updated_at_least_one_package = true;
}
} else {
$has_updated_at_least_one_package = true;
}
}
if ($this->shouldThrowOnUnupdated && $version_to === $version_from) {
if ($this->shouldThrowOnUnupdated && !$has_updated_at_least_one_package) {
// Nothing has happened here. Although that can be alright (like we
// have updated some dependencies of this package) this is not what
// this service does, currently, and also the title of the PR would be
// wrong.
// In theory though, the reference sources can be the same (the same commit), but the
// version is different. In which case it does not really matter much to update, but it
// can be frustrating to get an error. So let's not give an error.
if ($post_update_data->version === $pre_update_data->version) {
$this->log($process->getErrorOutput(), [
'package' => $this->package,
]);
throw new NotUpdatedException('The version installed is still the same after trying to update.');
}
// have updated some dependencies of this package), we have at least
// not updated any of the expected dependencies at this point.
throw new NotUpdatedException('The version installed is still the same after trying to update.');
}
// We still want the post update data to be from the actual package though, no matter if we were actually
// checking if a dependency was updated or not.
Expand All @@ -397,6 +419,16 @@ protected function handlePostComposerCommand($pre_update_data, Process $process)
$this->log($process->getErrorOutput());
}

protected function getPreUpdataDataForPackageFromArray(array $pre_update_data_array, $package) : ?\stdClass
{
foreach ($pre_update_data_array as $item) {
if ($item->name === $package) {
return $item;
}
}
return null;
}

/**
* @return \stdClass
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/Integration/UpdateSecondTryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Integration;

use Violinist\ComposerUpdater\Exception\NotUpdatedException;
use Violinist\ComposerUpdater\Tests\Integration\IntegrationBase;

class UpdateSecondTryTest extends IntegrationBase
{
protected $package = 'drupal/captcha';
protected $directory = 'drupal-captcha';
protected $newVersion = '2.0.5';

public function testEndToEnd()
{
if (version_compare(phpversion(), "8.0.0", "<")) {
return;
}
parent::testEndToEnd();
}

protected function createUpdater($directory)
{
$updater = parent::createUpdater($directory);
$updater->setPackagesToCheckHasUpdated(['doctrine/annotations', 'drupal/captcha']);
return $updater;
}
}

0 comments on commit 730662b

Please sign in to comment.