[Bps-public-commit] Prophet branch, master, updated. 0.743-16-g2c47e47

? sunnavy sunnavy at bestpractical.com
Thu Aug 5 05:53:51 EDT 2010


The branch, master has been updated
       via  2c47e476d8dda9fc650169068d0f31741a157a01 (commit)
       via  a5deef94ec9cb3b86ad4c47ac675bf06080c9574 (commit)
      from  8a52961adf92b730d639ab2f20646139bb9decea (commit)

Summary of changes:
 lib/Prophet/Server.pm |   14 +++++++++-----
 lib/Prophet/Util.pm   |   22 ++++++++++++++++++----
 t/util.t              |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 9 deletions(-)
 create mode 100644 t/util.t

- Log -----------------------------------------------------------------
commit a5deef94ec9cb3b86ad4c47ac675bf06080c9574
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Aug 5 15:35:08 2010 +0800

    improve updir so we can specify arbitary depth and make path canonical if possible

diff --git a/lib/Prophet/Server.pm b/lib/Prophet/Server.pm
index 970e309..18d5530 100644
--- a/lib/Prophet/Server.pm
+++ b/lib/Prophet/Server.pm
@@ -92,8 +92,10 @@ sub prophet_static_root {
     my $self = shift;
     unless ($PROPHET_STATIC_ROOT) {
 
-        $PROPHET_STATIC_ROOT = File::Spec->catdir( Prophet::Util->updir( $INC{'Prophet.pm'} ),
-            "..", "share", "web", "static" );
+        $PROPHET_STATIC_ROOT = File::Spec->catdir(
+            Prophet::Util->updir( $INC{'Prophet.pm'}, 2 ), "share",
+            "web", "static"
+        );
 
         eval { require File::ShareDir; 1 }
             or die "requires File::ShareDir to determine default static root";
@@ -120,8 +122,10 @@ sub app_static_root {
         my $app_file = ref($self->app_handle) .".pm";
         $app_file =~ s|::|/|g;
 
-         $APP_STATIC_ROOT = File::Spec->catdir( Prophet::Util->updir( $INC{$app_file} ),
-          "..",  "..", "share", "web", "static" );
+        $APP_STATIC_ROOT = File::Spec->catdir(
+            Prophet::Util->updir( $INC{$app_file}, 3 ), "share",
+            "web", "static"
+        );
 
         my $dist = ref($self->app_handle);
         $dist =~ s/::/-/g;
diff --git a/lib/Prophet/Util.pm b/lib/Prophet/Util.pm
index 88f2f3f..e37de4c 100644
--- a/lib/Prophet/Util.pm
+++ b/lib/Prophet/Util.pm
@@ -4,19 +4,33 @@ use File::Basename;
 use File::Spec;
 use File::Path;
 use Params::Validate;
+use Cwd;
 
-=head2 updir PATH
+=head2 updir PATH, DEPTH
 
 Strips off the filename in the given path and returns the absolute
 path of the remaining directory.
 
+Default depth is 1.
+If depth are great than 1, will go up more according to the depth value.
+
 =cut
 
 sub updir {
-    my $self = shift;
-    my $path = shift;
+    my $self  = shift;
+    my ( $path, $depth ) = validate_pos( @_, 1, { default => 1 } );
+    die "depth must be positive" unless $depth > 0;
+
     my ($file, $dir, undef) = fileparse(File::Spec->rel2abs($path));
-    return $dir;
+
+    $depth-- if $file; # we stripped the file part
+
+    if ($depth) {
+        $dir = File::Spec->catdir( $dir, ( File::Spec->updir ) x $depth );
+    }
+
+    # if $dir doesn't exists in file system, abs_path will return empty
+    return Cwd::abs_path($dir) || $dir;
 }
 
 =head2 slurp FILENAME
diff --git a/t/util.t b/t/util.t
new file mode 100644
index 0000000..0bf6f7a
--- /dev/null
+++ b/t/util.t
@@ -0,0 +1,41 @@
+use warnings;
+use strict;
+use Prophet::Test tests => 9;
+use_ok('Prophet::Util');
+use File::Temp 'tempdir';
+use File::Path;
+use File::Spec;
+use Cwd;
+my $base = Cwd::abs_path( tempdir( CLEANUP => 1 ) );
+mkpath( File::Spec->catdir($base, 'foo', 'bar', 'baz', 'foo' ) ) or die $!; 
+
+my %updir = (
+
+    # 0 here means no depth arg, should act as depth 1
+    0 => {
+        'foo/bar/baz/foo'  => 'foo/bar/baz',
+        'foo/bar/baz/foo/' => 'foo/bar/baz',
+    },
+    1 => {
+        'foo/bar/baz/foo'  => 'foo/bar/baz',
+        'foo/bar/baz/foo/' => 'foo/bar/baz',
+    },
+    2 => {
+        'foo/bar/baz/foo'  => 'foo/bar',
+        'foo/bar/baz/foo/' => 'foo/bar',
+    },
+    3 => {
+        'foo/bar/baz/foo'  => 'foo',
+        'foo/bar/baz/foo/' => 'foo',
+    }
+);
+
+for my $depth ( keys %updir ) {
+    for my $path ( keys %{ $updir{$depth} } ) {
+        my $value = join '/', $base, $updir{$depth}{$path};
+        $path = join '/', $base, $path;
+        is( Prophet::Util->updir($path, $depth || () ),
+            $value, "updir of $path with depth $depth is $value" );
+    }
+}
+

commit 2c47e476d8dda9fc650169068d0f31741a157a01
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Aug 5 15:47:27 2010 +0800

    a bit fix

diff --git a/lib/Prophet/Server.pm b/lib/Prophet/Server.pm
index 18d5530..55b289b 100644
--- a/lib/Prophet/Server.pm
+++ b/lib/Prophet/Server.pm
@@ -134,7 +134,7 @@ sub app_static_root {
             or die "requires File::ShareDir to determine default static root";
 
         $APP_STATIC_ROOT
-            = File::Spec->catfile( File::ShareDir::dist_dir($dist), 'web/static' )
+            = File::Spec->catfile( File::ShareDir::dist_dir($dist), 'web', 'static' )
             if ( !-d $APP_STATIC_ROOT );
 
         $APP_STATIC_ROOT = Cwd::abs_path($APP_STATIC_ROOT);

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list