Commit | Line | Data |
7bf1a6f5 |
1 | package Gitalist::Model::CollectionOfRepos; |
21336a02 |
2 | |
3 | use Moose; |
afbb1a52 |
4 | use Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive; |
cd169152 |
5 | use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories; |
b70462a4 |
6 | use Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList; |
bd7cb7a1 |
7 | use MooseX::Types::Moose qw/Maybe ArrayRef/; |
bddfb71e |
8 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
a18818fd |
9 | use Moose::Util::TypeConstraints; |
a7010acf |
10 | use Moose::Autobox; |
21336a02 |
11 | use namespace::autoclean; |
12 | |
bddfb71e |
13 | extends 'Catalyst::Model'; |
21336a02 |
14 | |
bddfb71e |
15 | with 'Catalyst::Component::InstancePerContext'; |
16 | |
a18818fd |
17 | my $repo_dir_t = subtype NonEmptySimpleStr, |
18 | where { -d $_ }, |
19 | 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' }; |
20 | |
a7010acf |
21 | my $arrayof_repos_dir_t = subtype ArrayRef[$repo_dir_t], |
22 | where { 1 }, |
23 | message { 'Cannot find repository directories listed in config - these are invalid directories: ' . join(', ', $_->flatten) }; |
24 | |
25 | coerce $arrayof_repos_dir_t, |
26 | from NonEmptySimpleStr, |
27 | via { [ $_ ] }; |
28 | |
a18818fd |
29 | has config_repo_dir => ( |
bddfb71e |
30 | isa => NonEmptySimpleStr, |
31 | is => 'ro', |
a18818fd |
32 | init_arg => 'repo_dir', |
33 | predicate => 'has_config_repo_dir', |
bddfb71e |
34 | ); |
35 | |
a18818fd |
36 | has repo_dir => ( |
a7010acf |
37 | isa => $repo_dir_t, |
a18818fd |
38 | is => 'ro', |
39 | lazy_build => 1 |
40 | ); |
41 | |
bd7cb7a1 |
42 | has repos => ( |
a7010acf |
43 | isa => $arrayof_repos_dir_t, |
bd7cb7a1 |
44 | is => 'ro', |
45 | default => sub { [] }, |
46 | traits => ['Array'], |
47 | handles => { |
48 | _repos_count => 'count', |
49 | }, |
a7010acf |
50 | coerce => 1, |
bd7cb7a1 |
51 | ); |
52 | |
1891c774 |
53 | |
54 | has search_recursively => ( |
55 | is => 'ro', |
56 | isa => 'Bool', |
57 | default => 0, |
58 | ); |
59 | |
1d727634 |
60 | has export_ok => ( |
61 | is => 'ro', |
62 | isa => 'Str', |
63 | ); |
64 | |
b70462a4 |
65 | has whitelist => ( |
66 | is => 'ro', |
67 | isa => 'Str', |
68 | ); |
69 | |
a18818fd |
70 | sub _build_repo_dir { |
71 | my $self = shift; |
72 | $ENV{GITALIST_REPO_DIR} ? |
73 | $ENV{GITALIST_REPO_DIR} |
74 | : $self->has_config_repo_dir |
75 | ? $self->config_repo_dir |
76 | : ''; |
77 | } |
78 | |
79 | after BUILD => sub { |
80 | my $self = shift; |
bd7cb7a1 |
81 | # Explode loudly at app startup time if there is no list of |
82bc0f05 |
82 | # repositories or repos dir, rather than on first hit |
bd7cb7a1 |
83 | $self->_repos_count || $self->repo_dir; |
a18818fd |
84 | }; |
85 | |
bddfb71e |
86 | sub build_per_context_instance { |
87 | my ($self, $app) = @_; |
1d727634 |
88 | |
89 | my %args = (export_ok => $self->export_ok || ''); |
90 | my $class; |
b70462a4 |
91 | if($self->whitelist && -f $self->whitelist) { |
92 | $class = 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList'; |
93 | $args{repo_dir} = $self->repo_dir; |
94 | $args{whitelist} = $self->whitelist; |
1891c774 |
95 | } elsif ($self->_repos_count && !$self->search_recursively) { |
1d727634 |
96 | $class = 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories'; |
97 | $args{repos} = $self->repos; |
07ee9dc1 |
98 | } elsif($self->search_recursively) { |
1d727634 |
99 | $class = 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive'; |
100 | $args{repo_dir} = $self->repo_dir; |
07ee9dc1 |
101 | } else { |
102 | $class = 'Gitalist::Git::CollectionOfRepositories::FromDirectory'; |
103 | $args{repo_dir} = $self->repo_dir; |
bd7cb7a1 |
104 | } |
1d727634 |
105 | |
106 | return $class->new(%args); |
21336a02 |
107 | } |
108 | |
109 | __PACKAGE__->meta->make_immutable; |
bddfb71e |
110 | |
775e96e0 |
111 | __END__ |
112 | |
2298d93f |
113 | =encoding UTF-8 |
114 | |
115 | =head1 NAME |
116 | |
117 | Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist |
118 | |
775e96e0 |
119 | =head1 AUTHORS |
120 | |
121 | See L<Gitalist> for authors. |
122 | |
123 | =head1 LICENSE |
124 | |
125 | See L<Gitalist> for the license. |
126 | |
127 | =cut |