File Coverage

blib/lib/WG.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package WG;
2              
3 1     1   4843 use strict;
  1         3  
  1         55  
4 1     1   5 use warnings;
  1         2  
  1         29  
5              
6 1     1   222 use Moose;
  0            
  0            
7              
8             use Config::General;
9             use Cwd;
10             use File::Spec;
11             use Log::Any qw/$log/;
12              
13              
14             use WG::Dev;
15              
16             BEGIN{
17             # The test compatible File::Share
18             eval{ require File::Share; File::Share->import('dist_dir'); };
19             if( $@ ){
20             # The production only File::ShareDir
21             require File::ShareDir;
22             File::ShareDir->import('dist_dir');
23             }
24             };
25              
26             =head1 NAME
27              
28             WG - The Wigoot application
29              
30             =head1 ATTRIBUTES
31              
32             config_file - The config file. Not mandatory. Will try to find a wigoot.conf in /etc/wigoot/ or in the current dir
33              
34             config - A hash of the whole config.
35              
36             developer - a WG::Dev object
37              
38             share_dir - The share directory of this application (contains resource files).
39              
40             =cut
41              
42             has 'config_file' => ( is => 'ro' , isa => 'Str' , lazy_build => 1 );
43              
44             has 'config' => ( is => 'ro' , isa => 'HashRef' , lazy_build => 1 );
45              
46             has 'share_dir' => ( is => 'ro' , isa => 'Str', lazy_build => 1 );
47              
48             has 'developer' => ( is => 'ro' , isa => 'WG::Dev' , lazy_build => 1 );
49              
50             {
51             my $ETC_CONFIG = '/etc/wigoot/wigoot-conf';
52              
53             sub _build_config_file{
54             my ($self) = @_;
55             if( -e $ETC_CONFIG ){
56             $log->info("Config file is $ETC_CONFIG");
57             return $ETC_CONFIG;
58             }
59              
60             my $cwd_based = Cwd::abs_path('wigoot.conf');
61             unless( -e $cwd_based ){
62             confess("No $cwd_based file found. Please specify the config file");
63             }
64             return $cwd_based;
65             }
66             }
67              
68             sub _build_config{
69             my ($self) = @_;
70             my $file = $self->config_file();
71             $log->info("Building config from '$file'");
72             my $cf = Config::General->new($file) or confess("Cannot load '$file'");
73             return { $cf->getall() };
74             }
75              
76             sub _build_share_dir{
77             my ($self) = @_;
78             my $file_based_dir = File::Spec->rel2abs(__FILE__);
79             my $package = __PACKAGE__;
80              
81             $file_based_dir =~ s|lib/$package.+||;
82             $file_based_dir .= 'share/';
83             if( -d $file_based_dir ){
84              
85             my $real_sharedir = Cwd::realpath($file_based_dir);
86             unless( $real_sharedir ){
87             confess("Could not build Cwd::realpath from '$file_based_dir'");
88             }
89             $real_sharedir .= '/';
90              
91             $log->info("Will use file based shared directory '$real_sharedir'");
92             return $real_sharedir;
93             }
94              
95             my $dist_based_dir = Cwd::realpath(dist_dir(__PACKAGE__));
96              
97             my $real_sharedir = Cwd::realpath($dist_based_dir);
98             unless( $real_sharedir ){
99             confess("Could not build Cwd::realpath from '$dist_based_dir'");
100             }
101              
102             $real_sharedir .= '/';
103              
104             $log->info("Will use File::Share based directory ".$real_sharedir);
105             return $real_sharedir;
106             }
107              
108             sub _build_developer{
109             my ($self) = @_;
110             return WG::Dev->new({ wg => $self });
111             }
112              
113              
114             __PACKAGE__->meta->make_immutable();