Skip to content

Instantly share code, notes, and snippets.

@issm
Created March 30, 2013 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save issm/5275540 to your computer and use it in GitHub Desktop.
Save issm/5275540 to your computer and use it in GitHub Desktop.
#!/bin/bash
BASEDIR=$(cd $(dirname $0)/.. && pwd)
exec perl -I$BASEDIR/lib $BASEDIR/script/make_schema.pl $@
=head1 NAME
make_schema - generates schema definition for Teng
=head1 SYNOPSIS
./bin/make_schema <options>
# or
perl scrip/make_schema.pl <options>
=head1 OPTIONS
=over4
=item --namespace=MyApp::DB
=item --inflate-dir=etc/schema/inflate [optional]
=back
=cut
use strict;
use warnings;
use Getopt::Long qw/:config posix_default no_ignore_case bundling/;
use Teng::Schema::Dumper;
use Text::Xslate qw/mark_raw/;
use File::Basename;
use Pod::Usage;
use Data::Section::Simple qw/get_data_section/;
use MyApp;
my $c = MyApp->bootstrap();
my %opts = (
ns => undef,
inflate_dir => $c->base_dir() . '/etc/schema/inflate',
);
GetOptions (
'ns|namespace=s' => \$opts{ns},
'inflate-dir=s' => \$opts{inflate_dir},
) or pod2usage(1);
$opts{ns} or pod2usage(1);
my $ns = $opts{ns};
my $inflate_dir = $opts{inflate_dir};
my %inflate;
for my $f ( glob( "$inflate_dir/*.pl" ) ) {
my $name = basename $f, '.pl';
open my $fh, '<', $f or die $!;
my $code = do { local $/; <$fh> };
$code =~ s/^(.*\S)/ $1/mg; # indent 4
$inflate{$name} = $code;
}
my $schema = Teng::Schema::Dumper->dump(
dbh => $c->dbh(),
namespace => $ns,
inflate => \%inflate,
);
my $out = Text::Xslate->new->render_string(
get_data_section('template.tx'),
{
namespace => $ns,
schema => mark_raw( $schema ),
plugins => [],
},
);
print $out;
__DATA__
@@ template.tx
package <: $namespace :>;
use parent 'Teng';
: for $plugins -> $plugin {
__PACKAGE__->load_plugin('<: $plugin :>');
: }
1;
<: $schema :>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment