File Coverage

blib/lib/Minima/Setup.pm
Criterion Covered Total %
statement 55 55 100.0
branch 18 18 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod 3 3 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1 5     5   789633 use v5.40;
  5         25  
2              
3             package Minima::Setup;
4              
5 5     5   40 use Carp;
  5         19  
  5         499  
6 5     5   3068 use Minima::App;
  5         17  
  5         258  
7 5     5   57 use Path::Tiny;
  5         11  
  5         336  
8 5     5   2698 use Plack::Test;
  5         3951  
  5         390  
9 5     5   2592 use YAML::XS 'LoadFile';
  5         18871  
  5         4366  
10              
11             our $config = {};
12             our $app;
13             our $base;
14              
15             sub import
16             {
17 27     27   42394 shift; # discard package name
18              
19             # If we were called from a .psgi, save
20             # its parent as the base directory
21 27         122 my $caller = path( (caller)[1] );
22 27 100       1090 if ($caller->basename =~ /\.psgi$/) {
23 1         30 $base = $caller->absolute->parent;
24             } else {
25 26         1012 $base = path('.')->absolute;
26             }
27              
28 27         2375 prepare(@_);
29             }
30              
31             sub prepare ($file = undef)
32 28     28 1 74 {
  28         50  
  28         31  
33 28         77 my $default_prefix = $base->child('etc/config');
34 28         919 my @files = map { "$default_prefix.$_" } qw/ yaml yml pl /;
  84         496  
35              
36 28 100       191 if ($file) {
37 14         44 my $file_abs = path($file)->absolute;
38              
39 14 100       497 croak "Config file `$file` does not exist.\n"
40             unless -e $file_abs;
41              
42 13         189 $file = $file_abs;
43             }
44 27         58 unshift @files, $file;
45              
46 27         42 my $adjective;
47              
48 27         92 for (my $i = 0; $i < @files; $i++) {
49              
50 60         92 my $f = $files[$i];
51 60 100 100     749 next unless defined $f && -e $f;
52              
53 19 100       133 $adjective = $i ? 'default ' : '';
54              
55 19 100       43 if ($f =~ /\.ya?ml$/) {
56 10         36 try {
57 10         26 $config = LoadFile $f;
58             } catch ($e) {
59 2         359 croak "Failed to parse ${adjective}config file "
60             . "`$f`:\n$e\n";
61             }
62             } else {
63 9         428 $config = do $f;
64 9 100       171 croak "Failed to parse ${adjective}config file "
65             . "`$f`: $@\n" if $@;
66             }
67              
68 16         899 last;
69             }
70              
71 24 100       674 croak "Config is not a hash reference.\n"
72             unless ref $config eq ref {};
73              
74             # If the loaded config does not set base_dir,
75             # set it to the saved .psgi directory
76             $config->{base_dir} = $base->stringify
77 18 100       124 unless defined $config->{base_dir};
78              
79             # Initialize app
80 18         204 $app = Minima::App->new(
81             configuration => $config,
82             );
83             }
84              
85             sub init ($env)
86 5     5 1 52608 {
  5         12  
  5         9  
87 5         37 $app->set_env($env);
88 5         110 $app->run;
89             }
90              
91             sub test
92             {
93 1     1 1 258774 Plack::Test->create(\&init);
94             }
95              
96             1;
97              
98             __END__