File Coverage

blib/lib/Minima/Project.pm
Criterion Covered Total %
statement 58 58 100.0
branch 14 14 100.0
condition 2 2 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 84 84 100.0


line stmt bran cond sub pod time code
1 2     2   1416 use v5.40;
  2         9  
2              
3             package Minima::Project;
4              
5 2     2   1164 use File::Share;
  2         66073  
  2         110  
6 2     2   18 use Path::Tiny;
  2         4  
  2         96  
7 2     2   1326 use Template;
  2         41277  
  2         675  
8              
9             our $tdir = path(
10             File::Share::dist_dir('Minima')
11             )->child('/templates');
12             our $verbose = 0;
13              
14 7         19 sub create ($dir, $user_config = {})
15 7     7 1 307796 {
  7         19  
  7         16  
16 7   100     45 my $project = path($dir // '.')->absolute;
17 7         661 my %config = (
18             'cpanfile' => 0,
19             'static' => 1,
20             'verbose' => 0,
21             %$user_config
22             );
23 7         23 $verbose = $config{verbose};
24              
25             # Test if directory can be used
26 7 100       31 if ($project->exists) {
27 4 100       157 die "Project destination must be a directory\n"
28             unless $project->is_dir;
29 3 100       58 die "Project destination must be empty.\n"
30             if $project->children;
31             } else {
32 3         290 $project->mkdir;
33             }
34              
35 5         1386 chdir $project;
36              
37             # Create files
38 5         108 for my ($file, $content) (get_templates(\%config)) {
39 56         61644 my $dest = path($file);
40 56         3070 my $dir = $dest->parent;
41              
42 56 100       4806 unless ($dir->is_dir) {
43 20         1217 _info("mkdir $dir");
44 20         93 $dir->mkdir;
45             }
46              
47 56         9144 _info(" spew $dest");
48 56         212 $dest->spew_utf8($content);
49             }
50             }
51              
52             sub get_templates ($config)
53 5     5 1 15 {
  5         12  
  5         7  
54 5         49 my %files;
55 2     2   18 use Template::Constants qw/ :debug /;
  2         4  
  2         1121  
56 5         69 my $tt = Template->new(
57             INCLUDE_PATH => $tdir,
58             OUTLINE_TAG => '@@',
59             TAG_STYLE => 'star',
60             );
61              
62 5         34538 $config->{version} = Minima->VERSION;
63              
64 5         38 foreach (glob "$tdir/*.[sd]tpl") {
65 60         1769 my $content = path($_)->slurp_utf8;
66 60         16556 $_ = path($_)->basename;
67 60         6287 tr|-|/|;
68 60         112 tr|+|.|;
69 60 100       292 if (/\.dtpl$/) {
70             # Process .d(ynamic) template
71 20         47 my $template = $content;
72 20         39 my $processed;
73 20         111 $tt->process(\$template, $config, \$processed);
74 20         114710 $content = $processed;
75             }
76 60         447 s/\.\w+$//;
77 60 100       331 $files{$_} = $content if $content;
78             }
79              
80 5         87 map { $_, $files{$_} } sort keys %files;
  56         152  
81             }
82              
83             sub _info ($m)
84 76     76   636 {
  76         175  
  76         113  
85 76 100       366 say $m if ($verbose);
86             }
87              
88             1;
89              
90             __END__