-
-
Save anonymous/4d6054d4725713f97aea6308d87c7b6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Changes b/Changes | |
index 7e03754..37aefb6 100644 | |
--- a/Changes | |
+++ b/Changes | |
@@ -1,5 +1,7 @@ | |
-7.11 2016-11-07 | |
+7.11 2016-11-17 | |
+ - Improved spurt function in Mojo::Util to create directories if they don't | |
+ exist yet. | |
- Improved one_tick method in Mojo::IOLoop to protect from recursion, similar | |
to the start method. | |
diff --git a/lib/Mojo/Util.pm b/lib/Mojo/Util.pm | |
index 2dc8324..77746fa 100644 | |
--- a/lib/Mojo/Util.pm | |
+++ b/lib/Mojo/Util.pm | |
@@ -7,7 +7,9 @@ use Digest::MD5 qw(md5 md5_hex); | |
use Digest::SHA qw(hmac_sha1_hex sha1 sha1_hex); | |
use Encode 'find_encoding'; | |
use Exporter 'import'; | |
+use File::Basename 'dirname'; | |
use File::Find 'find'; | |
+use File::Path 'mkpath'; | |
use Getopt::Long 'GetOptionsFromArray'; | |
use IO::Poll qw(POLLIN POLLPRI); | |
use List::Util 'min'; | |
@@ -268,9 +270,13 @@ sub split_header { _header(shift, 0) } | |
sub spurt { | |
my ($content, $path) = @_; | |
+ | |
+ my $d = dirname $path; | |
+ mkpath $d or croak qq{Can't make directory "$d": $!} if defined $d && !-d $d; | |
open my $file, '>', $path or croak qq{Can't open file "$path": $!}; | |
defined $file->syswrite($content) | |
or croak qq{Can't write to file "$path": $!}; | |
+ | |
return $content; | |
} | |
@@ -751,7 +757,7 @@ its own array reference, and keys without a value get C<undef> assigned. | |
$bytes = spurt $bytes, '/etc/passwd'; | |
-Write all data at once to file. | |
+Write all data at once to file and create the directory if necessary. | |
=head2 steady_time | |
diff --git a/lib/Mojolicious/Command.pm b/lib/Mojolicious/Command.pm | |
index 7b57259..da95d93 100644 | |
--- a/lib/Mojolicious/Command.pm | |
+++ b/lib/Mojolicious/Command.pm | |
@@ -3,7 +3,6 @@ use Mojo::Base -base; | |
use Carp 'croak'; | |
use Cwd 'getcwd'; | |
-use File::Basename 'dirname'; | |
use File::Path 'mkpath'; | |
use File::Spec::Functions qw(catdir catfile); | |
use Mojo::Loader 'data_section'; | |
@@ -71,7 +70,6 @@ sub run { croak 'Method "run" not implemented by subclass' } | |
sub write_file { | |
my ($self, $path, $data) = @_; | |
return $self->_loud(" [exist] $path") if -f $path; | |
- $self->create_dir(dirname $path); | |
spurt $data, $path; | |
return $self->_loud(" [write] $path"); | |
} | |
diff --git a/t/mojo/util.t b/t/mojo/util.t | |
index ab59b15..3003624 100644 | |
--- a/t/mojo/util.t | |
+++ b/t/mojo/util.t | |
@@ -421,6 +421,11 @@ my $dir = tempdir CLEANUP => 1; | |
my $file = catfile $dir, 'test.txt'; | |
spurt "just\nworks!", $file; | |
is slurp($file), "just\nworks!", 'successful roundtrip'; | |
+my $file2 = catfile $dir, 'test', '123'; | |
+ok !-d $file2, 'directory does not exist'; | |
+$file2 = catfile $file2, 'test2.txt'; | |
+spurt "just\nworks too!", $file2; | |
+is slurp($file2), "just\nworks too!", 'successful roundtrip'; | |
# files | |
is_deeply [files 'does_not_exist'], [], 'no files'; | |
diff --git a/t/mojolicious/command.t b/t/mojolicious/command.t | |
index 3257fcc..3d42571 100644 | |
--- a/t/mojolicious/command.t | |
+++ b/t/mojolicious/command.t | |
@@ -46,7 +46,7 @@ $buffer = ''; | |
local *STDOUT = $handle; | |
$command->render_to_rel_file('foo_bar', 'bar/baz.txt'); | |
} | |
-like $buffer, qr/\[mkdir\].*\[write\]/s, 'right output'; | |
+like $buffer, qr/\[write\].*baz\.txt/s, 'right output'; | |
open my $txt, '<', $command->rel_file('bar/baz.txt'); | |
is join('', <$txt>), "just works!\n", 'right result'; | |
$buffer = ''; | |
@@ -63,7 +63,7 @@ $buffer = ''; | |
local *STDOUT = $handle; | |
$command->write_rel_file('123.xml', "seems\nto\nwork"); | |
} | |
-like $buffer, qr/\[exist\].*\[write\]/s, 'right output'; | |
+like $buffer, qr/\[write\].*123\.xml/s, 'right output'; | |
$buffer = ''; | |
{ | |
open my $handle, '>', \$buffer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment