]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Command.pm
Store the repos as-is in the command object
[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 Object::Tiny qw/cmd cwd_as_repo args repos/;
9
10 use App::Rgit::Utils qw/validate/;
11 use App::Rgit::Repository;
12
13 =head1 NAME
14
15 App::Rgit::Command - Base class for App::Rgit commands.
16
17 =head1 VERSION
18
19 Version 0.01
20
21 =cut
22
23 our $VERSION = '0.01';
24
25 =head1 DESCRIPTION
26
27 Base class for L<App::Rgit> commands.
28
29 This is an internal class to L<rgit>.
30
31 =head1 METHODS
32
33 =head2 C<< new cmd => $cmd, args => \@args, repos => \@repos >>
34
35 Creates a new command object for C<$cmd> that will called for all repositories C<@repos> with arguments C<@args>.
36
37 =cut
38
39 my %commands;
40 __PACKAGE__->action($_ => 'Once') for qw/version help daemon init/, ' ';
41
42 sub new {
43  my ($class, %args) = &validate;
44  my $cmd = $args{cmd};
45  $cmd = ' ' unless defined $cmd;
46  my $action = $class->action($cmd);
47  croak "Command $cmd shouldn't be executed as an $action"
48                            unless $class eq __PACKAGE__ or $class->isa($action);
49  eval "require $action; 1" or croak "Couldn't load $action: $@";
50  my $r = App::Rgit::Repository->new(fake => 1);
51  return unless defined $r;
52  $action->SUPER::new(
53   cmd         => $cmd,
54   args        => $args{args} || [ ],
55   repos       => $args{repos},
56   cwd_as_repo => $r,
57  );
58 }
59
60 =head2 C<< action $cmd [ => $pkg ] >>
61
62 If C<$pkg> is supplied, handles command C<$cmd> with C<$pkg> objects.
63 Otherwise, returns the current class for C<$cmd>.
64
65 =cut
66
67 sub action {
68  my ($self, $cmd, $pkg) = @_;
69  $cmd = $self->cmd if !defined $cmd
70                    and defined $self and $self->isa(__PACKAGE__);
71  return unless defined $cmd;
72  unless (defined $pkg) {
73   return __PACKAGE__ . '::Each' unless defined $commands{$cmd};
74   return $commands{$cmd}
75  }
76  $pkg = __PACKAGE__ . '::' . $pkg unless $pkg =~ /:/;
77  $commands{$cmd} = $pkg;
78 }
79
80 =head2 C<cmd>
81
82 =head2 C<cwd_as_repo>
83
84 =head2 C<args>
85
86 =head2 C<repos>
87
88 Accessors.
89
90 =head2 C<run $conf>
91
92 Runs the command with a L<App::Rgit::Config> configuration object.
93 Stops as soon as one of the executed commands fails, and returns the corresponding exit code.
94 Returns zero when all went fine.
95 Implemented in subclasses.
96
97 =head1 SEE ALSO
98
99 L<rgit>.
100
101 =head1 AUTHOR
102
103 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
104    
105 You can contact me by mail or on C<irc.perl.org> (vincent).
106
107 =head1 BUGS
108
109 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.
110
111 =head1 SUPPORT
112
113 You can find documentation for this module with the perldoc command.
114
115     perldoc App::Rgit::Command
116
117 =head1 COPYRIGHT & LICENSE
118
119 Copyright 2008 Vincent Pit, all rights reserved.
120
121 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
122
123 =cut
124
125 1; # End of App::Rgit::Command