|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hidev |
|
6
|
|
|
* @package hidev |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hidev\console; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use hidev\base\File; |
|
15
|
|
|
use hidev\helpers\Helper; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\base\InvalidParamException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Init controller. |
|
21
|
|
|
* Builds .hidev/config.yml by template and params. |
|
22
|
|
|
*/ |
|
23
|
|
|
class InitController extends \yii\console\Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string package full name like: vendor/package |
|
27
|
|
|
*/ |
|
28
|
|
|
public $name; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string package type e.g.: package, yii2-extension |
|
32
|
|
|
*/ |
|
33
|
|
|
public $type; |
|
34
|
|
|
|
|
35
|
|
|
public $title; |
|
36
|
|
|
public $package; |
|
37
|
|
|
public $license; |
|
38
|
|
|
public $headline; |
|
39
|
|
|
public $keywords; |
|
40
|
|
|
public $namespace; |
|
41
|
|
|
public $description; |
|
42
|
|
|
|
|
43
|
|
|
public $vendor; |
|
44
|
|
|
public $nick; |
|
45
|
|
|
public $author; |
|
46
|
|
|
public $email; |
|
47
|
|
|
|
|
48
|
|
|
public $vendorRequire; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool don't put vendor config |
|
52
|
|
|
*/ |
|
53
|
|
|
public $noVendor; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var bool don't put requires |
|
56
|
|
|
*/ |
|
57
|
|
|
public $noRequire; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var bool don't generate `composer.json` file |
|
61
|
|
|
*/ |
|
62
|
|
|
public $noComposer; |
|
63
|
|
|
|
|
64
|
3 |
|
public function options($actionId) |
|
65
|
|
|
{ |
|
66
|
3 |
|
return array_diff( |
|
67
|
3 |
|
array_keys(get_object_vars($this)), |
|
68
|
3 |
|
explode(',', 'color,help,id,module,layout,action,interactive,defaultAction') |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
public function prepareData($name) |
|
73
|
|
|
{ |
|
74
|
3 |
|
$this->name = $name; |
|
75
|
3 |
|
list($vendor, $package) = explode('/', $name, 2); |
|
76
|
3 |
|
if ($vendor) { |
|
77
|
3 |
|
$this->vendor = $vendor; |
|
78
|
3 |
|
$vendorPlugin = "$vendor/hidev-$vendor"; |
|
79
|
|
|
try { |
|
80
|
3 |
|
$exists = @file_get_contents("https://packagist.org/packages/$vendorPlugin.json"); |
|
81
|
|
|
} catch (Exception $e) { |
|
82
|
|
|
$exists = false; |
|
83
|
|
|
} |
|
84
|
3 |
|
if ($exists) { |
|
85
|
1 |
|
$this->vendorRequire = $vendorPlugin; |
|
86
|
1 |
|
$this->noVendor = true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
3 |
|
if ($package) { |
|
90
|
3 |
|
$this->package = $package; |
|
91
|
|
|
} |
|
92
|
3 |
|
if (!$this->package || !$this->vendor) { |
|
93
|
|
|
throw new InvalidParamException('Wrong vendor/package given: ' . $name); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
3 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Creates initial configuration for hidev: `hidev.yml` and `composer.json`. |
|
99
|
|
|
* @param string $name package full name like: vendor/package |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function actionIndex($name = null) |
|
102
|
|
|
{ |
|
103
|
3 |
|
$this->prepareData($name); |
|
104
|
3 |
|
if (!$this->noComposer) { |
|
105
|
2 |
|
$this->writeComposer(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
3 |
|
return $this->writeConfig(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function actionComposer() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->writeComposer(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
public function writeConfig($path = 'hidev.yml') |
|
117
|
|
|
{ |
|
118
|
3 |
|
$file = new File(['path' => $path]); |
|
119
|
3 |
|
$file->save(array_filter([ |
|
120
|
3 |
|
'package' => array_filter([ |
|
121
|
3 |
|
'type' => $this->getType(), |
|
122
|
3 |
|
'name' => $this->package, |
|
123
|
3 |
|
'title' => $this->getTitle(), |
|
124
|
3 |
|
'license' => $this->license, |
|
125
|
3 |
|
'headline' => $this->headline, |
|
126
|
3 |
|
'keywords' => $this->getKeywords(), |
|
127
|
3 |
|
'namespace' => $this->namespace, |
|
128
|
3 |
|
'description' => $this->description, |
|
129
|
|
|
]), |
|
130
|
3 |
|
'vendor' => $this->noVendor ? null : array_filter([ |
|
131
|
1 |
|
'name' => $this->vendor, |
|
132
|
1 |
|
'authors' => array_filter([ |
|
133
|
1 |
|
$this->getNick() => [ |
|
134
|
1 |
|
'name' => $this->getAuthor(), |
|
135
|
3 |
|
'email' => $this->getEmail(), |
|
136
|
|
|
], |
|
137
|
|
|
]), |
|
138
|
|
|
]), |
|
139
|
|
|
])); |
|
140
|
3 |
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
public function writeComposer($path = 'composer.json') |
|
143
|
|
|
{ |
|
144
|
2 |
|
$file = new File(['path' => $path]); |
|
145
|
2 |
|
$file->save(array_filter([ |
|
146
|
2 |
|
'name' => $this->name, |
|
147
|
2 |
|
'type' => $this->getType(), |
|
148
|
2 |
|
'description' => $this->getTitle(), |
|
149
|
2 |
|
'keywords' => preg_split('/\s*,\s*/', $this->getKeywords()), |
|
150
|
2 |
|
'require-dev' => $this->getPlugins(), |
|
151
|
2 |
|
'license' => $this->license, |
|
152
|
|
|
])); |
|
153
|
2 |
|
} |
|
154
|
|
|
|
|
155
|
3 |
|
public function getType() |
|
156
|
|
|
{ |
|
157
|
3 |
|
return $this->type ?: 'project'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
3 |
|
public function getTitle() |
|
161
|
|
|
{ |
|
162
|
3 |
|
return $this->title ?: Helper::titleize($this->package); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
3 |
|
public function getKeywords() |
|
166
|
|
|
{ |
|
167
|
3 |
|
return $this->keywords ?: implode(', ', explode('-', $this->package)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/// TODO think of better getting nick |
|
171
|
1 |
|
public function getNick() |
|
172
|
|
|
{ |
|
173
|
1 |
|
return $this->nick ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
public function getAuthor() |
|
177
|
|
|
{ |
|
178
|
1 |
|
return $this->author ?: Yii::$app->get('vcs')->getUserName(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
public function getEmail() |
|
182
|
|
|
{ |
|
183
|
1 |
|
return $this->email ?: Yii::$app->get('vcs')->getUserEmail(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns list of plugins in composer requirements format: name => version. |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
2 |
|
public function getPlugins() |
|
191
|
|
|
{ |
|
192
|
2 |
|
if ($this->noRequire) { |
|
193
|
|
|
return []; |
|
194
|
|
|
} |
|
195
|
|
|
$res = [ |
|
196
|
2 |
|
'hiqdev/hidev-php' => 'dev-master', |
|
197
|
|
|
]; |
|
198
|
2 |
|
if ($this->vendorRequire) { |
|
199
|
1 |
|
$res[$this->vendorRequire] = 'dev-master'; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
2 |
|
return $res; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|