X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FGitalist%2FModel%2FCollectionOfRepos.pm;h=090cb8cdc4265ce6059acd58ec5dade06c32d527;hb=13c42902c178ccbb5d2e9fd174e06e8995490ff3;hp=1e709c4f9f4bf28510f78afee9e05622ba08eb02;hpb=0fd9a84a1f3002b002585a3cb3dc59de8eb8d2c6;p=catagits%2FGitalist.git diff --git a/lib/Gitalist/Model/CollectionOfRepos.pm b/lib/Gitalist/Model/CollectionOfRepos.pm index 1e709c4..090cb8c 100644 --- a/lib/Gitalist/Model/CollectionOfRepos.pm +++ b/lib/Gitalist/Model/CollectionOfRepos.pm @@ -1,60 +1,48 @@ package Gitalist::Model::CollectionOfRepos; use Moose; -use Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive; -use Gitalist::Git::CollectionOfRepositories::FromListOfDirectories; -use Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList; -use MooseX::Types::Moose qw/Maybe ArrayRef/; +use MooseX::Types::Moose qw/Undef Maybe ArrayRef Str/; use MooseX::Types::Common::String qw/NonEmptySimpleStr/; +use MooseX::Types::LoadableClass qw/ LoadableClass /; +use Gitalist::Git::Types qw/ ArrayRefOfDirs Dir DirOrUndef /; use Moose::Util::TypeConstraints; use Moose::Autobox; +use Path::Class qw/ dir /; use namespace::autoclean; extends 'Catalyst::Model'; +with 'Catalyst::Component::ApplicationAttribute'; with 'Catalyst::Component::InstancePerContext'; -my $repo_dir_t = subtype NonEmptySimpleStr, - where { -d $_ }, - 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' }; - -my $arrayof_repos_dir_t = subtype ArrayRef[$repo_dir_t], - where { 1 }, - message { 'Cannot find repository directories listed in config - these are invalid directories: ' . join(', ', $_->flatten) }; - -coerce $arrayof_repos_dir_t, - from NonEmptySimpleStr, - via { [ $_ ] }; - -has config_repo_dir => ( - isa => NonEmptySimpleStr, - is => 'ro', - init_arg => 'repo_dir', - predicate => 'has_config_repo_dir', -); - -has repo_dir => ( - isa => $repo_dir_t, - is => 'ro', - lazy_build => 1 -); - -has repos => ( - isa => $arrayof_repos_dir_t, - is => 'ro', - default => sub { [] }, - traits => ['Array'], - handles => { - _repos_count => 'count', - }, - coerce => 1, -); - has class => ( - isa => NonEmptySimpleStr, + isa => LoadableClass, is => 'ro', + lazy => 1, + coerce => 1, + builder => '_build_class', ); +sub _build_class { + my ($self) = @_; + + if($self->whitelist && -f $self->whitelist) { + return 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList'; + } + elsif($self->search_recursively) { + return 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive'; + } + elsif ($self->repos) { + return 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories'; + } + elsif ($self->repos_dir) { + return 'Gitalist::Git::CollectionOfRepositories::FromDirectory'; + } + else { + return "Don't know where to get repositores from. Try a --repos_dir option, or setting up config"; + } +} + has args => ( isa => 'HashRef', is => 'ro', @@ -67,6 +55,7 @@ has search_recursively => ( default => 0, ); +## XX What is this for? has export_ok => ( is => 'ro', isa => 'Str', @@ -75,60 +64,62 @@ has export_ok => ( has whitelist => ( is => 'ro', isa => 'Str', + predicate => '_has_whitelist', ); -sub _build_repo_dir { - my $self = shift; - $ENV{GITALIST_REPO_DIR} ? - $ENV{GITALIST_REPO_DIR} - : $self->has_config_repo_dir - ? $self->config_repo_dir - : ''; -} - -after BUILD => sub { - my $self = shift; - # Explode loudly at app startup time if there is no list of - # repositories or repos dir, rather than on first hit - $self->_repos_count || $self->repo_dir; -}; +has repo_dir => ( + is => 'ro', + isa => DirOrUndef, + coerce => 1, + predicate => '_has_repo_dir', +); -sub _default_model_class { - my($self) = @_; +# Simple directory of repositories (for list) +has repos_dir => ( + is => 'ro', + isa => DirOrUndef, + coerce => 1, + builder => '_build_repos_dir', + lazy => 1, +); - if($self->whitelist && -f $self->whitelist) { - return 'FromDirectory::WhiteList'; - } elsif ($self->_repos_count && !$self->search_recursively) { - return 'FromListOfDirectories'; - } elsif($self->search_recursively) { - return 'FromDirectoryRecursive'; - } +# Directory containing list of one or more repositories +has repos => ( + is => 'ro', + isa => ArrayRefOfDirs, + coerce => 1, +); - return 'FromDirectory'; +sub _build_repos_dir { + my $self = shift; + my $opts = $self->_application->run_options || {}; + return $self->_has_repo_dir && $self->repo_dir + || $opts->{repos_dir} || $ENV{GITALIST_REPO_DIR} || undef; } sub build_per_context_instance { - my ($self, $app) = @_; + my ($self, $ctx) = @_; + + $self->class(); + + if ($self->repos_dir) { $self->repos_dir->resolve } my %args = ( export_ok => $self->export_ok || '', + $self->_has_whitelist ? (whitelist => $self->whitelist) : (), + repos => $self->repos, + repo_dir => $self->repos_dir, + vhost => $ctx->request->uri->host, %{ $self->args } ); my $class = $self->class; - Class::MOP::load_class($class) if $class; - - my $default = $self->_default_model_class; - $args{whitelist} = $self->whitelist if $default eq 'FromDirectory::WhiteList'; - $args{repos} = $self->repos if $default eq 'FromListOfDirectories'; - $args{repo_dir} = $self->repo_dir if $default =~ /\b(?:WhiteList|FromDirectory(?:Recursive)?)$/; + my $model = $class->new(%args); - $class ||= "Gitalist::Git::CollectionOfRepositories::$default"; + $ctx->log->debug("Using class '$class' " . $model->debug_string) if $ctx->debug; - $app->log->debug("Using class '$class'"); - - return $class->new(%args); + return $model; } __PACKAGE__->meta->make_immutable; @@ -141,6 +132,24 @@ __END__ Gitalist::Model::CollectionOfRepos - Model::CollectionOfRepos module for Gitalist +=head1 DESCRIPTION + +This Model is a factory for an object implementing the L +interface. + +The simple options passed on the command line (like C<--repos_dir>), a class will by picked by default +L. + +This can be overridden from config by explicitly passing in a class name and args for that class +in config: + + + class MyClassName + + ... + + + =head1 AUTHORS See L for authors.