This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
/
Init.php
345 lines (313 loc) · 10.8 KB
/
Init.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
namespace Jumpstart;
use Composer\Script\Event;
use Composer\Json\JsonFile;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
/**
* Class Init
* @package Jumpstart
*/
class Init
{
protected static $filePattern = '/^.+\.php$/';
/**
* Publish th plugin to the WordPress plugin repository
* @param Event $event
*/
public static function jumpstart(Event $event)
{
// get information
$info = static::questionnaire($event);
// update file headers
$files = new \AppendIterator();
$files->append(
new RegexIterator(
new RecursiveDirectoryIterator('.'),
static::$filePattern,
RegexIterator::GET_MATCH
)
);
$files->append(
new RegexIterator(
new RecursiveIteratorIterator(new RecursiveDirectoryIterator('src')),
static::$filePattern,
RegexIterator::GET_MATCH
)
);
static::updateFiles($event, $files, $info);
// create composer.json
static::mergeConfig($event, $info);
}
/**
* Merge the current config with the updated data
* @param Event $event
* @param array $info
*/
public static function mergeConfig(Event $event, array $info)
{
// start
$io = $event->getIO();
$options = array(
"name" => $info['package'],
"description" => $info['description'],
"license" => $info['license'],
"keywords" => array(),
"type" => "wordpress-plugin",
"authors" => array(
array(
"name" => $info['author'],
"email" => $info['author_email']
) + (!empty($info['author_url']) ? array("homepage" => $info['author_url']) : array())
),
"scripts" => array(
"publish" => "Jumpstart\\Deploy::publish"
),
"minimum-stability" => "dev",
"prefer-stable" => true,
"require" => array(
"composer/installers" => "~1.0",
"mrgrain/jumpstart-battery" => "~1.0"
),
"require-dev" => array(
"mrgrain/jumpstart-deploy" => "@stable"
),
"autoload" => array(
"psr-4" => array(
$info['namespace'] . "\\" => "src/"
)
)
);
$file = new JsonFile('composer.json');
$json = $file->encode($options);
$writeFile = true;
$io->write('Jumpstart has generated a composer.json file for you.');
if ($io->askConfirmation('Do you want to check it before writing to disk? [yes]' . "\t")) {
$writeFile = false;
$io->write($json);
if ($io->askConfirmation('Write the generated composer.json to disk? [yes]' . "\t")) {
$writeFile = true;
}
}
if ($writeFile) {
$file->write($options);
}
}
/**
* Update the boilerplate files with the additional information
* @param Event $event
* @param $files
* @param array $info
*/
public static function updateFiles(Event $event, $files, array $info)
{
// start
$io = $event->getIO();
if ($io->askConfirmation('Jumpstart will now update your files with the provided information. Okay? [yes]:' . "\t")) {
foreach ($files as $file) {
$file = array_pop($file);
$io->write("\t" . $file . "\t", false);
$io->write(static::updateFile($file, $info) ? 'OK' : 'Error');
}
if (file_exists('plugin.php')) {
rename('plugin.php', $info['plugin_slug'] . '.php');
}
$io->write('');
}
}
public static function updateFile($file, array $info)
{
$content = file_get_contents($file);
// headers
foreach ($info as $key => $value) {
$content = str_replace("{{" . $key . "}}", $value, $content, $num);
}
// namespace
$content = str_replace('namespace Vendor\Plugin', 'namespace ' . $info['namespace'], $content);
$content = str_replace('use Vendor\Plugin', 'use ' . $info['namespace'], $content);
return (false !== file_put_contents($file, $content));
}
/**
* The plugin data storage
*/
protected static $package;
protected static $namespace;
protected static $plugin_name;
protected static $plugin_slug;
protected static $version;
protected static $description;
protected static $url;
protected static $author;
protected static $author_email;
protected static $author_url;
protected static $license;
/**
* Collect information about the plugin
* @param Event $event
* @return array
*/
public static function questionnaire(Event $event)
{
// start
$io = $event->getIO();
$io->write(array(
PHP_EOL . PHP_EOL,
'Welcome to the Jumpstart Plugin generator',
PHP_EOL . PHP_EOL . PHP_EOL,
'This command will guide you through creating your basic plugin setup.'
));
// Collect data
static::$package = $io->askAndValidate(
'Package (<vendor>/<name>):' . "\t",
'Jumpstart\\Init::validatePackage'
);
static::$namespace = $io->askAndValidate(
'PHP namespace (<vendor>\<name>) [' . static::suggestNamespace(static::$package) . ']:' . "\t",
'Jumpstart\\Init::validateNamespace',
null,
static::suggestNamespace(static::$package)
);
static::$plugin_name = $io->ask(
'Plugin Name [' . static::suggestPluginName(static::$package) . ']:' . "\t",
static::suggestPluginName(static::$package)
);
static::$plugin_slug = $io->askAndValidate(
'Plugin Slug [' . static::suggestPluginSlug(static::$plugin_name) . ']:' . "\t",
'Jumpstart\\Init::validateSlug',
null,
static::suggestPluginSlug(static::$plugin_name)
);
static::$version = $io->askAndValidate(
'Version (x.x.x) [1.0.0]:' . "\t",
'Jumpstart\\Init::validateVersion',
null,
'1.0.0'
);
static::$description = $io->askAndValidate(
'Description []:' . "\t",
'Jumpstart\\Init::validateDescription',
null,
''
);
static::$url = $io->askAndValidate(
'URL []:' . "\t",
'Jumpstart\\Init::validateURL'
);
static::$author = $io->ask(
'Author [' . static::suggestAuthor() . ']:' . "\t",
static::suggestAuthor()
);
static::$author_email = $io->ask(
'Author Email [' . static::suggestAuthorEmail() . ']:' . "\t",
static::suggestAuthorEmail()
);
static::$author_url = $io->askAndValidate(
'Author URL []:' . "\t",
'Jumpstart\\Init::validateURL'
);
static::$license = $io->ask(
'License [' . static::suggestLicense() . ']:' . "\t",
static::suggestLicense()
);
return array(
'package' => static::$package,
'namespace' => static::$namespace,
'plugin_name' => static::$plugin_name,
'plugin_slug' => static::$plugin_slug,
'version' => static::$version,
'description' => static::$description,
'url' => static::$url,
'author' => static::$author,
'author_email' => static::$author_email,
'author_url' => static::$author_url,
'license' => static::$license,
);
}
public static function validateNotEmpty($value)
{
if (strlen(trim($value)) <= 0) {
throw new \Exception('The value must not be empty.');
}
return $value;
}
public static function validatePackage($value)
{
if (1 !== preg_match("~^[a-z0-9_.-]+/[a-z0-9_.-]+$~", $value)) {
throw new \Exception('Package name must only be in lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+');
}
return $value;
}
public static function validateNamespace($value)
{
if (1 !== preg_match("~^[a-zA-Z0-9-_]+\\\\[a-zA-Z0-9-_]+$~", $value)) {
throw new \Exception('Namespace must have a vendor name, a backward slash, and a package name, matching: [a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+');
}
return $value;
}
public static function validateSlug($value)
{
if (1 !== preg_match("~^[a-z0-9-_]+$~", $value)) {
throw new \Exception('Slug must must only be in lowercase, matching: [a-z0-9-_]+');
}
return $value;
}
public static function validateVersion($value)
{
if (1 !== preg_match("~^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-z0-9-_]+)?$~", $value)) {
throw new \Exception('Version must only be in the format <x.x.x>, possible followed by a hyphen and a pre-release suffix, e.g. 1.0.0-alpha');
}
return $value;
}
public static function validateDescription($value)
{
if (strlen($value) > 150) {
throw new \Exception('Description must only have 150 characters');
}
return $value;
}
public static function validateURL($value)
{
if (empty($value)) {
return '';
}
if (false === strpos($value, 'http://') && false === strpos($value, 'https://')) {
throw new \Exception('Please enter a valid URL');
}
return $value;
}
public static function suggestNamespace($value)
{
$vendor = strtok($value, "/");
$package = strtok("/");
return ucfirst($vendor) . "\\" . ucfirst($package);
}
public static function suggestPluginName($value)
{
return ucfirst(substr($value, strpos($value, "/") + 1));
}
public static function suggestPluginSlug($value)
{
return strtolower(preg_replace("/[^A-Za-z0-9_]/", "", str_replace(" ", "_", $value)));
}
public static function suggestAuthor()
{
$author = `git config --global user.name`;
if (!isset($author) || false === $author || empty($author)) {
return '';
}
return trim($author);
}
public static function suggestAuthorEmail()
{
$email = `git config --global user.email`;
if (!isset($email) || false === $email || empty($email)) {
return '';
}
return trim($email);
}
public static function suggestLicense()
{
return 'GPL-3.0+';
}
}