| 1 | package Zing::Cartridge; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Entity'; | ||
| 15 | |||
| 16 | use File::Spec; | ||
| 17 | |||
| 18 | our $VERSION = '0.26'; # VERSION | ||
| 19 | |||
| 20 | # ATTRIBUTES | ||
| 21 | |||
| 22 | has appdir => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Str', | ||
| 25 | new => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | fun new_appdir($self) { | ||
| 29 | $self->env->appdir || $self->env->home || File::Spec->curdir | ||
| 30 | } | ||
| 31 | |||
| 32 | has appfile => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'Str', | ||
| 35 | new => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | fun new_appfile($self) { | ||
| 39 | File::Spec->catfile($self->appdir, $self->name) | ||
| 40 | } | ||
| 41 | |||
| 42 | has libdir => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'ArrayRef[Str]', | ||
| 45 | new => 1, | ||
| 46 | ); | ||
| 47 | |||
| 48 | fun new_libdir($self) { | ||
| 49 | ['.'] | ||
| 50 | } | ||
| 51 | |||
| 52 | has piddir => ( | ||
| 53 | is => 'ro', | ||
| 54 | isa => 'Str', | ||
| 55 | new => 1, | ||
| 56 | ); | ||
| 57 | |||
| 58 | fun new_piddir($self) { | ||
| 59 | $self->env->piddir || $self->env->home | ||
| 60 | } | ||
| 61 | |||
| 62 | has pidfile => ( | ||
| 63 | is => 'ro', | ||
| 64 | isa => 'Str', | ||
| 65 | new => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | fun new_pidfile($self) { | ||
| 69 | File::Spec->catfile($self->piddir, "@{[$self->name]}.pid") | ||
| 70 | } | ||
| 71 | |||
| 72 | has name => ( | ||
| 73 | is => 'ro', | ||
| 74 | isa => 'Str', | ||
| 75 | opt => 1, | ||
| 76 | ); | ||
| 77 | |||
| 78 | has scheme => ( | ||
| 79 | is => 'rw', | ||
| 80 | isa => 'Scheme', | ||
| 81 | new => 1, | ||
| 82 | ); | ||
| 83 | |||
| 84 | fun new_scheme($self) { | ||
| 85 | my %seen = map {$_, 1} @INC; | ||
| 86 | for my $dir (@{$self->libdir}) { | ||
| 87 | push @INC, $dir if !$seen{$dir}++; | ||
| 88 | } | ||
| 89 | local $@; eval { | ||
| 90 | do $self->appfile | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # METHODS | ||
| 95 | |||
| 96 | method pid() { | ||
| 97 | local $@; return eval { | ||
| 98 | do $self->pidfile | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | method install(Scheme $scheme = $self->scheme) { | ||
| 103 | require Data::Dumper; | ||
| 104 | |||
| 105 | no warnings 'once'; | ||
| 106 | |||
| 107 | local $Data::Dumper::Indent = 0; | ||
| 108 | local $Data::Dumper::Purity = 0; | ||
| 109 | local $Data::Dumper::Quotekeys = 0; | ||
| 110 | local $Data::Dumper::Deepcopy = 1; | ||
| 111 | local $Data::Dumper::Deparse = 1; | ||
| 112 | local $Data::Dumper::Sortkeys = 1; | ||
| 113 | local $Data::Dumper::Terse = 1; | ||
| 114 | local $Data::Dumper::Useqq = 1; | ||
| 115 | |||
| 116 | open(my $fh, '>', $self->appfile) or die "Can't create cartridge: $!"; | ||
| 117 | print $fh Data::Dumper::Dumper($scheme); | ||
| 118 | close $fh; | ||
| 119 | |||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | =encoding utf8 | ||
| 126 | |||
| 127 | =head1 NAME | ||
| 128 | |||
| 129 | Zing::Cartridge - Executable Process File | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | =head1 ABSTRACT | ||
| 134 | |||
| 135 | Executable Process File Abstraction | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | =head1 SYNOPSIS | ||
| 140 | |||
| 141 | use Zing::Cartridge; | ||
| 142 | |||
| 143 | my $cartridge = Zing::Cartridge->new(name => 'myapp'); | ||
| 144 | |||
| 145 | # $cartridge->pid; | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | =head1 DESCRIPTION | ||
| 150 | |||
| 151 | This package provides an executable process file abstraction. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | =head1 INHERITS | ||
| 156 | |||
| 157 | This package inherits behaviors from: | ||
| 158 | |||
| 159 | L<Zing::Entity> | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | =head1 LIBRARIES | ||
| 164 | |||
| 165 | This package uses type constraints from: | ||
| 166 | |||
| 167 | L<Zing::Types> | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | =head1 ATTRIBUTES | ||
| 172 | |||
| 173 | This package has the following attributes: | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | =head2 appdir | ||
| 178 | |||
| 179 | appdir(Str) | ||
| 180 | |||
| 181 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | =head2 appfile | ||
| 186 | |||
| 187 | appfile(Str) | ||
| 188 | |||
| 189 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | =head2 libdir | ||
| 194 | |||
| 195 | libdir(ArrayRef[Str]) | ||
| 196 | |||
| 197 | This attribute is read-only, accepts C<(ArrayRef[Str])> values, and is optional. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | =head2 name | ||
| 202 | |||
| 203 | name(Str) | ||
| 204 | |||
| 205 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | =head2 piddir | ||
| 210 | |||
| 211 | piddir(Str) | ||
| 212 | |||
| 213 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | =head2 pidfile | ||
| 218 | |||
| 219 | pidfile(Str) | ||
| 220 | |||
| 221 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | =head2 scheme | ||
| 226 | |||
| 227 | scheme(Scheme) | ||
| 228 | |||
| 229 | This attribute is read-only, accepts C<(Scheme)> values, and is optional. | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | =head1 METHODS | ||
| 234 | |||
| 235 | This package implements the following methods: | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | =head2 install | ||
| 240 | |||
| 241 | install(Scheme $scheme) : Object | ||
| 242 | |||
| 243 | The install method creates an executable process file on disk for the scheme | ||
| 244 | provided. | ||
| 245 | |||
| 246 | =over 4 | ||
| 247 | |||
| 248 | =item install example #1 | ||
| 249 | |||
| 250 | # given: synopsis | ||
| 251 | |||
| 252 | $cartridge = $cartridge->install(['MyApp', [], 1]); | ||
| 253 | |||
| 254 | =back | ||
| 255 | |||
| 256 | =over 4 | ||
| 257 | |||
| 258 | =item install example #2 | ||
| 259 | |||
| 260 | use Zing::Cartridge; | ||
| 261 | |||
| 262 | my $cartridge = Zing::Cartridge->new(scheme => ['MyApp', [], 1]); | ||
| 263 | |||
| 264 | $cartridge = $cartridge->install; | ||
| 265 | |||
| 266 | =back | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | =head2 pid | ||
| 271 | |||
| 272 | pid() : Maybe[Int] | ||
| 273 | |||
| 274 | The pid method returns the process ID of the executed process (if any). | ||
| 275 | |||
| 276 | =over 4 | ||
| 277 | |||
| 278 | =item pid example #1 | ||
| 279 | |||
| 280 | # given: synopsis | ||
| 281 | |||
| 282 | my $pid = $cartridge->pid; | ||
| 283 | |||
| 284 | =back | ||
| 285 | |||
| 286 | =cut | ||
| 287 | |||
| 288 | =head1 AUTHOR | ||
| 289 | |||
| 290 | Al Newkirk, C<awncorp@cpan.org> | ||
| 291 | |||
| 292 | =head1 LICENSE | ||
| 293 | |||
| 294 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 295 | |||
| 296 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 297 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 298 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 299 | |||
| 300 | =head1 PROJECT | ||
| 301 | |||
| 302 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 303 | |||
| 304 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 305 | |||
| 306 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 307 | |||
| 308 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 309 | |||
| 310 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 311 | |||
| 312 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 313 | |||
| 314 | =cut |