|
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 ValidateCommand extends \Composer\Command\BaseCommand |
|
15
|
|
|
{ |
|
16
|
|
|
protected function configure() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->setName('changelog:validate'); |
|
19
|
|
|
|
|
20
|
|
|
$this->setDescription('Validate package changelog markup and structure'); |
|
21
|
|
|
|
|
22
|
|
|
$this->addArgument( |
|
23
|
|
|
'name', |
|
24
|
|
|
\Symfony\Component\Console\Input\InputArgument::OPTIONAL, |
|
25
|
|
|
'Targeted package name. Default: root package' |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$this->addOption( |
|
29
|
|
|
'--from-source', |
|
30
|
|
|
null, |
|
31
|
|
|
\Symfony\Component\Console\Input\InputOption::VALUE_NONE, |
|
32
|
|
|
'Extract configuration from vendor package instead of using global installation data' |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
37
|
|
|
{ |
|
38
|
|
|
$packageName = $input->getArgument('name'); |
|
39
|
|
|
$fromSource = $input->getOption('from-source'); |
|
40
|
|
|
|
|
41
|
|
|
$composerRuntime = $this->getComposer(); |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
$package = $this->resolvePackage(is_string($packageName) ? $packageName : ''); |
|
45
|
|
|
} catch (PackageResolverException $exception) { |
|
46
|
|
|
$this->printException($exception, $output); |
|
47
|
|
|
|
|
48
|
|
|
return 1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$chLogLoaderFactory = new Factories\Changelog\LoaderFactory($composerRuntime); |
|
52
|
|
|
|
|
53
|
|
|
$chLogLoader = $chLogLoaderFactory->create($fromSource); |
|
54
|
|
|
|
|
55
|
|
|
$validator = new \Vaimo\ComposerChangelogs\Validators\ChangelogValidator($chLogLoader, array( |
|
56
|
|
|
'failure' => '<error>%s</error>', |
|
57
|
|
|
'success' => '<info>%s</info>' |
|
58
|
|
|
)); |
|
59
|
|
|
|
|
60
|
|
|
$result = $validator->validateForPackage($package, $output->getVerbosity()); |
|
61
|
|
|
|
|
62
|
|
|
array_map(array($output, 'writeln'), $result->getMessages()); |
|
63
|
|
|
|
|
64
|
|
|
return (int)!$result(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function printException($exception, OutputInterface $output) |
|
68
|
|
|
{ |
|
69
|
|
|
$errorOutputGenerator = new \Vaimo\ComposerChangelogs\Console\OutputGenerator(); |
|
70
|
|
|
|
|
71
|
|
|
\array_map( |
|
72
|
|
|
array($output, 'writeln'), |
|
73
|
|
|
$errorOutputGenerator->generateForResolverException($exception) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $packageName |
|
79
|
|
|
* @return \Composer\Package\PackageInterface |
|
80
|
|
|
* @throws PackageResolverException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function resolvePackage($packageName) |
|
83
|
|
|
{ |
|
84
|
|
|
$composerRuntime = $this->getComposer(); |
|
85
|
|
|
|
|
86
|
|
|
if (!$packageName) { |
|
87
|
|
|
$packageName = $composerRuntime->getPackage()->getName(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$packageRepoFactory = new Factories\PackageRepositoryFactory($composerRuntime); |
|
91
|
|
|
|
|
92
|
|
|
$packageRepository = $packageRepoFactory->create(); |
|
93
|
|
|
|
|
94
|
|
|
return $packageRepository->getByName($packageName); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|