-
Notifications
You must be signed in to change notification settings - Fork 188
/
GitPackage.php
116 lines (91 loc) · 3.54 KB
/
GitPackage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace JeroenG\Packager\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use JeroenG\Packager\Conveyor;
use JeroenG\Packager\FileHandlerInterface;
use JeroenG\Packager\ProgressBar;
use JeroenG\Packager\Wrapping;
/**
* Get an existing package from a remote git repository with its VCS.
*
* @author JeroenG
**/
class GitPackage extends Command
{
use ProgressBar;
protected $signature = 'packager:git
{url : The url of the git repository}
{vendor? : The vendor part of the namespace}
{name? : The name of package for the namespace}';
protected $description = 'Retrieve an existing package with git.';
/**
* Packages roll off of the conveyor.
*/
protected Conveyor $conveyor;
/**
* Packages are packed in wrappings to personalise them.
*/
protected Wrapping $wrapping;
protected FileHandlerInterface $fileHandler;
public function __construct(Conveyor $conveyor, Wrapping $wrapping, FileHandlerInterface $fileHandler)
{
parent::__construct();
$this->conveyor = $conveyor;
$this->wrapping = $wrapping;
$this->fileHandler = $fileHandler;
}
public function handle(): void
{
// Start the progress bar
$this->startProgressBar(4);
// Common variables
$source = $this->argument('url');
$origin = mb_strtolower(rtrim($source, '/'));
if (is_null($this->argument('vendor')) || is_null($this->argument('name'))) {
$this->setGitVendorAndPackage($origin);
} else {
$this->conveyor->vendor($this->argument('vendor'));
$this->conveyor->package($this->argument('name'));
}
// Start creating the package
$this->info('Creating package '.$this->conveyor->vendor().'\\'.$this->conveyor->package().'...');
$this->fileHandler->checkIfPackageExists($this->conveyor->vendor(), $this->conveyor->package());
$this->makeProgress();
// Create the package directory
$this->info('Creating packages directory...');
$this->fileHandler->makeDir($this->fileHandler->packagesPath());
// Clone the repository
$this->info('Cloning repository...');
exec("git clone -q $source ".$this->fileHandler->packagePath($this->conveyor->vendor(), $this->conveyor->package()), $output, $exit_code);
if ($exit_code !== 0) {
$this->error('Unable to clone repository');
$this->warn('Please check credentials and try again');
return;
}
$this->makeProgress();
// Create the vendor directory
$this->info('Creating vendor...');
$this->fileHandler->makeDir($this->fileHandler->vendorPath($this->conveyor->vendor()));
$this->makeProgress();
$this->info('Installing package...');
$this->conveyor->installPackage();
$this->makeProgress();
// Finished creating the package, end of the progress bar
$this->finishProgress('Package cloned successfully!');
}
protected function setGitVendorAndPackage($origin): void
{
$pieces = explode('/', $origin);
if (Str::contains($origin, 'https')) {
$vendor = $pieces[3];
$package = $pieces[4];
} else {
$vendor = explode(':', $pieces[0])[1];
$package = rtrim($pieces[1], '.git');
}
$this->conveyor->vendor($vendor);
$this->conveyor->package($package);
}
}