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 | |
5e26dc93 |
15 | with 'Catalyst::Component::ApplicationAttribute'; |
bddfb71e |
16 | with 'Catalyst::Component::InstancePerContext'; |
17 | |
a18818fd |
18 | my $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 |
22 | my $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 | |
26 | coerce $arrayof_repos_dir_t, |
27 | from NonEmptySimpleStr, |
28 | via { [ $_ ] }; |
29 | |
a18818fd |
30 | has 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 |
37 | has repo_dir => ( |
a7010acf |
38 | isa => $repo_dir_t, |
a18818fd |
39 | is => 'ro', |
40 | lazy_build => 1 |
41 | ); |
42 | |
bd7cb7a1 |
43 | has 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 |
54 | has class => ( |
55 | isa => NonEmptySimpleStr, |
56 | is => 'ro', |
57 | ); |
58 | |
59 | has args => ( |
60 | isa => 'HashRef', |
61 | is => 'ro', |
62 | default => sub { {} }, |
63 | ); |
1891c774 |
64 | |
65 | has search_recursively => ( |
66 | is => 'ro', |
67 | isa => 'Bool', |
68 | default => 0, |
69 | ); |
70 | |
1d727634 |
71 | has export_ok => ( |
72 | is => 'ro', |
73 | isa => 'Str', |
74 | ); |
75 | |
b70462a4 |
76 | has whitelist => ( |
77 | is => 'ro', |
78 | isa => 'Str', |
79 | ); |
80 | |
a18818fd |
81 | sub _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 | |
92 | after 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 |
99 | sub _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 | |
113 | sub 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 | |
145 | Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist |
146 | |
775e96e0 |
147 | =head1 AUTHORS |
148 | |
149 | See L<Gitalist> for authors. |
150 | |
151 | =head1 LICENSE |
152 | |
153 | See L<Gitalist> for the license. |
154 | |
155 | =cut |