-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.php
314 lines (270 loc) · 8.64 KB
/
module.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
<?php
if (!@include_once __DIR__ . "/vendor/autoload.php") {
echo 'You need to run `composer install` first.';
exit;
}
use League\CLImate\CLImate;
$climate = new CLImate();
$dockerBuildDir = './resources/docker/build/';
$editableFiles = [
'docker' => [
'./docker-compose.yml',
$dockerBuildDir . 'nginx/default.conf',
],
'xdebug' =>
$dockerBuildDir . 'php-fpm/.env.example',
'laravel' => [],
'vue' => [],
];
$placeHolders = [
'name' => 'module_[MODULE_NAME]',
'port' => '${MODULE_PORT}:80 #WEB_PORT',
];
global $climate;
// Entrypoint.
init($climate);
/**
* Define the callable functions that are available
*
* the key should resolve to a function in this file
* the title is what get's displayed to the user
*
* @param CLImate $climate
* @return void
*/
function init(CLImate $climate)
{
// Draw the mallcomm picture.
$climate->addArt('./resources/terminal/')
->animation('banner')
->enterFrom('bottom');
$input = $climate->radio('What would you like to do?: ', [
'setupModule' => 'Setup Module',
'developModule' => 'Develop Module',
'deploy' => 'Deploy Module',
'listModules' => 'List Modules', // Use CLImate table
]);
$response = $input->prompt();
callFunction($climate, $response);
}
/**
* Try resolve the response to a function
* exit out with an error if there is no matching function
*
* @param CLImate $climate
* @param $response
*/
function callFunction(CLImate $climate, $response)
{
if (function_exists($response)) {
$climate->clear();
$response($climate);
} else {
$climate->lightRed('Not Setup Yet.');
exit(1);
}
}
/**
* Setup or update a module by following the prompted questions.
*
* @param CLImate $climate
* @return void
*/
function setupModule(CLImate $climate)
{
global $editableFiles, $placeHolders;
$options = [
'name' => [ // Select a region for the app
'type' => 'text',
'text' => 'Module a name<red>*</red>',
'required' => true,
],
'port' => [ // the name of the whitelabel
'type' => 'text',
'text' => 'Module web port',
'default' => '8080',
],
'xdebug' => [
'type' => 'text',
'text' => 'Enable XDebug (y/n)?',
'default' => 'y',
],
];
$climate->blue("Alright, new Module. Let's get started...")->br();
$values = ask_questions($climate, $options);
setupXDebug($climate, $values);
try {
if (($currentValues = @file_get_contents('./module.json')) && null !== $currentValues) {
$currentValues = json_decode($currentValues, true);
$placeHolders['name'] = 'module_' . $currentValues['name'];
$placeHolders['port'] = $currentValues['port'] . ':80 #WEB_PORT';
}
foreach ($editableFiles['docker'] as $file) {
$content = file_get_contents($file);
// Assuming case-sensitive search and replace here, as per the `sed` used.
$content = str_replace($placeHolders['name'], 'module_' . $values['name'], $content);
if (strpos($file, 'docker-compose') !== false) {
$content = str_replace($placeHolders['port'], $values['port'] . ':80 #WEB_PORT', $content);
}
if (in_array($values['xdebug'], ['n', 'N', 'no', 'NO'])) {
$content = str_replace('WITH_XDEBUG=true', 'WITH_XDEBUG=false', $content);
}
if (!file_put_contents($file, $content)) {
$climate->error('Could not to write to file ' . $file);
exit(1);
}
if (!file_put_contents('./module.json', json_encode($values))) {
$climate->error('Could to write to file module.json');
exit(1);
}
}
} catch (Exception $e) {
$climate->error($e->getMessage());
exit(1);
}
$climate->br()->lightGreen('Module setup complete.');
}
/**
* Commands for module development.
*
* @param CLImate $climate
* @return void
*/
function developModule(CLImate $climate)
{
if (!@file_get_contents('./module.json')) {
$climate->error('Please run module setup before running the development options.');
exit(1);
}
$input = $climate->radio('What would you like to do?: ', [
'up' => 'Start Container', // Maybe check the module port has been saved?
'down' => 'Stop Container',
'watch' => 'Watch',
'hot' => 'Hot',
'build' => 'Build',
]);
$response = $input->prompt();
switch ($response) {
case 'up':
exec('docker-compose up -d');
break;
case 'down':
exec('docker-compose down');
break;
case 'watch':
exec('npm run dev');
break;
case 'hot':
exec('npm run hot');
break;
case 'build':
$input = $climate->radio('What environment do you want to build for?: ', [
'dev' => 'Development',
'test' => 'Testing',
'prod' => 'Production',
]);
$env = $input->prompt();
exec('npm run ' . $env);
break;
}
}
/**
* Setup PHP container .env XDebug values.
*
* @param CLImate $climate
* @param array $values
* @return void
*/
function setupXDebug(CLImate $climate, array $values)
{
global $editableFiles;
$xdebugQuestions = [
'serverName' => [
'type' => 'text',
'text' => "Server Name",
'default' => 'local.dev',
],
'hostIP' => [
'type' => 'text',
'text' => "Host IP (output from ifconfig/ipconfig)",
'required' => true,
]
];
if (!in_array($values['xdebug'], ['y', 'Y', 'YES'])) {
return;
}
$values = ask_questions($climate, $xdebugQuestions);
$content = file_get_contents($editableFiles['xdebug']);
// Assuming case-sensitive search and replace here, as per the `sed` used.
$content = str_replace('{{ SERVER_NAME }}', $values['serverName'], $content);
$content = str_replace('{{ HOST_IP }}', $values['hostIP'], $content);
if (!file_put_contents(rtrim($editableFiles['xdebug'], '.example'), $content)) {
$climate->error('Could not to write to file ' . $editableFiles['xdebug']);
exit(1);
}
}
/**
* Makes a value in the questions function required by re prompting the answer
* returns the value the user entered
*
* @param $climate
* @param $item
* @param $value
* @return mixed
*/
function required_field($climate, $item, $value)
{
if (empty($value)) {
$input = $climate->input($item['text']. ": <red>Required</red>");
$value = $input->prompt();
$value = required_field($climate, $item, $value);
}
return $value;
}
/**
* Asks user a list of questions and returns an array of answers.
*
* The questions array can contina the following values for each question
*
* @param $climate
* @param $questions
* @return array
* type: text|boolean|choose text allows user to input text, boolean allows users to
* choose between yes and no, choose is an array of options for the user to select from
*
* text: the text that gets showed to the users
*
* default: the default value for the field
*
* required: true|false whether the values is required or not, currently only works for text
*
* options: [] used only for choose field types key value select options
*/
function ask_questions($climate, $questions)
{
$values = [];
foreach ($questions as $key => $question) {
// Display question.
$text = ($question['default'] !== null) ? $question['text'] . ' ('.$question['default']. ') ': $question['text'];
$value = null;
// Handle the boolean question type.
if ($question['type'] == 'boolean') {
$input = $climate->confirm($text . ": ");
$value = $input->confirmed();
}
// Hangle the select type.
if ($question['type'] == 'choose') {
$input = $climate->radio($text . ": ", $question['options']);
$value = $input->prompt();
}
// Handle the text response question type.
if ($question['type'] == 'text') {
$input = $climate->input($text . ": ");
$value = $input->prompt();
$value = (isset($question['required']) && $question['required'] == true) ? required_field($climate, $question, $value) : $value;
$value = (empty($value)) ? $question['default'] : $value;
}
$values[$key] = $value;
}
return $values;
}