From: Rafael Kitover Date: Wed, 8 Dec 2010 13:34:30 +0000 (+0000) Subject: make use_moose detection more robust X-Git-Tag: v0.46~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Model-DBIC-Schema.git;a=commitdiff_plain;h=1fcd7804144856dad9148ced28e5648f28fb86d1 make use_moose detection more robust --- diff --git a/Changes b/Changes index f1aedb3..a73e8b7 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension Catalyst::Model::DBIC::Schema + - make use_moose detection more robust + 0.45 Wed Dec 8 12:05:58 UTC 2010 - fix bug where non-result files were picked up for Moose check diff --git a/lib/Catalyst/Helper/Model/DBIC/Schema.pm b/lib/Catalyst/Helper/Model/DBIC/Schema.pm index 0a5b490..c16a0d1 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -17,6 +17,7 @@ use List::MoreUtils 'firstidx'; use Scalar::Util 'looks_like_number'; use File::Find 'finddepth'; use Try::Tiny; +use Cwd 'getcwd'; =head1 NAME @@ -146,6 +147,7 @@ has loader_args => (is => 'rw', isa => HashRef); has connect_info => (is => 'rw', isa => HashRef); has old_schema => (is => 'rw', isa => Bool, lazy_build => 1); has is_moose_schema => (is => 'rw', isa => Bool, lazy_build => 1); +has result_namespace => (is => 'rw', isa => Str, lazy_build => 1); has components => (is => 'rw', isa => ArrayRef); =head1 METHODS @@ -389,31 +391,25 @@ sub _build_helper_connect_info { sub _build_old_schema { my $self = shift; - my @schema_pm = split '::', $self->schema_class; - $schema_pm[-1] .= '.pm'; - my $schema_file = - File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm); - - if (-f $schema_file) { - my $schema_code = do { local (@ARGV, $/) = $schema_file; <> }; - return 1 if $schema_code =~ /->load_classes/; - } - - 0; + return $self->result_namespace eq '' ? 1 : 0; } sub _build_is_moose_schema { my $self = shift; my @schema_parts = split '::', $self->schema_class; - my $schema_dir = - File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts); + + my $result_dir = File::Spec->catfile( + $self->helper->{base}, 'lib', @schema_parts, $self->result_namespace + ); # assume yes for new schemas - return 1 if not -d $schema_dir; + return 1 if not -d $result_dir; my $uses_moose = 1; + my $cwd = getcwd; + try { finddepth(sub { return if $File::Find::name !~ /\.pm\z/; @@ -427,12 +423,47 @@ sub _build_is_moose_schema { $uses_moose = 0 if $code !~ /\nuse Moose;\n/; die; - }, $schema_dir); + }, $result_dir); }; + chdir $cwd; + return $uses_moose; } +sub _build_result_namespace { + my $self = shift; + + my @schema_parts = split '::', $self->schema_class; + my $schema_pm = + File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts) . '.pm'; + + if (not -f $schema_pm) { + try { Class::MOP::load_class('DBIx::Class::Schema::Loader') }; + + return 'Result' if $@; + + return DBIx::Class::Schema::Loader->VERSION('0.05') ? 'Result' : ''; + } + + open my $fh, '<', $schema_pm or die "Could not open $schema_pm: $!"; + my $code = do { local $/; <$fh> }; + close $fh; + + my ($result_namespace) = $code =~ /result_namespace => '([^']+)'/; + + if (not $result_namespace) { + if ($code =~ /->load_classes/) { + $result_namespace = ''; + } + else { + $result_namespace = 'Result'; + } + } + + return $result_namespace; +} + sub _data_struct_to_string { my ($self, $data) = @_; @@ -557,7 +588,7 @@ sub _gen_static_schema { my $schema_dir = File::Spec->catfile($helper->{base}, 'lib'); - eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') }; + try { Class::MOP::load_class('DBIx::Class::Schema::Loader') }; die "Cannot load DBIx::Class::Schema::Loader: $@" if $@; DBIx::Class::Schema::Loader->import( diff --git a/t/05testapp.t b/t/05testapp.t index f8f133a..552ef45 100644 --- a/t/05testapp.t +++ b/t/05testapp.t @@ -113,6 +113,43 @@ foreach my $tparam (@$test_params) { } } +# Test that a moose schema is not detected as a non-moose schema due to an +# errant file. +{ + cleanup_schema(); + + system($^X, "-I$blib_dir", $creator, 'model', + 'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN', + 'create=static', 'dbi:SQLite:testdb.db' + ); + + mkdir "$schema_dir/.svn"; + open my $fh, '>', "$schema_dir/.svn/foo" + or die "Could not open $schema_dir/.svn/foo for writing: $!"; + print $fh "gargle\n"; + close $fh; + + mkdir "$schema_dir/Result/.svn"; + open $fh, '>', "$schema_dir/Result/.svn/foo" + or die "Could not open $schema_dir/Result/.svn/foo for writing: $!"; + print $fh "hlagh\n"; + close $fh; + + system($^X, "-I$blib_dir", $creator, 'model', + 'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN', + 'create=static', 'dbi:SQLite:testdb.db' + ); + + for my $file (result_files()) { + my $code = code_for($file); + + like $code, qr/use Moose;\n/, + 'use_moose detection not confused by version control files'; + like $code, qr/__PACKAGE__->meta->make_immutable;\n/, + 'use_moose detection not confused by version control files'; + } +} + done_testing; sub rm_rf {