-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple Pod::Cache, doc dir lookup, and simple display
This Pod::Cache is stripped down almost to a minimum. In the future it might be possible to use Documentable::Registry or some other module, but in order to find the fastest method possible for our specific needs, it is best to use our own.
- Loading branch information
Showing
6 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
unit class Pod::Cache; | ||
|
||
has %.sources is SetHash; | ||
|
||
has IO::Path $!cache-path; | ||
has $!precomp-repo; | ||
has %!errors; | ||
has %!ids; | ||
|
||
|
||
submethod BUILD( | ||
:$cache-path = 'rakudoc_cache', | ||
) | ||
{ | ||
$!cache-path = $cache-path.IO.resolve(:completely); | ||
$!precomp-repo = CompUnit::PrecompilationRepository::Default.new( | ||
:store(CompUnit::PrecompilationStore::File.new(:prefix($!cache-path))), | ||
); | ||
} | ||
|
||
method !compunit-handle($pod-file-path) { | ||
my $t = $pod-file-path.IO.modified; | ||
my $id = CompUnit::PrecompilationId.new-from-string($pod-file-path); | ||
%!ids{$pod-file-path} = $id; | ||
my ($handle, $) = $!precomp-repo.load( $id, :src($pod-file-path), :since($t) ); | ||
unless $handle { | ||
note "Caching '$pod-file-path' to '$!cache-path'"; | ||
$handle = $!precomp-repo.try-load( | ||
CompUnit::PrecompilationDependency::File.new( | ||
:src($pod-file-path), | ||
:$id, | ||
:spec(CompUnit::DependencySpecification.new(:short-name($pod-file-path))), | ||
) | ||
); | ||
#note " - Survived with handle {$handle // 'NULL'}"; | ||
} | ||
|
||
++%!sources{$pod-file-path}; | ||
return $handle; | ||
} | ||
|
||
#| pod(Str $pod-file-path) returns the pod tree in the pod file | ||
multi method pod(CompUnit::Handle $handle) { | ||
use nqp; | ||
nqp::atkey($handle.unit, '$=pod') | ||
} | ||
|
||
multi method pod(IO::Path $pod-file-path) { | ||
my $handle; | ||
# Canonical form for lookup consistency | ||
my $src = $pod-file-path.resolve(:completely).absolute; | ||
if %!ids{$src}:exists { | ||
$handle = $!precomp-repo.try-load( | ||
CompUnit::PrecompilationDependency::File.new( | ||
:$src, | ||
:id(%!ids{$src}) | ||
), | ||
); | ||
} | ||
else { | ||
$handle = self!compunit-handle($src); | ||
} | ||
|
||
self.pod: $handle; | ||
} | ||
|
||
multi method pod(Str $pod-file-path) { | ||
self.pod: $pod-file-path.IO; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use Test; | ||
use File::Temp; | ||
use Pod::Cache; | ||
|
||
plan 6; | ||
|
||
my $test-doc = 't/testdata/mini-doc/test-doc/Type/Map.pod6'; | ||
|
||
my $cache-path = tempdir.IO.add('cache'); | ||
my $pc = Pod::Cache.new: :$cache-path; | ||
|
||
isa-ok $pc, Pod::Cache, | ||
"Create cache object"; | ||
|
||
is $cache-path.e, False, | ||
"Cache dir is not created until needed"; | ||
|
||
like $pc.pod($test-doc).?first.^name, /^ 'Pod::'/, | ||
"pod('relative/path.ext') returns a Pod"; | ||
|
||
is $cache-path.d, True, | ||
"Cache dir is created when needed"; | ||
|
||
like $pc.pod($test-doc.IO).?first.^name, /^ 'Pod::'/, | ||
"pod('relative/path.ext'.IO) returns a Pod"; | ||
|
||
throws-like { $pc.pod('nonexistent') }, X::Multi::NoMatch, | ||
"pod('nonexistent') dies"; |