[Bps-public-commit] r16885 - Prophet/trunk/lib/Prophet

jesse at bestpractical.com jesse at bestpractical.com
Mon Nov 17 18:27:50 EST 2008


Author: jesse
Date: Mon Nov 17 18:27:49 2008
New Revision: 16885

Modified:
   Prophet/trunk/lib/Prophet/CLI.pm
   Prophet/trunk/lib/Prophet/Config.pm

Log:
* Extract a bit of code used for alias matching


Modified: Prophet/trunk/lib/Prophet/CLI.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI.pm	Mon Nov 17 18:27:49 2008
@@ -89,40 +89,45 @@
     # we need to substitute $1, $2 ... in the value if there's any
 
     my $ori_cmd = join ' ', @args;
-    my $cmd = $ori_cmd;
-    my $aliases = $self->app_handle->config->aliases;
-    for my $key ( keys %$aliases ) {
-        if ( $cmd =~ /^$key/ ) {
-            my $value    = $aliases->{$key};
-            no strict 'refs';
-
-            # we want to start at index 1, as @+
-            my @captures = ( undef, map { ${$_} } 1 .. @+ );
-            $value =~ s/\$$_\b/$captures[$_]/g for 1 .. @+;
-            $cmd =~ s/$key/$value/;
-
+    for my $alias ( keys %{ $self->app_handle->config->aliases } ) {
+   
+            my $command = $self->_command_matches_alias($ori_cmd => $alias) || next; 
+        
             # we don't want to recursively call if people stupidly write
             # alias pull --local = pull --local
-            if ( $cmd ne $ori_cmd ) {
-                $self->run_one_command( split /\s+/, $cmd );
-                return;
-            }
-        }
+            next if ( $command ne $ori_cmd );
+            return $self->run_one_command( split /\s+/, $command );
     }
 
     #  really, we shouldn't be doing this stuff from the command dispatcher
-    $self->context(
-        Prophet::CLIContext->new( app_handle => $self->app_handle ) );
+    $self->context( Prophet::CLIContext->new( app_handle => $self->app_handle ) );
     $self->context->setup_from_args(@args);
-
     my $dispatcher = $self->dispatcher_class->new( cli => $self );
 
-    my $command = join ' ', @{ $self->context->primary_commands };
-    my $dispatch = $dispatcher->dispatch($command);
+    my $dispatch = $dispatcher->dispatch( join ' ', @{ $self->context->primary_commands });
 
     $dispatch->run($dispatcher);
 }
 
+sub _command_matches_alias {
+    my $self = shift;
+    my $cmd = shift;
+    my $alias = shift;
+
+
+        if ( $cmd =~ /^$alias/ ) {
+            my $value    = $self->app_handle->config->aliases->{$alias};
+            no strict 'refs';
+
+            # we want to start at index 1, as @+
+            my @captures = ( undef, map { ${$_} } 1 .. @+ );
+            $value =~ s/\$$_\b/$captures[$_]/g for 1 .. @+;
+            $cmd =~ s/$alias/$value/;
+            return $cmd;
+        } 
+        return undef;
+}
+
 =head2 invoke outhandle, ARGV_COMPATIBLE_ARRAY
 
 Run the given command. If outhandle is true, select that as the file handle

Modified: Prophet/trunk/lib/Prophet/Config.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Config.pm	(original)
+++ Prophet/trunk/lib/Prophet/Config.pm	Mon Nov 17 18:27:49 2008
@@ -31,7 +31,7 @@
 );
 
 sub aliases {
-    return $_[0]->config->{_aliases};
+    return $_[0]->config->{_aliases} || {};
 }
 
 



More information about the Bps-public-commit mailing list