This file is indexed.

/usr/share/perl5/Dancer2/CLI/Command/gen.pm is in libdancer2-perl 0.204002+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# ABSTRACT: create new Dancer2 application
package Dancer2::CLI::Command::gen;
$Dancer2::CLI::Command::gen::VERSION = '0.204002';
use strict;
use warnings;

use App::Cmd::Setup -command;

use HTTP::Tiny;
use File::Find;
use File::Path 'mkpath';
use File::Spec::Functions;
use File::ShareDir 'dist_dir';
use File::Basename qw/dirname basename/;
use Dancer2::Template::Simple;
use Module::Runtime 'require_module';

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

sub description { 'Helper script to create new Dancer2 applications' }

sub opt_spec {
    return (
        [ 'application|a=s', 'application name' ],
        [ 'directory|d=s',   'application folder (default: same as application name)' ],
        [ 'path|p=s',        'application path (default: current directory)',
            { default => '.' } ],
        [ 'overwrite|o',     'overwrite existing files' ],
        [ 'no-check|x',      'don\'t check latest Dancer2 version (requires internet)' ],
        [ 'skel|s=s',        'skeleton directory' ],
    );
}

sub validate_args {
    my ($self, $opt, $args) = @_;

    my $name = $opt->{application}
        or $self->usage_error('Application name must be defined');

    if ( $name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/ ) {
        $self->usage_error(
            "Invalid application name.\n" .
            "Application names must not contain single colons, dots, " .
            "hyphens or start with a number.\n"
        );
    }

    my $path = $opt->{path};
    -d $path or $self->usage_error("directory '$path' does not exist");
    -w $path or $self->usage_error("directory '$path' is not writeable");

    if ( my $skel_path = $opt->{skel} ) {
        -d $skel_path
            or $self->usage_error("skeleton directory '$skel_path' not found");
    }
}

sub execute {
    my ($self, $opt, $args) = @_;
    $self->_version_check() unless $opt->{'no_check'};

    my $dist_dir = dist_dir('Dancer2');
    my $skel_dir = $opt->{skel} || catdir($dist_dir, 'skel');
    -d $skel_dir or die "$skel_dir doesn't exist";

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

    if( my $dir = $opt->{directory} ) {
        $app_path = catdir( $opt->{path}, $dir );
    }

    my $files_to_copy = _build_file_list($skel_dir, $app_path);
    foreach my $pair (@$files_to_copy) {
        if ($pair->[0] =~ m/$SKEL_APP_FILE$/) {
            $pair->[1] = catfile($app_path, $app_file);
            last;
        }
    }

    my $vars = {
        appname          => $app_name,
        appfile          => $app_file,
        appdir           => File::Spec->rel2abs($app_path),
        perl_interpreter => _get_perl_interpreter(),
        cleanfiles       => _get_dashed_name($app_name),
        dancer_version   => $self->version(),
    };

    _copy_templates($files_to_copy, $vars, $opt->{overwrite});
    _create_manifest($files_to_copy, $app_path);
    _add_to_manifest_skip($app_path);

    if ( ! eval { require_module('YAML'); 1; } ) {
        print <<'NOYAML';
*****
WARNING: YAML.pm is not installed.  This is not a full dependency, but is highly
recommended; in particular, the scaffolded Dancer app being created will not be
able to read settings from the config file without YAML.pm being installed.

To resolve this, simply install YAML from CPAN, for instance using one of the
following commands:

  cpan YAML
  perl -MCPAN -e 'install YAML'
  curl -L http://cpanmin.us | perl - --sudo YAML
*****
NOYAML
    }

    return 0;
}

sub version {
    require_module('Dancer2');
    return Dancer2->VERSION;
}

# skel creation routines
sub _build_file_list {
    my ($from, $to) = @_;
    $from =~ s{/+$}{};
    my $len = length($from) + 1;

    my @result;
    my $wanted = sub {
        return unless -f;
        my $file = substr($_, $len);

        # ignore .git and git/*
        my $is_git = $file =~ m{^\.git(/|$)}
            and return;

        push @result, [ $_, catfile($to, $file) ];
    };

    find({ wanted => $wanted, no_chdir => 1 }, $from);
    return \@result;
}

