File Coverage

blib/lib/App/Starter.pm
Criterion Covered Total %
statement 33 104 31.7
branch 0 34 0.0
condition 0 4 0.0
subroutine 11 14 78.5
pod 1 1 100.0
total 45 157 28.6


line stmt bran cond sub pod time code
1             package App::Starter;
2              
3 1     1   21234 use warnings;
  1         2  
  1         28  
4 1     1   6 use strict;
  1         1  
  1         31  
5 1     1   6 use File::Find;
  1         10  
  1         60  
6 1     1   5 use File::Spec;
  1         2  
  1         30  
7 1     1   4 use File::Path;
  1         2  
  1         45  
8 1     1   5 use Cwd;
  1         1  
  1         62  
9 1     1   1137 use Data::Dumper;
  1         10199  
  1         63  
10 1     1   784 use YAML::Syck;
  1         2200  
  1         76  
11 1     1   2262 use Template;
  1         22361  
  1         33  
12 1     1   1100 use IO::All;
  1         14177  
  1         12  
13 1     1   83 use base qw/Class::Accessor/;
  1         2  
  1         3736  
14              
15             our $VERSION = '0.13';
16              
17             my $DIR = {};
18              
19             __PACKAGE__->mk_accessors(
20             qw/config name from replace ignore tag_style template/);
21              
22             sub create {
23 0     0 1   my $self = shift;
24              
25             # get config
26 0           my $config = {};
27 0 0         $config->{replace} = $self->{replace} ? $self->{replace} : {};
28 0 0         $config->{ignore} = $self->{ignore} ? $self->{ignore} : [];
29 0 0         $config->{from} = $self->{from} if $self->{from};
30 0 0         $config->{name} = $self->{name} if $self->{name};
31 0 0         $config->{tag_style} = $self->{tag_style} if $self->{tag_style};
32 0 0         $config->{template} = $self->{template} if $self->{template};
33              
34 0 0         if ( $config->{template} ) {
35 0           $config->{from}
36             = File::Spec->catfile( $ENV{HOME}, '/.app-starter/skel',
37             $config->{template} );
38              
39 0           my $conf_file = '';
40 0 0         if (-e File::Spec->catfile(
41             $ENV{HOME}, '/.app-starter/conf',
42             $config->{template} . '.yml'
43             )
44             )
45             {
46 0           $conf_file
47             = File::Spec->catfile( $ENV{HOME}, '/.app-starter/conf',
48             $config->{template} . '.yml' );
49             }
50             else {
51 0           $conf_file
52             = File::Spec->catfile( $ENV{HOME}, '/.app-starter/conf',
53             $config->{template} . '.yaml' );
54             }
55              
56 0           $self->{config} = $conf_file;
57             }
58              
59 0 0         if ( $self->{config} ) {
60 0           my $config_from_file = LoadFile( $self->{config} );
61 0           %$config = ( %$config_from_file, %$config );
62              
63 0           $config->{replace}
64 0 0         = { %{ $config_from_file->{replace} }, %{ $config->{replace} } }
  0            
65             if $config_from_file->{replace};
66              
67 0 0         push @{ $config->{ignore} }, @{ $config_from_file->{ignore} }
  0            
  0            
68             if $config_from_file->{ignore};
69             }
70              
71 0           my $to = getcwd;
72 0           my $from = $config->{from};
73 0           my $name = $config->{name};
74 0   0       my $tag_style = $config->{tag_style} || 'template';
75              
76             # check
77 0 0         die 'you must set [from]' unless $from;
78 0 0         die 'you must set [name]' unless $name;
79              
80 0 0         if ( -e File::Spec->catfile( $to, $name ) ) {
81 0           die File::Spec->catfile( $to, $name ) . 'is already exist';
82             }
83              
84             # load tree
85 0     0     find( sub { $self->_wanted( $from, $config->{ignore} ) }, $from );
  0            
86              
87             # create directory
88 0           mkpath( File::Spec->catfile( $to, $name ) );
89 0           for my $dir ( @{ $self->{dirs} } ) {
  0            
90 0           $dir = File::Spec->catfile( $to, $name, $dir );
91 0           foreach my $key ( keys %{ $config->{replace} } ) {
  0            
92 0           $dir =~ s/__$key\__/$config->{replace}{$key}/g;
93             }
94 0           mkpath($dir);
95             }
96              
97             # create files
98 0           my $template
99             = Template->new( { INCLUDE_PATH => $from, TAG_STYLE => $tag_style } );
100 0           for my $file ( @{ $self->{files} } ) {
  0            
101 0           my $to_file = $file;
102              
103 0           foreach my $key ( keys %{ $config->{replace} } ) {
  0            
104 0           $to_file =~ s/__$key\__/$config->{replace}{$key}/g;
105             }
106 0           $to_file = File::Spec->catfile( $to, $name, $to_file );
107 0           my $content;
108 0           $template->process( $file, $config->{replace}, \$content );
109 0           $content > io($to_file);
110             }
111              
112             }
113              
114             sub _wanted {
115 0     0     my $self = shift;
116 0           my $from = shift;
117 0   0       my $ignore = shift || [];
118              
119 0 0         return if $_ eq '.';
120              
121 0           my $name = $File::Find::name;
122              
123 0           for my $regexp ( @{$ignore} ) {
  0            
124 0 0         if ( $name =~ /$regexp/ ) {
125 0           return;
126             }
127             }
128              
129 0 0         if ( -d $name ) {
130 0           $name =~ s/$from//;
131 0           $name =~ s{^/}{};
132 0           push @{ $self->{dirs} }, $name;
  0            
133             }
134             else {
135 0           $name =~ s/$from//;
136 0           $name =~ s{^/}{};
137 0           push @{ $self->{files} }, $name;
  0            
138             }
139              
140             }
141              
142             1;
143              
144             =head1 NAME
145              
146             App::Starter - Application Starter
147              
148             =head1 SYNOPSIS
149              
150             my $app
151             = App::Starter->new(
152             { config => ' /tmp/conf/config.yml' } )
153             ->create;
154            
155             # or
156             # from = 'tmp/a' , replace => { module => 'MyApp' } overwrite config.yml setting.
157             my $app = App::Starter->new(
158             { config => '/tmp/conf/config.yml',
159             from => '/tmp/a',
160             name => 'my_app',
161             replace => { module => 'MyApp' }
162             }
163             )->create;
164            
165             # or even you can use ~/.app-sterter so taht you do not need to hve from and config options
166            
167             #~/.app-starter
168             #|-- conf
169             #| `-- sample.conf
170             #`-- skel
171             # `-- sample
172             # |-- bin
173             # | `-- __app__.pl
174             # `-- lib
175             # `-- __app__
176             # `-- Foo.pm
177             my $app
178             = App::Starter->new( { template => 'sample', name => 'foo' } )->create;
179              
180             =head1 DESCRIPTION
181              
182             you can start your application quickly once you create skeleton with this module. This module only does is rename key to value. in your template file, you can set like this [% key_name %]
183             which replace with value you set in config. and also you can use __key_name__ format as file or directory name which replace as rule you set at config
184              
185             I recommend to use ~/.app-starter directory to store your app-starter data
186              
187             =head1 CONFIG
188              
189             name : my_app # ${current_dir}/my_app is created as new appication skeleton
190             from : /foo/bar/my-skell # where to fine your skel setup. if you use ~/.app-starter then you do not need this.
191             tag_style : star # SEE ALSO L