1
|
|
|
<?php namespace Magestead\Service; |
2
|
|
|
|
3
|
|
|
use Magestead\Command\ProcessCommand; |
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
|
6
|
|
|
class VersionControl |
7
|
|
|
{ |
8
|
|
|
protected $_repoUrl; |
9
|
|
|
protected $_projectPath; |
10
|
|
|
protected $_output; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* VersionControl constructor. |
14
|
|
|
* @param $repoUrl |
15
|
|
|
* @param $projectPath |
16
|
|
|
* @param OutputInterface $output |
17
|
|
|
*/ |
18
|
|
|
public function __construct($repoUrl, $projectPath, OutputInterface $output) |
19
|
|
|
{ |
20
|
|
|
$this->_repoUrl = $repoUrl; |
21
|
|
|
$this->_projectPath = $projectPath; |
22
|
|
|
$this->_output = $output; |
23
|
|
|
|
24
|
|
|
$this->execute($output); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param OutputInterface $output |
29
|
|
|
*/ |
30
|
|
|
protected function execute(OutputInterface $output) |
31
|
|
|
{ |
32
|
|
|
$output->writeln('<info>Configuring GIT repo</info>'); |
33
|
|
|
$this->init(); |
34
|
|
|
|
35
|
|
|
$output->writeln('<comment>Adding files to repo</comment>'); |
36
|
|
|
$this->addFiles(); |
37
|
|
|
|
38
|
|
|
$output->writeln('<comment>Committing files to repo</comment>'); |
39
|
|
|
$this->commitFiles(); |
40
|
|
|
|
41
|
|
|
$output->writeln('<comment>Pushing to remote</comment>'); |
42
|
|
|
$this->pushFiles(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initialise the GIT repo |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function init() |
51
|
|
|
{ |
52
|
|
|
$command = 'git init; git remote add origin ' . $this->_repoUrl; |
53
|
|
|
new ProcessCommand($command, $this->_projectPath, $this->_output); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add all file to the GIT index |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function addFiles() |
64
|
|
|
{ |
65
|
|
|
$command = 'git add -A'; |
66
|
|
|
new ProcessCommand($command, $this->_projectPath, $this->_output); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Commit the files to the repo |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function commitFiles() |
77
|
|
|
{ |
78
|
|
|
$command = "git commit -m 'Initial commit'"; |
79
|
|
|
new ProcessCommand($command, $this->_projectPath, $this->_output); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Push all the files to remote repo |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function pushFiles() |
90
|
|
|
{ |
91
|
|
|
$command = "git push -u origin master"; |
92
|
|
|
new ProcessCommand($command, $this->_projectPath, $this->_output); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|