File Coverage

blib/lib/PFT/Tree.pm
Criterion Covered Total %
statement 53 57 92.9
branch 8 10 80.0
condition 3 6 50.0
subroutine 17 21 80.9
pod 3 9 33.3
total 84 103 81.5


line stmt bran cond sub pod time code
1             # Copyright 2014-2016 - Giovanni Simoni
2             #
3             # This file is part of PFT.
4             #
5             # PFT is free software: you can redistribute it and/or modify it under the
6             # terms of the GNU General Public License as published by the Free
7             # Software Foundation, either version 3 of the License, or (at your
8             # option) any later version.
9             #
10             # PFT is distributed in the hope that it will be useful, but WITHOUT ANY
11             # WARRANTY; without even the implied warranty of MERCHANTABILITY or
12             # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with PFT. If not, see .
17             #
18             package PFT::Tree v1.4.1;
19              
20             =encoding utf8
21              
22             =head1 NAME
23              
24             PFT::Tree - Filesystem tree mapping a PFT site
25              
26             =head1 SYNOPSIS
27              
28             PFT::Tree->new();
29             PFT::Tree->new($basedir);
30             PFT::Tree->new($basedir, {create => 1});
31              
32             =head1 DESCRIPTION
33              
34             The structure is the following:
35              
36             ├── build
37             ├── content
38             │   └── ...
39             ├── inject
40             ├── pft.yaml
41             └── templates
42              
43             Where:
44              
45             =over
46              
47             =item C is a directory is handled with a C
48             instance.
49              
50             =item C is a configuration file handled with C
51              
52             =item The remaining directories are just created, but the content is not
53             handled by the C structure.
54              
55             =cut
56              
57 2     2   154428 use utf8;
  2         21  
  2         13  
58 2     2   84 use v5.16;
  2         8  
59 2     2   10 use strict;
  2         11  
  2         43  
60 2     2   9 use warnings;
  2         4  
  2         69  
61              
62 2     2   11 use File::Spec;
  2         5  
  2         47  
63 2     2   9 use File::Path qw/make_path/;
  2         4  
  2         132  
64 2     2   12 use Carp;
  2         4  
  2         127  
65 2     2   18 use Cwd;
  2         4  
  2         102  
66              
67 2     2   880 use PFT::Content;
  2         5  
  2         66  
68 2     2   882 use PFT::Conf;
  2         5  
  2         86  
69 2     2   838 use PFT::Map;
  2         6  
  2         923  
70              
71             sub new {
72 5     5 0 2549 my($cls, $given, $opts) = @_;
73              
74 5 100       23 if ($opts->{create}) {
    100          
75 3 50       12 defined $given or confess "Base dir is mandatory if creating";
76 3         11 my $root = PFT::Conf::locate($given);
77 3 100 66     24 if (defined $root and $root ne $given) {
78 1         183 confess "Cannot nest. Found a root in $root";
79             }
80 2         9 my $self = bless { root => $given }, $cls;
81 2         10 $self->_create();
82 2         11 $self
83             } elsif (defined(my $root = PFT::Conf::locate($given))) {
84 1         11 bless { root => $root }, $cls;
85             } else {
86 1   33     213 croak 'Cannot find tree in ', $given || Cwd::cwd;
87             }
88             }
89              
90             sub _create {
91 2     2   5 my $self = shift;
92 2         6 make_path map({ $self->$_ } qw/
  6         39  
93             dir_content
94             dir_templates
95             dir_inject
96             /), {
97             #verbose => 1,
98             mode => 0711,
99             };
100              
101 2 50       373 unless (PFT::Conf::isroot(my $root = $self->{root})) {
102 2         16 PFT::Conf->new_default->save_to($root);
103             }
104              
105 2         25 $self->content(create => 1);
106             }
107              
108             =head2 Properties
109              
110             =over 1
111              
112             =item dir_content
113              
114             =cut
115              
116 5     5 1 52 sub dir_content { File::Spec->catdir(shift->{root}, 'content') }
117              
118 0     0 0 0 sub dir_base { shift->{root} }
119 2     2 0 12 sub dir_templates { File::Spec->catdir(shift->{root}, 'templates') }
120 2     2 0 403 sub dir_inject { File::Spec->catdir(shift->{root}, 'inject') }
121 0     0 0 0 sub dir_build { File::Spec->catdir(shift->{root}, 'build') }
122              
123             =item content
124              
125             Returns a C object, abstracting the access to the I
126             directory.
127              
128             =cut
129              
130 3     3 1 14 sub content { PFT::Content->new(shift->dir_content, {@_}) }
131              
132             =item map
133              
134             Returns a C object, abstracting the the content graph.
135              
136             =cut
137              
138 0     0 0   sub content_map { PFT::Map->new(shift->content) }
139              
140             =item conf
141              
142             Returns a C object, abstracting the configuration file.
143              
144             =cut
145              
146 0     0 1   sub conf { PFT::Conf->new_load(shift->{root}) }
147              
148             =back
149              
150             =cut
151              
152             1;