1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Juanber84\Console\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\Console\Question\Question; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
11
|
|
|
use Symfony\Component\Console\Helper\Table; |
12
|
|
|
use Juanber84\Services\DatabaseService; |
13
|
|
|
|
14
|
|
|
class DeployProjectsCommand extends Command |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
use NameProjectTrait; |
18
|
|
|
|
19
|
|
|
const DEVELOP = 'develop'; |
20
|
|
|
const STAGING = 'staging'; |
21
|
|
|
const QUALITY = 'quality'; |
22
|
|
|
const MASTER = 'master'; |
23
|
|
|
|
24
|
|
|
const COMMAND_NAME = 'deploy-project'; |
25
|
|
|
const COMMAND_DESC = 'Auto Deploy Project.'; |
26
|
|
|
|
27
|
|
|
private $databaseService; |
28
|
|
|
|
29
|
|
|
public function __construct(DatabaseService $databaseService) |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
|
33
|
|
|
$this->databaseService = $databaseService; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName(self::COMMAND_NAME) |
40
|
|
|
->setDescription(self::COMMAND_DESC) |
41
|
|
|
->addArgument( |
42
|
|
|
'project', |
43
|
|
|
InputArgument::OPTIONAL, |
44
|
|
|
'What\'s the name of project?' |
45
|
|
|
) |
46
|
|
|
->addArgument( |
47
|
|
|
'branch', |
48
|
|
|
InputArgument::OPTIONAL, |
49
|
|
|
'What\'s the name of branch?' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$hieranchy = [self::DEVELOP, self::STAGING, self::QUALITY, self::MASTER]; |
56
|
|
|
$validBranchs = [self::STAGING, self::QUALITY, self::MASTER]; |
57
|
|
|
$nameOfProject = $input->getArgument('project'); |
58
|
|
|
$branchOfProject = $input->getArgument('branch'); |
59
|
|
|
$helper = $this->getHelper('question'); |
60
|
|
|
|
61
|
|
|
$nameOfProject = $this->getNameOfProject($input, $output, $helper, $nameOfProject); |
62
|
|
|
|
63
|
|
|
if (!$branchOfProject) { |
64
|
|
|
$question = new Question('<question>What is the branch to deploy?</question>: '); |
65
|
|
|
do { |
66
|
|
|
$branchOfProject = trim($helper->ask($input, $output, $question)); |
67
|
|
|
} while (empty($branchOfProject) || !in_array($branchOfProject, $validBranchs)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$jsonDb = $this->databaseService->getProjects(); |
71
|
|
|
|
72
|
|
|
if (is_null($jsonDb)) { |
73
|
|
|
$output->writeln(''); |
74
|
|
|
$output->writeln('<info>0 projects configurated</info>'); |
75
|
|
|
} else { |
76
|
|
|
if (isset($jsonDb[$nameOfProject])) { |
77
|
|
|
$merge = self::DEVELOP; |
78
|
|
|
$final = self::STAGING; |
79
|
|
|
foreach ($hieranchy as $k => $v) { |
80
|
|
|
if ($v == $branchOfProject) { |
81
|
|
|
$merge = $hieranchy[$k-1]; |
82
|
|
|
$final = $v; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
chdir($jsonDb[$nameOfProject]); |
87
|
|
|
|
88
|
|
|
$pipetask = [ |
89
|
|
|
'git checkout ' .$merge, |
90
|
|
|
'git pull', |
91
|
|
|
'git checkout ' .$final, |
92
|
|
|
'git pull', |
93
|
|
|
'git merge '.$merge, |
94
|
|
|
'git push --progress 2>&1', |
95
|
|
|
'git checkout develop', |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$progressBar = new ProgressBar($output, count($pipetask)); |
99
|
|
|
$progressBar->setBarCharacter('<fg=magenta>=</>'); |
100
|
|
|
$progressBar->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%"); |
101
|
|
|
$progressBar->setBarWidth(50); |
102
|
|
|
|
103
|
|
|
$table = new Table($output); |
104
|
|
|
$table->setHeaders(array('<fg=white>Command</>', '<fg=white>Result</>')); |
105
|
|
|
|
106
|
|
|
$exitCode = 0; |
107
|
|
|
$exitCodeMessage = ""; |
108
|
|
|
foreach ($pipetask as $t){ |
109
|
|
|
if ($exitCode == 0){ |
|
|
|
|
110
|
|
|
$command = new \mikehaertl\shellcommand\Command($t); |
111
|
|
|
if ($command->execute()) { |
112
|
|
|
$message = $command->getOutput(); |
113
|
|
|
$message = trim(preg_replace('/\t+/', '', $message)); |
114
|
|
|
$message = trim(preg_replace('/Ma\n+/', '', $message)); |
115
|
|
|
$message = trim(preg_replace('/a\n+/', '', $message)); |
116
|
|
|
$message = trim(preg_replace('/|/', '', $message)); |
117
|
|
|
$exitCode = $command->getExitCode(); |
118
|
|
|
} else { |
119
|
|
|
$message = $command->getError(); |
120
|
|
|
$message = trim(preg_replace('/\t+/', '', $message)); |
121
|
|
|
$message = trim(preg_replace('/Ma\n+/', '', $message)); |
122
|
|
|
$message = trim(preg_replace('/a\n+/', '', $message)); |
123
|
|
|
$message = trim(preg_replace('/|/', '', $message)); |
124
|
|
|
$exitCode = $command->getExitCode(); |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
$message = ''; |
128
|
|
|
$exitCode = -1; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($exitCode == 0){ |
|
|
|
|
132
|
|
|
$exitCodeMessage = '<fg=green>SUCCESS</>'; |
133
|
|
|
} elseif ($exitCode == 1){ |
134
|
|
|
$exitCodeMessage = '<fg=red>FAILED</>'; |
135
|
|
|
} elseif ($exitCode == -1){ |
136
|
|
|
$exitCodeMessage = '<fg=blue>ABORTED</>'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$command = '<fg=magenta>'.$t.'</>'; |
140
|
|
|
if (strlen(trim($message))>0){ |
141
|
|
|
$command .= "\n".$message; |
142
|
|
|
} |
143
|
|
|
$table->addRow([$command, $exitCodeMessage]); |
144
|
|
|
usleep(300000); |
145
|
|
|
$progressBar->setMessage($t); |
146
|
|
|
$progressBar->advance(); |
147
|
|
|
} |
148
|
|
|
$progressBar->setMessage(""); |
149
|
|
|
|
150
|
|
|
$progressBar->finish(); |
151
|
|
|
|
152
|
|
|
$output->writeln(''); |
153
|
|
|
$table->render(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|