root/lang/perl/App-Starter/trunk/lib/App/Starter.pm @ 8490

Revision 8490, 4.1 kB (checked in by tomyhero, 5 years ago)

lang/perl/App-Starter : rename OreOre? to App

Line 
1package App::Starter;
2
3use warnings;
4use strict;
5use File::Find;
6use File::Spec;
7use File::Path;
8use Cwd;
9use Data::Dumper;
10use YAML::Syck;
11use Template;
12use IO::All;
13use base qw/Class::Accessor/;
14
15our $VERSION = '0.04';
16
17my $DIR = {};
18
19__PACKAGE__->mk_accessors(qw/config name from replace ignore tag_style template/);
20
21sub create {
22    my $self = shift;
23
24    # get config
25    my $config = {};
26    $config->{from}      = $self->{from}      if $self->{from};
27    $config->{replace}   = $self->{replace}   if $self->{replace};
28    $config->{ignore}    = $self->{ignore}    if $self->{ignore};
29    $config->{name}      = $self->{name}      if $self->{name};
30    $config->{tag_style} = $self->{tag_style} if $self->{tag_style};
31    $config->{template}  = $self->{template} if $self->{template};
32
33    if ( $config->{template} ) {
34        $config->{from}     = File::Spec->catfile( $ENV{HOME} , '/.app/skel', $config->{template} ) ;
35        $self->{config} =  File::Spec->catfile( $ENV{HOME} , '/.app/conf' , $config->{template} . '.yml' );
36    };
37
38    if ( $self->{config} ) {
39        $config = { %{ LoadFile( $self->{config} ) }, %{$config}, };
40    }
41
42    my $to        = getcwd;
43    my $from      = $config->{from};
44    my $name      = $config->{name};
45    my $tag_style = $config->{tag_style} || 'template';
46
47
48
49    # check
50    die 'you must set [from]' unless $from;
51    die 'you must set [name]' unless $name;
52
53    if ( -e File::Spec->catfile( $to, $name ) ) {
54        die File::Spec->catfile( $to, $name ) . 'is already exist';
55    }
56
57    # load tree
58    find( sub { $self->_wanted( $from, $config->{ignore} ) }, $from );
59
60    # create directory
61    mkpath( File::Spec->catfile( $to, $name ) );
62    for my $dir ( @{ $self->{dirs} } ) {
63        $dir = File::Spec->catfile( $to, $name, $dir );
64        foreach my $key ( keys %{ $config->{replace} } ) {
65            $dir =~ s/__$key\__/$config->{replace}{$key}/g;
66        }
67        mkpath($dir);
68    }
69
70    # create files
71    my $template
72        = Template->new( { INCLUDE_PATH => $from, TAG_STYLE => $tag_style } );
73    for my $file ( @{ $self->{files} } ) {
74        my $to_file = $file;
75
76        foreach my $key ( keys %{ $config->{replace} } ) {
77            $to_file =~ s/__$key\__/$config->{replace}{$key}/g;
78        }
79        $to_file = File::Spec->catfile( $to, $name, $to_file );
80        my $content;
81        $template->process( $file, $config->{replace}, \$content );
82        $content > io($to_file);
83    }
84
85}
86
87sub _wanted {
88    my $self   = shift;
89    my $from   = shift;
90    my $ignore = shift || [];
91
92    return if $_ eq '.';
93
94    my $name = $File::Find::name;
95
96    for my $regexp ( @{$ignore} ) {
97        if ( $name =~ /$regexp/ ) {
98            return;
99        }
100    }
101
102    if ( -d $name ) {
103        $name =~ s/$from//;
104        $name =~ s/^\///;
105        push @{ $self->{dirs} }, $name;
106    }
107    else {
108        $name =~ s/$from//;
109        $name =~ s/^\///;
110        push @{ $self->{files} }, $name;
111    }
112
113}
114
1151;
116
117=head1 NAME
118
119App::Starter - App Starter
120
121=head1 SYNPSYS
122
123 my $app = App::Starter->new( { config => ' /home/tomyhero/work/App-Starter/conf/config.yaml' } )->create;
124 
125 # or
126
127 my $app = App::Starter->new( { config => ' /home/tomyhero/work/App-Starter/conf/config.yaml' ,  from => '/tmp/a' , name => 'my_app' , replace => { module => 'MyApp' } } )->create;
128 
129 # or
130 # This setting map to ~/.app/skell/pand/* as from and ~/.app/conf/panda.yml  as config
131 my $app = App::Starter->new( { template => 'panda' , name =>'foo'  } )->create;
132
133=head1 DESCRIPTION
134
135you can start your project easier once you craete start files with this module. This module only does is rename key to value. in your template file , you can set like this  [% key_name %]
136which 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
137
138=head1 CONFIG
139
140 name    : my_app
141 from    : /foo/bar/my-skell
142 # tag_style : star # SEE ALSO L<Template> TAG_STYLE OPTION
143 ignore  :
144    - \.svn
145    - \.cvs
146 replace :
147    module : MyApp
148
149=head1 METHODS
150
151=head2 new
152
153constractor
154
155=head2 create
156
157create starter dir
158
159=head1 AUTHOR
160
161Tomohiro Teranishi<tomohiro.teranishi@gmail.com>
162
163dann
164
165=cut
Note: See TracBrowser for help on using the browser.