File Coverage

blib/lib/File/TreeBuilder.pm
Criterion Covered Total %
statement 10 57 17.5
branch 0 24 0.0
condition 0 8 0.0
subroutine 4 7 57.1
pod 1 1 100.0
total 15 97 15.4


line stmt bran cond sub pod time code
1 1     1   887 use 5.008001;
  1         4  
  1         43  
2 1     1   6 use strict;
  1         2  
  1         38  
3 1     1   23 use warnings;
  1         2  
  1         63  
4             package File::TreeBuilder;
5             BEGIN {
6 1     1   916 $File::TreeBuilder::VERSION = '0.02';
7             }
8             # ABSTRACT: Build simple trees of files and directories.
9              
10             # --------------------------------------------------------------------
11             require Exporter;
12             our @ISA = qw(Exporter);
13             our @EXPORT_OK = qw(build_tree);
14              
15             # --------------------------------------------------------------------
16             sub build_tree {
17 0     0 1   my ($dir, $str) = @_;
18 0           my $caller_pkg = (caller)[0];
19 0 0         $str = q[] unless defined $str;
20 0           my @lines = split /\n/, $str;
21             # Remove blank lines and comments.
22 0           @lines = grep ! /^\s*(?:#|$)/, @lines;
23 0           my $err_str = q[];
24 0           _build_tree($dir, $caller_pkg, \$err_str, @lines);
25 0           return $err_str;
26             }
27              
28             # --------------------------------------------------------------------
29             sub _build_tree {
30 0     0     my ($dir, $caller_pkg, $err_str_ref, @lines) = @_;
31 0 0         if (! defined $dir) {
32 0           $$err_str_ref .= "Directory not defined.\n";
33 0           return;
34             }
35 0 0         if ($dir eq '') {
36 0           $$err_str_ref .= "\$dir is empty string.\n";
37 0           return;
38             }
39 0 0         if (! -d $dir) {
40 0 0         mkdir($dir) or do {
41 0           $$err_str_ref .= "Couldn't create '$dir'.\n";
42 0           return;
43             }
44             }
45 0 0         return unless @lines;
46             # Remove from the beginning of each line as many leading
47             # spaces as there are on the first one.
48 0           my ($leading_spaces) = $lines[0] =~ /^( +)/;
49 0   0       my ($nb_leading_spaces) = length($leading_spaces || '');
50 0           @lines = map { s/ {$nb_leading_spaces}//; $_ } @lines;
  0            
  0            
51 0           my $i = 0;
52 0           while ($i < @lines) {
53 0 0         if ($lines[$i] =~ /^\./) {
    0          
54 0           _build_file($dir, $caller_pkg, $err_str_ref, $lines[$i]);
55 0 0         return unless $$err_str_ref eq q[];
56 0           ++$i;
57             }
58             elsif ($lines[$i] =~ /^\//) {
59 0           my ($to_eval) = $lines[$i] =~ / ^ \/ \s+ (.*) /x;
60 0           my $sub_dir;
61 0           eval "no strict; package $caller_pkg; (\$sub_dir) = ($to_eval)";
62 0 0         ++$i, next unless defined $sub_dir;
63 0           my @sub_lines;
64 0   0       push @sub_lines, $lines[$i]
65             while ++$i < @lines && substr($lines[$i], 0, 1) eq ' ';
66 0           _build_tree("$dir/$sub_dir", $caller_pkg, $err_str_ref, @sub_lines);
67             }
68             else {
69 0           ++$i;
70             }
71             }
72             }
73              
74             # --------------------------------------------------------------------
75             sub _build_file {
76 0     0     my ($dir, $caller_pkg, $err_str_ref, $line) = @_;
77 0           my ($to_eval) = $line =~ / ^ \. \s+ (.*) /x;
78 0           my ($fname, $contents);
79 0           eval "no strict; package $caller_pkg; (\$fname, \$contents) = ($to_eval)";
80 0           my $file_spec = "$dir/$fname";
81 0 0         open my $hdl, ">", $file_spec
82             or $$err_str_ref .= "Couldn't open '$file_spec' for writing: $!";
83 0 0 0       print $hdl $contents if defined($contents) && $contents ne '';
84 0           $hdl->close();
85             }
86              
87             # --------------------------------------------------------------------
88             1;
89              
90             __END__