-
Notifications
You must be signed in to change notification settings - Fork 554
/
GeneratePackageCommand.php
129 lines (101 loc) · 3.42 KB
/
GeneratePackageCommand.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
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace A17\Twill\Commands;
use A17\Twill\Commands\Traits\HandlesStubs;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class GeneratePackageCommand extends Command
{
use HandlesStubs;
protected $signature = 'twill:make:package';
protected $description = 'Make a new twill package';
/**
* @var string
*/
private $packageName;
/**
* @var string
*/
private $packageVendor;
/**
* @var string
*/
private $homepage;
/**
* @var string
*/
private $licence;
/**
* @var string
*/
private $psr4Base;
/**
* @var string
*/
private $targetDirectory;
public function handle(): void
{
$this->packageName = $this->ask("What's the package name", 'twill-extension');
$this->packageVendor = $this->ask("What's the package vendor", 'area17');
$this->homepage = $this->ask(
'The package homepage',
'https://github.com/' . $this->packageVendor . '/' . $this->packageName
);
$this->licence = $this->askWithCompletion('What is the licence', ['MIT', 'Apache-2.0'], 'MIT');
$this->psr4Base = $this->ask('The psr4 base name', Str::studly($this->packageName));
$this->targetDirectory = $this->ask(
'Where should we put the package',
base_path('packages') . '/' . $this->packageName
);
$this->generatePackage();
}
protected function generatePackage(): void
{
File::ensureDirectoryExists($this->targetDirectory);
$this->writeComposerJson();
$this->writeServiceProvider();
$this->displayMessage();
}
protected function writeComposerJson(): void
{
$stub = file_get_contents(__DIR__ . '/stubs/package/package-composer-json.stub');
$stub = $this->replaceVariables([
'name' => $this->packageVendor . '/' . $this->packageName,
'homepage' => $this->homepage,
'licence' => $this->licence,
'namespace' => $this->psr4Base,
'providerName' => $this->getProviderName(),
], $stub);
file_put_contents($this->targetDirectory . '/composer.json', $stub);
}
protected function writeServiceProvider(): void
{
File::ensureDirectoryExists($this->targetDirectory . '/src');
$stub = file_get_contents(__DIR__ . '/stubs/package/package-service-provider.stub');
$providerName = $this->getProviderName();
$stub = $this->replaceVariables([
'namespace' => $this->psr4Base,
'providerName' => $providerName,
], $stub);
file_put_contents($this->targetDirectory . "/src/{$providerName}ServiceProvider.php", $stub);
}
protected function displayMessage(): void
{
$stub = file_get_contents(__DIR__ . '/stubs/package/instructions.stub');
$path = $this->targetDirectory;
if (Str::startsWith($path, base_path())) {
$path = '.' . Str::replaceFirst(base_path(), '', $path);
}
$stub = $this->replaceVariables([
'path' => $path,
'vendor' => $this->packageVendor,
'name' => $this->packageName,
'namespace' => $this->getProviderName(),
], $stub);
$this->line($stub);
}
protected function getProviderName()
{
return str_after_last($this->psr4Base, '\\');
}
}