| 1 | # -*- mode: perl; coding: utf-8 -*- | ||||||||||
| 2 | package YATT::Util::DirTreeBuilder; | ||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 10 | ▶ | use base qw(YATT::Class::Configurable File::Spec); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 10 | ▶ | BEGIN {require Exporter; *import = \&Exporter::import} | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | our @EXPORT_OK = qw(tmpbuilder); | |||||||||
| 9 | |||||||||||
| 10 | 10 | ▶ | use YATT::Fields qw(^cf_DIR cf_TESTNO cf_AUTO_REMOVE); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 10 | ▶ | use overload '&{}' => 'as_sub'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 10 | ▶ | use File::Remove qw(remove); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 10 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 3 | ▶ | sub tmpbuilder { | ||||||||
|
Subroutine
tmpbuilder: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 16 | 3 | my ($tmpdir) = @_; | |||||||||
| 17 | 3 | ▶ | unless (-d $tmpdir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 2 | ▶ | mkdir $tmpdir or die "Can't mkdir $tmpdir: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | } | ||||||||||
| 20 | MY->new(DIR => $tmpdir, TESTNO => 0 | ||||||||||
| 21 | 3 | , AUTO_REMOVE => !$ENV{DEBUG_TMP}); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 3 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
| |||||||||||
| 25 | 3 | my MY $self = shift; | |||||||||
| 26 | 3 | ▶ | remove \1, $self->{cf_DIR} if $self->{cf_AUTO_REMOVE}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 20 | ▶ | sub as_sub { | ||||||||
|
Subroutine
as_sub: called CC=3 SCAR=11.9
Pod
undocumented
| |||||||||||
| 30 | 20 | my MY $self = shift; | |||||||||
| 31 | 20 | my $basedir = $self->{cf_DIR} . '/t' . ++$self->{cf_TESTNO}; | |||||||||
| 32 | 20 | ▶ | unless (-d $basedir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 20 | ▶ | mkdir $basedir or die "Can't mkdir $basedir! $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | } | ||||||||||
| 35 | 181 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.4
| |||||||||||
| 36 | 181 | $self->build($basedir, @_); | |||||||||
| 37 | 181 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | ▶ | ($basedir, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | $self->build($basedir, [FILE => @_]) | |||||||||
| 40 | 0 | }); | |||||||||
| 41 | } else { | ||||||||||
| 42 | 181 | $basedir; | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | 20 | } | |||||||||
| 46 | |||||||||||
| 47 | 411 | ▶ | sub build { | ||||||||
|
Subroutine
build: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||
| 48 | 411 | my ($self, $basedir, @action) = @_; | |||||||||
| 49 | 411 | foreach my $action (@action) { | |||||||||
| 50 | 413 | ▶ | next unless ref $action eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 413 | ▶ | my $sub = $self->can("build_" . $action->[0]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | or die "Invalid builder spec: $action->[0]"; | ||||||||||
| 53 | 413 | $sub->($self, $basedir, @{$action}[1 .. $#$action]); | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 230 | ▶ | sub build_DIR { | ||||||||
|
Subroutine
build_DIR: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 58 | 230 | my ($self, $basedir, $name, @action) = @_; | |||||||||
| 59 | 230 | my $dir = "$basedir/$name"; | |||||||||
| 60 | 230 | ▶ | unless (-d $dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 44 | ▶ | mkdir($dir) or die "Can't mkdir $dir: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | } | ||||||||||
| 63 | 230 | $self->build($dir, @action); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 183 | ▶ | sub build_FILE { | ||||||||
|
Subroutine
build_FILE: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 67 | 183 | my ($self, $basedir, $name, @body) = @_; | |||||||||
| 68 | 183 | my $fn = "$basedir/$name"; | |||||||||
| 69 | 183 | ▶ | open(my $out, '>', $fn), "file $fn" or die "Can't create $fn: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 183 | print $out @body; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 159 | ▶ | sub path2desc { | ||||||||
|
Subroutine
path2desc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 74 | 159 | my ($self, $path, $content) = @_; | |||||||||
| 75 | 159 | $self->path2desc_1([$self->splitdir($path), $content]); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 357 | ▶ | sub path2desc_1 { | ||||||||
|
Subroutine
path2desc_1: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 79 | 357 | my ($self, $desc) = @_; | |||||||||
| 80 | 357 | ▶ | if (@$desc > 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 198 | [DIR => shift @$desc | |||||||||
| 82 | , $self->path2desc_1($desc)]; | ||||||||||
| 83 | } else { | ||||||||||
| 84 | 159 | unshift @$desc, 'FILE'; | |||||||||
| 85 | 159 | $desc; | |||||||||
| 86 | } | ||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 10 | 1; | |||||||||