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