Skip to content

Commit

Permalink
Update helper
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Jun 15, 2022
1 parent 3872920 commit 43202b9
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 25 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"symfony/console": "^5.0",
"symfony/yaml": "^5.0",
"nunomaduro/termwind": "^1.5",
"composer-runtime-api": "^2.2.2"
"composer-runtime-api": "^2.2.2",
"ahinkle/packagist-latest-version": "^2.0"
}
}
129 changes: 127 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 2 additions & 21 deletions mantis2github
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Artemeon\M2G\Command\CreateGithubIssueFromMantisIssue;
use Artemeon\M2G\Command\ReadGithubIssueCommand;
use Artemeon\M2G\Command\ReadMantisIssueCommand;
use Artemeon\M2G\Config\ConfigReader;
use Artemeon\M2G\Helper\VersionHelper;
use Artemeon\M2G\Service\GithubConnector;
use Artemeon\M2G\Service\MantisConnector;
use Symfony\Component\Console\Application;
Expand Down Expand Up @@ -65,26 +66,6 @@ use Symfony\Component\Console\Application;
$name = $packageJson['name'] ?? null;
$this->name = explode('/', $name)[1] ?? null;

foreach (
[
__DIR__ . '/../../composer/installed.php',
__DIR__ . '/../vendor/composer/installed.php',
__DIR__ . '/vendor/composer/installed.php'
] as $file
) {
if (file_exists($file)) {
$installed = require $file;
$this->version = $installed['versions'][$name]['pretty_version'] ?? null;

break;
}
}

unset($file, $installed);
$this->version = VersionHelper::fetchVersion();
}
})->main();





31 changes: 30 additions & 1 deletion src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Artemeon\M2G\Command;

use Artemeon\M2G\Config\ConfigReader;
use Artemeon\M2G\Helper\VersionHelper;
use Exception;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -15,12 +17,39 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
protected InputInterface $input;
protected OutputInterface $output;

/**
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;

return $this->handle();
$commandOutput = $this->handle();

$currentVersion = VersionHelper::fetchVersion();
$latestVersion = VersionHelper::latestVersion();
$updateAvailable = VersionHelper::checkForUpdates();
$name = VersionHelper::getPackageName();

if (!$updateAvailable) {
render(<<<HTML
<table>
<thead>
<tr>
<th>Update available! $currentVersion -> $latestVersion</th>
</tr>
</thead>
<tbody>
<tr>
<td><br>Please run:<br><br><code class="font-bold">composer global update $name</code><br></td>
</tr>
</tbody>
</table>
HTML);
}

return $commandOutput;
}

protected function arguments(): array
Expand Down
69 changes: 69 additions & 0 deletions src/Helper/VersionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Artemeon\M2G\Helper;

use ahinkle\PackagistLatestVersion\PackagistLatestVersion;
use Exception;

class VersionHelper
{
public static function getPackageName(): ?string
{
$packageJson = json_decode(file_get_contents(__DIR__ . '/../../composer.json'), true);

return $packageJson['name'] ?? null;
}

public static function fetchVersion(): string
{
$name = self::getPackageName();

$version = null;

foreach (
[
__DIR__ . '/../../../../composer/installed.php',
__DIR__ . '/../../../vendor/composer/installed.php',
__DIR__ . '/../../vendor/composer/installed.php'
] as $file
) {
if (file_exists($file)) {
$installed = require $file;
$version = $installed['versions'][$name]['pretty_version'] ?? null;

break;
}
}

unset($file, $installed);

return $version;
}

/**
* @throws Exception
*/
public static function latestVersion(): ?string
{
$packagist = new PackagistLatestVersion();

return $packagist->getLatestRelease(self::getPackageName())['version'] ?? null;
}

/**
* @throws Exception
*/
public static function checkForUpdates(): bool
{
$currentVersion = self::fetchVersion();
$latestVersion = self::latestVersion();

if (!preg_match("/^[0-9]+\.[0-9]+\.[0-9]+$/", $currentVersion)) {
return false;
}

return version_compare($currentVersion, $latestVersion, '<');
}
}

0 comments on commit 43202b9

Please sign in to comment.