-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclitool
executable file
·349 lines (262 loc) · 13.3 KB
/
clitool
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
346
347
348
349
#!/usr/bin/env php
<?php
// Require the core files of the system.
// Sour jelly's back bone
require('config/const.config.php');
require(CORE_PATH . 'require.php');
new \core\build\Sourjelly;
//--------------------------------------------------------------------------------------------------------------------||
//Check if Request is made from terminal/command line.
//--------------------------------------------------------------------------------------------------------------------||
if (PHP_SAPI !== 'cli') {
die('Only terminal access');
}
$ret = '';
//--------------------------------------------------------------------------------------------------------------------||
//Check if the proper arguement is set, and if it's not empty.
//Check for help request aswell.
//--------------------------------------------------------------------------------------------------------------------||
if(!isset($argv) || !isset($argv[1]) || $argv[1] == '' || $argv[1] == '-h' || $argv[1] == '--h'|| $argv[1] == '-help' || $argv[1] == '--help')
{
$ret .= "Please provide a method which this bash file should execute. Usage: " . PHP_EOL ." [--path-to-php] [path-to-bash-file] [method] " . PHP_EOL . "Methods: " . PHP_EOL . "\t install " . PHP_EOL. "\t migrate " . PHP_EOL . "\t backupdb" . PHP_EOL . "\t Generate ";
}
else
{
//--------------------------------------------------------------------------------------------------------------------||
//Let the good stuff begin.
//Require config files, and Classes.
//--------------------------------------------------------------------------------------------------------------------||
require_once('core/cron.php');
require_once('core/helpers.php');
//--------------------------------------------------------------------------------------------------------------------||
// Check which method a user wants to execute
//--------------------------------------------------------------------------------------------------------------------||
switch($argv[1])
{
case 'install':
require_once('core/build/install.class.php');
//--------------------------------------------------------------------------------------------------------------------||
// Make sure user wanted to use the install function..
//--------------------------------------------------------------------------------------------------------------------||
$line = result('Are you sure you want to do this? typ "yes" to continue');
if(trim($line) != 'yes'){
echo "ABORTING! " . PHP_EOL;
exit;
}
echo "Thank you, continuing... " . PHP_EOL;
//--------------------------------------------------------------------------------------------------------------------||
// Set all variables used in install class...
// See @result and @whileres functions for explination.
//--------------------------------------------------------------------------------------------------------------------||
$email = result('E-mail address:...');
$name = result('First name:...');
$lastname = result('Last name:...');
$dob = result('Date of birth; YYYY-mm-dd:...');
$password = result('password:...');
$password2 = result('retype password:...');
$tries = 0;
//--------------------------------------------------------------------------------------------------------------------||
// Check if passwords match, if passwords don't match, retry password match, else retry password fully. else abort.
//--------------------------------------------------------------------------------------------------------------------||
while($password2 !== $password)
{
if($tries > 1)
{
$password = result('password:...');
$password2 = result('retype password:...');
$tries = 0;
}
else
{
$password2 = result('passwords don\'t Match please try again:... ');
$tries++;
}
}
//--------------------------------------------------------------------------------------------------------------------||
// Check if migration is successfull for up to date Database..
// Check if user data is inserted correctly into database..
// Check if backup is made from new user data..
// Note that this file is only used when a new install is been made.., can't install multipul users on same database..
//--------------------------------------------------------------------------------------------------------------------||
if(\core\autoMigrate())
if($install = new \core\build\Install($email,$password,$name,$lastname,$dob))
if(\core\databaseBackup())
$ret .= "installed succesfully! your app is now intiallised on your system." . PHP_EOL . "Your username is standard your e-mail adress" . PHP_EOL . "An activation e-mail is sent to:" . $email ." please click the link in the e-mail to activate your account." . PHP_EOL;
else
$ret .= "Couldn't create backup for database.";
else
$ret .= "Something went wrong saving the user data in the database, please try again";
else
$ret .= "could not create Database, please run the script again";
break;
//--------------------------------------------------------------------------------------------------------------------||
// Update database structure
//--------------------------------------------------------------------------------------------------------------------||
case 'migrate':
if(\core\autoMigrate())
$ret .= "Migration executed succesfully";
else
$ret .= "Something went wrong migrating to the Database";
break;
//--------------------------------------------------------------------------------------------------------------------||
// Backup database with current rows in it in .sql file...
//--------------------------------------------------------------------------------------------------------------------||
case 'backupdb':
if(\core\databaseBackup())
$ret .= "Backup made successfully";
else
$ret .= "Something went wrong createing Database Backup.";
break;
//--------------------------------------------------------------------------------------------------------------------||
// create controllers models and and view files with predefined functinos in the controller
//--------------------------------------------------------------------------------------------------------------------||
case 'generate':
require_once(BUILD_PATH . 'template.class.php');
$values = $argv;
$functions = '';
$functionTmp = \core\build\Template::getSnippet('functions.tpl','cli');
$classTmp = \core\build\Template::getSnippet('class.tpl','cli');
array_shift($values);
array_shift($values);
$controller = ucfirst($values[0]);
$classname = ucfirst(substr($values[0],0,-1));
array_shift($values);
$modelTmp = str_replace(
array('{classname}','{functions}','{namespace}','{extends}'),
array($classname,'','models','Model'),
$classTmp
);
$controllerTmp = str_replace(
array('{classname}','{namespace}','{extends}'),
array($controller,'controllers','Controller'),
$classTmp
);
echo 'Writing model..' . PHP_EOL;
sleep(1);
$modelHandle = fopen('models' . DIRECTORY_SEPARATOR . strtolower($classname) . '.class.php','w');
if(fwrite($modelHandle,$modelTmp))
echo 'Model ' . $classname .' created...' . PHP_EOL . 'Writing controller' . PHP_EOL;
else
die('Something went wrong creating the files.. aborting..');
fclose($modelHandle);
sleep(1);
foreach($values as $value)
$functions .= str_replace('{functionname}', $value , $functionTmp);
$controllerTmp = str_replace('{functions}',$functions,$controllerTmp);
$controllerHandle = fopen('controllers' . DIRECTORY_SEPARATOR . strtolower($controller) . '.class.php', 'w');
if(fwrite($controllerHandle,$controllerTmp))
echo 'Controller Created...' . PHP_EOL . 'Creating view folder' . PHP_EOL;
else
die('Something went wrong creating the files.. aborting..');
fclose($controllerHandle);
sleep(1);
if(!is_dir('views' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . strtolower($classname)))
if(mkdir('views' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . strtolower($classname)))
echo 'View folder ' . $classname . ' created..' . PHP_EOL . 'Creating view index' . PHP_EOL;
else
die('Something went wrong creating the files.. aborting..');
//--------------------------------------------------------------------------------------------------------------------||
// Sleep before Fopen -> Otherwhise code creates a file when the folder is not yet existing.
//--------------------------------------------------------------------------------------------------------------------||
sleep(1);
$viewIndex = fopen('views' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . strtolower($classname) . DIRECTORY_SEPARATOR . 'index.html.tpl','w');
fwrite($viewIndex,'');
fclose($viewIndex);
echo 'View file created';
sleep(1);
$ret .= 'Files generated succesfully!';
break;
case 'creator':
switch(trim(strtolower($argv[2])))
{
case 'newesil':
case 'kevin':
case 'kevinnewesil':
echo file_get_contents('views' . DIRECTORY_SEPARATOR . 'snippets' . DIRECTORY_SEPARATOR . '_EN' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'extra' . DIRECTORY_SEPARATOR . 'kevin.ascii');
break;
case 'hall':
case 'vanhall':
case 'alain':
case 'alainhall':
case 'alainvanhall':
case 'ajcvhall':
echo file_get_contents('views' . DIRECTORY_SEPARATOR . 'snippets' . DIRECTORY_SEPARATOR . '_EN' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'extra' . DIRECTORY_SEPARATOR . 'alain.ascii');
break;
default:
echo file_get_contents('views' . DIRECTORY_SEPARATOR . 'snippets' . DIRECTORY_SEPARATOR . '_EN' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'extra' . DIRECTORY_SEPARATOR . 'default.ascii');
break;
}
break;
case 'test':
echo 'Checking for argv... ';
if(!isset($argv[2]) || $argv[2] == '')
{
echo \core\colourString("No test location provided","red");
break;
}
echo \core\colourString("Argv's loaded ","green") . PHP_EOL . 'Checking if test file exists... ';
if(!file_exists('tests/' . $argv[2] . '_Tests.php'))
{
echo \core\colourString("Test file not found","red");
break;
}
require('tests/' . $argv[2] . '_Tests.php');
echo \core\colourString('File loaded ','green') . PHP_EOL . 'Checking if test class exists... ';
$className = "\\tests\\" . ucfirst($argv[2] . '_Tests');
if(!class_exists($className))
{
echo \core\colourString("Test class could not be loaded","red");
break;
}
echo \core\colourString("Test class exists","green") . PHP_EOL . 'Starting up test envoiroment... ';
if(!$class = new $className)
{
echo \core\colourString("Test class could not be called.","red");
break;
}
echo \core\colourString("Test class initialised","green") . PHP_EOL;
echo PHP_EOL . \core\colourString("Loading test class: " . ucfirst($argv[2]) . "... " ,"blue") . PHP_EOL;
$class -> start();
break;
//--------------------------------------------------------------------------------------------------------------------||
// If the method does not exists, give error message to the user.
//--------------------------------------------------------------------------------------------------------------------||
default:
$ret .= "Please provide a method which this bash file should execute. Usage: " . PHP_EOL ." [--path-to-php] [path-to-bash-file] [method] " . PHP_EOL . "Methods: " . PHP_EOL . "\t install " . PHP_EOL. "\t migrate " . PHP_EOL . "\t backupdb";
break;
}
}
$ret .= PHP_EOL;
//--------------------------------------------------------------------------------------------------------------------||
// Functions result , opens a handle, and reads input user put into CLI
//--------------------------------------------------------------------------------------------------------------------||
function result($message)
{
echo $message . PHP_EOL;
$handle = fopen ("php://stdin","r");
$input = trim(fgets($handle));
return whileRes($input);
}
//--------------------------------------------------------------------------------------------------------------------||
// Loops result so that you can't have empty result, or stops entire script after to many tries
//--------------------------------------------------------------------------------------------------------------------||
function whileRes($input){
$tries = 0;
while(trim($input) == '')
{
if($tries > 1)
{
echo "to many invalid tries.. Aborting" . PHP_EOL;
exit();
}
echo 'invalid input try again:..' . PHP_EOL;
$handle = fopen ("php://stdin","r");
$input = trim(fgets($handle));
$tries++;
}
return $input;
}
//--------------------------------------------------------------------------------------------------------------------||
//Show the result of the bash to the 'user', end stop the script fully...
//--------------------------------------------------------------------------------------------------------------------||
die($ret);