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