1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHINT package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ahc\Phint\Console; |
13
|
|
|
|
14
|
|
|
use Ahc\Cli\Exception\InvalidArgumentException; |
15
|
|
|
use Ahc\Cli\Input\Command; |
16
|
|
|
use Ahc\Cli\IO\Interactor; |
17
|
|
|
use Ahc\Phint\Generator\CollisionHandler; |
18
|
|
|
use Ahc\Phint\Generator\TwigGenerator; |
19
|
|
|
use Ahc\Phint\Util\Inflector; |
20
|
|
|
|
21
|
|
|
class InitCommand extends BaseCommand |
22
|
|
|
{ |
23
|
|
|
/** @var string Command name */ |
24
|
|
|
protected $_name = 'init'; |
25
|
|
|
|
26
|
|
|
/** @var string Command description */ |
27
|
|
|
protected $_desc = 'Create and Scaffold a bare new PHP project'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Configure the command options/arguments. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
protected function onConstruct() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->argument('<project>', 'The project name without slashes') |
38
|
|
|
->option('-T --type', 'Project type (project | library | composer-plugin)') |
39
|
|
|
->option('-n --name', 'Vendor full name', null, $this->_git->getConfig('user.name')) |
40
|
|
|
->option('-e --email', 'Vendor email', null, $this->_git->getConfig('user.email')) |
41
|
|
|
->option('-u --username', 'Vendor handle/username') |
42
|
|
|
->option('-N --namespace', 'Root namespace (use `/` separator)') |
43
|
|
|
->option('-P --php', 'Minimum PHP version', 'floatval') |
44
|
|
|
->option('-p --path', 'The project path (Auto resolved)') |
45
|
|
|
->option('-S --sync', "Only create missing files\nUse with caution, take backup if needed", null, false) |
46
|
|
|
->option('-f --force', "Run even if the project exists\nUse with caution, take backup if needed", null, false) |
47
|
|
|
->option('-g --package', 'Packagist name (Without vendor handle)') |
48
|
|
|
->option('-x --template', "User supplied template path\nIt has higher precedence than inbuilt templates") |
49
|
|
|
->option('-d --descr', 'Project description') |
50
|
|
|
->option('-w --keywords [words...]', 'Project Keywords') |
51
|
|
|
->option('-y --year', 'License Year', null, date('Y')) |
52
|
|
|
->option('-b --bin [binaries...]', 'Executable binaries') |
53
|
|
|
->option('-z --using', 'Reference package (should be known to composer)') |
54
|
|
|
->option('-C --config', 'JSON filepath to read config from') |
55
|
|
|
->option('-G --gh-template', "Use `.github/` as template path\nBy default uses `docs/`", null, false) |
56
|
|
|
->option('-R --req [pkgs...]', 'Required packages') |
57
|
|
|
->option('-D --dev [pkgs...]', 'Developer packages') |
58
|
|
|
->option('-t --no-travis', 'Disable travis') |
59
|
|
|
->option('-c --no-codecov', 'Disable codecov') |
60
|
|
|
->option('-s --no-scrutinizer', 'Disable scrutinizer') |
61
|
|
|
->option('-l --no-styleci', 'Disable StyleCI') |
62
|
|
|
->option('-L --license', 'License (m: MIT, g: GNULGPL, a: Apache2, b: BSDSimple, i: ISC, w: WTFPL)') |
63
|
|
|
->usage($this->writer()->colorizer()->colors( |
64
|
|
|
'' |
65
|
|
|
. '<bold> phint init</end> <line><project></end> ' |
66
|
|
|
. '<comment>--force --descr "Awesome project" --name "YourName" --email [email protected]</end><eol/>' |
67
|
|
|
. '<bold> phint init</end> <line><project></end> ' |
68
|
|
|
. '<comment>--using laravel/lumen --namespace Project/Api --type project --license m</end><eol/>' |
69
|
|
|
. '<bold> phint init</end> <line><project></end> ' |
70
|
|
|
. '<comment>--php 7.0 --config /path/to/json --dev mockery/mockery --req adhocore/cli</end><eol/>' |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Execute the command action. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function execute() |
80
|
|
|
{ |
81
|
|
|
$io = $this->app()->io(); |
82
|
|
|
|
83
|
|
|
if ($using = $this->using) { |
|
|
|
|
84
|
|
|
$io->colors("Using <cyanBold>$using</end> to create project <comment>(takes some time)</end><eol/>"); |
85
|
|
|
|
86
|
|
|
$this->_composer->createProject($this->path, $this->using); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$io->comment('Generating files ...', true); |
90
|
|
|
$this->generate($this->path, $this->values()); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$io->colors('Setting up <cyanBold>git</end><eol/>'); |
93
|
|
|
$this->_git->withWorkDir($this->path)->init()->addRemote($this->username, $this->project); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$io->colors('Setting up <cyanBold>composer</end> <comment>(takes some time)</end><eol>'); |
96
|
|
|
if ($using) { |
97
|
|
|
$this->_composer->withWorkDir($this->path)->update(); |
98
|
|
|
} else { |
99
|
|
|
$this->_composer->withWorkDir($this->path)->install(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$success = $this->_composer->successful(); |
103
|
|
|
|
104
|
|
|
$success ? $io->ok('Done', true) : $io->error('Composer setup failed', true); |
105
|
|
|
|
106
|
|
|
$this->logging('end'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function interact(Interactor $io) |
110
|
|
|
{ |
111
|
|
|
$project = $this->project; |
|
|
|
|
112
|
|
|
|
113
|
|
|
if (!\preg_match('/[a-z0-9_-]/i', $project)) { |
|
|
|
|
114
|
|
|
throw new InvalidArgumentException('Project argument should only contain [a-z0-9_-]'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$io->okBold('Phint Setup', true); |
118
|
|
|
$this->logging('start'); |
119
|
|
|
|
120
|
|
|
$this->set('path', $path = $this->prepareProjectPath()); |
121
|
|
|
$this->loadConfig($this->config); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->collectMissing($io); |
124
|
|
|
$this->collectPackages($io); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function prepareProjectPath(): string |
128
|
|
|
{ |
129
|
|
|
$path = $this->project; |
|
|
|
|
130
|
|
|
$io = $this->app()->io(); |
131
|
|
|
|
132
|
|
|
if (!$this->_pathUtil->isAbsolute($path)) { |
|
|
|
|
133
|
|
|
$path = $this->_workDir . '/' . $path; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (\is_dir($path)) { |
137
|
|
|
$this->projectExists($io, $path); |
138
|
|
|
} else { |
139
|
|
|
\mkdir($path, 0777, true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $path; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function projectExists(Interactor $io, string $path) |
146
|
|
|
{ |
147
|
|
|
if (!$this->force && !$this->sync) { |
|
|
|
|
148
|
|
|
throw new InvalidArgumentException( |
149
|
|
|
\sprintf('Something with the name "%s" already exists!', \basename($path)) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($this->force && !$this->sync) { |
154
|
|
|
$io->error('You have set --force flag, existing files will be overwritten', true); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function loadConfig(string $path = null) |
159
|
|
|
{ |
160
|
|
|
if (empty($path)) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (!$this->_pathUtil->isAbsolute($path)) { |
165
|
|
|
$path = \getcwd() . '/' . $path; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!\is_file($path)) { |
169
|
|
|
$this->app()->io()->error('Invalid path specified for config', true); |
170
|
|
|
|
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
foreach ($this->_pathUtil->readAsJson($path) as $key => $value) { |
175
|
|
|
$this->$key ?? $this->set($key, $value); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function collectMissing(Interactor $io) |
180
|
|
|
{ |
181
|
|
|
$promptConfig = [ |
182
|
|
|
'type' => [ |
183
|
|
|
'choices' => ['p' => 'project', 'l' => 'library', 'c' => 'composer-plugin'], |
184
|
|
|
'default' => 'l', |
185
|
|
|
'restore' => true, |
186
|
|
|
], |
187
|
|
|
'package' => ['default' => $this->project, 'retry' => 0], |
|
|
|
|
188
|
|
|
'license' => [ |
189
|
|
|
'choices' => ['m' => 'MIT', 'g' => 'GNULGPL', 'a' => 'Apache2', 'b' => 'BSDSimple', 'i' => 'ISC', 'w' => 'WTFPL'], |
190
|
|
|
'default' => 'm', |
191
|
|
|
], |
192
|
|
|
'php' => [ |
193
|
|
|
'choices' => ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3'], |
194
|
|
|
'default' => '7.0', |
195
|
|
|
], |
196
|
|
|
'using' => ['retry' => 0, 'extra' => ' (ENTER to skip)'], |
197
|
|
|
'keywords' => ['retry' => 0, 'extra' => ' (CSV, ENTER to skip)', 'default' => "php, {$this->project}"], |
198
|
|
|
'bin' => ['retry' => 0, 'extra' => ' (CSV, ENTER to skip)'], |
199
|
|
|
|
200
|
|
|
// Donot prompt these here! |
201
|
|
|
'req' => false, |
202
|
|
|
'dev' => false, |
203
|
|
|
'config' => false, |
204
|
|
|
'template' => false, |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
$this->promptAll($io, $promptConfig); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function collectPackages(Interactor $io) |
211
|
|
|
{ |
212
|
|
|
foreach (['req' => 'Required', 'dev' => 'Developer'] as $key => $label) { |
213
|
|
|
$pkgs = $this->$key ?: $this->promptPackages($label, $io); |
214
|
|
|
|
215
|
|
|
foreach ($pkgs as &$pkg) { |
216
|
|
|
$pkg = \strpos($pkg, ':') === false ? "{$pkg}:@stable" : $pkg; |
217
|
|
|
$pkg = \array_combine(['name', 'version'], \explode(':', $pkg, 2)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->set($key, $pkgs); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected function promptPackages(string $label, Interactor $io): array |
225
|
|
|
{ |
226
|
|
|
$pkgs = []; |
227
|
|
|
|
228
|
|
|
do { |
229
|
|
|
if (!$pkg = $io->prompt($label . ' package (ENTER to skip)', null, [$this, 'validatePackage'], 0)) { |
230
|
|
|
break; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$pkgs[] = $pkg; |
234
|
|
|
} while (true); |
235
|
|
|
|
236
|
|
|
return $pkgs; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function validatePackage(string $pkg): string |
240
|
|
|
{ |
241
|
|
|
$pkg = \trim($pkg); |
242
|
|
|
|
243
|
|
|
if ($pkg && \strpos($pkg, '/') === false) { |
244
|
|
|
throw new InvalidArgumentException( |
245
|
|
|
'Package name format should be vendor/package:version (version can be omitted)' |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $pkg; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
protected function generate(string $projectPath, array $parameters) |
253
|
|
|
{ |
254
|
|
|
$generator = new TwigGenerator($this->getTemplatePaths($parameters), $this->getCachePath()); |
255
|
|
|
|
256
|
|
|
// Normalize license (default MIT) |
257
|
|
|
$parameters['license'] = \strtolower($parameters['license'][0] ?? 'm'); |
258
|
|
|
$parameters['namespace'] = $this->makeNamespace($parameters['namespace']); |
259
|
|
|
$parameters['keywords'] = $this->makeArray($parameters['keywords'], ['php', $this->project]); |
|
|
|
|
260
|
|
|
$parameters['bin'] = $this->makeArray($parameters['bin']); |
261
|
|
|
|
262
|
|
|
$generator->generate($projectPath, $parameters, new CollisionHandler); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
protected function makeNamespace(string $value): string |
266
|
|
|
{ |
267
|
|
|
$in = new Inflector; |
268
|
|
|
|
269
|
|
|
$project = $this->package; |
|
|
|
|
270
|
|
|
$value = $in->stuldyCase(\str_replace([' ', '/'], '\\', $value)); |
271
|
|
|
$project = $in->stuldyCase(\str_replace([' ', '/', '\\'], '-', $project)); |
272
|
|
|
|
273
|
|
|
if (\stripos($value, $project) === false) { |
274
|
|
|
$value .= '\\' . $project; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
protected function makeArray($value, array $default = []): array |
281
|
|
|
{ |
282
|
|
|
if (empty($value)) { |
283
|
|
|
return $default; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if (\is_string($value)) { |
287
|
|
|
$value = \array_map('trim', \explode(',', $value)); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return \array_values(\array_unique(\array_merge($default, $value))); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|