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   1206 use v5.40;
  2         12  
2              
3             package Minima::Project;
4              
5 2     2   1007 use File::Share;
  2         66025  
  2         328  
6 2     2   16 use Path::Tiny;
  2         3  
  2         105  
7 2     2   1008 use Template;
  2         42491  
  2         669  
8              
9             our $tdir = path(
10             File::Share::dist_dir('Minima')
11             )->child('/templates');
12             our $verbose = 0;
13              
14 7         18 sub create ($dir, $user_config = {})
15 7     7 1 183992 {
  7         17  
  7         13  
16 7   100     39 my $project = path($dir // '.')->absolute;
17 7         1147 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       28 if ($project->exists) {
27 4 100       128 die "Project destination must be a directory\n"
28             unless $project->is_dir;
29 3 100       44 die "Project destination must be empty.\n"
30             if $project->children;
31             } else {
32 3         157 $project->mkdir;
33             }
34              
35 5         1030 chdir $project;
36              
37             # Create files
38 5         88 for my ($file, $content) (get_templates(\%config)) {
39 56         55378 my $dest = path($file);
40 56         3113 my $dir = $dest->parent;
41              
42 56 100       4800 unless ($dir->is_dir) {
43 20         1431 _info("mkdir $dir");
44 20         80 $dir->mkdir;
45             }
46              
47 56         8126 _info(" spew $dest");
48 56         211 $dest->spew_utf8($content);
49             }
50             }
51              
52             sub get_templates ($config)
53 5     5 1 23 {
  5         11  
  5         28  
54 5         7 my %files;
55 2     2   15 use Template::Constants qw/ :debug /;
  2         4  
  2         1131  
56 5         46 my $tt = Template->new(
57             INCLUDE_PATH => $tdir,
58             OUTLINE_TAG => '@@',
59             TAG_STYLE => 'star',
60             );
61              
62 5         21681 $config->{version} = Minima->VERSION;
63              
64 5         99 foreach (glob "$tdir/*.[sd]tpl") {
65 60         2024 my $content = path($_)->slurp_utf8;
66 60         17083 $_ = path($_)->basename;
67 60         6503 tr|-|/|;
68 60         118 tr|+|.|;
69 60 100       547 if (/\.dtpl$/) {
70             # Process .d(ynamic) template
71 20         47 my $template = $content;
72 20         28 my $processed;
73 20         118 $tt->process(\$template, $config, \$processed);
74 20         108869 $content = $processed;
75             }
76 60         464 s/\.\w+$//;
77 60 100       306 $files{$_} = $content if $content;
78             }
79              
80 5         122 map { $_, $files{$_} } sort keys %files;
  56         149  
81             }
82              
83             sub _info ($m)
84 76     76   709 {
  76         151  
  76         151  
85 76 100       305 say $m if ($verbose);
86             }
87              
88             1;
89              
90             __END__