1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement; |
6
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Exceptions\ApplicationException; |
8
|
|
|
use Vasoft\VersionIncrement\Exceptions\InvalidConfigFileException; |
9
|
|
|
|
10
|
|
|
class Application |
11
|
|
|
{ |
12
|
|
|
private bool $debug = false; |
13
|
|
|
private bool $showList = false; |
14
|
|
|
private bool $modeHelp = false; |
15
|
|
|
private string $changeType = ''; |
16
|
|
|
|
17
|
10 |
|
public function run(array $argv): int |
18
|
|
|
{ |
19
|
10 |
|
$exitCode = 0; |
20
|
|
|
|
21
|
|
|
try { |
22
|
10 |
|
$this->checkParams($argv); |
23
|
10 |
|
if ($this->modeHelp) { |
24
|
1 |
|
$this->displayHelp(); |
25
|
|
|
|
26
|
1 |
|
return 0; |
27
|
|
|
} |
28
|
|
|
|
29
|
9 |
|
$composerJsonPath = getenv('COMPOSER') ?: getcwd(); |
30
|
8 |
|
$configFile = $composerJsonPath . '/.vs-version-increment.php'; |
31
|
8 |
|
$config = $this->loadConfig($configFile); |
32
|
7 |
|
if ($this->showList) { |
33
|
4 |
|
$this->displayList($config); |
34
|
|
|
|
35
|
4 |
|
return 0; |
36
|
|
|
} |
37
|
3 |
|
$versionUpdater = new SemanticVersionUpdater($composerJsonPath, $config, $this->changeType); |
38
|
3 |
|
$versionUpdater |
39
|
3 |
|
->setDebug($this->debug) |
40
|
3 |
|
->updateVersion(); |
41
|
2 |
|
} catch (ApplicationException $e) { |
42
|
1 |
|
fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL); |
43
|
1 |
|
$exitCode = $e->getCode(); |
44
|
1 |
|
} catch (\Throwable $e) { |
45
|
1 |
|
fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL); |
46
|
1 |
|
$exitCode = ApplicationException::CODE; |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
return $exitCode; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
private function displayList(Config $config): void |
53
|
|
|
{ |
54
|
4 |
|
$output = 'Available sections:' . PHP_EOL; |
55
|
4 |
|
$output .= ' ' . implode(PHP_EOL . ' ', $config->getSectionDescriptions()) . PHP_EOL; |
56
|
4 |
|
$scopes = $config->getScopes(); |
57
|
4 |
|
if (!empty($scopes)) { |
58
|
1 |
|
$output .= PHP_EOL . 'Available scopes:' . PHP_EOL; |
59
|
1 |
|
foreach ($scopes as $key => $title) { |
60
|
1 |
|
$output .= ' ' . $key . ' - ' . $title . PHP_EOL; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
fwrite(STDOUT, $output); |
65
|
|
|
} |
66
|
|
|
|
67
|
10 |
|
private function checkParams(array $argv): void |
68
|
|
|
{ |
69
|
10 |
|
unset($argv[0]); |
70
|
|
|
|
71
|
10 |
|
$this->showList = false; |
72
|
10 |
|
$this->debug = false; |
73
|
10 |
|
$this->changeType = ''; |
74
|
|
|
|
75
|
10 |
|
foreach ($argv as $arg) { |
76
|
|
|
switch ($arg) { |
77
|
10 |
|
case '--help': |
78
|
1 |
|
$this->modeHelp = true; |
79
|
|
|
|
80
|
1 |
|
return; |
81
|
|
|
|
82
|
9 |
|
case '--list': |
83
|
6 |
|
$this->showList = true; |
84
|
|
|
|
85
|
6 |
|
return; |
86
|
|
|
|
87
|
3 |
|
case '--debug': |
88
|
1 |
|
$this->debug = true; |
89
|
1 |
|
break; |
90
|
|
|
|
91
|
|
|
default: |
92
|
2 |
|
if (empty($this->changeType)) { |
93
|
2 |
|
$this->changeType = $arg; |
94
|
|
|
} |
95
|
2 |
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @throws InvalidConfigFileException |
102
|
|
|
*/ |
103
|
8 |
|
private function loadConfig(string $configFile): Config |
104
|
|
|
{ |
105
|
8 |
|
if (file_exists($configFile)) { |
106
|
6 |
|
$config = include $configFile; |
107
|
6 |
|
if (!$config instanceof Config) { |
108
|
6 |
|
throw new InvalidConfigFileException(); |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
2 |
|
$config = new Config(); |
112
|
|
|
} |
113
|
|
|
|
114
|
7 |
|
return $config; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function displayHelp(): void |
118
|
|
|
{ |
119
|
|
|
echo <<<'HELP' |
120
|
|
|
Vasoft Semantic Version Increment |
121
|
|
|
run vs-version-increment [--debug] [--list] [--help] [major|minor|patch] |
122
|
|
|
Usage: |
123
|
|
|
--debug Enable debug mode |
124
|
|
|
--help Display this help message |
125
|
|
|
--list Show list of sections |
126
|
|
|
major|minor|patch Increment type |
127
|
|
|
|
128
|
|
|
HELP; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|