Net/Script/Bot.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 105 CC 5 SCAR 15.8
CC
5
Cov
0%
_create_bot_config
12.0
run
6.0
_create_bot_module
6.0
CRAP 4.9
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Script::Bot;
5 use base qw/ App::CLI::Command Class::Accessor::Fast /;
6  
7 use Bot::Net;
8 use File::Copy;
9 use File::Spec;
10 use FindBin;
11 use UNIVERSAL::require;
12  
13 __PACKAGE__->mk_accessors( qw{
14 bot_class bot_file bot_conf bot_state
15  
16 mixin_classes
17  
18 clone_class clone_conf
19 });
20  
21 =head1 NAME
22  
23 Bot::Net::Script::Bot - Create a new bot
24  
25 =head1 SYNOPSIS
26  
27 bin/botnet bot --name <bot name> [ <options> ]
28  
29 =head1 DESCRIPTION
30  
31 This command will create a single bot and automatically generate any additional files such as basic tests, stub configuration file, etc.
32  
33 With no options other than the bot name, this will create a vanilla bot that uses the L<Bot::Net::Bot> mixin and nothing else. The options can further customize this situation.
34  
35 =head1 OPTIONS
36  
37 --name <bot name> - The name of the bot class to create
38 --mixin <mixin name> - The name of a bot mixin to add to the class. This
39 option may be specified multiple times.
40 --clone <bot name> - The name of a bot to clone from. If given, the new
41 bot will inherit from the given bot's class and
42 the configuration for that bot will also be used
43 as the basis for the new class
44  
45 =head1 METHODS
46  
47 =head2 options
48  
49 Returns the arguments used by this script. See L<App::CLI::Command>.
50  
51 =cut
52  
53 sub options {
54 (
55 'name=s' => 'name',
56 'mixin=s@' => 'mixins',
57 'clone=s' => 'clone',
58 );
59 }
60  
61 =head2 run
62  
63 Creates the bot according to the options specified.
64  
65 =cut
66  
67 sub run {
68 my ($self, @args) = @_;
69  
70 defined $self->{name}
71 or die "No bot name given with required --name option.\n";
72  
73 $self->bot_class( Bot::Net->net_class('Bot', $self->{name}) );
74 $self->bot_file(
75 File::Spec->catfile(
76 $FindBin::Bin, '..', 'lib',
77 split(/::/, $self->bot_class)
78 ) . '.pm'
79 );
80 $self->bot_conf(
81 File::Spec->catfile(
82 $FindBin::Bin, '..', 'etc', 'bot',
83 split(/::/, $self->{name})
84 ) . '.yml'
85 );
86 $self->bot_state(
87 File::Spec->catfile(
88 $FindBin::Bin, '..', 'var', 'bot',
89 split(/::/, $self->{name})
90 ) . '.db'
91 );
92  
93 my @mixins = @{ $self->{mixins} || [] };
94 my @mixin_classes = (
95 'Bot::Net::Bot',
96 map { 'Bot::Net::Mixin::Bot::'.$_ } @mixins
97 );
98  
99 $self->mixin_classes( \@mixin_classes );
100  
101 if ($self->{clone}) {
102 $self->clone_class( Bot::Net->net_class('Bot', $self->{clone}) );
103  
104 $self->clone_config(
105 File::Spec->catfile(
106 $FindBin::Bin, '..', 'etc', 'bot',
107 split(/::/, $self->{clone})
108 ) . '.yml'
109 );
110 }
111
112 $self->_create_bot_module;
113 $self->_create_bot_config;
114 # TODO XXX FIXME Add _create_bot_test()
115 # $self->_create_bot_test;
116 }
117  
118 sub _create_bot_module {
119 my $self = shift;
120
121 open my $botmod, '>', $self->bot_file
122 or die "Cannot write to @{[$self->bot_file]}: $!";
123  
124 print "Creating ",$self->bot_file,"...\n";
125  
126 print $botmod <<"END_OF_BOT_MODULE_START";
127 use strict;
128 use warnings;
129  
130 package @{[$self->bot_class]};
131 END_OF_BOT_MODULE_START
132  
133 if ($self->{clone}) {
134 print $botmod <<"END_OF_BOT_MODULE_CLONE";
135  
136 use base qw/ @{[$self->clone_class]} /;
137  
138 END_OF_BOT_MODULE_CLONE
139 }
140  
141 else {
142 print $botmod <<"END_OF_BOT_MODULE_NEW";
143  
144 @{[join "\n", map { 'use '.$_.';' } @{ $self->mixin_classes }]}
145  
146 END_OF_BOT_MODULE_NEW
147 }
148  
149 print $botmod <<"END_OF_BOT_MODULE_END";
150 =head1 NAME
151  
152 @{[$self->bot_class]} - A semi-autonomous agent that does something
153  
154 =head1 SYNOPSIS
155  
156 bin/botnet run --bot $self->{name}
157  
158 =head1 DESCRIPTION
159  
160 A semi-autonomous agent that does something. This documentation needs replacing.
161  
162 =cut
163  
164 1;
165  
166 END_OF_BOT_MODULE_END
167 }
168  
169 sub _create_bot_config {
170 my $self = shift;
171  
172 if ($self->{clone} and -f $self->clone_conf) {
173 print "Copying ",$self->clone_conf," to ",$self->bot_conf,"...\n";
174 copy($self->clone_conf, $self->bot_conf);
175 }
176  
177 else {
178 print "Creating ",$self->bot_conf,"...\n";
179  
180 my @configs;
181 for my $mixin_class (@{ $self->mixin_classes || [] }) {
182 $mixin_class->require;
183 if (my $method = $mixin_class->can('default_configuration')) {
184 push @configs, $method->($mixin_class, $self->bot_class);
185 }
186 }
187  
188 my $bot_conf = Hash::Merge::merge( @configs );
189  
190 YAML::Syck::DumpFile( $self->bot_conf, $bot_conf );
191 }
192 }
193  
194 =head1 AUTHORS
195  
196 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
197  
198 =head1 COPYRIGHT AND LICENSE
199  
200 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
201  
202 This program is free software and may be modified and distributed under the same terms as Perl itself.
203  
204 =cut
205  
206 1;