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