Skip to content

Commit

Permalink
Merge pull request #23 from artemeon/feat/improve-update
Browse files Browse the repository at this point in the history
Feat/improve update
  • Loading branch information
marcreichel authored Nov 30, 2022
2 parents d629335 + 586e111 commit 00b6411
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 54 deletions.
2 changes: 2 additions & 0 deletions mantis2github
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php

use Artemeon\M2G\Command\CheckUpdateCommand;
use Artemeon\M2G\Command\ConfigurationCommand;
use Artemeon\M2G\Command\CreateGithubIssueFromMantisIssue;
use Artemeon\M2G\Command\ReadGithubIssueCommand;
Expand Down Expand Up @@ -29,6 +30,7 @@ use Symfony\Component\Console\Application;
$app->add(new ReadMantisIssueCommand($mantisConnector));
$app->add(new ReadGithubIssueCommand($githubConnector));
$app->add(new CreateGithubIssueFromMantisIssue($mantisConnector, $githubConnector));
$app->add(new CheckUpdateCommand());
$app->run();
}

Expand Down
43 changes: 43 additions & 0 deletions src/Command/CheckUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Artemeon\M2G\Command;

use Artemeon\M2G\Helper\VersionHelper;

use function Termwind\{render};

class CheckUpdateCommand extends Command
{
protected function configure()
{
$this->setName('check_update')
->setDescription('Checks whether a new version is available');
}

protected function handle(): int
{
$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 0;
}
}
27 changes: 1 addition & 26 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand All @@ -25,31 +24,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->input = $input;
$this->output = $output;

$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;
return $this->handle();
}

protected function arguments(): array
Expand Down
24 changes: 2 additions & 22 deletions src/Helper/VersionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Artemeon\M2G\Helper;

use ahinkle\PackagistLatestVersion\PackagistLatestVersion;
use Composer\InstalledVersions;
use Exception;

class VersionHelper
Expand All @@ -18,28 +19,7 @@ public static function getPackageName(): ?string

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;
return InstalledVersions::getPrettyVersion('artemeon/mantis2github');
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Service/GithubConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public function __construct(private ?ConfigValues $config)
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'token ' . $this->config->getGithubToken(),
],
'defaults' => [
'verify' => false,
],
'verify' => false,
'base_uri' => 'https://api.github.com/repos/' . $config->getGithubRepo() . '/',
]);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Service/MantisConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public function __construct(private ?ConfigValues $config)
'Authorization' => $this->config->getMantisToken(),
'Content-Type' => 'application/json',
],
'defaults' => [
'verify' => false,
],
'verify' => false,
'base_uri' => rtrim($this->config->getMantisUrl(), '/') . '/api/rest/issues/',
]);
}
Expand Down

0 comments on commit 00b6411

Please sign in to comment.