| 1 |
package Dancer2::CLI::Gen; |
| 2 |
# ABSTRACT: Create new Dancer2 application |
| 3 |
$Dancer2::CLI::Gen::VERSION = '2.2.2'; |
| 4 |
use Moo; |
| 5 |
use URI; |
| 6 |
use HTTP::Tiny; |
| 7 |
use Path::Tiny; |
| 8 |
use JSON::MaybeXS; |
| 9 |
use Dancer2::Template::Tiny; |
| 10 |
use Module::Runtime qw( use_module is_module_name ); |
| 11 |
use CLI::Osprey |
| 12 |
desc => 'Helper script to create new Dancer2 applications'; |
| 13 |
|
| 14 |
# For git integration |
| 15 |
use Symbol; |
| 16 |
use IPC::Open3 qw(); |
| 17 |
use Try::Tiny; |
| 18 |
use File::Which; |
| 19 |
|
| 20 |
option application => ( |
| 21 |
is => 'ro', |
| 22 |
short => 'a', |
| 23 |
doc => 'application name', |
| 24 |
format => 's', |
| 25 |
format_doc => 'appname', |
| 26 |
required => 1, |
| 27 |
spacer_before => 1, |
| 28 |
); |
| 29 |
|
| 30 |
option directory => ( |
| 31 |
is => 'ro', |
| 32 |
short => 'd', |
| 33 |
doc => 'application directory (default: dashed application name)', |
| 34 |
format => 's', |
| 35 |
format_doc => 'directory', |
| 36 |
required => 0, |
| 37 |
); |
| 38 |
|
| 39 |
# This was causing conflict with Path::Tiny's path(), so renaming to avoid |
| 40 |
# the overhead of making Path::Tiny an object. |
| 41 |
option app_path => ( |
| 42 |
is => 'ro', |
| 43 |
short => 'p', |
| 44 |
option => 'path', |
| 45 |
doc => 'application path (default: current directory)', |
| 46 |
format => 's', |
| 47 |
format_doc => 'directory', |
| 48 |
required => 0, |
| 49 |
default => '.', |
| 50 |
); |
| 51 |
|
| 52 |
option overwrite => ( |
| 53 |
is => 'ro', |
| 54 |
short => 'o', |
| 55 |
doc => 'overwrite existing files', |
| 56 |
required => 0, |
| 57 |
default => 0, |
| 58 |
); |
| 59 |
|
| 60 |
option skel => ( |
| 61 |
is => 'ro', |
| 62 |
short => 's', |
| 63 |
doc => 'skeleton directory', |
| 64 |
format => 's', |
| 65 |
format_doc => 'directory', |
| 66 |
required => 0, |
| 67 |
default => sub{ |
| 68 |
my $self = shift; |
| 69 |
path( $self->parent_command->_dist_dir, 'skel' ); |
| 70 |
}, |
| 71 |
); |
| 72 |
|
| 73 |
option skel_name => ( |
| 74 |
is => 'ro', |
| 75 |
short => 'n', |
| 76 |
doc => 'skeleton name', |
| 77 |
format => 's', |
| 78 |
format_doc => 'skelname', |
| 79 |
required => 0, |
| 80 |
default => 'default', |
| 81 |
); |
| 82 |
|
| 83 |
option docker => ( |
| 84 |
is => 'ro', |
| 85 |
short => 'c', |
| 86 |
doc => 'create a dockerfile (container definition)', |
| 87 |
required => 0, |
| 88 |
default => 0, |
| 89 |
); |
| 90 |
|
| 91 |
option git => ( |
| 92 |
is => 'ro', |
| 93 |
short => 'g', |
| 94 |
doc => 'init git repository', |
| 95 |
required => 0, |
| 96 |
default => 0, |
| 97 |
); |
| 98 |
|
| 99 |
option remote => ( |
| 100 |
is => 'ro', |
| 101 |
short => 'r', |
| 102 |
doc => 'URI for git repository (implies -g)', |
| 103 |
format => 's', |
| 104 |
format_doc => 'URI', |
| 105 |
required => 0, |
| 106 |
); |
| 107 |
|
| 108 |
option no_package_files => ( |
| 109 |
is => 'ro', |
| 110 |
doc => "don't create files needed for CPAN packaging", |
| 111 |
required => 0, |
| 112 |
default => 0, |
| 113 |
); |
| 114 |
|
| 115 |
has _engine => ( |
| 116 |
is => 'ro', |
| 117 |
default => sub { |
| 118 |
return Dancer2::Template::Tiny->new( config => { start_tag => '[d2%', end_tag => '%2d]' } ); |
| 119 |
}, |
| 120 |
); |
| 121 |
|
| 122 |
# Last chance to validate args before we attempt to do something with them |
| 123 |
sub BUILD { |
| 124 |
my ( $self, $args ) = @_; |
| 125 |
|
| 126 |
$self->osprey_usage( 1, qq{ |
| 127 |
Invalid application name. Application names must not contain single colons, |
| 128 |
dots, hyphens or start with a number. |
| 129 |
}) unless is_module_name( $self->application ); |
| 130 |
|
| 131 |
if ( my $remote = $self->remote ) { |
| 132 |
my $scheme = URI->new( $remote )->scheme // $self->remote; # This feels dirty |
| 133 |
$self->osprey_usage( 1, "'$remote' must be a valid URI to git repository") |
| 134 |
unless $scheme =~ / ^ git \@ .+ : .+ \.git $ | ^ http /x; |
| 135 |
} |
| 136 |
|
| 137 |
my $path = $self->app_path; |
| 138 |
-d $path or $self->osprey_usage( 1, "path: directory '$path' does not exist" ); |
| 139 |
-w $path or $self->osprey_usage( 1, "path: directory '$path' is not writeable" ); |
| 140 |
|
| 141 |
if ( my $skel = $self->skel ) { |
| 142 |
-d $skel or $self->osprey_usage( 1, "skel: directory '$skel' not found" ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
sub run { |
| 147 |
my $self = shift; |
| 148 |
|
| 149 |
my $app_name = $self->application; |
| 150 |
my $app_file = $self->parent_command->_get_app_file( $app_name ); |
| 151 |
my $app_path = $self->parent_command->_get_app_path( $self->app_path, $app_name ); |
| 152 |
|
| 153 |
if( my $dir = $self->directory ) { |
| 154 |
$app_path = path( $self->app_path, $dir ); |
| 155 |
} |
| 156 |
|
| 157 |
my $files_to_copy = $self->_build_file_list( $self->skel . '/' . $self->skel_name, $app_path ); |
| 158 |
foreach my $pair( @$files_to_copy ) { |
| 159 |
if( $pair->[0] =~ m/lib\/AppFile.pm$/ ) { |
| 160 |
$pair->[1] = path( $app_path, $app_file ); |
| 161 |
last; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if( $self->docker ) { |
| 166 |
push @$files_to_copy, [ path( $self->parent_command->_dist_dir, 'docker/Dockerfile' ), "$app_name/Dockerfile" ]; |
| 167 |
} |
| 168 |
|
| 169 |
my $vars = { |
| 170 |
appname => $app_name, |
| 171 |
appfile => $app_file->stringify, |
| 172 |
apppath => $app_path->stringify, |
| 173 |
appdir => $app_path->absolute->stringify, |
| 174 |
perl_interpreter => $self->parent_command->_get_perl_interpreter, |
| 175 |
cleanfiles => $self->parent_command->_get_dashed_name( $app_name ), |
| 176 |
dancer_version => $self->parent_command->_dancer2_version, |
| 177 |
docker => $self->docker, |
| 178 |
}; |
| 179 |
|
| 180 |
$self->_copy_templates( $files_to_copy, $vars, $self->overwrite ); |
| 181 |
unless( $self->no_package_files ) { |
| 182 |
$self->_create_manifest( $files_to_copy, $app_path ); |
| 183 |
$self->_add_to_manifest_skip( $app_path, $vars->{cleanfiles} ); |
| 184 |
} |
| 185 |
|
| 186 |
$self->_check_git( $vars ); |
| 187 |
$self->_check_yaml; |
| 188 |
$self->_how_to_run( $vars ); |
| 189 |
} |
| 190 |
|
| 191 |
sub _check_git { |
| 192 |
my( $self, $vars ) = @_; |
| 193 |
|
| 194 |
if( my $remote = $self->remote or $self->git ) { |
| 195 |
my $app_name = $vars->{ appname }; |
| 196 |
my $git_error = qq{ |
| 197 |
***** |
| 198 |
|
| 199 |
WARNING: Couldn't initialize a git repo despite being asked to do so. |
| 200 |
|
| 201 |
To resolve this, cd to your application directory and run the following |
| 202 |
commands: |
| 203 |
|
| 204 |
git init |
| 205 |
git add . |
| 206 |
git commit -m"Initial commit of $app_name by Dancer2" |
| 207 |
}; |
| 208 |
|
| 209 |
my $git = which 'git'; |
| 210 |
-x $git or die "Can't execute git: $!"; |
| 211 |
|
| 212 |
#my $dist_dir = $self->parent_command->_dist_dir; |
| 213 |
my $app_path = $vars->{ apppath }; |
| 214 |
|
| 215 |
# The shipped template lives at share/gitignore, not share/.gitignore -- |
| 216 |
# a leading dot there would make git itself honour it against this |
| 217 |
# distribution's own share/ tree, which kept the skeleton's environment |
| 218 |
# configs out of the shipped dist entirely. The generated application |
| 219 |
# still needs a file literally named .gitignore, so the destination |
| 220 |
# name is given explicitly rather than left to whatever copy() does |
| 221 |
# with a directory target. |
| 222 |
my $gitignore = path( $self->parent_command->_dist_dir, 'gitignore' ); |
| 223 |
path( $gitignore )->copy( path( $app_path, '.gitignore' ) ); |
| 224 |
|
| 225 |
# $vars->{apppath} is a plain string (see the die message below, which |
| 226 |
# wants it as one); the absolute path was already computed once into |
| 227 |
# $vars->{appdir} at the point of $app_path->absolute->stringify in |
| 228 |
# run(), so re-derive it from there instead of calling ->absolute on |
| 229 |
# a string -- which is what used to make 'dancer2 gen -g' die with |
| 230 |
# "Can't locate object method \"absolute\" via package ...". |
| 231 |
chdir $vars->{ appdir } |
| 232 |
or die "Can't cd to $app_path: $!"; |
| 233 |
if( _run_shell_cmd( 'git', 'init') != 0 or |
| 234 |
_run_shell_cmd( 'git', 'add', '.') != 0 or |
| 235 |
_run_shell_cmd( 'git', 'commit', "-m 'Initial commit of $app_name by Dancer2'" ) != 0 ) { |
| 236 |
print $git_error; |
| 237 |
} |
| 238 |
else { |
| 239 |
if( $self->remote && |
| 240 |
_run_shell_cmd( 'git', 'remote', 'add', 'origin', $self->remote ) != 0 ) { |
| 241 |
print $git_error; |
| 242 |
print " git remote add origin " . $self->remote . "\n"; |
| 243 |
} |
| 244 |
} |
| 245 |
print "\n*****\n"; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
sub _check_yaml { |
| 250 |
if ( ! eval { use_module( 'YAML' ); 1; } ) { |
| 251 |
print qq{ |
| 252 |
***** |
| 253 |
|
| 254 |
WARNING: YAML.pm is not installed. This is not a full dependency, but is highly |
| 255 |
recommended; in particular, the scaffolded Dancer app being created will not be |
| 256 |
able to read settings from the config file without YAML.pm being installed. |
| 257 |
|
| 258 |
To resolve this, simply install YAML from CPAN, for instance using one of the |
| 259 |
following commands: |
| 260 |
|
| 261 |
cpan YAML |
| 262 |
perl -MCPAN -e 'install YAML' |
| 263 |
curl -L https://cpanmin.us | perl - --sudo YAML |
| 264 |
|
| 265 |
***** |
| 266 |
}; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
sub _how_to_run { |
| 271 |
my( $self, $vars ) = @_; |
| 272 |
|
| 273 |
my $app_path = $vars->{ apppath }; |
| 274 |
my $app_name = $vars->{ appname }; |
| 275 |
|
| 276 |
print "\nYour new application is ready! To run it:\n"; |
| 277 |
|
| 278 |
if( $vars->{ docker } ) { |
| 279 |
my $image = lc $app_name; |
| 280 |
print qq{ |
| 281 |
|
| 282 |
cd $app_path |
| 283 |
docker build -t ${image} . |
| 284 |
docker run -d -p 5000:4000 --name $app_name ${image} |
| 285 |
|
| 286 |
where 5000 is the external port, and 4000 is the port your application |
| 287 |
runs on inside of the container. |
| 288 |
|
| 289 |
(note: you may need to run the docker commands with sudo) |
| 290 |
|
| 291 |
You may also run your app without Docker: |
| 292 |
}; |
| 293 |
} |
| 294 |
|
| 295 |
my $install_deps = ''; |
| 296 |
$install_deps = "\n cpanm --installdeps ." |
| 297 |
if $self->skel_name ne 'default'; |
| 298 |
|
| 299 |
print qq{ |
| 300 |
cd $app_path$install_deps |
| 301 |
plackup bin/app.psgi |
| 302 |
|
| 303 |
To access your application, point your browser to http://localhost:5000/ |
| 304 |
|
| 305 |
If you need community assistance, the following resources are available: |
| 306 |
- Dancer website: https://perldancer.org |
| 307 |
- GitHub: https://github.com/PerlDancer/Dancer2/ |
| 308 |
- Mailing list: https://lists.perldancer.org/mailman/listinfo/dancer-users |
| 309 |
- IRC: irc.perl.org#dancer |
| 310 |
|
| 311 |
Happy Dancing! |
| 312 |
|
| 313 |
}; |
| 314 |
} |
| 315 |
|
| 316 |
# skel creation routines |
| 317 |
sub _build_file_list { |
| 318 |
my ( $self, $from, $to ) = @_; |
| 319 |
$from =~ s{/+$}{}; |
| 320 |
|
| 321 |
my @result; |
| 322 |
my $iter = path( $from )->iterator({ recurse => 1 }); |
| 323 |
while( my $file = $iter->() ) { |
| 324 |
warn "File not found: $file" unless $file->exists; # Paranoia |
| 325 |
next if $file->basename =~ m{^\.git(/|$)}; |
| 326 |
next if $file->is_dir; |
| 327 |
|
| 328 |
my $filename = $file->relative( $from ); |
| 329 |
push @result, [ $file, path( $to, $filename )]; |
| 330 |
} |
| 331 |
return \@result; |
| 332 |
} |
| 333 |
|
| 334 |
sub _copy_templates { |
| 335 |
my ( $self, $files, $vars, $overwrite ) = @_; |
| 336 |
my $app_name = $vars->{ appname }; |
| 337 |
|
| 338 |
foreach my $pair (@$files) { |
| 339 |
my ( $from, $to ) = @{$pair}; |
| 340 |
next if $self->no_package_files && $from =~ /MANIFEST\.SKIP$/; |
| 341 |
next if $self->no_package_files && $from =~ /Makefile.PL$/; |
| 342 |
|
| 343 |
if ( -f $to && !$overwrite ) { |
| 344 |
print "! $to exists, overwrite? (or rerun this command with -o) [N/y/a]: "; |
| 345 |
my $res = <STDIN>; chomp($res); |
| 346 |
$overwrite = 1 if $res eq 'a'; |
| 347 |
next unless ( $res eq 'y' ) or ( $res eq 'a' ); |
| 348 |
} |
| 349 |
|
| 350 |
$to =~ s/AppFile/$app_name/; |
| 351 |
my $to_dir = path( $to )->parent; |
| 352 |
if ( ! $to_dir->is_dir ) { |
| 353 |
print "+ $to_dir\n"; |
| 354 |
$to_dir->mkpath; |
| 355 |
} |
| 356 |
|
| 357 |
# Skeleton files whose names are prefixed with + need to be executable, but we must strip |
| 358 |
# that from the name when copying them |
| 359 |
my $to_file = path( $to )->basename; |
| 360 |
my $ex = ( $to_file =~ s/^\+// ); |
| 361 |
$to = path( $to_dir, $to_file ) if $ex; |
| 362 |
|
| 363 |
print "+ $to\n"; |
| 364 |
my $content; |
| 365 |
{ |
| 366 |
local $/; |
| 367 |
open( my $fh, '<:raw', $from ) or die "unable to open file `$from' for reading: $!"; |
| 368 |
$content = <$fh>; |
| 369 |
close $fh; |
| 370 |
} |
| 371 |
|
| 372 |
if( $from !~ m/\.(db|ico|jpg|png|css|eot|map|swp|ttf|svg|woff|woff2|js)$/ ) { |
| 373 |
$content = $self->_process_template($content, $vars); |
| 374 |
} |
| 375 |
|
| 376 |
path( $to )->spew_raw( $content ); |
| 377 |
if( $ex ) { |
| 378 |
$to->chmod( 0755 ) or warn "unable to change permissions for $to: $!"; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
sub _create_manifest { |
| 384 |
my ( $self, $files, $dir ) = @_; |
| 385 |
|
| 386 |
my $manifest_name = path( $dir, 'MANIFEST' ); |
| 387 |
open( my $manifest, '>', $manifest_name ) or die $!; |
| 388 |
print $manifest "MANIFEST\n"; |
| 389 |
|
| 390 |
foreach my $file( @{ $files } ) { |
| 391 |
my $filename = path( $file->[1] )->relative( $dir ); |
| 392 |
my $basename = $filename->basename; |
| 393 |
my $clean_basename = $basename; |
| 394 |
$clean_basename =~ s/^\+//; |
| 395 |
$filename =~ s/\Q$basename\E/$clean_basename/; |
| 396 |
print {$manifest} "$filename\n"; |
| 397 |
} |
| 398 |
|
| 399 |
close $manifest; |
| 400 |
} |
| 401 |
|
| 402 |
sub _add_to_manifest_skip { |
| 403 |
my ( $self, $dir, $dist_name ) = @_; |
| 404 |
|
| 405 |
my $filename = path( $dir, 'MANIFEST.SKIP' ); |
| 406 |
open my $fh, '>>', $filename or die $!; |
| 407 |
print {$fh} "^$dist_name-\n"; |
| 408 |
close $fh; |
| 409 |
} |
| 410 |
|
| 411 |
sub _process_template { |
| 412 |
my ( $self, $template, $tokens ) = @_; |
| 413 |
|
| 414 |
return $self->_engine->render( \$template, $tokens ); |
| 415 |
} |
| 416 |
|
| 417 |
sub _version_check { |
| 418 |
my $self = shift; |
| 419 |
my $version = $self->parent_command->_dancer2_version; |
| 420 |
return if $version =~ m/_/; |
| 421 |
|
| 422 |
my $latest_version = 0; |
| 423 |
my $resp = HTTP::Tiny->new( timeout => 5 )->get( 'https://fastapi.metacpan.org/release/Dancer2' ); |
| 424 |
if( $resp->{ success } && decode_json( $resp->{ content } )->{ version } =~ /(\d\.\d+)/ ) { |
| 425 |
$latest_version = $1; |
| 426 |
if ($latest_version gt $version) { |
| 427 |
print qq{ |
| 428 |
The latest stable Dancer2 release is $latest_version. You are currently using $version. |
| 429 |
Please check https://metacpan.org/pod/Dancer2/ for updates. |
| 430 |
|
| 431 |
}; |
| 432 |
} |
| 433 |
} else { |
| 434 |
warn "\nCouldn't determine latest version of Dancer2. Please check your internet |
| 435 |
connection, or pass -x to gen to bypass this check in the future.\n\n"; |
| 436 |
|
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
# Shell out to run git |
| 441 |
sub _run_shell_cmd { |
| 442 |
my @cmds = @_; |
| 443 |
|
| 444 |
my $exit_status = try { |
| 445 |
my $pid = IPC::Open3::open3( |
| 446 |
my $stdin, |
| 447 |
my $stdout, |
| 448 |
my $stderr = Symbol::gensym, |
| 449 |
@cmds, |
| 450 |
); |
| 451 |
|
| 452 |
waitpid( $pid, 0 ); |
| 453 |
return $? >> 8; |
| 454 |
} catch { |
| 455 |
print STDERR "$_\n"; |
| 456 |
return 1; |
| 457 |
}; |
| 458 |
|
| 459 |
return $exit_status; |
| 460 |
} |
| 461 |
|
| 462 |
1; |
| 463 |
|
| 464 |
__END__ |
| 465 |
|
| 466 |
=pod |
| 467 |
|
| 468 |
=encoding UTF-8 |
| 469 |
|
| 470 |
=head1 NAME |
| 471 |
|
| 472 |
Dancer2::CLI::Gen - Create new Dancer2 application |
| 473 |
|
| 474 |
=head1 VERSION |
| 475 |
|
| 476 |
version 2.2.2 |
| 477 |
|
| 478 |
=head1 AUTHOR |
| 479 |
|
| 480 |
Dancer Core Developers |
| 481 |
|
| 482 |
=head1 COPYRIGHT AND LICENSE |
| 483 |
|
| 484 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 485 |
|
| 486 |
This is free software; you can redistribute it and/or modify it under |
| 487 |
the same terms as the Perl 5 programming language system itself. |
| 488 |
|
| 489 |
=cut |