Permalink
Browse files

Core: api for work with template file based on Template toolkit

  • Loading branch information...
1 parent 48c0096 commit 9f36d8b4306d74122e53027fa9998607d9df3669 @dboys dboys committed Jun 27, 2013
View
@@ -1,59 +1,99 @@
#!/usr/bin/env perl
use Mojolicious::Lite;
-
-use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
-use File::Temp qw/tempfile tempdir/;
-use File::Spec::Functions;
-use File::Path qw/make_path/;
-use autodie qw/:all/;
-use Data::Dumper;
+use Mojo::Home;
+use Parrot::HLL;
+use Parrot::Library;
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
# Read configuration file
plugin 'yaml_config';
-# send file to the client
+# Send file to the client
plugin 'RenderFile';
+app->secret(app->config->{passphrase});
+
get '/' => 'index';
post '/' => sub {
my $self = shift;
-
- my ($name, $builder, $harness, $with_pmc, $with_ops, $with_doc);
+
+ my ($name, $builder, $harness, $with_pmc, $with_ops, $with_doc, $archive_path);
if (defined $self->param("hll")) {
($name, $builder, $harness, $with_pmc, $with_ops, $with_doc)
= map { $self->param($_) } qw/hll_name hll_builder hll_test with_pmc with_ops with_doc/;
+
+ my $hll = Parrot::HLL->new();
+ $hll->init($name, $builder, $harness, $with_pmc, $with_ops, $with_doc);
+ $hll->template($self->app->config->{hll_template});
+
+ if ($archive_path = $hll->generate()) {
+ $self->app->log->debug("The HLL created!");
+ }
}
elsif(defined $self->param("lib")){
($name, $builder, $harness) = map { $self->param($_) } qw/lib_name lib_builder lib_test/;
- }
-
- $name =~ s/[^A-z]*//g;
-
- my $time = time;
- my $tmp_base = tempdir( "app-parrot-create-XXXXXXX", TMPDIR => 1,
- # CLEANUP => 1
- );
- my $dir = "$tmp_base/$time/$name";
-
- $self->app->log->debug("Going to run bin/new_parrot_language.pl $name $dir");
- my @args = ($^X,"bin/new_parrot_language.pl",$name, $dir);
- system @args;
-
- my $zip = Archive::Zip->new();
- my $dir_member = $zip->addTree("$dir/");
-
- $self->app->log->debug("Going to write a zip file to $dir.zip");
- unless ( $zip->writeToFileNamed("/tmp/$time-$name.zip") == AZ_OK ) {
- die 'write error';
+
+ my $library = Parrot::Library->new();
+ $library->init($name, $builder, $harness);
+ $library->template($self->app->config->{library_template});
+
+ if ($archive_path = $library->generate()) {
+ $self->app->log->debug("The Library created!");
+ }
}
$self->render_file(
- 'filepath' => "/tmp/$time-$name.zip",
- 'format' => "zip"
+ 'filepath' => $archive_path,
+ 'format' => 'zip'
);
-
} => 'index';
+
+#use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
+#use File::Temp qw/tempfile tempdir/;
+#use File::Spec::Functions;
+#use File::Path qw/make_path/;
+#use autodie qw/:all/;
+#
+#post '/' => sub {
+# my $self = shift;
+#
+# my ($name, $builder, $harness, $with_pmc, $with_ops, $with_doc);
+# if (defined $self->param("hll")) {
+# ($name, $builder, $harness, $with_pmc, $with_ops, $with_doc)
+# = map { $self->param($_) } qw/hll_name hll_builder hll_test with_pmc with_ops with_doc/;
+# }
+# elsif(defined $self->param("lib")){
+# ($name, $builder, $harness) = map { $self->param($_) } qw/lib_name lib_builder lib_test/;
+# }
+#
+# $name =~ s/[^A-z]*//g;
+#
+# my $time = time;
+# my $tmp_base = tempdir( "app-parrot-create-XXXXXXX", TMPDIR => 1,
+# # CLEANUP => 1
+# );
+# my $dir = "$tmp_base/$time/$name";
+#
+# $self->app->log->debug("Going to run bin/new_parrot_language.pl $name $dir");
+# my @args = ($^X,"bin/new_parrot_language.pl",$name, $dir,
+# "--with-doc $with_doc","--with-ops $with_ops","--with-pmc $with_pmc");
+# system @args;
+#
+# my $zip = Archive::Zip->new();
+# my $dir_member = $zip->addTree("$dir/");
+#
+# $self->app->log->debug("Going to write a zip file to $dir.zip");
+# unless ( $zip->writeToFileNamed("/tmp/$time-$name.zip") == AZ_OK ) {
+# die 'write error';
+# }
+#
+# $self->render_file(
+# 'filepath' => "/tmp/$time-$name.zip",
+# 'format' => "zip"
+# );
+#
+#} => 'index';
+
app->start;
@@ -22,3 +22,12 @@ template: "simple"
# start_tag: '[%'
# end_tag: '%]'
+#Mojolicious uses a secret passphrase for security features such as signed cookies
+#md5("Parrot Foundation") = "8bf2d0c8051519c9bdbb574ed61a6217"
+passphrase: "8bf2d0c8051519c9bdbb574ed61a6217"
+
+#File path on hll template file
+hll_template: "project-templates/hll.parrot"
+
+#File path on library template file
+library_template: "project-templates/library.parrot"
View
@@ -3,8 +3,14 @@ package Parrot::Base;
use v5.14;
use Moose::Role;
use Parrot::Type;
-use Try::Tiny;
+use Template;
use Method::Signatures;
+use File::Spec;
+use File::Path;
+use File::Temp qw/tempfile tempdir/;
+use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
+
+requires 'init';
has 'name' => (
is => 'rw',
@@ -30,6 +36,112 @@ has 'test_system' => (
default => 'Perl 5'
);
-requires 'generate';
+has 'archive_path' => (
+ isa => 'Str',
+ is => 'rw',
+ predicate => 'has_archive_path'
+
+);
+
+has 'template' => (
+ isa => 'Str',
+ is => 'rw',
+ predicate => 'has_template',
+ trigger => sub {
+ my $self = shift;
+
+ unless (-f $self->template) {
+ die "File $self->template doesn't found\n";
+ }
+ }
+);
+
+method get_template() {
+ my ($volume, $dir, $file) = File::Spec->splitpath($self->template);
+ my $filedir = File::Spec->catpath($volume, $dir);
+
+ return ($filedir, $file);
+}
+
+method generate_template($template_dir,$template) {
+ my $config = {
+ INCLUDE_PATH => $template_dir,
+ EVAL_PERL => 1,
+ };
+
+ my $tt = Template->new($config);
+ my $objects = {
+ object => $self,
+ };
+
+ my $output;
+ $tt->process($template, $objects, \$output)
+ || die $tt->error;
+
+ return $output;
+}
+
+method generate_project($content_template) {
+ my @lines_template = split(/\n/, $content_template);
+
+ my $time = time;
+ my $tmp_base = tempdir( "app-parrot-create-XXXXXXX", TMPDIR => 1,
+ # CLEANUP => 1
+ );
+ my $filepath = "$tmp_base/$time/".$self->name;
+ my $fh;
+
+ while (@lines_template) {
+ my $line_template = shift @lines_template;
+ last if ($line_template =~ /^__END__$/);
+
+ #TODO:
+ #Change normal for human
+ if ($line_template =~ /^__(.*)__$/) {
+
+ my ($volume, $dir, $file) = File::Spec->splitpath("$filepath/$1");
+ my $filedir = File::Spec->catpath($volume, $dir);
+ unless (-d $filedir) {
+ say "creating $filedir";
+ mkpath( [ $filedir ], 0, 0777 );
+ }
+
+ say "creating $filedir$file";
+ if ($fh) {
+ close $fh;
+ undef $fh;
+ }
+ open $fh, '>', "$filedir/$file" or die $!;
+ }
+ elsif($fh) {
+ say $fh $line_template;
+ }
+ }
+
+ close($fh) if($fh);
+ return $filepath;
+}
+
+method generate_archive($path_to_project) {
+ my $zip = Archive::Zip->new();
+ my $dir_member = $zip->addTree("$path_to_project/");
+ my $time = time;
+
+ #TODO:
+ #Change file path
+ unless ( $zip->writeToFileNamed("/tmp/$time-".$self->name.".zip") == AZ_OK ) {
+ die 'write error';
+ }
+
+ $self->archive_path("/tmp/$time-".$self->name.".zip");
+ return $self->archive_path;
+}
+
+method generate() {
+ my $content_template = $self->generate_template($self->get_template());
+ my $project_path = $self->generate_project($content_template);
+ my $archive_path = $self->generate_archive($project_path);
+ return $archive_path;
+}
1;
View
@@ -8,38 +8,39 @@ use Method::Signatures;
with 'Parrot::Base';
-has 'pmc' => (
+has 'with_pmc' => (
isa => 'Bool',
is => 'rw',
required => 1,
predicate => 'has_pmc',
default => '0'
);
-has 'ops' => (
+has 'with_ops' => (
isa => 'Bool',
is => 'rw',
required => 1,
predicate => 'has_ops',
default => '0'
);
-has 'pod' => (
+has 'with_doc' => (
isa => 'Bool',
is => 'rw',
required => 1,
- predicate => 'has_pod',
+ predicate => 'has_doc',
default => '0'
);
-=head2 generate()
-Args: none
-Returns: zip hll file path
-Description:
-Generate a new zip hll project
-=cut
-method generate() {
-
+method init($name, $build_system, $test_system, $with_pmc, $with_ops, $with_doc) {
+ $self->name($name);
+ $self->build_system($build_system);
+ $self->test_system($test_system);
+ $self->with_pmc($with_pmc);
+ $self->with_ops($with_ops);
+ $self->with_doc($with_doc);
+
+ return 1;
}
no Moose;
View
@@ -7,14 +7,14 @@ use Method::Signatures;
with 'Parrot::Base';
-=head2 generate()
-Args: none
-Returns: zip parrot library file path
-Description:Generate a new zip parrot library project
-=cut
-method generate() {
+method init($name, $build_system, $test_system) {
+ $self->name($name);
+ $self->build_system($build_system);
+ $self->test_system($test_system);
+ return 1;
}
+
no Moose;
__PACKAGE__->meta->make_immutable;
Oops, something went wrong.

1 comment on commit 9f36d8b

@leto
Member
leto commented on 9f36d8b Jun 28, 2013

Why are all the tests commented out?

Please sign in to comment.