-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move VersionChecker from helfi_api_base to helfi_drupal_tools
- Loading branch information
Showing
8 changed files
with
298 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalTools\Package; | ||
|
||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Process for `composer outdated` command. | ||
*/ | ||
class ComposerOutdatedProcess { | ||
|
||
/** | ||
* Runs `composer outdated`. | ||
* | ||
* @return array | ||
* Decoded JSON from `composer outdated`. | ||
* | ||
* @throws \Symfony\Component\Process\Exception\ProcessFailedException | ||
*/ | ||
public function run(string $workingDir): array { | ||
$process = new Process([ | ||
'composer', 'outdated', '--direct', '--format=json', '--working-dir=' . $workingDir, | ||
]); | ||
$process->mustRun(); | ||
return json_decode($process->getOutput(), TRUE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalTools\Package\Exception; | ||
|
||
/** | ||
* Indicated failure to check versions. | ||
*/ | ||
class VersionCheckException extends \RuntimeException { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalTools\Package; | ||
|
||
/** | ||
* A value object to store package version data. | ||
*/ | ||
final class Version { | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param string $name | ||
* The package name. | ||
* @param string $latestVersion | ||
* The latest version. | ||
* @param string $version | ||
* The current version. | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public string $latestVersion, | ||
public string $version, | ||
) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DrupalTools\Package; | ||
|
||
use DrupalTools\Package\Exception\VersionCheckException; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
/** | ||
* Provides a package version checker. | ||
*/ | ||
class VersionChecker { | ||
|
||
/** | ||
* Constructs a new instance. | ||
*/ | ||
public function __construct( | ||
private readonly ComposerOutdatedProcess $process, | ||
) { | ||
} | ||
|
||
/** | ||
* Gets outdated package versions. | ||
* | ||
* @param string $composerLockFile | ||
* Path to composer lock file. | ||
* | ||
* @return \DrupalTools\Package\Version[] | ||
* Outdated packages. | ||
* | ||
* @throws \DrupalTools\Package\Exception\VersionCheckException | ||
*/ | ||
public function getOutdated(string $composerLockFile) : array { | ||
$packages = $this->getPackages($composerLockFile); | ||
$versions = []; | ||
|
||
foreach ($packages as $packageName => $package) { | ||
$versions[] = new Version($packageName, $package['latest'], $package['version']); | ||
} | ||
|
||
return $versions; | ||
} | ||
|
||
/** | ||
* Get outdated packages. | ||
* | ||
* @throws \DrupalTools\Package\Exception\VersionCheckException | ||
*/ | ||
private function getPackages(string $composerLockFile): array { | ||
if (!$composerLockFile = realpath($composerLockFile)) { | ||
throw new VersionCheckException('Composer lock file not found'); | ||
} | ||
|
||
$workingDir = dirname($composerLockFile); | ||
try { | ||
$packages = $this->process->run($workingDir); | ||
$packages = $packages['installed'] ?? []; | ||
} | ||
catch (ProcessFailedException) { | ||
throw new VersionCheckException("Composer process failed"); | ||
} | ||
|
||
$result = []; | ||
|
||
// Key with package name. | ||
foreach ($packages as $package) { | ||
$result[$package['name']] = $package; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.