Skip to content

Commit ce201ca

Browse files
committed
Move Dancer2::CLI::Command::gen to Path::Tiny:
The majority of this is rather simple, but annoying to type. We're not making full use of Path::Tiny here, which we could, once we clear it up.
1 parent 9e0db21 commit ce201ca

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

lib/Dancer2/CLI/Command/gen.pm

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ use warnings;
77
use App::Cmd::Setup -command;
88

99
use HTTP::Tiny;
10-
use File::Find;
11-
use File::Path 'mkpath';
12-
use File::Spec::Functions;
10+
use Path::Tiny ();
11+
use File::Find; # Also
1312
use File::ShareDir 'dist_dir';
14-
use File::Basename qw/dirname basename/;
15-
use Dancer2::Template::Simple;
1613
use Module::Runtime 'require_module';
14+
use Dancer2::Template::Simple;
1715

1816
my $SKEL_APP_FILE = 'lib/AppFile.pm';
1917

@@ -45,12 +43,12 @@ sub validate_args {
4543
);
4644
}
4745

48-
my $path = $opt->{path};
49-
-d $path or $self->usage_error("directory '$path' does not exist");
46+
my $path = Path::Tiny::path( $opt->{path} );
47+
$path->is_dir or $self->usage_error("directory '$path' does not exist");
5048
-w $path or $self->usage_error("directory '$path' is not writeable");
5149

5250
if ( my $skel_path = $opt->{skel} ) {
53-
-d $skel_path
51+
Path::Tiny::path($skel_path)->is_dir
5452
or $self->usage_error("skeleton directory '$skel_path' not found");
5553
}
5654
}
@@ -59,30 +57,35 @@ sub execute {
5957
my ($self, $opt, $args) = @_;
6058
$self->_version_check() unless $opt->{'no_check'};
6159

62-
my $dist_dir = dist_dir('Dancer2');
63-
my $skel_dir = $opt->{skel} || catdir($dist_dir, 'skel');
64-
-d $skel_dir or die "$skel_dir doesn't exist";
60+
my @dirs =
61+
$opt->{'skel'}
62+
? ($opt->{'skel'})
63+
: (dist_dir('Dancer2'), 'skel');
64+
65+
my $skel_dir = Path::Tiny::path(@dirs);
66+
$skel_dir->is_dir
67+
or die "Skeleton directory '$skel_dir' doesn't exist";
6568

6669
my $app_name = $opt->{application};
6770
my $app_file = _get_app_file($app_name);
6871
my $app_path = _get_app_path($opt->{path}, $app_name);
6972

7073
if( my $dir = $opt->{directory} ) {
71-
$app_path = catdir( $opt->{path}, $dir );
74+
$app_path = Path::Tiny::path( $opt->{path}, $dir );
7275
}
7376

7477
my $files_to_copy = _build_file_list($skel_dir, $app_path);
7578
foreach my $pair (@$files_to_copy) {
7679
if ($pair->[0] =~ m/$SKEL_APP_FILE$/) {
77-
$pair->[1] = catfile($app_path, $app_file);
80+
$pair->[1] = Path::Tiny::path($app_path, $app_file);
7881
last;
7982
}
8083
}
8184

8285
my $vars = {
8386
appname => $app_name,
8487
appfile => $app_file,
85-
appdir => File::Spec->rel2abs($app_path),
88+
appdir => Path::Tiny::path($app_path)->absolute,
8689
perl_interpreter => _get_perl_interpreter(),
8790
cleanfiles => _get_dashed_name($app_name),
8891
dancer_version => $self->version(),
@@ -132,7 +135,7 @@ sub _build_file_list {
132135
my $is_git = $file =~ m{^\.git(/|$)}
133136
and return;
134137

135-
push @result, [ $_, catfile($to, $file) ];
138+
push @result, [ $_, Path::Tiny::path($to, $file) ];
136139
};
137140

138141
find({ wanted => $wanted, no_chdir => 1 }, $from);
@@ -151,15 +154,15 @@ sub _copy_templates {
151154
next unless ($res eq 'y') or ($res eq 'a');
152155
}
153156

154-
my $to_dir = dirname($to);
155-
if (! -d $to_dir) {
157+
my $to_dir = Path::Tiny::path($to)->parent;
158+
if (! $to_dir->is_dir ) {
156159
print "+ $to_dir\n";
157-
mkpath $to_dir or die "could not mkpath $to_dir: $!";
160+
$to_dir->mkpath or die "could not mkpath $to_dir: $!";
158161
}
159162

160-
my $to_file = basename($to);
163+
my $to_file = Path::Tiny::path($to)->parent->stringify;
161164
my $ex = ($to_file =~ s/^\+//);
162-
$to = catfile($to_dir, $to_file) if $ex;
165+
$to = Path::Tiny::path($to_dir, $to_file)->stringify if $ex;
163166

164167
print "+ $to\n";
165168
my $content;
@@ -194,7 +197,7 @@ sub _create_manifest {
194197

195198
foreach my $file (@{$files}) {
196199
my $filename = substr $file->[1], length($dir) + 1;
197-
my $basename = basename $filename;
200+
my $basename = path($filename)->parent->stringify;
198201
my $clean_basename = $basename;
199202
$clean_basename =~ s/^\+//;
200203
$filename =~ s/\Q$basename\E/$clean_basename/;
@@ -223,13 +226,14 @@ sub _process_template {
223226

224227
sub _get_app_path {
225228
my ($path, $appname) = @_;
226-
return catdir($path, _get_dashed_name($appname));
229+
return Path::Tiny::path($path, _get_dashed_name($appname) );
227230
}
228231

229232
sub _get_app_file {
230233
my $appname = shift;
234+
231235
$appname =~ s{::}{/}g;
232-
return catfile('lib', "$appname.pm");
236+
return Path::Tiny::path( 'lib', "$appname.pm" );
233237
}
234238

235239
sub _get_perl_interpreter {

0 commit comments

Comments
 (0)