Passed
Push — master ( dfdfc0...171152 )
by Andrea
16:31 queued 19s
created

CheckgitversionCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.0105

Importance

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