Merge branch script_roles
Tomas Doran [Sat, 22 Oct 2011 00:20:08 +0000 (17:20 -0700)]
Makefile.PL
lib/Catalyst/Script/CGI.pm
lib/Catalyst/Script/Create.pm
lib/Catalyst/Script/FastCGI.pm
lib/Catalyst/Script/Server.pm
lib/Catalyst/Script/Test.pm
lib/Catalyst/ScriptRunner.pm
t/aggregate/unit_core_scriptrunner.t
t/lib/ScriptTestApp/TraitFor/Script.pm [new file with mode: 0644]
t/lib/ScriptTestApp/TraitFor/Script/Bar.pm [new file with mode: 0644]
t/lib/ScriptTestApp/TraitFor/Script/Foo.pm [new file with mode: 0644]

index a3a25a4..71b4f1d 100644 (file)
@@ -18,6 +18,7 @@ requires 'namespace::autoclean' => '0.09';
 requires 'namespace::clean' => '0.13';
 requires 'B::Hooks::EndOfScope' => '0.08';
 requires 'MooseX::Emulate::Class::Accessor::Fast' => '0.00903';
+requires 'Class::Load' => '0.08';
 requires 'Class::MOP' => '0.95';
 requires 'Data::OptList';
 requires 'Moose' => '1.03';
index dc7f20f..3a9f449 100644 (file)
@@ -23,6 +23,10 @@ Catalyst::Script::CGI - The CGI Catalyst Script
 
 This is a script to run the Catalyst engine specialized for the CGI environment.
 
+=head1 SEE ALSO
+
+L<Catalyst::ScriptRunner>
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm
index 95c0710..721c192 100644 (file)
@@ -90,6 +90,10 @@ Existing component files are not overwritten.  If any of the component files
 to be created already exist the file will be written with a '.new' suffix.
 This behavior can be suppressed with the C<--force> option.
 
+=head1 SEE ALSO
+
+L<Catalyst::ScriptRunner>
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm
index 781c327..c3d2360 100644 (file)
@@ -153,6 +153,10 @@ Catalyst::Script::FastCGI - The FastCGI Catalyst Script
 
 Run a Catalyst application as fastcgi.
 
+=head1 SEE ALSO
+
+L<Catalyst::ScriptRunner>
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm
index 2de60b9..ed8aef0 100644 (file)
@@ -322,6 +322,10 @@ Catalyst::Script::Server - Catalyst test server
 
 Run a Catalyst test server for this application.
 
+=head1 SEE ALSO
+
+L<Catalyst::ScriptRunner>
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm
index 36934b2..554026e 100644 (file)
@@ -33,6 +33,10 @@ Catalyst::Script::Test - Test Catalyst application on the command line
 
 Script to perform a test hit against your application and display the output.
 
+=head1 SEE ALSO
+
+L<Catalyst::ScriptRunner>
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm
index ee2b1c1..1e9626e 100644 (file)
@@ -3,21 +3,48 @@ use Moose;
 use FindBin;
 use lib;
 use File::Spec;
-use namespace::autoclean;
+use Class::Load qw/ load_first_existing_class load_optional_class /;
+use namespace::autoclean -also => 'subclass_with_traits';
+use Try::Tiny;
+
+sub find_script_class {
+    my ($self, $app, $script) = @_;
+    return load_first_existing_class("${app}::Script::${script}", "Catalyst::Script::$script");
+}
+
+sub find_script_traits {
+    my ($self, @try) = @_;
+
+    return grep { load_optional_class($_) } @try;
+}
+
+sub subclass_with_traits {
+    my ($base, @traits) = @_;
+
+    my $meta = Class::MOP::class_of($base)->create_anon_class(
+        superclasses => [ $base   ],
+        roles        => [ @traits ],
+        cache        => 1,
+    );
+    $meta->add_method(meta => sub { $meta });
+
+    return $meta->name;
+}
 
 sub run {
-    my ($self, $class, $scriptclass, %args) = @_;
-    my $classtoload = "${class}::Script::$scriptclass";
+    my ($self, $appclass, $scriptclass) = @_;
 
     lib->import(File::Spec->catdir($FindBin::Bin, '..', 'lib'));
 
-    unless ( eval { Class::MOP::load_class($classtoload) } ) {
-        warn("Could not load $classtoload - falling back to Catalyst::Script::$scriptclass : $@\n")
-            if $@ !~ /Can't locate/;
-        $classtoload = "Catalyst::Script::$scriptclass";
-        Class::MOP::load_class($classtoload);
-    }
-    $classtoload->new_with_options( application_name => $class, %args )->run;
+    my $class = $self->find_script_class($appclass, $scriptclass);
+
+    my @possible_traits = ("${appclass}::TraitFor::Script::${scriptclass}", "${appclass}::TraitFor::Script");
+    my @traits = $self->find_script_traits(@possible_traits);
+
+    $class = subclass_with_traits($class, @traits)
+        if @traits;
+
+    $class->new_with_options( application_name => $appclass )->run;
 }
 
 __PACKAGE__->meta->make_immutable;
