]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Config.pm
Get rid of validate()
[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/: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 = shift;
43  $class = ref $class || $class;
44
45  my %args = @_;
46
47  my $root = $args{root};
48  return unless defined $root and -d $root;
49  $root = abs_path $root unless file_name_is_absolute $root;
50
51  my $git = $args{git};
52  return unless defined $git;
53  if (IS_WIN32) {
54   unless (-x $git) {
55    $git .= '.bat';
56    return unless -x $git;
57   }
58  } else {
59   return unless -x $git;
60  }
61
62  my $conf = 'App::Rgit::Config::Default';
63  eval "require $conf; 1" or croak "Couldn't load $conf: $@";
64
65  my $r = App::Rgit::Repository->new(fake => 1);
66  return unless defined $r;
67
68  bless {
69   root     => $root,
70   git      => $git,
71   cwd_repo => $r,
72   debug    => defined $args{debug} ? int $args{debug} : WARN,
73  }, $conf;
74 }
75
76 =head2 C<info $msg>
77
78 =head2 C<warn $msg>
79
80 =head2 C<err $msg>
81
82 =head2 C<crit $msg>
83
84 Notifies a message C<$msg> of the corresponding level.
85
86 =cut
87
88 sub _notify {
89  my $self = shift;
90  my $level = shift;
91  if ($self->debug >= $level) {
92   print STDERR @_;
93   return 1;
94  }
95  return 0;
96 }
97
98 sub info { shift->_notify(INFO, @_) }
99
100 sub warn { shift->_notify(WARN, @_) }
101
102 sub err  { shift->_notify(ERR, @_) }
103
104 sub crit { shift->_notify(CRIT, @_) }
105
106 =head2 C<root>
107
108 =head2 C<git>
109
110 =head2 C<repos>
111
112 =head2 C<cwd_repo>
113
114 =head2 C<debug>
115
116 Read-only accessors.
117
118 =cut
119
120 BEGIN {
121  eval "sub $_ { \$_[0]->{$_} }" for qw/root git cwd_repo debug/;
122 }
123
124 =head1 SEE ALSO
125
126 L<rgit>.
127
128 =head1 AUTHOR
129
130 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
131
132 You can contact me by mail or on C<irc.perl.org> (vincent).
133
134 =head1 BUGS
135
136 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.
137
138 =head1 SUPPORT
139
140 You can find documentation for this module with the perldoc command.
141
142     perldoc App::Rgit::Config
143
144 =head1 COPYRIGHT & LICENSE
145
146 Copyright 2008-2009 Vincent Pit, all rights reserved.
147
148 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
149
150 =cut
151
152 1; # End of App::Rgit::Config