And support a list of directories in the Catalyst model as config
[catagits/Gitalist.git] / lib / Gitalist / Model / GitRepos.pm
CommitLineData
21336a02 1package Gitalist::Model::GitRepos;
2
3use Moose;
4use Gitalist::Git::Repo;
bd7cb7a1 5use Gitalist::Git::CollectionOfProjects::FromListOfDirectories;
6use MooseX::Types::Moose qw/Maybe ArrayRef/;
bddfb71e 7use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
a18818fd 8use Moose::Util::TypeConstraints;
21336a02 9use namespace::autoclean;
10
bddfb71e 11extends 'Catalyst::Model';
21336a02 12
bddfb71e 13with 'Catalyst::Component::InstancePerContext';
14
a18818fd 15my $repo_dir_t = subtype NonEmptySimpleStr,
16 where { -d $_ },
17 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' };
18
19has config_repo_dir => (
bddfb71e 20 isa => NonEmptySimpleStr,
21 is => 'ro',
a18818fd 22 init_arg => 'repo_dir',
23 predicate => 'has_config_repo_dir',
bddfb71e 24);
25
a18818fd 26has repo_dir => (
bd7cb7a1 27 isa => Maybe[$repo_dir_t],
a18818fd 28 is => 'ro',
29 lazy_build => 1
30);
31
bd7cb7a1 32has repos => (
33 isa => ArrayRef[$repo_dir_t],
34 is => 'ro',
35 default => sub { [] },
36 traits => ['Array'],
37 handles => {
38 _repos_count => 'count',
39 },
40);
41
a18818fd 42sub _build_repo_dir {
43 my $self = shift;
44 $ENV{GITALIST_REPO_DIR} ?
45 $ENV{GITALIST_REPO_DIR}
46 : $self->has_config_repo_dir
47 ? $self->config_repo_dir
48 : '';
49}
50
51after BUILD => sub {
52 my $self = shift;
bd7cb7a1 53 # Explode loudly at app startup time if there is no list of
54 # projects or repos dir, rather than on first hit
55 $self->_repos_count || $self->repo_dir;
a18818fd 56};
57
bddfb71e 58sub build_per_context_instance {
59 my ($self, $app) = @_;
bd7cb7a1 60 if ($self->_repos_count) {
61 Gitalist::Git::CollectionOfProjects::FromListOfDirectories->new(repos => $self->repos);
62 }
63 else {
64 Gitalist::Git::Repo->new(repo_dir => $self->repo_dir);
65 }
21336a02 66}
67
68__PACKAGE__->meta->make_immutable;
bddfb71e 69
775e96e0 70__END__
71
72=head1 AUTHORS
73
74See L<Gitalist> for authors.
75
76=head1 LICENSE
77
78See L<Gitalist> for the license.
79
80=cut