-
Notifications
You must be signed in to change notification settings - Fork 0
/
mason
executable file
·348 lines (263 loc) · 8.2 KB
/
mason
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
#!/usr/bin/env perl
use common::sense;
use experimental 'smartmatch';
use Mustache::Simple;
use Path::Tiny qw/path/;
use DBI;
use Data::Dmp;
# ===================================================================
# = INITIALIZATION =
# ===================================================================
# lets get the arguements passed
my @args = @ARGV;
usage() if ( scalar( @args ) < 1 || grep( /^help$/i, @args ) );
# ===================================================================
# = CONFIG =
# ===================================================================
my ( $base, $command, @params ) = ( path($0)->parent->absolute, shift( @args), @args );
my ( $docroot , $dbroot, $mason, $tache ) = (
$base->child( ( scalar( @params ) > 1 ) ? $params[1] : 'public' ),
$base->child('db'),
$base->child('.mason'),
Mustache::Simple->new()
);
# ===================================================================
# = DASHBOARD. =
# ===================================================================
given ($command) {
# DB
when( /^db:make$/ ) {
database( @params );
}
when( /^db:truncate$/ ) {
refresh( @params );
}
# Migrate
when( /^migration:make$/ ) {
sqlfile( @params );
}
when( /^migration:run$/ ) {
migrate( @params );
}
# Console
when( /^console:make$/ ) {
console( @params );
}
# Route
when( /^route:make$/ ) {
route( @params );
}
when( /^route:silence$/ ) {
silence( @params);
}
default {
usage();
}
}
#--------------------------------------------------------------------------#
# Helper functions
#--------------------------------------------------------------------------#
sub usage
{
cls();
say join( "\n", (
"# Mason v1.0",
"-------------------------------------------------------------",
"Usage: perl mason [(route|db|console):*] [*] [private|public]",
"options:",
" [help]",
" help - this help screen",
" [route]",
" route:make - create a route",
" route:silence - make a specific route not available to browsers (404)",
" [database]",
" db:make - create a database",
" db:truncate - truncate the database and re-run migrations",
" [migration]",
" migration:make - create blank migration file",
" migration:run - execute the database migrations",
" [console]",
" console:make - create a console only route"
));
}
#pod =construct route:make
#pod
#pod perl mason route:make <path/to/route>
#pod
#pod Creates a route in the public/private folder to create logic
#pod =cut
sub route
{
my ( $name, @params ) = @_;
my $route = $docroot->child( lc( $name ) );
if ( $route->child( 'index.cgi' )->exists )
{
say ' [route:make] file '.lc( $route ).'/index.cgi already exists [aborting]';
return;
}
# lets generate the core files
generate( $route );
say ' [make:route] '.lc( $route ).'/index.cgi created (OK)';
return;
}
#pod =construct db:make
#pod
#pod perl mason db:make <path/to/route>
#pod
#pod Creates an empty SQLite database
#pod =cut
sub database
{
my ( $name, @params ) = @_;
my $dbfile = $dbroot->child( lc( $name ) )->child( 'database.sqlite' ) ;
if ( $dbfile->exists )
{
say " [make:db] error - database already exists (FAIL).. did you mean 'migrate:refresh $name' ?";
return;
}
$dbfile->touchpath->sibling('migration.0.sql')->touch;
say ' [database] created .. db/'.lc($name).'/database.sqlite';
return;
}
#pod =construct db:migrate
#pod
#pod perl mason db:migrate <name/of/db>
#pod
#pod Migrates the migration.[0-9]*.sql files to the named SQLite database
#pod =cut
sub migrate
{
my ( $name, @params ) = @_;
my $dbfile = $dbroot->child( lc($name) )->child( 'database.sqlite' );
if ( ! $dbfile->exists )
{
say " [migrate:db] error - no database was found (FAIL).. maybe run 'make:database $name' ?";
return;
}
my $dbh = DBI->connect( "dbi:SQLite:dbname=$dbfile","","" );
say " [migrate:db] running migrations files for - $name";
my @migrations = $dbfile->parent->children( qr/^migration\.(.*)\.sql$/ );
foreach my $file ( sort @migrations ) {
print " [parsing] SQL ( ".$file. " ) ...";
my $sql = path( $file )->slurp_utf8;
if ( $sql !~ /\S/ )
{
print " no SQL found (FAIL)\n";
continue;
}
$dbh->do( _parseSQL( $sql ) );
print " executed (OK)\n";
}
}
#pod =construct db:refresh
#pod
#pod perl mason db:refresh <path/to/route>
#pod
#pod Truncates the database and re-runs the migration files
#pod =cut
sub refresh
{
my ( $name, @params ) = @_;
my $dbfile = $dbroot->child( lc($name) )->child( 'database.sqlite' );
if ( $dbfile->exists )
{
$dbfile->remove;
}
$dbfile->touchpath;
say " [migrate:refresh] truncated the database - $name";
migrate( $name );
}
#pod =construct migration:make
#pod
#pod perl mason db:refresh <path/to/route>
#pod
#pod Truncates the database and re-runs the migration files
#pod =cut
sub sqlfile
{
my ( $name, $idx ) = ( shift , 0 );
my $dbroot = $dbroot->child( lc($name) );
my $sqlfile = 'migration.'.scalar( $dbroot->children( qr/^migration\.(.*)\.sql$/ ) ).'.sql' ;
$dbroot->child( $sqlfile )->touchpath;
say " [make] created a new migration file (".$sqlfile.")"
}
# _parseSQL - remove SQL comments from SQL files
# @param <String> SQL text
# @returns <String> cleansed SQL text
sub _parseSQL
{
my ( $sql ) = @_;
# remove comments
#$sql =~ s/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))//g;
$sql =~ s#("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(\/\*[\w\W]*?(?=\*\/)\*\/)##g;
$sql =~ s/\n+/ /g;
$sql =~ s/\s+/ /g;
return _trim( $sql );
}
# _trimL - remove leading and ending space
# @param <String> text
# @returns <String> text
sub _trim
{
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};
#pod =construct route:silence
#pod
#pod perl mason route:silence <path/to/route>
#pod
#pod Makes a specific route not available to browsers (404)
#pod =cut
sub silence
{
my ( $name, $type ) = @_;
my $type = ( $type ) ? $type : 'route';
my $silence = $docroot->child( lc($name) )->child('.htaccess') ;
if ( $silence->exists )
{
$silence->move( $silence->sibling( '.htaccess.'.time.'.bkp' ) );
}
$silence->touchpath->spew_utf8( $mason->child( 'silence.tmpl' )->slurp_utf8 );
say ' ['.$type.'] silenced .. public/'.lc($name);
return;
}
sub console
{
my ( $name, @params ) = @_;
my $route = $docroot->child( lc($name) );
if ( $route->child('index.cgi')->exists )
{
say ' [console] error index.cgi already exists (FAIL).. did you mean "perl mason route:silence '.$route.'"?';
return;
}
# lets generate the core files
generate( $route, 'console.tmpl', 'config.tmpl' );
say ' [console] created .. public/'.lc($name);
return silence( $name, 'console' );
}
# # ========================
# # = COMMAND LINE HELPERS =
# # ========================
# cls - clears the terminal window
# @returns void
sub cls
{
my $cmd = ( $^O eq 'MSWin32' ) ? 'cls' : 'clear';
system( $cmd );
}
# generate - creates index.cgi and index.yaml in route locations
# @param - route <Path::Tiny> route to add the existing files
# @returns - route
sub generate
{
my $route = shift;
my $level = scalar( split( /\//, substr( $route->absolute->stringify, length( $base->stringify ) ) ) );
my @templates = ( @_ ) ? @_ : ( 'route.tmpl', 'config.tmpl' );
my ( $template, $ini ) = map { $mason->child( $_ )->slurp_utf8 } @templates ;
# lets create the index file
$route->child( 'index.cgi' )->touchpath->spew_utf8( $tache->render( $template, { root => { levels => $level } } ) );
$route->child( 'index.yml' )->touchpath->spew_utf8( $tache->render( $ini, { root => { levels => $level } } ) );
return $route;
}
#