22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
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.
116library dartdoc.resource_loader;
127
13- import 'dart:async' show Future;
148import '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 ;
1711
1812/// Loads a `package:` resource as a String.
1913Future <String > loadAsString (String path) async {
@@ -28,6 +22,19 @@ Future<List<int>> loadAsBytes(String path) async {
2822 throw ArgumentError ('path must begin with package:' );
2923 }
3024
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));
3340}
0 commit comments