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 | |||||
11 | use Composer\Package\PackageInterface as Package; |
||||
12 | |||||
13 | use Vaimo\ComposerChangelogs\Resolvers; |
||||
14 | use Vaimo\ComposerChangelogs\Factories; |
||||
15 | use Vaimo\ComposerChangelogs\Composer\Context as ComposerContext; |
||||
16 | |||||
17 | /** |
||||
18 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
||||
19 | */ |
||||
20 | class InfoCommand extends \Composer\Command\BaseCommand |
||||
21 | { |
||||
22 | protected function configure() |
||||
23 | { |
||||
24 | $this->setName('changelog:info'); |
||||
25 | |||||
26 | $this->setDescription('Generates summary of a given release'); |
||||
27 | |||||
28 | $this->addArgument( |
||||
29 | 'name', |
||||
30 | \Symfony\Component\Console\Input\InputArgument::OPTIONAL, |
||||
31 | 'Targeted package name. Default: root package' |
||||
32 | ); |
||||
33 | |||||
34 | $this->addOption( |
||||
35 | '--from-source', |
||||
36 | null, |
||||
37 | \Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
||||
38 | 'Extract configuration from vendor package instead of using global installation data' |
||||
39 | ); |
||||
40 | |||||
41 | $this->addOption( |
||||
42 | '--brief', |
||||
43 | null, |
||||
44 | \Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
||||
45 | 'Output overview on the targeted release' |
||||
46 | ); |
||||
47 | |||||
48 | $this->addOption( |
||||
49 | '--release', |
||||
50 | null, |
||||
51 | \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
||||
52 | 'Target specific release in the changelog. Default: latest valid versioned release' |
||||
53 | ); |
||||
54 | |||||
55 | $this->addOption( |
||||
56 | '--branch', |
||||
57 | null, |
||||
58 | \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
||||
59 | 'Match release branch (if provided in changelog item)' |
||||
60 | ); |
||||
61 | |||||
62 | $pluginConfig = new \Vaimo\ComposerChangelogs\Composer\Plugin\Config(); |
||||
63 | |||||
64 | $this->addOption( |
||||
65 | '--format', |
||||
66 | null, |
||||
67 | \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL, |
||||
68 | sprintf('Format of the output (%s)', implode(', ', $pluginConfig->getAvailableFormats())), |
||||
69 | 'json' |
||||
70 | ); |
||||
71 | |||||
72 | $this->addOption( |
||||
73 | '--upcoming', |
||||
74 | null, |
||||
75 | \Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
||||
76 | 'Show upcoming version (if there is one)' |
||||
77 | ); |
||||
78 | } |
||||
79 | |||||
80 | protected function execute(InputInterface $input, OutputInterface $output) |
||||
81 | { |
||||
82 | $packageName = $input->getArgument('name'); |
||||
83 | |||||
84 | $fromSource = $input->getOption('from-source'); |
||||
85 | $briefMode = $input->getOption('brief'); |
||||
86 | $version = $input->getOption('release'); |
||||
87 | $format = $input->getOption('format'); |
||||
88 | $branch = $input->getOption('branch'); |
||||
89 | $showUpcoming = $input->getOption('upcoming'); |
||||
90 | |||||
91 | $composerCtxFactory = new \Vaimo\ComposerChangelogs\Factories\ComposerContextFactory( |
||||
92 | $this->getComposer() |
||||
0 ignored issues
–
show
|
|||||
93 | ); |
||||
94 | |||||
95 | $composerCtx = $composerCtxFactory->create(); |
||||
96 | |||||
97 | $chLogRepoFactory = new Factories\ChangelogRepositoryFactory($composerCtx, $output); |
||||
98 | $chLogRepo = $chLogRepoFactory->create($fromSource); |
||||
99 | |||||
100 | $changelog = $chLogRepo->getByPackageName( |
||||
101 | $packageName, |
||||
102 | $output->getVerbosity() |
||||
103 | ); |
||||
104 | |||||
105 | if ($changelog === null) { |
||||
106 | return 1; |
||||
107 | } |
||||
108 | |||||
109 | $releases = $changelog->getReleases(); |
||||
110 | |||||
111 | if (!$version) { |
||||
112 | $version = $this->resolveVersion($releases, $branch, $showUpcoming); |
||||
113 | } |
||||
114 | |||||
115 | if (!$version || !isset($releases[$version])) { |
||||
116 | return 0; |
||||
117 | } |
||||
118 | |||||
119 | $groups = $this->resolveOutputGroups( |
||||
120 | $releases[$version], |
||||
121 | $briefMode |
||||
122 | ); |
||||
123 | |||||
124 | try { |
||||
125 | $result = $this->generateOutput( |
||||
126 | $composerCtx, |
||||
127 | $changelog->getOwner(), |
||||
128 | $groups, |
||||
129 | $format, |
||||
130 | $fromSource |
||||
131 | ); |
||||
132 | } catch (\Exception $exception) { |
||||
133 | $output->writeln( |
||||
134 | sprintf('<error>%s</error>', $exception->getMessage()) |
||||
135 | ); |
||||
136 | |||||
137 | return 1; |
||||
138 | } |
||||
139 | |||||
140 | $output->writeln($result); |
||||
141 | |||||
142 | return 0; |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||||
147 | * |
||||
148 | * @param array $details |
||||
149 | * @param bool $briefMode |
||||
150 | * @return array |
||||
151 | */ |
||||
152 | private function resolveOutputGroups(array $details, $briefMode = false) |
||||
153 | { |
||||
154 | $detailsResolver = new \Vaimo\ComposerChangelogs\Resolvers\ReleaseDetailsResolver(); |
||||
155 | |||||
156 | $generalInfo = $detailsResolver->resolveOverview($details); |
||||
157 | $groups = $detailsResolver->resolveChangeGroups($details); |
||||
158 | |||||
159 | if ($briefMode) { |
||||
160 | $summary = array_map(function ($key, $group) { |
||||
161 | return sprintf('%s (%s)', $key, count($group)); |
||||
162 | }, array_keys($groups), $groups); |
||||
163 | |||||
164 | $generalInfo = $detailsResolver->resolveOverview($details); |
||||
165 | |||||
166 | $groups = array_filter(array( |
||||
167 | 'overview' => $generalInfo['overview'], |
||||
168 | 'summary' => sprintf('Includes: %s', implode(', ', $summary)) |
||||
169 | )); |
||||
170 | } elseif ($generalInfo['overview']) { |
||||
171 | $groups = array_merge( |
||||
172 | $groups, |
||||
173 | array('overview' => $generalInfo['overview']) |
||||
174 | ); |
||||
175 | } |
||||
176 | |||||
177 | return $groups; |
||||
178 | } |
||||
179 | |||||
180 | private function generateOutput(ComposerContext $composerCtx, Package $package, $groups, $format, $fromSource) |
||||
181 | { |
||||
182 | $composerRuntime = $this->getComposer(); |
||||
0 ignored issues
–
show
The function
Composer\Command\BaseCommand::getComposer() has been deprecated: since Composer 2.3.0 use requireComposer or tryComposer depending on whether you have $required set to true or false
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
183 | |||||
184 | if ($format === 'json') { |
||||
185 | $jsonEncoder = new \Camspiers\JsonPretty\JsonPretty(); |
||||
186 | |||||
187 | return $jsonEncoder->prettify($groups, null, ' '); |
||||
188 | } |
||||
189 | |||||
190 | $confResolverFactory = new Factories\Changelog\ConfigResolverFactory($composerCtx); |
||||
191 | |||||
192 | $confResolver = $confResolverFactory->create($fromSource); |
||||
193 | |||||
194 | $templateResolver = new \Vaimo\ComposerChangelogs\Resolvers\ChangelogTemplateResolver($confResolver); |
||||
195 | |||||
196 | $templates = $templateResolver->getTemplates($package); |
||||
197 | |||||
198 | $renderCtxGenerator = new \Vaimo\ComposerChangelogs\Generators\Changelog\RenderContextGenerator(); |
||||
199 | $templateRenderer = new \Vaimo\ComposerChangelogs\Generators\TemplateOutputGenerator(); |
||||
200 | |||||
201 | $infoResolver = new Resolvers\PackageInfoResolver( |
||||
202 | $composerRuntime->getInstallationManager() |
||||
203 | ); |
||||
204 | |||||
205 | $repositoryRoot = $infoResolver->getInstallPath($package); |
||||
206 | |||||
207 | $ctxData = $renderCtxGenerator->generate( |
||||
208 | array('' => $groups), |
||||
209 | '', |
||||
210 | $repositoryRoot |
||||
211 | ); |
||||
212 | |||||
213 | if (!isset($templates[$format])) { |
||||
214 | throw new \Vaimo\ComposerChangelogs\Exceptions\GeneratorException(sprintf( |
||||
215 | 'Unknown format: %s; available options: %s', |
||||
216 | $format, |
||||
217 | implode(', ', array_merge(array('json'), array_keys($templates))) |
||||
218 | )); |
||||
219 | } |
||||
220 | |||||
221 | return $templateRenderer->generateOutput( |
||||
222 | reset($ctxData['releases']), |
||||
223 | array('root' => $templates[$format]['release']) |
||||
224 | ); |
||||
225 | } |
||||
226 | |||||
227 | private function resolveVersion($changelog, $branch, $showUpcoming) |
||||
228 | { |
||||
229 | $releaseResolver = new \Vaimo\ComposerChangelogs\Resolvers\ChangelogReleaseResolver(); |
||||
230 | |||||
231 | if (!$showUpcoming) { |
||||
232 | return $releaseResolver->resolveLatestVersionedRelease($changelog, $branch); |
||||
233 | } |
||||
234 | |||||
235 | return $releaseResolver->resolveUpcomingRelease($changelog, $branch); |
||||
236 | } |
||||
237 | } |
||||
238 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.