sub _copy_templates {
    my ($files, $vars, $overwrite) = @_;

    foreach my $pair (@$files) {
        my ($from, $to) = @{$pair};
        if (-f $to && !$overwrite) {
            print "! $to exists, overwrite? [N/y/a]: ";
            my $res = <STDIN>; chomp($res);
            $overwrite = 1 if $res eq 'a';
            next unless ($res eq 'y') or ($res eq 'a');
        }

        my $to_dir = dirname($to);
        if (! -d $to_dir) {
            print "+ $to_dir\n";
            mkpath $to_dir or die "could not mkpath $to_dir: $!";
        }

        my $to_file = basename($to);
        my $ex = ($to_file =~ s/^\+//);
        $to = catfile($to_dir, $to_file) if $ex;

        print "+ $to\n";
        my $content;

        {
            local $/;
            open(my $fh, '<', $from) or die "unable to open file `$from' for reading: $!";
            $content = <$fh>;
            close $fh;
        }

        if ($from !~ m/\.(ico|jpg|png|css|eot|map|swp|ttf|svg|woff|woff2|js)$/) {
            $content = _process_template($content, $vars);
        }

        open(my $fh, '>', $to) or die "unable to open file `$to' for writing: $!";
        print $fh $content;
        close $fh;

        if ($ex) {
            chmod(0755, $to) or warn "unable to change permissions for $to: $!";
        }
    }
}

sub _create_manifest {
    my ($files, $dir) = @_;

    my $manifest_name = catfile($dir, 'MANIFEST');
    open(my $manifest, '>', $manifest_name) or die $!;
    print $manifest "MANIFEST\n";

    foreach my $file (@{$files}) {
        my $filename = substr $file->[1], length($dir) + 1;
        my $basename = basename $filename;
        my $clean_basename = $basename;
        $clean_basename =~ s/^\+//;
        $filename =~ s/\Q$basename\E/$clean_basename/;
        print {$manifest} "$filename\n";
    }

    close($manifest);
}

sub _add_to_manifest_skip {
    my $dir = shift;

    my $filename = catfile($dir, 'MANIFEST.SKIP');
    open my $fh, '>>', $filename or die $!;
    print {$fh} "^$dir-\n";
    close $fh;
}

sub _process_template {
    my ($template, $tokens) = @_;
    my $engine = Dancer2::Template::Simple->new;
    $engine->{start_tag} = '[d2%';
    $engine->{stop_tag} = '%2d]';
    return $engine->render(\$template, $tokens);
}

sub _get_app_path {
    my ($path, $appname) = @_;
    return catdir($path, _get_dashed_name($appname));
}

sub _get_app_file {
    my $appname = shift;
    $appname =~ s{::}{/}g;
    return catfile('lib', "$appname.pm");
}

sub _get_perl_interpreter {
    return -r '/usr/bin/env' ? '#!/usr/bin/env perl' : "#!$^X";
}

sub _get_dashed_name {
    my $name = shift;
    $name =~ s{::}{-}g;
    return $name;
}

# version check routines
sub _version_check {
    my $self = shift;
    my $version = $self->version();
    return if $version =~  m/_/;

    my $latest_version = 0;
    my $resp = _send_http_request('http://search.cpan.org/api/module/Dancer2');

    if ($resp) {
        if ( $resp =~ /"version" (?:\s+)? \: (?:\s+)? "(\d\.\d+)"/x ) {
            $latest_version = $1;
        } else {
            die "Can't understand search.cpan.org's reply.\n";
        }
    }

    if ($latest_version > $version) {
        print qq|
The latest stable Dancer2 release is $latest_version, you are currently using $version.
Please check http://search.cpan.org/dist/Dancer2/ for updates.

|;
    }
}

sub _send_http_request {
    my $url = shift;

    my $ua = HTTP::Tiny->new( timeout => 5 );

    my $response = $ua->get($url);
    return $response->{'success'} ? $response->{'content'} : undef;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Dancer2::CLI::Command::gen - create new Dancer2 application

=head1 VERSION

version 0.204002

=head1 AUTHOR

Dancer Core Developers

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut