| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Bot::Net::Script::Net; | ||
| 5 | use base qw/ App::CLI::Command Class::Accessor::Fast /; | ||
| 6 | |||
| 7 | use Bot::Net; | ||
| 8 | use File::Basename; | ||
| 9 | use File::Copy; | ||
| 10 | use File::Path; | ||
| 11 | use File::Spec; | ||
| 12 | use FindBin; | ||
| 13 | use YAML::Syck qw/ DumpFile /; | ||
| 14 | |||
| 15 | __PACKAGE__->mk_accessors(qw/ prefix dist_name mod_name /); | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Bot::Net::Script::Net - Create the scaffolding for a new bot net | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | bin/botnet net --name <bot net app name> | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This will create a folder named according to the C<--name> argument and fill it in with a skeleton L<Bot::Net> application. | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | =head2 actions | ||
| 32 | |||
| 33 | Returns the arguments used by the this script. See L<App::CLI::Command>. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub options { | ||
| 38 | ( 'name=s' => 'name' ); | ||
| 39 | } | ||
| 40 | |||
| 41 | =head2 run | ||
| 42 | |||
| 43 | Creates the bot net scaffoldering. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub run { | ||
| 48 | my ($self, @args) = @_; | ||
| 49 | |||
| 50 | defined $self->{name} | ||
| 51 | or die "No bot net name given with required --name option.\n"; | ||
| 52 | |||
| 53 | $self->prefix( $self->{name} ); | ||
| 54 | |||
| 55 | # Split by - or :: and DWIM | ||
| 56 | my @path_comp = split /\-|::/, $self->prefix; | ||
| 57 | $self->mod_name( join '::', @path_comp ); | ||
| 58 | $self->dist_name( join '-', @path_comp ); | ||
| 59 | |||
| 60 | print "Creating new application $self->{name}\n"; | ||
| 61 | $self->_make_directories; | ||
| 62 | $self->_install_botnet_binary; | ||
| 63 | $self->_write_makefile; | ||
| 64 | $self->_write_config_file; | ||
| 65 | $self->_create_log4perl_config_file; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _make_directories { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | # Make the main directory | ||
| 72 | print "Creating directories...\n"; | ||
| 73 | mkpath($self->dist_name, 1); | ||
| 74 | |||
| 75 | my @lib_dirs = split /::/, $self->mod_name; | ||
| 76 | my $lib_dir = File::Spec->catfile(@lib_dirs); | ||
| 77 | |||
| 78 | my @dirs = map { | ||
| 79 | s/__APP__/$lib_dir/; | ||
| 80 | File::Spec->catfile($self->dist_name, $_) | ||
| 81 | } $self->_directories; | ||
| 82 | |||
| 83 | mkpath(\@dirs, 1); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _install_botnet_binary { | ||
| 87 | my $self = shift; | ||
| 88 | my $bin = $FindBin::Bin; | ||
| 89 | my $script = basename($0); | ||
| 90 | |||
| 91 | # Get ready to copy | ||
| 92 | my $source_file = File::Spec->catfile($bin, $script); | ||
| 93 | my $dest_file = File::Spec->catfile($self->dist_name, 'bin', $script); | ||
| 94 | |||
| 95 | # Copy and make it executable | ||
| 96 | print "Copying in $dest_file...\n"; | ||
| 97 | copy($source_file, $dest_file); | ||
| 98 | chmod 0555, $dest_file; | ||
| 99 | |||
| 100 | # If on a DOSish platform make bat file too | ||
| 101 | if (-e $source_file.'.bat') { | ||
| 102 | print "Copying in $dest_file.bat...\n"; | ||
| 103 | copy($source_file.'.bat', $dest_file.'.bat'); | ||
| 104 | chmod 0555, $dest_file.'.bat'; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _write_makefile { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | my $makefile = File::Spec->catfile($self->dist_name, 'Makefile.PL'); | ||
| 112 | |||
| 113 | print "Creating $makefile...\n"; | ||
| 114 | open my $makefh, '>', $makefile | ||
| 115 | or die "Cannot write $makefile: $!"; | ||
| 116 | print $makefh <<"END_OF_MAKEFILE_PL"; | ||
| 117 | use inc::Module::Install; | ||
| 118 | |||
| 119 | name '@{[$self->mod_name]}'; | ||
| 120 | version '0.01'; | ||
| 121 | |||
| 122 | requires 'Bot::Net' => '@{[$Bot::Net::VERSION]}'; | ||
| 123 | |||
| 124 | WriteAll; | ||
| 125 | END_OF_MAKEFILE_PL | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _write_config_file { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | my $config_file = File::Spec->catfile($self->dist_name, 'etc/net.yml'); | ||
| 132 | |||
| 133 | print "Creating $config_file...\n"; | ||
| 134 | DumpFile($config_file, { | ||
| 135 | ApplicationClass => $self->mod_name, | ||
| 136 | ApplicationName => $self->mod_name, | ||
| 137 | }); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _create_log4perl_config_file { | ||
| 141 | my $self = shift; | ||
| 142 | my $bin = $FindBin::Bin; | ||
| 143 | |||
| 144 | # Get ready to create | ||
| 145 | my $dest_file = File::Spec->catfile( | ||
| 146 | $self->dist_name, 'etc', 'log4perl.conf'); | ||
| 147 | open my $log4perl, '>', $dest_file | ||
| 148 | or die "Could not write to $dest_file: $!"; | ||
| 149 | |||
| 150 | print "Creating $dest_file...\n"; | ||
| 151 | print $log4perl <<'END_OF_LOG4PERL_CONF'; | ||
| 152 | log4perl.rootLogger=DEBUG, SCREEN | ||
| 153 | |||
| 154 | log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen | ||
| 155 | log4perl.appender.SCREEN.stderr=0 | ||
| 156 | |||
| 157 | log4perl.appender.SCREEN.layout=PatternLayout | ||
| 158 | log4perl.appender.SCREEN.layout.ConversionPattern=[%d] %c - %m%n | ||
| 159 | END_OF_LOG4PERL_CONF | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _directories { | ||
| 163 | return qw{ | ||
| 164 | bin | ||
| 165 | doc | ||
| 166 | etc | ||
| 167 | etc/bot | ||
| 168 | etc/server | ||
| 169 | lib/__APP__/Bot | ||
| 170 | lib/__APP__/Server | ||
| 171 | log | ||
| 172 | t | ||
| 173 | var/bot | ||
| 174 | var/server | ||
| 175 | }; | ||
| 176 | } | ||
| 177 | |||
| 178 | =head1 AUTHORS | ||
| 179 | |||
| 180 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||
| 181 | |||
| 182 | =head1 COPYRIGHT AND LICENSE | ||
| 183 | |||
| 184 | Copyright 2007 Boomer Consulting, Inc. All Rights Reserved. | ||
| 185 | |||
| 186 | This program is free software and may be modified and distributed under the same terms as Perl itself. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | 1; |