/
filesystem.dart
54 lines (43 loc) · 1.77 KB
/
filesystem.dart
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
// Copyright 2017 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import '../importer.dart';
import '../io.dart' as io;
import '../syntax.dart';
import '../util/nullable.dart';
import 'utils.dart';
/// An importer that loads files from a load path on the filesystem.
///
/// {@category Importer}
@sealed
class FilesystemImporter extends Importer {
/// The path relative to which this importer looks for files.
final String _loadPath;
/// Creates an importer that loads files relative to [loadPath].
FilesystemImporter(String loadPath) : _loadPath = p.absolute(loadPath);
Uri? canonicalize(Uri url) {
if (url.scheme != 'file' && url.scheme != '') return null;
return resolveImportPath(p.join(_loadPath, p.fromUri(url)))
.andThen((resolved) => p.toUri(io.canonicalize(resolved)));
}
ImporterResult? load(Uri url) {
var path = p.fromUri(url);
return ImporterResult(io.readFile(path),
sourceMapUrl: url, syntax: Syntax.forPath(path));
}
DateTime modificationTime(Uri url) => io.modificationTime(p.fromUri(url));
bool couldCanonicalize(Uri url, Uri canonicalUrl) {
if (url.scheme != 'file' && url.scheme != '') return false;
if (canonicalUrl.scheme != 'file') return false;
var basename = p.url.basename(url.path);
var canonicalBasename = p.url.basename(canonicalUrl.path);
if (!basename.startsWith("_") && canonicalBasename.startsWith("_")) {
canonicalBasename = canonicalBasename.substring(1);
}
return basename == canonicalBasename ||
basename == p.url.withoutExtension(canonicalBasename);
}
String toString() => _loadPath;
}