-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVersionCommand.php
More file actions
114 lines (101 loc) · 3.22 KB
/
VersionCommand.php
File metadata and controls
114 lines (101 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace OpenForgeProject\MageForge\Console\Command\System;
use GuzzleHttp\Client;
use Magento\Framework\Console\Cli;
use Magento\Framework\Filesystem\Driver\File;
use OpenForgeProject\MageForge\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command for displaying version information
*/
class VersionCommand extends AbstractCommand
{
private const API_URL = 'https://api.github.com/repos/openforgeproject/mageforge/releases/latest';
private const UNKNOWN_VERSION = 'Unknown';
/**
* @param File $fileDriver
*/
public function __construct(
private readonly File $fileDriver,
) {
parent::__construct();
}
/**
* Configure command.
*
* @return void
*/
protected function configure(): void
{
$this
->setName($this->getCommandName('system', 'version'))
->setDescription('Displays the module version and the latest version')
->setAliases(['system:version']);
}
/**
* Execute command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function executeCommand(InputInterface $input, OutputInterface $output): int
{
$moduleVersion = $this->getModuleVersion();
$latestVersion = $this->getLatestVersion();
$this->io->title('MageForge Version Information');
$this->io->section('Versions');
$this->io->listing([
"Module Version: $moduleVersion",
"Latest Version: $latestVersion",
]);
return Cli::RETURN_SUCCESS;
}
/**
* Get the current module version
*
* @return string
*/
private function getModuleVersion(): string
{
try {
$composerJson = $this->fileDriver->fileGetContents(__DIR__ . '/../../../../composer.json');
$composerData = json_decode($composerJson, true);
if (is_array($composerData) && isset($composerData['version']) && is_string($composerData['version'])) {
return $composerData['version'];
}
return self::UNKNOWN_VERSION;
} catch (\Exception $e) {
return self::UNKNOWN_VERSION;
}
}
/**
* Get the latest version from GitHub
*
* @return string
*/
private function getLatestVersion(): string
{
try {
$client = new Client();
$response = $client->get(self::API_URL, [
'headers' => [
'User-Agent' => 'MageForge-Version-Check',
],
]);
if ($response->getStatusCode() === 200) {
$data = json_decode($response->getBody()->getContents(), true);
if (is_array($data) && isset($data['tag_name']) && is_string($data['tag_name'])) {
return $data['tag_name'];
}
}
} catch (\Exception $e) {
if ($this->io->isVerbose()) {
$this->io->warning('Failed to fetch latest version: ' . $e->getMessage());
}
}
return self::UNKNOWN_VERSION;
}
}