@@ -34,8 +61,21 @@ Catalyst::ScriptRunner - The Catalyst Framework script runner
 
 =head1 DESCRIPTION
 
-This class is responsible for running scripts, either in the application specific namespace
-(e.g. C<MyApp::Script::Server>), or the Catalyst namespace (e.g. C<Catalyst::Script::Server>)
+This class is responsible for loading and running scripts, either in the application specific namespace
+(e.g. C<MyApp::Script::Server>), or the Catalyst namespace (e.g. C<Catalyst::Script::Server>).
+
+If your application contains a custom script, then it will be used in preference to the generic
+script, and is expected to sub-class the standard script.
+
+=head1 TRAIT LOADING
+
+Catalyst will automatically load and apply roles to the scripts in your appliction.
+
+C<MyApp::TraitFor::Script> will be loaded if present, and will be applied to B<ALL>
+scripts.
+
+C<MyApp::TraitFor::Script::XXXX> will be loaded (if present) and for script
+individually.
 
 =head1 METHODS
 
index d9af3bc..763ddec 100644 (file)
@@ -2,23 +2,21 @@ use strict;
 use warnings;
 use Test::More;
 use FindBin qw/$Bin/;
+use Test::Exception;
 use lib "$Bin/../lib";
 
 use_ok('Catalyst::ScriptRunner');
 
-is Catalyst::ScriptRunner->run('ScriptTestApp', 'Foo'), 'ScriptTestApp::Script::Foo',
-    'Script existing only in app';
-is Catalyst::ScriptRunner->run('ScriptTestApp', 'Bar'), 'ScriptTestApp::Script::Bar',
+is Catalyst::ScriptRunner->run('ScriptTestApp', 'Foo'), 'mooScriptTestApp::Script::Foo42',
+    'Script existing only in app got trait applied';
+is Catalyst::ScriptRunner->run('ScriptTestApp', 'Bar'), 'mooScriptTestApp::Script::Bar23',
     'Script existing in both app and Catalyst - prefers app';
-is Catalyst::ScriptRunner->run('ScriptTestApp', 'Baz'), 'Catalyst::Script::Baz',
+is Catalyst::ScriptRunner->run('ScriptTestApp', 'Baz'), 'mooCatalyst::Script::Baz',
     'Script existing only in Catalyst';
 # +1 test for the params passed to new_with_options in t/lib/Catalyst/Script/Baz.pm
-{
-    my $warnings = '';
-    local $SIG{__WARN__} = sub { $warnings .= shift };
-    is 'Catalyst::Script::CompileTest', Catalyst::ScriptRunner->run('ScriptTestApp', 'CompileTest');
-    like $warnings, qr/Does not compile/;
-    like $warnings, qr/Could not load ScriptTestApp::Script::CompileTest - falling back to Catalyst::Script::CompileTest/;
-}
+
+throws_ok(sub {
+    Catalyst::ScriptRunner->run('ScriptTestApp', 'CompileTest');
+}, qr/Couldn't load class/);
 
 done_testing;
diff --git a/t/lib/ScriptTestApp/TraitFor/Script.pm b/t/lib/ScriptTestApp/TraitFor/Script.pm
new file mode 100644 (file)
index 0000000..43a50cc
--- /dev/null
@@ -0,0 +1,10 @@
+package ScriptTestApp::TraitFor::Script;
+use Moose::Role;
+use namespace::autoclean;
+
+around run => sub {
+    my ($orig, $self, @args) = @_;
+    return 'moo' . $self->$orig(@args);
+};
+
+1;
diff --git a/t/lib/ScriptTestApp/TraitFor/Script/Bar.pm b/t/lib/ScriptTestApp/TraitFor/Script/Bar.pm
new file mode 100644 (file)
index 0000000..5d5ecc7
--- /dev/null
@@ -0,0 +1,10 @@
+package ScriptTestApp::TraitFor::Script::Bar;
+use Moose::Role;
+use namespace::autoclean;
+
+around run => sub {
+    my ($orig, $self, @args) = @_;
+    return $self->$orig(@args) . '23';
+};
+
+1;
diff --git a/t/lib/ScriptTestApp/TraitFor/Script/Foo.pm b/t/lib/ScriptTestApp/TraitFor/Script/Foo.pm
new file mode 100644 (file)
index 0000000..0536a73
--- /dev/null
@@ -0,0 +1,10 @@
+package ScriptTestApp::TraitFor::Script::Foo;
+use Moose::Role;
+use namespace::autoclean;
+
+around run => sub {
+    my ($orig, $self, @args) = @_;
+    return $self->$orig(@args) . '42';
+};
+
+1;