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
|
3 |
|
protected function configure() |
15
|
|
|
{ |
16
|
3 |
|
$this |
17
|
3 |
|
->setName('pannelloamministrazione:checkgitversion') |
18
|
3 |
|
->setDescription('Controllo versioni bundles') |
19
|
3 |
|
->setHelp('Controlla le versioni git dei bundles'); |
20
|
3 |
|
} |
21
|
|
|
|
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
23
|
|
|
{ |
24
|
|
|
if (self::isWindows()) { |
25
|
|
|
$output->writeln('<info>Non previsto in ambiente windows</info>'); |
26
|
|
|
|
27
|
|
|
return 0; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$projectDir = substr($this->getContainer()->get('kernel')->getRootDir(), 0, -4); |
31
|
|
|
|
32
|
|
|
$composerbundles = array(); |
33
|
|
|
$composerbundlespath = $projectDir.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'fi'; |
34
|
|
|
$findercomposerbundle = new Finder(); |
35
|
|
|
$findercomposerbundle->in($composerbundlespath)->sortByName()->directories()->depth('== 0'); |
36
|
|
|
|
37
|
|
|
foreach ($findercomposerbundle as $file) { |
38
|
|
|
$fullcomposerbundlepath = $composerbundlespath.DIRECTORY_SEPARATOR.$file->getBasename(); |
39
|
|
|
$local = $this->getGitVersion($fullcomposerbundlepath, false); |
40
|
|
|
$remote = $this->getGitVersion($fullcomposerbundlepath, true); |
41
|
|
|
$style = new OutputFormatterStyle('blue', 'white', array('bold', 'blink')); |
42
|
|
|
$output->getFormatter()->setStyle('warning', $style); |
43
|
|
|
if ($local !== $remote) { |
44
|
|
|
$remote = '<warning> * '.$remote.' * </warning>'; |
45
|
|
|
} |
46
|
|
|
$output->writeln('<info>'.$file->getBasename().'</info> '.$local.' -> '.$remote); |
47
|
|
|
|
48
|
|
|
$composerbundles[] = array( |
49
|
|
|
'name' => $file->getBasename(), |
50
|
|
|
'path' => $fullcomposerbundlepath, |
51
|
|
|
'version' => $this->getGitVersion($fullcomposerbundlepath), |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return 0; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getGitVersion($path, $remote = false) |
59
|
|
|
{ |
60
|
|
|
if (self::isWindows()) { |
61
|
|
|
return ''; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($remote) { |
65
|
|
|
//Remote |
66
|
|
|
$cmd = 'cd '.$path; |
67
|
|
|
$remotetag = $cmd.";git ls-remote -t | awk '{print $2}' | cut -d '/' -f 3 | cut -d '^' -f 1 | sort --version-sort | tail -1"; |
68
|
|
|
$process = new Process($remotetag); |
69
|
|
|
$process->setTimeout(60 * 100); |
70
|
|
|
$process->run(); |
71
|
|
|
if ($process->isSuccessful()) { |
72
|
|
|
$version = trim($process->getOutput()); |
73
|
|
|
if (preg_match('/\d+(?:\.\d+)+/', $version, $matches)) { |
74
|
|
|
return $matches[0]; //returning the first match |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return '?'; |
79
|
|
|
} else { |
80
|
|
|
//Local |
81
|
|
|
$cmd = 'cd '.$path; |
82
|
|
|
$process = new Process($cmd.';git branch | '."grep ' * '"); |
83
|
|
|
$process->setTimeout(60 * 100); |
84
|
|
|
$process->run(); |
85
|
|
|
if ($process->isSuccessful()) { |
86
|
|
|
$out = explode(chr(10), $process->getOutput()); |
87
|
|
|
foreach ($out as $line) { |
88
|
|
|
if (strpos($line, '* ') !== false) { |
89
|
|
|
$version = trim(strtolower(str_replace('* ', '', $line))); |
90
|
|
|
if ($version == 'master') { |
91
|
|
|
return $version; |
92
|
|
|
} else { |
93
|
|
|
if (preg_match('/\d+(?:\.\d+)+/', $version, $matches)) { |
94
|
|
|
return $matches[0]; //returning the first match |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} else { |
|
|
|
|
100
|
|
|
//echo $process->getErrorOutput(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return '?'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function isWindows() |
108
|
|
|
{ |
109
|
|
|
if (PHP_OS == 'WINNT') { |
110
|
|
|
return true; |
111
|
|
|
} else { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.