/
GenerateExtensionCommand.php
125 lines (100 loc) · 3.54 KB
/
GenerateExtensionCommand.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
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Console\Command;
use Flarum\Foundation\Application;
use Symfony\Component\Console\Question\Question;
class GenerateExtensionCommand extends AbstractCommand
{
protected function configure()
{
$this
->setName('generate:extension')
->setDescription('Generate a Flarum extension skeleton');
}
/**
* Execute the console command.
*
* @return mixed
*/
protected function fire()
{
do {
$name = $this->ask('Extension name:');
} while (! preg_match('/^([a-z0-9-]+)$/i', $name));
do {
$namespace = $this->ask('Namespace:');
} while (! preg_match('/^([a-z0-9_\\\\]+)$/i', $namespace));
do {
$title = $this->ask('Title:');
} while (! $title);
$description = $this->ask('Description:');
$authorName = $this->ask('Author name:');
$authorEmail = $this->ask('Author email:');
$license = $this->ask('License:');
$this->info('Generating extension skeleton for "'.$name.'"...');
$dir = public_path().'/extensions/'.$name;
$replacements = [
'{{namespace}}' => $namespace,
'{{escapedNamespace}}' => str_replace('\\', '\\\\', $namespace),
'{{name}}' => $name
];
$this->copyStub($dir, $replacements);
$manifest = [
'name' => $name,
'title' => $title,
'description' => $description,
'keywords' => [],
'version' => '0.1.0',
'author' => [
'name' => $authorName,
'email' => $authorEmail,
'homepage' => ''
],
'license' => $license,
'require' => [
'flarum' => '>='.Application::VERSION
],
'icon' => [
'name' => '',
'backgroundColor' => '',
'color' => ''
]
];
file_put_contents($dir.'/flarum.json', json_encode($manifest, JSON_PRETTY_PRINT));
passthru("cd $dir; composer install; cd js/forum; npm install; gulp; cd ../admin; npm install; gulp");
$this->info('Extension "'.$name.'" generated!');
}
protected function ask($question, $default = null)
{
$question = new Question("<question>$question</question> ", $default);
return $this->getHelperSet()->get('question')->ask($this->input, $this->output, $question);
}
protected function copyStub($destination, $replacements = [])
{
$this->recursiveCopy(__DIR__.'/../../../stubs/extension', $destination, $replacements);
}
protected function recursiveCopy($src, $dst, $replacements = [])
{
$dir = opendir($src);
@mkdir($dst);
while (($file = readdir($dir)) !== false) {
if ($file != '.' && $file != '..') {
if (is_dir($src.'/'.$file)) {
$this->recursiveCopy($src.'/'.$file, $dst.'/'.$file, $replacements);
} else {
$contents = file_get_contents($src.'/'.$file);
$contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
file_put_contents($dst.'/'.$file, $contents);
}
}
}
closedir($dir);
}
}