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
|
|
|
public function __construct(DatabaseService $databaseService) |
28
|
|
|
{ |
29
|
|
|
parent::__construct(); |
30
|
|
|
|
31
|
|
|
$this->databaseService = $databaseService; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName(self::COMMAND_NAME) |
38
|
|
|
->setDescription(self::COMMAND_DESC) |
39
|
|
|
->addArgument( |
40
|
|
|
'project', |
41
|
|
|
InputArgument::OPTIONAL, |
42
|
|
|
'What\'s the name of project?' |
43
|
|
|
) |
44
|
|
|
->addArgument( |
45
|
|
|
'branch', |
46
|
|
|
InputArgument::OPTIONAL, |
47
|
|
|
'What\'s the name of branch?' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
|
|
$hieranchy = [self::DEVELOP, self::STAGING, self::QUALITY, self::MASTER]; |
54
|
|
|
$validBranchs = [self::STAGING, self::QUALITY, self::MASTER]; |
55
|
|
|
$nameOfProject = $input->getArgument('project'); |
56
|
|
|
$branchOfProject = $input->getArgument('branch'); |
57
|
|
|
$helper = $this->getHelper('question'); |
58
|
|
|
|
59
|
|
|
$nameOfProject = $this->getNameOfProject($input, $output, $helper, $nameOfProject); |
60
|
|
|
|
61
|
|
|
if (!$branchOfProject) { |
62
|
|
|
$question = new Question('<question>What is the branch to deploy?</question>: '); |
63
|
|
|
do { |
64
|
|
|
$branchOfProject = trim($helper->ask($input, $output, $question)); |
65
|
|
|
} while (empty($branchOfProject) || !in_array($branchOfProject, $validBranchs)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$jsonDb = $this->databaseService->getProjects(); |
69
|
|
|
|
70
|
|
|
if (is_null($jsonDb)) { |
71
|
|
|
$output->writeln(''); |
72
|
|
|
$output->writeln('<info>0 projects configurated</info>'); |
73
|
|
|
} else { |
74
|
|
|
if (isset($jsonDb[$nameOfProject])) { |
75
|
|
|
$merge = self::DEVELOP; |
76
|
|
|
$final = self::STAGING; |
77
|
|
|
foreach ($hieranchy as $k => $v) { |
78
|
|
|
if ($v == $branchOfProject) { |
79
|
|
|
$merge = $hieranchy[$k-1]; |
80
|
|
|
$final = $v; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
chdir($jsonDb[$nameOfProject]); |
85
|
|
|
|
86
|
|
|
$pipetask = [ |
87
|
|
|
'git checkout ' .$merge, |
88
|
|
|
'git pull', |
89
|
|
|
'git checkout ' .$final, |
90
|
|
|
'git merge '.$merge, |
91
|
|
|
'git push --progress 2>&1', |
92
|
|
|
'git checkout develop', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$progressBar = new ProgressBar($output, count($pipetask)); |
96
|
|
|
$progressBar->setBarCharacter('<fg=magenta>=</>'); |
97
|
|
|
$progressBar->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%"); |
98
|
|
|
$progressBar->setBarWidth(50); |
99
|
|
|
|
100
|
|
|
$table = new Table($output); |
101
|
|
|
$table->setHeaders(array('<fg=white>Command</>', '<fg=white>Result</>')); |
102
|
|
|
|
103
|
|
|
$exitCode = 0; |
104
|
|
|
$exitCodeMessage = ""; |
105
|
|
|
foreach ($pipetask as $t){ |
106
|
|
|
if ($exitCode == 0){ |
107
|
|
|
$command = new \mikehaertl\shellcommand\Command($t); |
108
|
|
|
if ($command->execute()) { |
109
|
|
|
$message = $command->getOutput(); |
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
|
|
|
} else { |
116
|
|
|
$message = $command->getError(); |
117
|
|
|
$message = trim(preg_replace('/\t+/', '', $message)); |
118
|
|
|
$message = trim(preg_replace('/Ma\n+/', '', $message)); |
119
|
|
|
$message = trim(preg_replace('/a\n+/', '', $message)); |
120
|
|
|
$message = trim(preg_replace('/|/', '', $message)); |
121
|
|
|
$exitCode = $command->getExitCode(); |
122
|
|
|
} |
123
|
|
|
} else { |
124
|
|
|
$message = ''; |
125
|
|
|
$exitCode = -1; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($exitCode == 0){ |
129
|
|
|
$exitCodeMessage = '<fg=green>SUCCESS</>'; |
130
|
|
|
} elseif ($exitCode == 1){ |
131
|
|
|
$exitCodeMessage = '<fg=red>FAILED</>'; |
132
|
|
|
} elseif ($exitCode == -1){ |
133
|
|
|
$exitCodeMessage = '<fg=blue>ABORTED</>'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$command = '<fg=magenta>'.$t.'</>'; |
137
|
|
|
if (strlen(trim($message))>0){ |
138
|
|
|
$command .= "\n".$message; |
139
|
|
|
} |
140
|
|
|
$table->addRow([$command, $exitCodeMessage]); |
141
|
|
|
usleep(300000); |
142
|
|
|
$progressBar->setMessage($t); |
143
|
|
|
$progressBar->advance(); |
144
|
|
|
} |
145
|
|
|
$progressBar->setMessage(""); |
146
|
|
|
|
147
|
|
|
$progressBar->finish(); |
148
|
|
|
|
149
|
|
|
$output->writeln(''); |
150
|
|
|
$table->render(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: