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