Issues (8)

src/Commands/VersionCommand.php (1 issue)

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 Vaimo\ComposerChangelogs\Factories;
12
13
class VersionCommand extends \Composer\Command\BaseCommand
14
{
15
    protected function configure()
16
    {
17
        $this->setName('changelog:version');
18
19
        $this->setDescription(
20
            'Display version information from changelog. <comment>[default: latest stable]</comment>'
21
        );
22
23
        $this->addArgument(
24
            'name',
25
            \Symfony\Component\Console\Input\InputArgument::OPTIONAL,
26
            'Targeted package name. Default: root package'
27
        );
28
29
        $this->addOption(
30
            '--from-source',
31
            null,
32
            \Symfony\Component\Console\Input\InputOption::VALUE_NONE,
33
            'Extract configuration from vendor package instead of using global installation data'
34
        );
35
36
        $this->addOption(
37
            '--format',
38
            null,
39
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
40
            'Format of the output (regex)'
41
        );
42
43
        $this->addOption(
44
            '--segments',
45
            null,
46
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
47
            'Number of segments of the version to return. <comment>[default: all segments]</comment>'
48
        );
49
        
50
        $this->addOption(
51
            '--upcoming',
52
            null,
53
            \Symfony\Component\Console\Input\InputOption::VALUE_NONE,
54
            'Show upcoming version (if there is one)'
55
        );
56
57
        $this->addOption(
58
            '--branch',
59
            null,
60
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
61
            'Match release branch (if provided in changelog item)'
62
        );
63
64
        $this->addOption(
65
            '--tip',
66
            null,
67
            \Symfony\Component\Console\Input\InputOption::VALUE_NONE,
68
            'Show LATEST version (might be latest release, might be upcoming)'
69
        );
70
    }
71
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        $packageName = $input->getArgument('name');
75
        $fromSource = $input->getOption('from-source');
76
        $format = $input->getOption('format');
77
        $branch = $input->getOption('branch');
78
        $segmentsCount = (int)$input->getOption('segments');
79
80
        $showUpcoming = $input->getOption('upcoming');
81
        $showTip = $input->getOption('tip');
82
83
        $composerCtxFactory = new \Vaimo\ComposerChangelogs\Factories\ComposerContextFactory(
84
            $this->getComposer()
0 ignored issues
show
Deprecated Code introduced by
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 ignore-deprecated  annotation

84
            /** @scrutinizer ignore-deprecated */ $this->getComposer()

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.

Loading history...
85
        );
86
87
        $composerCtx = $composerCtxFactory->create();
88
        
89
        $chLogRepoFactory = new Factories\ChangelogRepositoryFactory(
90
            $composerCtx,
91
            $output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL ? $output : null
92
        );
93
        
94
        $chLogRepo = $chLogRepoFactory->create($fromSource);
95
96
        $changelog = $chLogRepo->getByPackageName($packageName);
97
98
        if ($changelog === null) {
99
            return 1;
100
        }
101
102
        $releases = $changelog->getReleases();
103
        
104
        $releaseResolver = new \Vaimo\ComposerChangelogs\Resolvers\ChangelogReleaseResolver();
105
106
        $version = key($releases);
107
108
        if (!$showTip) {
109
            $version = $releaseResolver->resolveLatestVersionedRelease($releases, $branch);
110
111
            if ($showUpcoming) {
112
                $version = $releaseResolver->resolveUpcomingRelease($releases, $branch);
113
            }
114
        }
115
116
        if (!$version) {
117
            return 0;
118
        }
119
120
        $versionResolver = new \Vaimo\ComposerChangelogs\Resolvers\VersionResolver();
121
122
        $version = $versionResolver->resolveValidVersion($version);
123
            
124
        if ($format === 'regex') {
125
            $version = preg_quote($version, '/');
126
        }
127
        
128
        if ($segmentsCount) {
129
            $version = implode(
130
                '.',
131
                array_slice(
132
                    explode('.', $version),
133
                    0,
134
                    $segmentsCount
135
                )
136
            );
137
        }
138
        
139
        $output->writeln($version);
140
        
141
        return 0;
142
    }
143
}
144