File Coverage

bin/kelp-generator
Criterion Covered Total %
statement 47 53 88.6
branch 10 16 62.5
condition 3 6 50.0
subroutine 6 6 100.0
pod n/a
total 66 81 81.4


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 3     3   16758 use Kelp::Base -strict;
  3         14  
  3         25  
4 3     3   2368 use Getopt::Long;
  3         40232  
  3         12  
5 3     3   2137 use Kelp::Generator;
  3         9  
  3         15  
6 3     3   16 use Path::Tiny;
  3         5  
  3         183  
7 3     3   1534 use Pod::Usage;
  3         158492  
  3         7296  
8              
9 3         574025 my $path = '.';
10 3         11 my $verbose = 1;
11 3         8 my $force = 0;
12 3         10 my $type = 'kelp';
13 3         6 my $tabs = 0;
14 3         8 my $help = 0;
15              
16 3         28 GetOptions(
17             "path=s" => \$path,
18             "type=s" => \$type,
19             "verbose!" => \$verbose,
20             "force!" => \$force,
21             "tabs!" => \$tabs,
22             "help" => \$help
23             );
24              
25 3         3554 my $name = $ARGV[0];
26 3         47 my $generator = Kelp::Generator->new;
27              
28 3 100 66     33 if (!$name || $help) {
29 1 50       3 say 'Error - no application name.' unless $help;
30 1         11 pod2usage(-verbose => 1, -exitval => 'NOEXIT');
31 1         12814 say 'Available application types:';
32 1         7 say for map { ' ' x 4 . $_ } $generator->list_templates;
  2         37  
33 1 50       0 exit ($help ? 0 : 1);
34             }
35              
36             # Remove the slash at the end
37 2         9 $path =~ s{/$}{};
38 2         14 my $files = $generator->get_template($type, $name);
39              
40 1         5 for my $filedata (@$files) {
41 10         16422 my ($filename, $contents) = @$filedata;
42              
43             # replace spaces with tabs
44             # each 4 spaces will become a tab character
45             # last 2 spaces will also become a tab due to rounding (+0.5)
46 10 50       34 $contents =~ s{ ^ ((?: [ ]{4} | [ ]{2} )+) }{ "\t" x (length($1) / 4 + 0.5) }xmeg
  0         0  
47             if $tabs;
48              
49 10         45 my $dir = $path . path("/$filename")->parent;
50 10         1538 my $file = path($path . '/' . $filename);
51              
52 10 100       723 if (!-d $dir) {
53 5         33 _say("Creating folder: $dir");
54 5         18 path($dir)->mkpath;
55             }
56              
57 10 50       1615 if ($file->is_dir) {
58 0         0 say "$filename is a directory - manual action required. Skipping...";
59 0         0 next;
60             }
61              
62 10 50 33     1366 if ($file->exists && !$force) {
63 0         0 say "File $filename exists. Use --force to overwrite. Skipping...";
64 0         0 next;
65             }
66              
67 10         187 _say("Writing file: $filename");
68 10         60 $file->spew({binmode => ':encoding(UTF-8)'}, $contents);
69             }
70              
71             sub _say {
72 15     15   31 my $what = shift;
73 15 50       44 if ($verbose) {
74 0           say $what;
75             }
76             }
77              
78             __END__
79              
80             =pod
81              
82             =head1 NAME
83              
84             kelp-generator - Generate Kelp applications
85              
86             =head1 SYNOPSIS
87              
88             kelp-generator [options] <Application::Package>
89              
90             =head1 OPTIONS
91              
92             =over 4
93              
94             =item B<--path=s>
95              
96             Path where to create the files
97              
98             =item B<--type=s>
99              
100             Type of application to create (default: kelp)
101              
102             =item B<--(no)verbose>
103              
104             Display more or less information
105              
106             =item B<--force>
107              
108             Force overwriting existing files
109              
110             =item B<--tabs>
111              
112             Use tabs for indentation instead of spaces
113              
114             =item B<--help>
115              
116             This help screen
117              
118             =back
119              
120             =head1 DESCRIPTION
121              
122             This program will generate a Kelp project from one of the registered
123             templates. C<< <Application::Package> >> will be used as the name of the package.
124