| 1 | package Statocles::Store; | ||||||||||||||||||||||||||||
| 2 | 101 | our $VERSION = '0.099'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: The source for data documents and files | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 101 | ▶ | use Statocles::Base 'Class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 101 | ▶ | use Scalar::Util qw( weaken blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 101 | ▶ | use Statocles::Util qw( derp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 101 | ▶ | use Statocles::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 101 | ▶ | use Statocles::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # A hash of PATH => COUNT for all the open store paths. Stores are not allowed to | ||||||||||||||||||||||||||||
| 12 | # discover the files or documents of other stores (unless the two stores have the same | ||||||||||||||||||||||||||||
| 13 | # path) | ||||||||||||||||||||||||||||
| 14 | 101 | my %FILE_STORES = (); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #pod =attr path | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod The path to the directory containing the L<documents|Statocles::Document>. | ||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||
| 20 | #pod =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 101 | has path => ( | |||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | isa => AbsPath, | ||||||||||||||||||||||||||||
| 25 | coerce => AbsPath->coercion, | ||||||||||||||||||||||||||||
| 26 | required => 1, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | #pod =attr document_extensions | ||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||
| 31 | #pod An array of file extensions that should be considered documents. Defaults to | ||||||||||||||||||||||||||||
| 32 | #pod "markdown" and "md". | ||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||
| 34 | #pod =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has document_extensions => ( | ||||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||
| 38 | isa => ArrayRef[Str], | ||||||||||||||||||||||||||||
| 39 | 107 | ▶ | default => sub { [qw( markdown md )] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 107 | ▶ | coerce => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 41 | 107 | my ( $ext ) = @_; | |||||||||||||||||||||||||||
| 42 | 107 | ▶ | if ( !ref $ext ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return [ split /[, ]/, $ext ]; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 107 | return $ext; | |||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||
| 47 | 101 | ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # If true, we've already checked if this store's path exists. We need to | ||||||||||||||||||||||||||||
| 50 | # check this lazily to ensure the site is created and the logger is | ||||||||||||||||||||||||||||
| 51 | # ready to go. | ||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||
| 53 | # XXX: Making sure the logger is ready before the thing that needs it is | ||||||||||||||||||||||||||||
| 54 | # the entire reason that dependency injection exists. We should use the | ||||||||||||||||||||||||||||
| 55 | # container to make sure the logger is wired up with every object that | ||||||||||||||||||||||||||||
| 56 | # needs it... | ||||||||||||||||||||||||||||
| 57 | has _check_exists => ( | ||||||||||||||||||||||||||||
| 58 | is => 'rw', | ||||||||||||||||||||||||||||
| 59 | isa => Bool, | ||||||||||||||||||||||||||||
| 60 | lazy => 1, | ||||||||||||||||||||||||||||
| 61 | 21 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | 21 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 63 | 21 | ▶ | if ( !$self->path->exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | Statocles->log( warn => sprintf qq{Store path "%s" does not exist}, $self->path ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 21 | return 1; | |||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | 101 | ); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | #pod =method is_document | ||||||||||||||||||||||||||||
| 71 | #pod | ||||||||||||||||||||||||||||
| 72 | #pod my $bool = $store->is_document( $path ); | ||||||||||||||||||||||||||||
| 73 | #pod | ||||||||||||||||||||||||||||
| 74 | #pod Returns true if the path looks like a document path (matches the L</document_extensions>). | ||||||||||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||||||||||
| 76 | #pod =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 134 | ▶ | sub is_document { | ||||||||||||||||||||||||||
|
Subroutine
is_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 134 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 80 | 134 | my $match = join "|", @{ $self->document_extensions }; | |||||||||||||||||||||||||||
| 81 | 134 | return $path =~ /[.](?:$match)$/; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | #pod =method has_file | ||||||||||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||||||||||
| 86 | #pod my $bool = $store->has_file( $path ) | ||||||||||||||||||||||||||||
| 87 | #pod | ||||||||||||||||||||||||||||
| 88 | #pod Returns true if a file exists with the given C<path>. | ||||||||||||||||||||||||||||
| 89 | #pod | ||||||||||||||||||||||||||||
| 90 | #pod =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | sub has_file { | ||||||||||||||||||||||||||
|
Subroutine
has_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 3 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 94 | 3 | return $self->path->child( $path )->is_file; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | #pod =method write_file | ||||||||||||||||||||||||||||
| 98 | #pod | ||||||||||||||||||||||||||||
| 99 | #pod $store->write_file( $path, $content ); | ||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||
| 101 | #pod Write the given C<content> to the given C<path>. This is mostly used to write | ||||||||||||||||||||||||||||
| 102 | #pod out L<page objects|Statocles::Page>. | ||||||||||||||||||||||||||||
| 103 | #pod | ||||||||||||||||||||||||||||
| 104 | #pod C<content> may be a simple string or a filehandle. If given a string, will | ||||||||||||||||||||||||||||
| 105 | #pod write the string using UTF-8 characters. If given a filehandle, will write out | ||||||||||||||||||||||||||||
| 106 | #pod the raw bytes read from it with no special encoding. | ||||||||||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||||||||||
| 108 | #pod =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 11 | ▶ | sub write_file { | ||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 11 | my ( $self, $path, $content ) = @_; | |||||||||||||||||||||||||||
| 112 | 11 | Statocles->log( debug => "Write file: " . $path ); | |||||||||||||||||||||||||||
| 113 | 11 | my $full_path = $self->path->child( $path )->touchpath; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | #; say "Writing full path: " . $full_path; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 11 | ▶ | if ( ref $content eq 'GLOB' ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 118 | 2 | my $fh = $full_path->openw_raw; | |||||||||||||||||||||||||||
| 119 | 2 | ▶ | while ( my $line = <$content> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 25 | $fh->print( $line ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | ▶ | elsif ( blessed $content && $content->isa( 'Path::Tiny' ) ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
builtin::blessed($content) and $content->isa("Path::Tiny")
MC/DC100%2/2
builtin::blessed($content) and $content->isa("Path::Tiny")
builtin::blessed($content)
$content->isa("Path::Tiny")
Truth table100%3/3
A and B
A= builtin::blessed($content)B= $content->isa("Path::Tiny")
| |||||||||||||||||||||||||||||
| 124 | 2 | $content->copy( $full_path ); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | ▶ | elsif ( blessed $content && $content->isa( 'Statocles::Document' ) ) { | |||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($content) and $content->isa("Statocles::Document")
MC/DC50%1/2
builtin::blessed($content) and $content->isa("Statocles::Document")
builtin::blessed($content)
$content->isa("Statocles::Document")
Truth table66%2/3
A and B
A= builtin::blessed($content)B= $content->isa("Statocles::Document")
| |||||||||||||||||||||||||||||
| 127 | 6 | $full_path->spew_utf8( $content->deparse_content ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | ▶ | elsif ( blessed $content && $content->isa( 'Statocles::File' ) ) { | |||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($content) and $content->isa("Statocles::File")
MC/DC0%0/2
builtin::blessed($content) and $content->isa("Statocles::File")
builtin::blessed($content)
$content->isa("Statocles::File")
Truth table33%1/3
A and B
A= builtin::blessed($content)B= $content->isa("Statocles::File")
| |||||||||||||||||||||||||||||
| 130 | 0 | $content->path->copy( $full_path ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | 1 | $full_path->spew_utf8( $content ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 11 | return; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | #pod =method remove | ||||||||||||||||||||||||||||
| 140 | #pod | ||||||||||||||||||||||||||||
| 141 | #pod $store->remove( $path ) | ||||||||||||||||||||||||||||
| 142 | #pod | ||||||||||||||||||||||||||||
| 143 | #pod Remove the given path from the store. If the path is a directory, the entire | ||||||||||||||||||||||||||||
| 144 | #pod directory is removed. | ||||||||||||||||||||||||||||
| 145 | #pod | ||||||||||||||||||||||||||||
| 146 | #pod =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 2 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 150 | 2 | $self->path->child( $path )->remove_tree; | |||||||||||||||||||||||||||
| 151 | 2 | return; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | #pod =method iterator | ||||||||||||||||||||||||||||
| 155 | #pod | ||||||||||||||||||||||||||||
| 156 | #pod my $iter = $store->iterator; | ||||||||||||||||||||||||||||
| 157 | #pod | ||||||||||||||||||||||||||||
| 158 | #pod Iterate over all the objects in this store. Returns an iterator that | ||||||||||||||||||||||||||||
| 159 | #pod will yield a L<Statocles::Document> object or a L<Statocles::File> | ||||||||||||||||||||||||||||
| 160 | #pod object. | ||||||||||||||||||||||||||||
| 161 | #pod | ||||||||||||||||||||||||||||
| 162 | #pod Hidden files and folders are automatically ignored by this method. | ||||||||||||||||||||||||||||
| 163 | #pod | ||||||||||||||||||||||||||||
| 164 | #pod my $iter = $store->iterator; | ||||||||||||||||||||||||||||
| 165 | #pod while ( my $obj = $iter->() ) { | ||||||||||||||||||||||||||||
| 166 | #pod if ( $obj->isa( 'Statocles::Document' ) ) { | ||||||||||||||||||||||||||||
| 167 | #pod ...; | ||||||||||||||||||||||||||||
| 168 | #pod } | ||||||||||||||||||||||||||||
| 169 | #pod else { | ||||||||||||||||||||||||||||
| 170 | #pod ...; | ||||||||||||||||||||||||||||
| 171 | #pod } | ||||||||||||||||||||||||||||
| 172 | #pod } | ||||||||||||||||||||||||||||
| 173 | #pod | ||||||||||||||||||||||||||||
| 174 | #pod =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 21 | ▶ | sub iterator { | ||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 21 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 178 | 21 | $self->_check_exists; | |||||||||||||||||||||||||||
| 179 | 21 | my $iter = $self->path->iterator({ recurse => 1 }); | |||||||||||||||||||||||||||
| 180 | 145 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.6
| |||||||||||||||||||||||||||||
| 181 | PATH: | ||||||||||||||||||||||||||||
| 182 | 145 | ▶ | while ( my $path = $iter->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 265 | ▶ | next if $path->is_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Check for hidden files and folders | ||||||||||||||||||||||||||||
| 186 | 168 | ▶ | next if $path->basename =~ /^[.]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 166 | my $parent = $path->realpath->parent; | |||||||||||||||||||||||||||
| 188 | 166 | ▶ | while ( $self->path->subsumes( $parent ) && !$parent->is_rootdir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->path->subsumes($parent) and not $parent->is_rootdir
MC/DC50%1/2
$self->path->subsumes($parent) and not $parent->is_rootdir
$self->path->subsumes($parent)
not $parent->is_rootdir
Truth table66%2/3
A and not(B)
A= $self->path->subsumes($parent)B= not $parent->is_rootdir
| |||||||||||||||||||||||||||||
| 189 | 350 | ▶ | last if !$parent->basename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 350 | ▶ | next PATH if $parent->basename =~ /^[.]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 314 | $parent = $parent->parent; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 130 | my $from = $path->relative( $self->path ); | |||||||||||||||||||||||||||
| 195 | 130 | ▶ | if ( $self->is_document( $path ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 71 | my $content = eval { $path->slurp_utf8 }; | |||||||||||||||||||||||||||
| 197 | 71 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | chomp $@; | |||||||||||||||||||||||||||
| 199 | 0 | die sprintf qq{Error reading file "%s": %s\n}, | |||||||||||||||||||||||||||
| 200 | $from, $@; | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 71 | my $obj = eval { | |||||||||||||||||||||||||||
| 204 | 71 | Statocles::Document->parse_content( | |||||||||||||||||||||||||||
| 205 | content => $content, | ||||||||||||||||||||||||||||
| 206 | path => $from.'', | ||||||||||||||||||||||||||||
| 207 | store => $self, | ||||||||||||||||||||||||||||
| 208 | ) | ||||||||||||||||||||||||||||
| 209 | }; | ||||||||||||||||||||||||||||
| 210 | 71 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 6 | ▶ | if ( ref $@ && $@->isa( 'Error::TypeTiny::Assertion' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $@ and $@->isa("Error::TypeTiny::Assertion")
MC/DC50%1/2
ref $@ and $@->isa("Error::TypeTiny::Assertion")
ref $@
$@->isa("Error::TypeTiny::Assertion")
Truth table66%2/3
A and B
A= ref $@B= $@->isa("Error::TypeTiny::Assertion")
| |||||||||||||||||||||||||||||
| 212 | 2 | ▶ | if ( $@->attribute_name eq 'date' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 1 | die sprintf qq{Could not parse date "%s" in "%s": Does not match "YYYY-MM-DD" or "YYYY-MM-DD HH:MM:SS"\n}, | |||||||||||||||||||||||||||
| 214 | $@->value, | ||||||||||||||||||||||||||||
| 215 | $from; | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | die sprintf qq{Error creating document in "%s": Value "%s" is not valid for attribute "%s" (expected "%s")\n}, | |||||||||||||||||||||||||||
| 219 | $from, | ||||||||||||||||||||||||||||
| 220 | $@->value, | ||||||||||||||||||||||||||||
| 221 | $@->attribute_name, | ||||||||||||||||||||||||||||
| 222 | $@->type; | ||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||
| 225 | 4 | die sprintf qq{Error creating document in "%s": %s\n}, | |||||||||||||||||||||||||||
| 226 | $from, | ||||||||||||||||||||||||||||
| 227 | $@; | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 65 | return $obj; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 59 | return Statocles::File->new( | |||||||||||||||||||||||||||
| 234 | store => $self, | ||||||||||||||||||||||||||||
| 235 | path => $from.'', | ||||||||||||||||||||||||||||
| 236 | ); | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 15 | return undef; | |||||||||||||||||||||||||||
| 239 | 21 | }; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 101 | 1; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | __END__ | ||||||||||||||||||||||||||||