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