/
FileSystem.pm6
228 lines (198 loc) Β· 7.63 KB
/
FileSystem.pm6
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
class CompUnit::Repository::FileSystem does CompUnit::Repository::Locally does CompUnit::Repository {
has %!loaded;
has $!precomp;
has $!id;
has %!meta;
has $!precomp-stores;
has $!precomp-store;
my @extensions = <pm6 pm>;
my $extensions := nqp::hash('pm6',1,'pm',1);
# 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.add($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 not %!meta and (my $meta = $!prefix.add('META6.json')) and $meta.f {
try {
%!meta = Rakudo::Internals::JSON.from-json: $meta.slurp;
CATCH {
when JSONException {
fail "Invalid JSON found in META6.json";
}
}
}
}
if %!meta {
if %!meta<provides>{$name} -> $file {
my $path = $file.IO.is-absolute ?? $file.IO !! $!prefix.add($file);
$found = $path if $path.f;
}
}
unless ?$found {
# deduce path to compilation unit from package name
for @extensions -> $extension {
my $path = ($base ~ $extension).IO;
$found = $path if $path.f;
last if $found;
}
}
return $base, $found if $found;
}
False
}
method !comp-unit-id($name) {
CompUnit::PrecompilationId.new-from-string($name);
}
method id() {
my $parts := nqp::list_s;
my $prefix = self.prefix;
my $dir := { .match(/ ^ <.ident> [ <[ ' - ]> <.ident> ]* $ /) }; # ' hl
my $file := -> str $file {
nqp::eqat($file,'.pm',nqp::sub_i(nqp::chars($file),3))
|| nqp::eqat($file,'.pm6',nqp::sub_i(nqp::chars($file),4))
};
nqp::if(
$!id,
$!id,
($!id = nqp::if(
$prefix.e,
nqp::stmts(
(my $iter := Rakudo::Internals.DIR-RECURSE(
$prefix.absolute,:$dir,:$file).iterator),
nqp::until(
nqp::eqaddr((my $pulled := $iter.pull-one),IterationEnd),
nqp::if(
nqp::filereadable($pulled),
nqp::push_s($parts,nqp::sha1(slurp($pulled, :enc<iso-8859-1>))),
)
),
nqp::if(
(my $next := self.next-repo),
nqp::push_s($parts,$next.id),
),
nqp::sha1(nqp::join('',$parts))
),
nqp::sha1('')
))
)
}
method resolve(CompUnit::DependencySpecification $spec --> CompUnit:D) {
my ($base, $file) = self!matching-file($spec);
return CompUnit.new(
:short-name($spec.short-name),
:repo-id(self!comp-unit-id($spec.short-name).Str),
:repo(self)
) if $base;
return self.next-repo.resolve($spec) if self.next-repo;
Nil
}
method !precomp-stores() {
$!precomp-stores //= Array[CompUnit::PrecompilationStore].new(
gather {
my $repo = $*REPO;
while $repo {
my \store = $repo.precomp-store;
take store if store.defined;
$repo = $repo.next-repo;
}
}
)
}
method need(
CompUnit::DependencySpecification $spec,
CompUnit::PrecompilationRepository $precomp = self.precomp-repository(),
CompUnit::PrecompilationStore :@precomp-stores = self!precomp-stores(),
--> 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 = self!comp-unit-id($name);
my $*RESOURCES = Distribution::Resources.new(:repo(self), :dist-id(''));
my $handle = $precomp.try-load(
CompUnit::PrecompilationDependency::File.new(
:$id,
:src($file.Str),
:$spec,
),
:@precomp-stores,
);
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.Str),
:$precompiled,
);
}
return self.next-repo.need($spec, $precomp, :@precomp-stores) if self.next-repo;
X::CompUnit::UnsatisfiedDependency.new(:specification($spec)).throw;
}
method load(IO::Path:D $file --> 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.add($file);
if $path.f {
return %!loaded{$file.Str} //= %seen{$path.Str} = 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(--> Iterable:D) {
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) {
# We now save the 'resources/' part of a resource's path in files, i.e:
# "files" : [ "resources/libraries/xxx" => "resources/libraries/xxx.so" ]
# but we also want to root any path request to the CUR's resources directory
# When $.prefix points at a directory containing a meta file (eg. -I.)
return $.prefix.add( %!meta<files>{$key} )
if %!meta<files> && %!meta<files>{$key};
return $.prefix.add( $key )
if %!meta<resources> && %!meta<resources>.first({ $_ eq $key.subst(/^resources\//, "") });
# When $.prefix is presumably the 'lib' folder (eg. -Ilib)
return $.prefix.parent.add($key);
}
method precomp-store(--> CompUnit::PrecompilationStore:D) {
$!precomp-store //= CompUnit::PrecompilationStore::File.new(
:prefix(self.prefix.add('.precomp')),
)
}
method precomp-repository(--> CompUnit::PrecompilationRepository:D) {
$!precomp := CompUnit::PrecompilationRepository::Default.new(
:store(self.precomp-store),
) unless $!precomp;
$!precomp
}
}
# vim: ft=perl6 expandtab sw=4