File: TempDirObject.pm

package info (click to toggle)
libtest-file-sharedir-perl 1.000005-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 440 kB
  • ctags: 45
  • sloc: perl: 505; makefile: 2
file content (206 lines) | stat: -rw-r--r-- 4,423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use 5.006;    # pragmas
use strict;
use warnings;

package Test::File::ShareDir::TempDirObject;

our $VERSION = '1.000005';

# ABSTRACT: Internal Object to make code simpler.

our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY












use Path::Tiny qw(path);
use Carp qw(confess);
## no critic (Subroutines::RequireArgUnpacking)
sub __rcopy { require File::Copy::Recursive; goto \&File::Copy::Recursive::rcopy; }







sub new {
  my ( $class, $config ) = @_;

  confess('Need -share => for Test::File::ShareDir') unless exists $config->{-share};

  my $realconfig = {
    root    => path(q{./})->absolute,    #->resolve->absolute,
    modules => {},
    dists   => {},
  };

  $realconfig->{root}    = path( delete $config->{-root} )->absolute if exists $config->{-root};
  $realconfig->{modules} = delete $config->{-share}->{-module}       if exists $config->{-share}->{-module};
  $realconfig->{dists}   = delete $config->{-share}->{-dist}         if exists $config->{-share}->{-dist};

  confess( 'Unsupported -share types : ' . join q{ }, keys %{ $config->{-share} } ) if keys %{ $config->{-share} };

  delete $config->{-share};

  confess( 'Unsupported parameter to import() : ' . join q{ }, keys %{$config} ) if keys %{$config};

  return bless $realconfig, $class;
}

my @cache;

sub _tempdir {
  my ($self) = shift;
  return $self->{tempdir} if exists $self->{tempdir};
  $self->{tempdir} = Path::Tiny::tempdir( CLEANUP => 1 );

  # Explicit keepalive till GC
  push @cache, $self->{tempdir};
  return $self->{tempdir};
}

sub _module_tempdir {
  my ($self) = shift;
  return $self->{module_tempdir} if exists $self->{module_tempdir};
  $self->{module_tempdir} = $self->_tempdir->child('auto/share/module');
  $self->{module_tempdir}->mkpath();
  return $self->{module_tempdir}->absolute;
}

sub _dist_tempdir {
  my ($self) = shift;
  return $self->{dist_tempdir} if exists $self->{dist_tempdir};
  $self->{dist_tempdir} = $self->_tempdir->child('auto/share/dist');
  $self->{dist_tempdir}->mkpath();
  return $self->{dist_tempdir}->absolute;
}

sub _root {
  my ($self) = shift;
  return $self->{root};
}

sub _modules { return shift->{modules}; }

sub _dists { return shift->{dists} }

sub _module_names {
  my ($self) = shift;
  return keys %{ $self->_modules };
}

sub _dist_names {
  my ($self) = shift;
  return keys %{ $self->_dists };
}

sub _module_share_target_dir {
  my ( $self, $modname ) = @_;

  ## no critic (RegularExpressions)
  $modname =~ s/::/-/g;

  return $self->_module_tempdir->child($modname);
}

sub _dist_share_target_dir {
  my ( $self, $distname ) = @_;
  return $self->_dist_tempdir->child($distname);
}

sub _module_share_source_dir {
  my ( $self, $module ) = @_;
  return path( $self->_modules->{$module} )->absolute( $self->_root );
}

sub _dist_share_source_dir {
  my ( $self, $dist ) = @_;
  return path( $self->_dists->{$dist} )->absolute( $self->_root );
}

sub _install_module {
  my ( $self, $module ) = @_;
  return __rcopy( $self->_module_share_source_dir($module), $self->_module_share_target_dir($module) );
}

sub _install_dist {
  my ( $self, $dist ) = @_;
  return __rcopy( $self->_dist_share_source_dir($dist), $self->_dist_share_target_dir($dist) );
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test::File::ShareDir::TempDirObject - Internal Object to make code simpler.

=head1 VERSION

version 1.000005

=head1 SYNOPSIS

    my $object = $class->new({
        -root => 'foo', # optional
        -share => {
            -module => {
                'baz' => 'dir',
            },
            -dist => {
                'Task-baz' => 'otherdir',
            },
        },
    });

    # installs a sharedir for 'baz' by copying 'foo/dir'
    $object->_install_module('baz');

    # installs a shardir for distribution 'Task-baz' by copying 'foo/otherdir'
    $object->_install_dist('Task-baz');

    # add to @INC
    unshift @INC, $object->_tempdir->stringify;

=head1 METHODS

=head2 new

Creates a new instance of this object.

=begin MetaPOD::JSON v1.1.0

{
    "namespace":"Test::File::ShareDir::TempDirObject",
    "interface":"class"
}


=end MetaPOD::JSON

=head1 AUTHOR

Kent Fredric <kentnl@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Kent Fredric <kentnl@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut