1package Minilla::CLI::New;
2use strict;
3use warnings;
4use utf8;
5use File::pushd;
6use File::Path qw(mkpath);
7
8use Minilla::Util qw(check_git cmd parse_options);
9use Minilla::Logger;
10
11sub run {
12    my ($self, @args) = @_;
13
14    my $username;
15    my $email;
16    my $profile = 'Default';
17    parse_options(
18        \@args,
19        'username=s' => \$username,
20        'email=s'    => \$email,
21        'p|profile=s' => \$profile,
22    );
23
24    my $module = shift @args or errorf("Missing module name\n");
25       $module =~ s!-!::!g;
26
27    check_git;
28
29    $username ||= `git config user.name`;
30    $username =~ s/\n$//;
31
32    $email ||= `git config user.email`;
33    $email =~ s/\n$//;
34
35    my $version = '0.01';
36
37    unless ($username) {
38        errorf("Please set user.name in git, or use `--username` option.\n");
39    }
40
41    # $module = "Foo::Bar"
42    # $suffix = "Bar"
43    # $dist   = "Foo-Bar"
44    # $path   = "Foo/Bar.pm"
45    my @pkg    = split /::/, $module;
46    my $suffix = $pkg[ @pkg - 1 ];
47    my $dist   = join "-", @pkg;
48    my $path   = join( "/", @pkg ) . ".pm";
49    ( my $dir = $dist ) =~ s/^App-//;
50
51    if (-d $dist) {
52        errorf("There is %s/\n", $dist);
53    }
54
55    my $author = $username;
56
57    my $profile_klass = "Minilla::Profile::${profile}";
58    eval "require $profile_klass; 1;" or die $@;
59    my $skelton = $profile_klass->new(
60        dist    => $dist,
61        path    => $path,
62        author  => $username,
63        suffix  => $suffix,
64        module  => $module,
65        version => $version,
66        email   => $email,
67    );
68    {
69        mkpath($dist);
70        my $guard = pushd($dist);
71        $skelton->generate();
72
73        # init git repo
74        infof("Initializing git $module\n");
75        cmd('git', 'init');
76
77        # generate project after initialize git repo
78        my $project = Minilla::Project->new();
79        $project->generate_minil_toml($profile);
80        $project->regenerate_files();
81
82        # and git add all things
83        cmd('git', 'add', '.');
84    }
85
86    infof("Finished to create $module\n");
87}
88
891;
90__END__
91
92=head1 NAME
93
94Minilla::CLI::New - Generate new module skeleton
95
96=head1 SYNOPSIS
97
98    # Create new app using Module::Build::Tiny(default)
99    % minil new MyApp
100
101    # Create new app using XS
102    % minil new -p XS MyApp
103
104=head1 DESCRIPTION
105
106This module creates module skeleton to current directory.
107
108=head1 OPTIONS
109
110=over 4
111
112=back
113