]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Config.pm
76a605085b6fc0a8777fde3669abe033505c18f2
[perl/modules/rgit.git] / lib / App / Rgit / Config.pm
1 package App::Rgit::Config;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7 use Cwd qw/abs_path/;
8 use File::Spec::Functions qw/file_name_is_absolute/;
9
10 use App::Rgit::Repository;
11 use App::Rgit::Utils qw/validate :levels/;
12
13 use constant IS_WIN32 => $^O eq 'MSWin32';
14
15 =head1 NAME
16
17 App::Rgit::Config - Base class for App::Rgit configurations.
18
19 =head1 VERSION
20
21 Version 0.06
22
23 =cut
24
25 our $VERSION = '0.06';
26
27 =head1 DESCRIPTION
28
29 Base class for L<App::Rgit> configurations.
30
31 This is an internal class to L<rgit>.
32
33 =head1 METHODS
34
35 =head2 C<< new root => $root, git => $git >>
36
37 Creates a new configuration object based on the root directory C<$root> and using C<$git> as F<git> executable.
38
39 =cut
40
41 sub new {
42  my ($class, %args) = &validate;
43
44  my $root = $args{root};
45  return unless defined $root and -d $root;
46  $root = abs_path $root unless file_name_is_absolute $root;
47
48  my $git = $args{git};
49  return unless defined $git;
50  if (IS_WIN32) {
51   unless (-x $git) {
52    $git .= '.bat';
53    return unless -x $git;
54   }
55  } else {
56   return unless -x $git;
57  }
58
59  my $conf = 'App::Rgit::Config::Default';
60  eval "require $conf; 1" or croak "Couldn't load $conf: $@";
61
62  my $r = App::Rgit::Repository->new(fake => 1);
63  return unless defined $r;
64
65  bless {
66   root     => $root,
67   git      => $git,
68   cwd_repo => $r,
69   debug    => defined $args{debug} ? int $args{debug} : WARN,
70  }, $conf;
71 }
72
73 =head2 C<info $msg>
74
75 =head2 C<warn $msg>
76
77 =head2 C<err $msg>
78
79 =head2 C<crit $msg>
80
81 Notifies a message C<$msg> of the corresponding level.
82
83 =cut
84
85 sub _notify {
86  my $self = shift;
87  my $level = shift;
88  if ($self->debug >= $level) {
89   print STDERR @_;
90   return 1;
91  }
92  return 0;
93 }
94
95 sub info { shift->_notify(INFO, @_) }
96
97 sub warn { shift->_notify(WARN, @_) }
98
99 sub err  { shift->_notify(ERR, @_) }
100
101 sub crit { shift->_notify(CRIT, @_) }
102
103 =head2 C<root>
104
105 =head2 C<git>
106
107 =head2 C<repos>
108
109 =head2 C<cwd_repo>
110
111 =head2 C<debug>
112
113 Read-only accessors.
114
115 =cut
116
117 BEGIN {
118  eval "sub $_ { \$_[0]->{$_} }" for qw/root git cwd_repo debug/;
119 }
120
121 =head1 SEE ALSO
122
123 L<rgit>.
124
125 =head1 AUTHOR
126
127 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
128
129 You can contact me by mail or on C<irc.perl.org> (vincent).
130
131 =head1 BUGS
132
133 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.
134
135 =head1 SUPPORT
136
137 You can find documentation for this module with the perldoc command.
138
139     perldoc App::Rgit::Config
140
141 =head1 COPYRIGHT & LICENSE
142
143 Copyright 2008-2009 Vincent Pit, all rights reserved.
144
145 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
146
147 =cut
148
149 1; # End of App::Rgit::Config