Less madness with the environment. Not tested
[catagits/Gitalist.git] / lib / Gitalist / Model / CollectionOfRepos.pm
CommitLineData
7bf1a6f5 1package Gitalist::Model::CollectionOfRepos;
21336a02 2
3use Moose;
afbb1a52 4use Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive;
cd169152 5use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories;
b70462a4 6use Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList;
bd7cb7a1 7use MooseX::Types::Moose qw/Maybe ArrayRef/;
bddfb71e 8use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
a18818fd 9use Moose::Util::TypeConstraints;
a7010acf 10use Moose::Autobox;
21336a02 11use namespace::autoclean;
12
bddfb71e 13extends 'Catalyst::Model';
21336a02 14
5e26dc93 15with 'Catalyst::Component::ApplicationAttribute';
bddfb71e 16with 'Catalyst::Component::InstancePerContext';
17
a18818fd 18my $repo_dir_t = subtype NonEmptySimpleStr,
19 where { -d $_ },
20 message { 'Cannot find repository dir: "' . $_ . '", please set up gitalist.conf, or set GITALIST_REPO_DIR environment or pass the --repo_dir parameter when starting the application' };
21
a7010acf 22my $arrayof_repos_dir_t = subtype ArrayRef[$repo_dir_t],
23 where { 1 },
24 message { 'Cannot find repository directories listed in config - these are invalid directories: ' . join(', ', $_->flatten) };
25
26coerce $arrayof_repos_dir_t,
27 from NonEmptySimpleStr,
28 via { [ $_ ] };
29
a18818fd 30has config_repo_dir => (
bddfb71e 31 isa => NonEmptySimpleStr,
32 is => 'ro',
a18818fd 33 init_arg => 'repo_dir',
34 predicate => 'has_config_repo_dir',
bddfb71e 35);
36
a18818fd 37has repo_dir => (
a7010acf 38 isa => $repo_dir_t,
a18818fd 39 is => 'ro',
40 lazy_build => 1
41);
42
bd7cb7a1 43has repos => (
a7010acf 44 isa => $arrayof_repos_dir_t,
bd7cb7a1 45 is => 'ro',
46 default => sub { [] },
47 traits => ['Array'],
48 handles => {
49 _repos_count => 'count',
50 },
a7010acf 51 coerce => 1,
bd7cb7a1 52);
53
e33993c9 54has class => (
55 isa => NonEmptySimpleStr,
56 is => 'ro',
57);
58
59has args => (
60 isa => 'HashRef',
61 is => 'ro',
62 default => sub { {} },
63);
1891c774 64
65has search_recursively => (
66 is => 'ro',
67 isa => 'Bool',
68 default => 0,
69);
70
1d727634 71has export_ok => (
72 is => 'ro',
73 isa => 'Str',
74);
75
b70462a4 76has whitelist => (
77 is => 'ro',
78 isa => 'Str',
79);
80
a18818fd 81sub _build_repo_dir {
82 my $self = shift;
5e26dc93 83 my $repo_dir = $self->_application->run_options->{repo_dir};
84
85 $repo_dir ?
86 $repo_dir
a18818fd 87 : $self->has_config_repo_dir
88 ? $self->config_repo_dir
89 : '';
90}
91
92after BUILD => sub {
93 my $self = shift;
bd7cb7a1 94 # Explode loudly at app startup time if there is no list of
82bc0f05 95 # repositories or repos dir, rather than on first hit
bd7cb7a1 96 $self->_repos_count || $self->repo_dir;
a18818fd 97};
98
e33993c9 99sub _default_model_class {
100 my($self) = @_;
1d727634 101
b70462a4 102 if($self->whitelist && -f $self->whitelist) {
e33993c9 103 return 'FromDirectory::WhiteList';
1891c774 104 } elsif ($self->_repos_count && !$self->search_recursively) {
e33993c9 105 return 'FromListOfDirectories';
07ee9dc1 106 } elsif($self->search_recursively) {
e33993c9 107 return 'FromDirectoryRecursive';
bd7cb7a1 108 }
1d727634 109
e33993c9 110 return 'FromDirectory';
111}
112
113sub build_per_context_instance {
114 my ($self, $app) = @_;
115
116 my %args = (
117 export_ok => $self->export_ok || '',
118 %{ $self->args }
119 );
120
121 my $class = $self->class;
122 Class::MOP::load_class($class) if $class;
123
124 my $default = $self->_default_model_class;
125
126 $args{whitelist} = $self->whitelist if $default eq 'FromDirectory::WhiteList';
127 $args{repos} = $self->repos if $default eq 'FromListOfDirectories';
128 $args{repo_dir} = $self->repo_dir if $default =~ /\b(?:WhiteList|FromDirectory(?:Recursive)?)$/;
129
130 $class ||= "Gitalist::Git::CollectionOfRepositories::$default";
131
132 $app->log->debug("Using class '$class'");
133
1d727634 134 return $class->new(%args);
21336a02 135}
136
137__PACKAGE__->meta->make_immutable;
bddfb71e 138
775e96e0 139__END__
140
2298d93f 141=encoding UTF-8
142
143=head1 NAME
144
145Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist
146
775e96e0 147=head1 AUTHORS
148
149See L<Gitalist> for authors.
150
151=head1 LICENSE
152
153See L<Gitalist> for the license.
154
155=cut