/
FileSystem.pm
151 lines (129 loc) Β· 5.02 KB
/
FileSystem.pm
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
class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does CompUnit::Repository {
has %!loaded;
has $!precomp;
my %extensions =
Perl6 => <pm6 pm>,
Perl5 => <pm5 pm>,
NQP => <nqp>,
JVM => ();
# global cache of files seen
my %seen;
method !matching-file(CompUnit::DependencySpecification $spec) {
if $spec.from eq 'Perl6' {
my $name = $spec.short-name;
return %!loaded{$name} if %!loaded{$name}:exists;
my $base := $!prefix.child($name.subst(:g, "::", $*SPEC.dir-sep) ~ '.').Str;
return $base if %seen{$base}:exists;
my $found;
# find source file
# pick a META6.json if it is there
if (my $meta = $!prefix.child('META6.json')) && $meta.f {
try {
my $json = from-json $meta.slurp;
if $json<provides>{$name} -> $file {
my $path = $file.IO.is-absolute ?? $file.IO !! $!prefix.child($file);
$found = $path if $path.f;
}
CATCH {
when JSONException {
fail "Invalid JSON found in META6.json";
}
}
}
}
# deduce path to compilation unit from package name
elsif %extensions<Perl6> -> @extensions {
for @extensions -> $extension {
my $path = $base ~ $extension;
$found = $path.IO if IO::Path.new-from-absolute-path($path).f;
}
}
return $base, $found if $found;
}
False
}
method resolve(CompUnit::DependencySpecification $spec) returns CompUnit {
my ($base, $file) = self!matching-file($spec);
return CompUnit.new(
:short-name($spec.short-name),
:repo-id($file.Str),
:repo(self)
) if $base;
return self.next-repo.resolve($spec) if self.next-repo;
Nil
}
method need(
CompUnit::DependencySpecification $spec,
CompUnit::PrecompilationRepository $precomp = self.precomp-repository(),
)
returns CompUnit:D
{
my ($base, $file) = self!matching-file($spec);
if $base {
my $name = $spec.short-name;
return %!loaded{$name} if %!loaded{$name}:exists;
return %seen{$base} if %seen{$base}:exists;
my $id = nqp::sha1($name ~ $*REPO.id);
my $*RESOURCES = Distribution::Resources.new(:repo(self), :dist-id(''));
my $handle = $precomp.try-load($id, $file, :source-name("$file ($spec.short-name())"));
my $precompiled = defined $handle;
$handle //= CompUnit::Loader.load-source-file($file); # precomp failed
return %!loaded{$name} = %seen{$base} = CompUnit.new(
:short-name($name),
:$handle,
:repo(self),
:repo-id($id),
:$precompiled,
);
}
return self.next-repo.need($spec, $precomp) if self.next-repo;
X::CompUnit::UnsatisfiedDependency.new(:specification($spec)).throw;
}
method load(IO::Path:D $file) returns CompUnit:D {
unless $file.is-absolute {
# We have a $file when we hit: require "PATH" or use/require Foo:file<PATH>;
my $precompiled =
$file.Str.ends-with(Rakudo::Internals.PRECOMP-EXT);
my $path = $!prefix.child($file);
if $path.f {
return %!loaded{$file} = %seen{$path} = CompUnit.new(
:handle(
$precompiled
?? CompUnit::Loader.load-precompilation-file($path)
!! CompUnit::Loader.load-source-file($path)
),
:short-name($file.Str),
:repo(self),
:repo-id($file.Str),
:$precompiled,
);
}
}
return self.next-repo.load($file) if self.next-repo;
nqp::die("Could not find $file in:\n" ~ $*REPO.repo-chain.map(*.Str).join("\n").indent(4));
}
method short-id() { 'file' }
method loaded() returns Iterable {
return %!loaded.values;
}
method files($file, :$name, :$auth, :$ver) {
my $base := $file.IO;
$base.f
?? { files => { $file => $base.path }, ver => Version.new('0') }
!! ();
}
method resource($dist-id, $key) {
$.prefix.parent.child('resources').child($key);
}
method precomp-repository() returns CompUnit::PrecompilationRepository {
$!precomp := CompUnit::PrecompilationRepository::Default.new(
:store(
CompUnit::PrecompilationStore::File.new(
:prefix(self.prefix.child('.precomp')),
)
),
) unless $!precomp;
$!precomp
}
}
# vim: ft=perl6 expandtab sw=4