]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Command.pm
Make sure the POD headings are linkable
[perl/modules/rgit.git] / lib / App / Rgit / Command.pm
1 package App::Rgit::Command;
2
3 use strict;
4 use warnings;
5
6 use Carp ();
7
8 use App::Rgit::Utils qw/:codes/;
9
10 =head1 NAME
11
12 App::Rgit::Command - Base class for App::Rgit commands.
13
14 =head1 VERSION
15
16 Version 0.08
17
18 =cut
19
20 our $VERSION = '0.08';
21
22 =head1 DESCRIPTION
23
24 Base class for L<App::Rgit> commands.
25
26 This is an internal class to L<rgit>.
27
28 =head1 METHODS
29
30 =head2 C<new>
31
32     my $arc = App::Rgit::Command->new(
33      cmd  => $cmd,
34      args => \@args,
35     );
36
37 Creates a new command object for C<$cmd> that is bound to be called with arguments C<@args>.
38
39 =cut
40
41 my %commands;
42 __PACKAGE__->action($_ => 'Once') for qw/daemon gui help init version/, ' ';
43
44 sub new {
45  my $class = shift;
46  $class = ref $class || $class;
47
48  my %args = @_;
49
50  my $cmd = $args{cmd};
51  $cmd = ' ' unless defined $cmd;
52
53  my $action = $class->action($cmd);
54
55  if ($class eq __PACKAGE__) {
56   $class = $action;
57  } else {
58   Carp::confess("Command $cmd should be executed as a $action")
59                                                     unless $class->isa($action);
60  }
61
62  eval "require $action; 1" or Carp::confess("Couldn't load $action: $@");
63
64  bless {
65   cmd    => $cmd,
66   args   => $args{args} || [ ],
67   policy => $args{policy},
68  }, $class;
69 }
70
71 =head2 C<action>
72
73     my $pkg = $arc->action($cmd);
74     $arc->action($cmd => $pkg);
75
76 If C<$pkg> is supplied, handles command C<$cmd> with C<$pkg> objects.
77 Otherwise, returns the current class for C<$cmd>.
78
79 =cut
80
81 sub action {
82  my ($self, $cmd, $pkg) = @_;
83  if (not defined $cmd) {
84   return unless defined $self and ref $self and $self->isa(__PACKAGE__);
85   $cmd = $self->cmd;
86  }
87  unless (defined $pkg) {
88   return __PACKAGE__ . '::Each' unless defined $commands{$cmd};
89   return $commands{$cmd}
90  }
91  $pkg = __PACKAGE__ . '::' . $pkg unless $pkg =~ /:/;
92  $commands{$cmd} = $pkg;
93 }
94
95 =head2 C<report>
96
97     my $code = $arc->report($conf, $repo, $status);
98
99 Reports that the execution of the command in C<$repo> exited with C<$status> to the current command's policy.
100 Returns what policy C<report> method returned, which should be one of the policy codes listed in C<App::Rgit::Utils>.
101
102 =cut
103
104 sub report {
105  my ($self) = @_;
106
107  my $code = $self->policy->handle(@_);
108
109  return defined $code ? $code : NEXT;
110 }
111
112 =head2 C<cmd>
113
114 =head2 C<args>
115
116 =head2 C<policy>
117
118 Read-only accessors.
119
120 =cut
121
122 BEGIN {
123  eval "sub $_ { \$_[0]->{$_} }" for qw/cmd args policy/;
124 }
125
126 =head2 C<run>
127
128     my $code = $arc->run($conf);
129
130 Runs the command with a L<App::Rgit::Config> configuration object.
131 Handles back the code to return to the system and the last policy.
132 Implemented in subclasses.
133
134 =head1 SEE ALSO
135
136 L<rgit>.
137
138 =head1 AUTHOR
139
140 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
141
142 You can contact me by mail or on C<irc.perl.org> (vincent).
143
144 =head1 BUGS
145
146 Please report any bugs or feature requests to C<bug-rgit at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=rgit>.
147 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
148
149 =head1 SUPPORT
150
151 You can find documentation for this module with the perldoc command.
152
153     perldoc App::Rgit::Command
154
155 =head1 COPYRIGHT & LICENSE
156
157 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
158
159 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
160
161 =cut
162
163 1; # End of App::Rgit::Command