| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Bot::Net::Script::Server; | ||
| 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 Hash::Merge (); | ||
| 12 | use UNIVERSAL::require; | ||
| 13 | use YAML::Syck (); | ||
| 14 | |||
| 15 | __PACKAGE__->mk_accessors( qw{ | ||
| 16 | server_class server_file server_conf server_state | ||
| 17 | |||
| 18 | mixin_classes | ||
| 19 | |||
| 20 | clone_class clone_conf | ||
| 21 | }); | ||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | Bot::Net::Script::Server - Create a new server | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | bin/botnet server --name <server name> [ <options> ] | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This script create the required class and configuration for a new bot server. Bot servers are used to host groups of bots (and possibly humans) where they can interact. This will also help build stubs for tests. | ||
| 34 | |||
| 35 | If you only specify the server name, a vanilla server will be created. This server will use L<Bot::Net::Server>, but no other mixins. This is probably not what you want. The options can be used to either clone another server you already have available in your bot net application or add additional mixins. | ||
| 36 | |||
| 37 | =head1 OPTIONS | ||
| 38 | |||
| 39 | --name <server name> - The name of the server class to create | ||
| 40 | --mixin <mixin name> - The name of the server mixin to add to the class. | ||
| 41 | You may use this option multiple times to add | ||
| 42 | multiple mixins. | ||
| 43 | --clone <server name> - The name of an existing server to clone from. By | ||
| 44 | using this option, the new server will inherit | ||
| 45 | from the existing server and receive a copy of | ||
| 46 | the original server's configuration. | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 options | ||
| 51 | |||
| 52 | Returns the arguments used by this script. See L<App::CLI::Command>. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub options { | ||
| 57 | ( | ||
| 58 | 'name=s' => 'name', | ||
| 59 | 'mixin=s@' => 'mixins', | ||
| 60 | 'clone=s' => 'clone', | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 run | ||
| 65 | |||
| 66 | Create the server class and associated files. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub run { | ||
| 71 | my ($self, @arg) = @_; | ||
| 72 | |||
| 73 | defined $self->{name} | ||
| 74 | or die "No server name given with required --name option.\n"; | ||
| 75 | |||
| 76 | $self->server_class( Bot::Net->net_class('Server', $self->{name}) ); | ||
| 77 | $self->server_file( | ||
| 78 | File::Spec->catfile( | ||
| 79 | $FindBin::Bin, '..', 'lib', | ||
| 80 | split(/::/, $self->server_class) | ||
| 81 | ) . '.pm' | ||
| 82 | ); | ||
| 83 | $self->server_conf( | ||
| 84 | File::Spec->catfile( | ||
| 85 | $FindBin::Bin, '..', 'etc', 'server', | ||
| 86 | split(/::/, $self->{name}) | ||
| 87 | ) . '.yml' | ||
| 88 | ); | ||
| 89 | $self->server_state( | ||
| 90 | File::Spec->catfile( | ||
| 91 | $FindBin::Bin, '..', 'var', 'server', | ||
| 92 | split(/::/, $self->{name}) | ||
| 93 | ) . '.db' | ||
| 94 | ); | ||
| 95 | |||
| 96 | my @mixins = @{ $self->{mixins} || [] }; | ||
| 97 | my @mixin_classes = ( | ||
| 98 | 'Bot::Net::Server', | ||
| 99 | map { 'Bot::Net::Mixin::Server::'.$_ } @mixins | ||
| 100 | ); | ||
| 101 | |||
| 102 | $self->mixin_classes( \@mixin_classes ); | ||
| 103 | |||
| 104 | if ($self->{clone}) { | ||
| 105 | $self->clone_class( Bot::Net->net_class('Server', $self->{clone}) ); | ||
| 106 | |||
| 107 | $self->clone_config( | ||
| 108 | File::Spec->catfile( | ||
| 109 | $FindBin::Bin, '..', 'etc', 'server', | ||
| 110 | split(/::/, $self->{clone}) | ||
| 111 | ) . '.yml' | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->_create_server_module; | ||
| 116 | $self->_create_server_config; | ||
| 117 | # TODO Add _create_server_test; | ||
| 118 | # $self->_create_server_test; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _create_server_module { | ||
| 122 | my $self = shift; | ||
| 123 | |||
| 124 | open my $servermod, '>', $self->server_file | ||
| 125 | or die "Cannot write to @{[$self->server_file]}: $!"; | ||
| 126 | |||
| 127 | print "Creating ",$self->server_file,"...\n"; | ||
| 128 | |||
| 129 | print $servermod <<"END_OF_SERVER_MODULE_START"; | ||
| 130 | use strict; | ||
| 131 | use warnings; | ||
| 132 | |||
| 133 | package @{[$self->server_class]}; | ||
| 134 | END_OF_SERVER_MODULE_START | ||
| 135 | |||
| 136 | if ($self->{clone}) { | ||
| 137 | print $servermod <<"END_OF_SERVER_MODULE_CLONE"; | ||
| 138 | |||
| 139 | use base qw/ @{[$self->clone_class]} /; | ||
| 140 | |||
| 141 | END_OF_SERVER_MODULE_CLONE | ||
| 142 | } | ||
| 143 | |||
| 144 | else { | ||
| 145 | print $servermod <<"END_OF_SERVER_MODULE_NEW"; | ||
| 146 | |||
| 147 | @{[join "\n", map { 'use '.$_.';' } @{ $self->mixin_classes }]} | ||
| 148 | |||
| 149 | END_OF_SERVER_MODULE_NEW | ||
| 150 | } | ||
| 151 | |||
| 152 | print $servermod <<"END_OF_SERVER_MODULE_END"; | ||
| 153 | =head1 NAME | ||
| 154 | |||
| 155 | @{[$self->server_class]} - A host for semi-autonomous agents | ||
| 156 | |||
| 157 | =head1 SYNOPSIS | ||
| 158 | |||
| 159 | bin/botnet run --server $self->{name} | ||
| 160 | |||
| 161 | =head1 DESCRIPTION | ||
| 162 | |||
| 163 | A host for semi-autonomous agents. This documentation needs replacing. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | 1; | ||
| 168 | |||
| 169 | END_OF_SERVER_MODULE_END | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _create_server_config { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | if ($self->{clone} and -f $self->clone_conf) { | ||
| 176 | print "Copying ",$self->clone_conf," to ",$self->server_conf,"...\n"; | ||
| 177 | copy($self->clone_conf, $self->server_conf); | ||
| 178 | } | ||
| 179 | |||
| 180 | else { | ||
| 181 | print "Creating ",$self->server_conf,"...\n"; | ||
| 182 | |||
| 183 | my @configs; | ||
| 184 | for my $mixin_class (@{ $self->mixin_classes || [] }) { | ||
| 185 | $mixin_class->require; | ||
| 186 | if (my $method = $mixin_class->can('default_configuration')) { | ||
| 187 | push @configs, $method->($mixin_class, $self->server_class); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | my $server_conf = Hash::Merge::merge( @configs ); | ||
| 192 | |||
| 193 | YAML::Syck::DumpFile( $self->server_conf, $server_conf ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | =head1 AUTHORS | ||
| 198 | |||
| 199 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||
| 200 | |||
| 201 | =head1 COPYRIGHT AND LICENSE | ||
| 202 | |||
| 203 | Copyright 2007 Boomer Consulting, Inc. All Rights Reserved. | ||
| 204 | |||
| 205 | This program is free software and may be modified and distributed under the same terms as Perl itself. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | 1; |