| 1 | use 5.006; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package File::Tempdir::ForPackage; | ||
| 6 | |||
| 7 | our $VERSION = '1.000001'; | ||
| 8 | |||
| 9 | # ABSTRACT: Easy temporary directories associated with packages. | ||
| 10 | |||
| 11 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 12 | |||
| 13 | use Carp qw( croak ); | ||
| 14 | use Moo qw( has ); | ||
| 15 | use File::Temp qw(); | ||
| 16 | ## no critic (Subroutines::ProhibitCallsToUnexportedSubs) | ||
| 17 | use constant MINX => File::Temp::MINX; | ||
| 18 | ## use critic | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | has package => ( | ||
| 37 | is => ro =>, | ||
| 38 | lazy => 1, | ||
| 39 | default => sub { scalar [ caller 1 ]->[0] }, | ||
| 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 | has with_version => ( is => ro =>, lazy => 1, default => sub { undef } ); | ||
| 92 | has with_timestamp => ( is => ro =>, lazy => 1, default => sub { undef } ); | ||
| 93 | has with_pid => ( is => ro =>, lazy => 1, default => sub { undef } ); | ||
| 94 | has num_random => ( | ||
| 95 | is => 'ro', | ||
| 96 | isa => sub { | ||
| 97 | return if $_[0] >= MINX(); | ||
| 98 | croak( "num_random ( $_[0] ) must be >= " . MINX() ); | ||
| 99 | }, | ||
| 100 | default => sub { 8 }, | ||
| 101 | ); | ||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | has '_preserve' => ( is => rw =>, init_arg => 'preserve', lazy => 1, default => sub { 0 } ); | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | has _dir => ( is => 'lazy', clearer => 1, predicate => 1 ); | ||
| 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 | sub preserve { | ||
| 144 | my ( $self, @args ) = @_; | ||
| 145 | if ( @args and not $args[0] ) { | ||
| 146 | $self->_preserve(0); | ||
| 147 | $self->_dir->unlink_on_destroy(1); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | $self->_preserve(1); | ||
| 151 | $self->_dir->unlink_on_destroy(0); | ||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | sub _clean_pkg { | ||
| 162 | my ($package) = @_; | ||
| 163 | $package =~ s/::/-/gsmx; | ||
| 164 | $package =~ s/[^\w-]+/_/gsmx; | ||
| 165 | return $package; | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | sub _clean_ver { | ||
| 175 | my ($ver) = @_; | ||
| 176 | return 'versionundef' if not defined $ver; | ||
| 177 | $ver =~ s/[^v\d_.]+/_/gsmx; | ||
| 178 | return $ver; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | sub _build__dir { | ||
| 188 | my ($self) = shift; | ||
| 189 | |||
| 190 | my $template = q{perl-}; | ||
| 191 | $template .= _clean_pkg( $self->package ); | ||
| 192 | |||
| 193 | if ( $self->with_version ) { | ||
| 194 | $template .= q{-} . _clean_ver( $self->package->VERSION ); | ||
| 195 | } | ||
| 196 | if ( $self->with_timestamp ) { | ||
| 197 | $template .= q{-} . time; | ||
| 198 | } | ||
| 199 | if ( $self->with_pid ) { | ||
| 200 | ## no critic ( ProhibitPunctuationVars ) | ||
| 201 | $template .= q{-} . $$; | ||
| 202 | } | ||
| 203 | $template .= q{-} . ( 'X' x $self->num_random ); | ||
| 204 | |||
| 205 | my $dir = File::Temp->newdir( TEMPLATE => $template, TMPDIR => 1 ); | ||
| 206 | if ( $self->_preserve ) { | ||
| 207 | $dir->unlink_on_destroy(0); | ||
| 208 | } | ||
| 209 | return $dir; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | sub dir { | ||
| 221 | my ($self) = shift; | ||
| 222 | return $self->_dir . q[]; | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | sub run_once_in { | ||
| 241 | my ( $self, $options, $code ) = @_; | ||
| 242 | $code = $options unless defined $code; | ||
| 243 | require File::pushd; | ||
| 244 | { | ||
| 245 | ## no critic (Variables::ProhibitUnusedVarsStricter) | ||
| 246 | my $marker = File::pushd::pushd( $self->dir ); | ||
| 247 | $code->( $self->dir ); | ||
| 248 | } | ||
| 249 | |||
| 250 | # Dir POP. | ||
| 251 | $self->_clear_dir; | ||
| 252 | return $self; | ||
| 253 | } | ||
| 254 | |||
| 255 | no Moo; | ||
| 256 | |||
| 257 | 1; | ||
| 258 | |||
| 259 | __END__ |