2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- /// Make it possible to load resources, independent of how the Dart app is run.
6
- ///
7
- /// Future<String> getTemplateFile(String templatePath) {
8
- /// return loadAsString('package:dartdoc/templates/$templatePath');
9
- /// }
10
- ///
5
+ /// Make it possible to load resources from the dartdoc code repository.
11
6
library dartdoc.resource_loader;
12
7
13
- import 'dart:async' show Future;
14
8
import 'dart:convert' show utf8;
15
-
16
- import 'package:resource/resource. dart' as resource ;
9
+ import 'dart:io' show File;
10
+ import 'dart:isolate' show Isolate ;
17
11
18
12
/// Loads a `package:` resource as a String.
19
13
Future <String > loadAsString (String path) async {
@@ -28,6 +22,19 @@ Future<List<int>> loadAsBytes(String path) async {
28
22
throw ArgumentError ('path must begin with package:' );
29
23
}
30
24
31
- var uri = Uri .parse (path);
32
- return await resource.ResourceLoader .defaultLoader.readAsBytes (uri);
25
+ var uri = await _resolveUri (Uri .parse (path));
26
+ return File .fromUri (uri).readAsBytes ();
27
+ }
28
+
29
+ /// Helper function for resolving to a non-relative, non-package URI.
30
+ Future <Uri > _resolveUri (Uri uri) {
31
+ if (uri.scheme == 'package' ) {
32
+ return Isolate .resolvePackageUri (uri).then ((resolvedUri) {
33
+ if (resolvedUri == null ) {
34
+ throw ArgumentError .value (uri, 'uri' , 'Unknown package' );
35
+ }
36
+ return resolvedUri;
37
+ });
38
+ }
39
+ return Future <Uri >.value (Uri .base .resolveUri (uri));
33
40
}
0 commit comments