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