|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\ChecksSSHConnection; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
use Illuminate\Support\Facades\File; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PublishGitDependencies. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Acacha\ForgePublish\Commands |
|
14
|
|
|
*/ |
|
15
|
|
|
class PublishGitDependencies extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
use ChecksSSHConnection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The name and signature of the console command. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $signature = 'publish:git_dependencies'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The console command description. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description = 'Publish ignored local git projects into production'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Server name |
|
35
|
|
|
* |
|
36
|
|
|
* @var String |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $server; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Guzzle http client. |
|
42
|
|
|
* |
|
43
|
|
|
* @var Client |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $http; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Create a new command instance. |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Client $http) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct(); |
|
54
|
|
|
$this->http = $http; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Execute the console command. |
|
59
|
|
|
* |
|
60
|
|
|
*/ |
|
61
|
|
|
public function handle() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->abortCommandExecution(); |
|
64
|
|
|
|
|
65
|
|
|
if (empty($ignored_repos = $this->ignoredRepos())) { |
|
66
|
|
|
$this->error('Sorry no ignored local github repos found in the current folder. Skipping'); |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$files_to_publish = $this->choice('Which Github projects in local do you want to publish (you could select multiple values separated by coma)?', $ignored_repos, null, null, true); |
|
71
|
|
|
|
|
72
|
|
|
foreach ((array) $files_to_publish as $file) { |
|
73
|
|
|
$repository_url= $this->repositoryURL($file); |
|
74
|
|
|
$file = starts_with($file, '/') ? str_after($file, '/') : $file; |
|
75
|
|
|
$this->call('publish:git', [ |
|
76
|
|
|
'git_command' => "clone $repository_url $file" |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Obtain ignored files |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function ignoredRepos() |
|
85
|
|
|
{ |
|
86
|
|
|
$lines = file($this->ignored_files_path()); |
|
87
|
|
|
|
|
88
|
|
|
$ignored_files = []; |
|
89
|
|
|
foreach ($lines as $line) { |
|
90
|
|
|
$file = preg_replace("/\r|\n/", "", $line); |
|
91
|
|
|
if (!File::exists(base_path($file)) || ! is_dir(base_path($file))) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
if (! $this->folderContainsValidGithubRepo($file)) { |
|
95
|
|
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
$ignored_files[] = $file; |
|
98
|
|
|
} |
|
99
|
|
|
return $ignored_files; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Repository URL. |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function repositoryURL($file) |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->isAGithubRepo($remote = $this->remoteGithub($file))) { |
|
108
|
|
|
return $remote; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->error("Sorry we could not find a valid Git URL for folder $file"); |
|
112
|
|
|
die(); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Check if string is a valid Github repo URL. |
|
117
|
|
|
* |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function isAGithubRepo($remote) |
|
121
|
|
|
{ |
|
122
|
|
|
if (! starts_with($remote, ['[email protected]:','https://github.com/'])) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
return true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Remote github. |
|
130
|
|
|
* |
|
131
|
|
|
* @param $file |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function remoteGithub($file) |
|
135
|
|
|
{ |
|
136
|
|
|
$path = base_path($file); |
|
137
|
|
|
if (! File::exists($path . '/.git')) { |
|
138
|
|
|
return ''; |
|
139
|
|
|
} |
|
140
|
|
|
return preg_replace("/\r|\n/", "", `cd $path;git remote get-url origin 2> /dev/null`); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Folder contains a valid Github repo. |
|
145
|
|
|
* |
|
146
|
|
|
* @param $folder |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function folderContainsValidGithubRepo($folder) |
|
150
|
|
|
{ |
|
151
|
|
|
$remote = $this->remoteGithub($folder); |
|
152
|
|
|
if ($this->isAGithubRepo($remote)) { |
|
153
|
|
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Git ignored files path. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function ignored_files_path() |
|
164
|
|
|
{ |
|
165
|
|
|
return base_path('.gitignore'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Abort command execution? |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function abortCommandExecution() |
|
172
|
|
|
{ |
|
173
|
|
|
if (! File::exists($path = $this->ignored_files_path())) { |
|
174
|
|
|
$this->error("$path file not exists"); |
|
175
|
|
|
die(); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$this->abortIfNoSSHConnection(); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.