ASP4

 view release on metacpan or  search on metacpan

sbin/asphelper  view on Meta::CPAN

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Getopt::Long;
use Term::ReadKey;
use File::Path;
use DBI;
use Cwd 'cwd';

*make_path = \&File::Path::make_path;


my (
  $appName,
  $domain,
  $dbName,
  $dbHost,
  $dbUser,
  $dbPass,
  $email,
);


my $result = GetOptions(
  "app=s"     => \$appName, # Required
  "domain=s"  => \$domain,  # Required
  "email=s"   => \$email,   # Required
  "db=s"      => \$dbName,
  "user=s"    => \$dbUser,
  "host=s"    => \$dbHost,
);

$appName && $domain && $email or die "Usage: $0 --app=AppName --domain=domain.com --email=you\@your-email.com [--host=dbhost --db=dbname  --user=dbusername]\n";
$dbHost ||= "localhost";

if( $dbName && $dbUser )
{
  print STDERR "Enter your database password: ";
  ReadMode('noecho');
  chomp($dbPass = <STDIN>);
  ReadMode('restore');
  print "\n";
}# end if()

my @DSN = (
  "DBI:mysql:$dbName:$dbHost",
  $dbUser,
  $dbPass
);

my $drh = DBI->install_driver("mysql");
my $rc = $drh->func('createdb', $dbName, $dbHost, $dbUser, $dbPass, 'admin');

my $dbh = eval { DBI->connect( @DSN, {RaiseError => 1} ) };
if( $@ )
{
  (my $error = $@) =~ s/\sat\s\Q$0\E\s+line.*//;
  die "[ERROR]: $error\n";
}# end if()

# Setup folder structure:
(my $project_path = lc($appName)) =~ s{::}{_}sg;
make_path($project_path);
chdir($project_path);
my $cwd = cwd();
my $appFolder = join( '/',  split(/::/, $appName) );
make_path("common/lib/$appFolder/db");
make_path("common/sbin");
make_path('www/t/010-basic');
make_path('www/conf');
make_path('www/etc');
make_path('www/htdocs');
make_path('www/handlers');



# Write the ddl.sql file:
unless( -f "common/sbin/ddl.sql" )
{
  warn "common/sbin/ddl.sql\n";
  open my $ofh, '>', "common/sbin/ddl.sql"
    or die "Cannot open 'common/sbin/ddl.sql' for writing: $!";
  print $ofh <<"SQL";

set foreign_key_checks = 0;
drop table if exists asp_sessions;
set foreign_key_checks = 1;

create table asp_sessions (
  session_id   char(32) not null primary key,
  session_data blob,
  created_on   datetime default null,
  modified_on  datetime default null
) engine=innodb charset=utf8;

SQL
  close($ofh);
  open my $ifh, "common/sbin/ddl.sql"
    or die "Cannot open 'common/sbin/ddl.sql' for reading: $!";
  local $/ = ';';
  while( my $cmd = <$ifh> )
  {
    $cmd =~ s/^\s+//s;
    $cmd =~ s/\s+$//s;
    next unless $cmd;
    $dbh->do($cmd);
  }# end while()
  close($ifh);
}# end unless()


# Write our configs:
unless( -f "www/conf/asp4-config.json" )
{

sbin/asphelper  view on Meta::CPAN

  close($ofh);
}# end unless()


# Test page:
make_path("www/htdocs");
unless( -f "www/htdocs/index.asp" )
{
  warn "www/htdocs/index.asp\n";
  open my $ofh, '>', "www/htdocs/index.asp"
    or die "Cannot open 'www/htdocs/index.asp' for writing: $!";
  print $ofh <<'ASP';
<html>
<body>
<h1>ASP4 Test Page</h1>
<p>
  The date and time is <%= scalar(localtime()) %>.
</p>
<p>
  You have visited this page <%= $Session->{count}++ %> time(s) recently.
</p>
</body>
</html>
ASP
  close($ofh);
}# end unless()


(my $hPath = lc($appName)) =~ s/::/\//g;
my $hClass = lc($appName);
make_path("www/handlers/$hPath/www");
unless( -f "www/handlers/$hClass/www/echo.pm" )
{
  open my $ofh, '>', "www/handlers/$hPath/www/echo.pm"
    or die "Cannot open 'www/handlers/$hPath/www/echo.pm' for writing: $!";
  print $ofh <<"CODE";

package $hClass\::www::echo;

use strict;
use warnings 'all';
use base 'ASP4::FormHandler';
use vars __PACKAGE__->VARS;
use Data::Dumper;

sub run
{
  my (\$s, \$context) = \@_;
  
  \$Response->Write("<h1>Form Contents:</h1><pre>" . Dumper(\$Form) . "</pre>");
}# end run()

1;# return true:

CODE
  close($ofh);
}# end unless()

# Only write the base Model class if we have Class::DBI::Lite
my $CDBIL_Version = 0;
eval {
  require Class::DBI::Lite;
  $CDBIL_Version = $Class::DBI::Lite::VERSION = $Class::DBI::Lite::VERSION;
};
if( $dbName && $Class::DBI::Lite::VERSION )
{
  unless( -f "common/lib/$appFolder/db/model.pm" )
  {
    warn "common/lib/$appFolder/db/model.pm\n";
    open my $ofh, '>', "common/lib/$appFolder/db/model.pm"
      or die "Cannot open 'common/lib/$appFolder/db/model.pm' for writing: $!";
    print $ofh <<"CODE";

@{[ 'package' ]} @{['']} $appFolder\::db::model;

use strict;
use warnings 'all';
use base 'Class::DBI::Lite::mysql';
use ASP4::ConfigLoader;

my \$Config = ASP4::ConfigLoader->load();
my \$conn = \$Config->data_connections->main;
__PACKAGE__->connection(
  \$conn->dsn,
  \$conn->username,
  \$conn->password
);

1;# return true:

\=pod

\=head1 NAME

$appName\::db::model - Base class for all $appName entity classes.

\=head1 SYNOPSIS

  # In your class:
  
  package $appName\::db::thing;
  
  use strict;
  use warnings 'all';
  use base '$appName\::db::model';
  
  __PACKAGE__->set_up_table('things');
  
  1;# return true:

\=head1 DESCRIPTION

This module was generated by $0 on @{[ scalar(localtime()) ]}.

B<***IT IS SAFE to make changes to this file, as it will not be overwritten.***>.

\=head1 SEE ALSO

L<Class::DBI::Lite>

\=cut



( run in 1.278 second using v1.01-cache-2.11-cpan-ceb78f64989 )