-
Notifications
You must be signed in to change notification settings - Fork 1
/
wptools
120 lines (102 loc) · 3.38 KB
/
wptools
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
#!/usr/bin/env php
<?php
unset( $argv[0] );
parse_str( implode( '&', $argv ), $_REQUEST );
$args = array_keys( $_REQUEST );
$env = [];
if ( is_file( '.env' ) ) {
$envFile = file_get_contents( '.env' );
$lines = explode( "\n", $envFile );
foreach ( $lines as $line ) {
if ( strlen( $line ) == 0 || $line[0] == '#' ) {
continue;
}
$line = explode( '=', $line );
if ( count( $line ) == 2 ) {
if ( is_numeric( $line[0] ) ) {
continue;
}
$env[ strtoupper( trim( $line[0] ) ) ] = trim( str_replace( "'", '', $line[1] ) );
}
}
if ( ! isset( $env['WORDPRESSTOOLS_DIR'] ) ) {
echo( "\033[31mERROR!: Please set the WORDPRESSTOOLS_DIR in the .env file\n" );
exit();
}
$wp_tools_dir = ltrim( $env['WORDPRESSTOOLS_DIR'], '/' );
if ( ! is_dir( $wp_tools_dir ) ) {
echo( "\033[31mERROR!: $wp_tools_dir is not a valid directory\n" );
exit();
}
$wp_tools_scripts_dir = $wp_tools_dir . '/scripts';
if ( ! is_dir( $wp_tools_scripts_dir ) ) {
echo( "\033[31mERROR!: $wp_tools_dir is not a valid WordPress Tools directory\n" );
exit();
}
if ( ! isset( $env['NAMESPACE'] ) ) {
echo( "\033[31mERROR!: Please set the NAMESPACE in .env file\n" );
exit();
}
if ( trim( $env['NAMESPACE'] ) == '' ) {
echo( "\033[31mERROR!: Please set the NAMESPACE in .env file\n" );
exit();
}
require $wp_tools_scripts_dir . '/migrations.php';
require $wp_tools_scripts_dir . '/models.php';
} else {
echo( "\033[31mERROR!: No .env file found\n" );
exit();
}
function clean_input( $string ) {
$string = str_replace( ' ', '_', $string ); // Replaces all spaces with hyphens.
$string = str_replace( '-', '_', $string ); // Replaces all spaces with hyphens.
$string = preg_replace( '/[^A-Za-z0-9\-]/', '_', $string ); // Removes special chars.
return preg_replace( '/_+/', '_', $string ); // Replaces multiple hyphens with single one.
}
function show_help_text(): void {
$help = "\033[1mInvalid command! Usage php wptools [command]\nSupported commands: \033[0m\n" .
"\033[32m\033[1mmake:migration \033[0m\033[39m=> Create a new migration file in the migrations folder. Example: php wptools make:migration add_is_creator_to_users_table\n" .
"\033[32m\033[1mmake:model \033[0m\033[39m=> Creates a new model in the models directory. Example: php wptools make:model User\n";
echo $help;
}
echo "----------------------------------------------------\n";
echo "\033[32m\033[1m|||||| WordPress Tools Command Line Interface ||||||\033[0m\033[39m\n";
echo "----------------------------------------------------\n";
if ( sizeof( $args ) == 0 ) {
show_help_text();
exit();
}
$plugin_root_dir = dirname( __FILE__ );
switch ( $args[0] ) {
case 'make:migration':
if ( sizeof( $args ) == 1 ) {
echo( "\033[31mERROR!: No migration name passed\n" );
exit();
}
$migration_name = $args[1];
$table_name = '';
$is_update = false;
if ( isset( $_REQUEST['--table'] ) ) {
$table_name = $_REQUEST['--table'];
}
if ( isset( $_REQUEST['--update'] ) ) {
$is_update = true;
}
make_migration( $migration_name, $table_name, $is_update );
break;
case 'make:model':
if ( sizeof( $args ) == 1 ) {
echo( "\033[31mERROR!: No model name passed\n" );
exit();
}
$model_name = $args[1];
$table_name = '';
if ( isset( $_REQUEST['--table'] ) ) {
$table_name = $_REQUEST['--table'];
}
make_model( $model_name, $table_name );
break;
default:
show_help_text();
break;
}