Skip to content

Commit 00b6411

Browse files
authored
Merge pull request #23 from artemeon/feat/improve-update
Feat/improve update
2 parents d629335 + 586e111 commit 00b6411

File tree

6 files changed

+50
-54
lines changed

6 files changed

+50
-54
lines changed

mantis2github

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33

4+
use Artemeon\M2G\Command\CheckUpdateCommand;
45
use Artemeon\M2G\Command\ConfigurationCommand;
56
use Artemeon\M2G\Command\CreateGithubIssueFromMantisIssue;
67
use Artemeon\M2G\Command\ReadGithubIssueCommand;
@@ -29,6 +30,7 @@ use Symfony\Component\Console\Application;
2930
$app->add(new ReadMantisIssueCommand($mantisConnector));
3031
$app->add(new ReadGithubIssueCommand($githubConnector));
3132
$app->add(new CreateGithubIssueFromMantisIssue($mantisConnector, $githubConnector));
33+
$app->add(new CheckUpdateCommand());
3234
$app->run();
3335
}
3436

src/Command/CheckUpdateCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Artemeon\M2G\Command;
4+
5+
use Artemeon\M2G\Helper\VersionHelper;
6+
7+
use function Termwind\{render};
8+
9+
class CheckUpdateCommand extends Command
10+
{
11+
protected function configure()
12+
{
13+
$this->setName('check_update')
14+
->setDescription('Checks whether a new version is available');
15+
}
16+
17+
protected function handle(): int
18+
{
19+
$currentVersion = VersionHelper::fetchVersion();
20+
$latestVersion = VersionHelper::latestVersion();
21+
$updateAvailable = VersionHelper::checkForUpdates();
22+
$name = VersionHelper::getPackageName();
23+
24+
if ($updateAvailable) {
25+
render(<<<HTML
26+
<table>
27+
<thead>
28+
<tr>
29+
<th>Update available! $currentVersion -> $latestVersion</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
<tr>
34+
<td><br>Please run:<br><br><code class="font-bold">composer global update $name</code><br></td>
35+
</tr>
36+
</tbody>
37+
</table>
38+
HTML);
39+
}
40+
41+
return 0;
42+
}
43+
}

src/Command/Command.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Artemeon\M2G\Command;
44

55
use Artemeon\M2G\Config\ConfigReader;
6-
use Artemeon\M2G\Helper\VersionHelper;
76
use Exception;
87
use Symfony\Component\Console\Helper\QuestionHelper;
98
use Symfony\Component\Console\Input\InputInterface;
@@ -25,31 +24,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2524
$this->input = $input;
2625
$this->output = $output;
2726

28-
$commandOutput = $this->handle();
29-
30-
$currentVersion = VersionHelper::fetchVersion();
31-
$latestVersion = VersionHelper::latestVersion();
32-
$updateAvailable = VersionHelper::checkForUpdates();
33-
$name = VersionHelper::getPackageName();
34-
35-
if ($updateAvailable) {
36-
render(<<<HTML
37-
<table>
38-
<thead>
39-
<tr>
40-
<th>Update available! $currentVersion -> $latestVersion</th>
41-
</tr>
42-
</thead>
43-
<tbody>
44-
<tr>
45-
<td><br>Please run:<br><br><code class="font-bold">composer global update $name</code><br></td>
46-
</tr>
47-
</tbody>
48-
</table>
49-
HTML);
50-
}
51-
52-
return $commandOutput;
27+
return $this->handle();
5328
}
5429

5530
protected function arguments(): array

src/Helper/VersionHelper.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Artemeon\M2G\Helper;
66

77
use ahinkle\PackagistLatestVersion\PackagistLatestVersion;
8+
use Composer\InstalledVersions;
89
use Exception;
910

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

1920
public static function fetchVersion(): string
2021
{
21-
$name = self::getPackageName();
22-
23-
$version = null;
24-
25-
foreach (
26-
[
27-
__DIR__ . '/../../../../composer/installed.php',
28-
__DIR__ . '/../../../vendor/composer/installed.php',
29-
__DIR__ . '/../../vendor/composer/installed.php'
30-
] as $file
31-
) {
32-
if (file_exists($file)) {
33-
$installed = require $file;
34-
$version = $installed['versions'][$name]['pretty_version'] ?? null;
35-
36-
break;
37-
}
38-
}
39-
40-
unset($file, $installed);
41-
42-
return $version;
22+
return InstalledVersions::getPrettyVersion('artemeon/mantis2github');
4323
}
4424

4525
/**

src/Service/GithubConnector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public function __construct(private ?ConfigValues $config)
2222
'Accept' => 'application/vnd.github.v3+json',
2323
'Authorization' => 'token ' . $this->config->getGithubToken(),
2424
],
25-
'defaults' => [
26-
'verify' => false,
27-
],
25+
'verify' => false,
2826
'base_uri' => 'https://api.github.com/repos/' . $config->getGithubRepo() . '/',
2927
]);
3028
}

src/Service/MantisConnector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public function __construct(private ?ConfigValues $config)
2222
'Authorization' => $this->config->getMantisToken(),
2323
'Content-Type' => 'application/json',
2424
],
25-
'defaults' => [
26-
'verify' => false,
27-
],
25+
'verify' => false,
2826
'base_uri' => rtrim($this->config->getMantisUrl(), '/') . '/api/rest/issues/',
2927
]);
3028
}

0 commit comments

Comments
 (0)