1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
5
|
|
|
*/ |
6
|
|
|
namespace Vaimo\ComposerChangelogs\Commands; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Vaimo\ComposerChangelogs\Exceptions\PackageResolverException; |
11
|
|
|
|
12
|
|
|
use Vaimo\ComposerChangelogs\Factories; |
13
|
|
|
|
14
|
|
|
class VersionCommand extends \Composer\Command\BaseCommand |
15
|
|
|
{ |
16
|
|
|
protected function configure() |
17
|
|
|
{ |
18
|
|
|
$this->setName('changelog:version'); |
19
|
|
|
|
20
|
|
|
$this->setDescription( |
21
|
|
|
'Display version information from changelog. <comment>[default: latest stable]</comment>' |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
$this->addArgument( |
25
|
|
|
'name', |
26
|
|
|
\Symfony\Component\Console\Input\InputArgument::OPTIONAL, |
27
|
|
|
'Targeted package name. Default: root package' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$this->addOption( |
31
|
|
|
'--from-source', |
32
|
|
|
null, |
33
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
34
|
|
|
'Extract configuration from vendor package instead of using global installation data' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$this->addOption( |
38
|
|
|
'--format', |
39
|
|
|
null, |
40
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
41
|
|
|
'Format of the output (regex)' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->addOption( |
45
|
|
|
'--segments', |
46
|
|
|
null, |
47
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
48
|
|
|
'Number of segments of the version to return. <comment>[default: all segments]</comment>' |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->addOption( |
52
|
|
|
'--upcoming', |
53
|
|
|
null, |
54
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
55
|
|
|
'Show upcoming version (if there is one)' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->addOption( |
59
|
|
|
'--branch', |
60
|
|
|
null, |
61
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
62
|
|
|
'Match release branch (if provided in changelog item)' |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$this->addOption( |
66
|
|
|
'--tip', |
67
|
|
|
null, |
68
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
69
|
|
|
'Show LATEST version (might be latest release, might be upcoming)' |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$packageName = $input->getArgument('name'); |
76
|
|
|
$fromSource = $input->getOption('from-source'); |
77
|
|
|
$format = $input->getOption('format'); |
78
|
|
|
$branch = $input->getOption('branch'); |
79
|
|
|
$segmentsCount = $input->getOption('segments'); |
80
|
|
|
|
81
|
|
|
$showUpcoming = $input->getOption('upcoming'); |
82
|
|
|
$showTip = $input->getOption('tip'); |
83
|
|
|
|
84
|
|
|
$composerRuntime = $this->getComposer(); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$package = $this->resolvePackage(is_string($packageName) ? $packageName : ''); |
88
|
|
|
} catch (PackageResolverException $exception) { |
89
|
|
|
$this->printException($exception, $output); |
90
|
|
|
|
91
|
|
|
return 1; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$chLogLoaderFactory = new Factories\Changelog\LoaderFactory($composerRuntime); |
95
|
|
|
$chLogLoader = $chLogLoaderFactory->create($fromSource); |
96
|
|
|
|
97
|
|
|
$validator = new \Vaimo\ComposerChangelogs\Validators\ChangelogValidator($chLogLoader, array( |
98
|
|
|
'failure' => '<error>%s</error>', |
99
|
|
|
'success' => '<info>%s</info>' |
100
|
|
|
)); |
101
|
|
|
|
102
|
|
|
$result = $validator->validateForPackage($package, $output->getVerbosity()); |
103
|
|
|
|
104
|
|
|
if (!$result()) { |
105
|
|
|
return 1; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$changelog = $chLogLoader->load($package); |
109
|
|
|
|
110
|
|
|
$releaseResolver = new \Vaimo\ComposerChangelogs\Resolvers\ChangelogReleaseResolver(); |
111
|
|
|
|
112
|
|
|
$version = key($changelog); |
113
|
|
|
|
114
|
|
|
if (!$showTip) { |
115
|
|
|
$version = $releaseResolver->resolveLatestVersionedRelease($changelog, $branch); |
116
|
|
|
|
117
|
|
|
if ($showUpcoming) { |
118
|
|
|
$version = $releaseResolver->resolveUpcomingRelease($changelog, $branch); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!$version) { |
123
|
|
|
return 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$versionResolver = new \Vaimo\ComposerChangelogs\Resolvers\VersionResolver(); |
127
|
|
|
|
128
|
|
|
$version = $versionResolver->resolveValidVersion($version); |
129
|
|
|
|
130
|
|
|
if ($format == 'regex') { |
131
|
|
|
$version = preg_quote($version); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($segmentsCount) { |
135
|
|
|
$version = implode('.', array_slice(explode('.', $version), 0, $segmentsCount)); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$output->writeln($version); |
139
|
|
|
|
140
|
|
|
return 0; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function printException($exception, OutputInterface $output) |
144
|
|
|
{ |
145
|
|
|
$errorOutputGenerator = new \Vaimo\ComposerChangelogs\Console\OutputGenerator(); |
146
|
|
|
|
147
|
|
|
\array_map( |
148
|
|
|
array($output, 'writeln'), |
149
|
|
|
$errorOutputGenerator->generateForResolverException($exception) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $packageName |
155
|
|
|
* @return \Composer\Package\PackageInterface |
156
|
|
|
* @throws PackageResolverException |
157
|
|
|
*/ |
158
|
|
|
private function resolvePackage($packageName) |
159
|
|
|
{ |
160
|
|
|
$composerRuntime = $this->getComposer(); |
161
|
|
|
|
162
|
|
|
if (!$packageName) { |
163
|
|
|
$packageName = $composerRuntime->getPackage()->getName(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$packageRepoFactory = new Factories\PackageRepositoryFactory($composerRuntime); |
167
|
|
|
|
168
|
|
|
$packageRepository = $packageRepoFactory->create(); |
169
|
|
|
|
170
|
|
|
return $packageRepository->getByName($packageName); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|