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 PublishIgnored. |
12
|
|
|
* |
13
|
|
|
* @package Acacha\ForgePublish\Commands |
14
|
|
|
*/ |
15
|
|
|
class PublishIgnored 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:ignored'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Publish ignored files 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_files = $this->ignoredFiles())) { |
66
|
|
|
$this->error('Sorry no ignored files found in the current folder. Skipping'); |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$files_to_publish = $this->choice('Which ignored files do you want to publish (you could select multiple values separated by coma)?', $ignored_files, null, null, true); |
71
|
|
|
|
72
|
|
|
foreach ((array) $files_to_publish as $file) { |
73
|
|
|
$this->call('publish:scp', [ |
74
|
|
|
'file' => $file |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Obtain ignored files |
81
|
|
|
*/ |
82
|
|
|
protected function ignoredFiles() |
83
|
|
|
{ |
84
|
|
|
$lines = file($this->path()); |
85
|
|
|
|
86
|
|
|
$ignored_files = []; |
87
|
|
|
foreach ($lines as $line) { |
88
|
|
|
$file = preg_replace("/\r|\n/", "", $line); |
89
|
|
|
if (!File::exists(base_path($file))) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
$ignored_files[] = $file; |
93
|
|
|
} |
94
|
|
|
return $ignored_files; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Git ignored files path. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function path() |
103
|
|
|
{ |
104
|
|
|
return base_path('.gitignore'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Abort command execution? |
109
|
|
|
*/ |
110
|
|
|
protected function abortCommandExecution() |
111
|
|
|
{ |
112
|
|
|
if (! File::exists($path = $this->path())) { |
113
|
|
|
$this->error("$path file not exists"); |
114
|
|
|
die(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->abortIfNoSSHConnection(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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
exit
expression 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.