CheckgitversionCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Process\Process;
10
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
11
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath;
12
13
/**
14
 * @codeCoverageIgnore
15
 */
16
class CheckgitversionCommand extends Command
17
{
18
    protected static $defaultName = 'pannelloamministrazione:checkgitversion';
19
    
20
    private ProjectPath $projectpath;
21
    
22
    protected function configure() : void
23
    {
24
        $this
25
                ->setDescription('Controllo versioni bundles')
26
                ->setHelp('Controlla le versioni git dei bundles');
27
    }
28
29
    public function __construct(ProjectPath $projectpath)
30
    {
31
        $this->projectpath = $projectpath;
32
33
        // you *must* call the parent constructor
34
        parent::__construct();
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output) : int
38
    {
39
        if (self::isWindows()) {
40
            $output->writeln('<info>Non previsto in ambiente windows</info>');
41
42
            return 0;
43
        }
44
45
        $composerbundles = array();
46
        $papath = $this->projectpath;
47
        $composerbundlespath = $papath->getVendorBinPath().'/../fi';
48
        $findercomposerbundle = new Finder();
49
        $findercomposerbundle->in($composerbundlespath)->sortByName()->directories()->depth('== 0');
50
51
        foreach ($findercomposerbundle as $file) {
52
            $fullcomposerbundlepath = $composerbundlespath.DIRECTORY_SEPARATOR.$file->getBasename();
53
            $local = $this->getGitVersion($fullcomposerbundlepath, false);
54
            $remote = $this->getGitVersion($fullcomposerbundlepath, true);
55
            $style = new OutputFormatterStyle('blue', 'white', array('bold', 'blink'));
56
            $output->getFormatter()->setStyle('warning', $style);
57
            if ($local !== $remote) {
58
                $remote = '<warning> * '.$remote.' * </warning>';
59
            }
60
            $output->writeln('<info>'.$file->getBasename().'</info> '.$local.' -> '.$remote);
61
62
            $composerbundles[] = array(
63
                'name' => $file->getBasename(),
64
                'path' => $fullcomposerbundlepath,
65
                'version' => $this->getGitVersion($fullcomposerbundlepath),
66
            );
67
        }
68
69
        return 0;
70
    }
71
72
    private function getGitVersion(string $path, bool $remote = false) : string
73
    {
74
        if (self::isWindows()) {
75
            return '';
76
        }
77
78
        if ($remote) {
79
            //Remote
80
            $cmd = 'cd '.$path;
81
            $remotetagscmd = "git ls-remote -t | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort --version-sort | tail -1";
82
            $remotetag = $cmd.';'.$remotetagscmd;
83
            $process = Process::fromShellCommandline($remotetag);
84
            $process->setTimeout(60 * 100);
85
            $process->run();
86
            if ($process->isSuccessful()) {
87
                $versions = trim($process->getOutput());
88
89
                return $this->getRemoteVersionString($versions);
90
            }
91
92
            return '?';
93
        } else {
94
            //Local
95
            $cmd = 'cd '.$path;
96
            $process = Process::fromShellCommandline($cmd.';git branch | '."grep ' * '");
97
            $process->setTimeout(60 * 100);
98
            $process->run();
99
            if ($process->isSuccessful()) {
100
                $versions = explode(chr(10), $process->getOutput());
101
102
                return $this->getLocalVersionString($versions);
103
            } else {
104
                //echo $process->getErrorOutput();
105
                return '?';
106
            }
107
        }
108
    }
109
110
    /**
111
     *
112
     * @param array<string> $versions
113
     * @return string
114
     */
115
    private function getLocalVersionString(array $versions) : string
116
    {
117
        foreach ($versions as $line) {
118
            if (false !== strpos($line, '* ')) {
119
                $version = trim(strtolower(str_replace('* ', '', $line)));
120
121
                return $this->getLocalVersionStringDetail($version);
122
            }
123
        }
124
125
        return '?';
126
    }
127
128
    private function getLocalVersionStringDetail(string $versions) :string
129
    {
130
        if ('master' == $versions) {
131
            return $versions;
132
        } else {
133
            $matches = [];
134
            if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
135
                return $matches[0]; //returning the first match
136
            }
137
        }
138
139
        return '?';
140
    }
141
142
    private function getRemoteVersionString(string $versions) :string
143
    {
144
        $matches = [];
145
        if (preg_match('/\d+(?:\.\d+)+/', $versions, $matches)) {
146
            return $matches[0]; //returning the first match
147
        }
148
149
        return '?';
150
    }
151
152
    public static function isWindows() : bool
153
    {
154
        if (PHP_OS == 'WINNT') {
155
            return true;
156
        } else {
157
            return false;
158
        }
159
    }
160
}
161