@@ -12,12 +12,8 @@ library dartdoc.resource_loader;
12
12
13
13
import 'dart:async' show Future;
14
14
import 'dart:convert' show UTF8;
15
- import 'dart:io' show Platform, File, Directory;
16
- import 'dart:typed_data' show Uint8List;
17
15
18
- import 'package:http/http.dart' as http;
19
- import 'package:path/path.dart' as p;
20
- import 'package:pub_cache/pub_cache.dart' ;
16
+ import 'package:resource/resource.dart' ;
21
17
22
18
/// Loads a `package:` resource as a String.
23
19
Future <String > loadAsString (String path) async {
@@ -32,135 +28,7 @@ Future<List<int>> loadAsBytes(String path) async {
32
28
throw new ArgumentError ('path must begin with package:' );
33
29
}
34
30
35
- // TODO: Remove once https://github.com/dart-lang/pub/issues/22 is fixed.
36
- try {
37
- return await _doLoad (path);
38
- } catch (_) {
39
- return new Resource (path).readAsBytes ();
40
- }
41
- }
42
-
43
- /// Determine how to do the load. HTTP? Snapshotted? From source?
44
- Future <Uint8List > _doLoad (final String path) {
45
- var scriptUri = Platform .script;
46
- if (scriptUri.toString ().startsWith ('http' )) {
47
- return _doLoadOverHttp (path);
48
- } else if (scriptUri.toString ().endsWith ('.snapshot' )) {
49
- return _doLoadWhenSnapshot (path);
50
- } else {
51
- return _doLoadFromFileFromPackagesDir (path);
52
- }
53
- }
54
-
55
- Future <Uint8List > _doLoadWhenSnapshot (final String resourcePath) {
56
- var scriptFilePath = Platform .script.toFilePath ();
57
- // Check if we're running as a pub globally installed package
58
- // Jump back out to where our source is
59
- var cacheDirPath = PubCache .getSystemCacheLocation ().path;
60
- if (scriptFilePath.startsWith (cacheDirPath)) {
61
- // find packages installed with pub
62
- var appName = _appNameWhenGloballyInstalled ();
63
- var installedApplication = new PubCache ()
64
- .getGlobalApplications ()
65
- .firstWhere ((app) => app.name == appName);
66
- if (installedApplication == null ) {
67
- throw new StateError (
68
- 'Could not find globally installed app $appName . Are you running as a snapshot from the global_packages directory?' );
69
- }
70
- var resourcePackageName = _packageNameForResource (resourcePath);
71
- var resourcePackageRef = installedApplication
72
- .getPackageRefs ()
73
- .firstWhere ((ref) => ref.name == resourcePackageName);
74
- if (resourcePackageRef == null ) {
75
- throw new StateError (
76
- 'Could not find package dependency for $resourcePackageName ' );
77
- }
78
- var resourcePackage = resourcePackageRef.resolve ();
79
- var resourcePackageDir = resourcePackage.location;
80
- var fullPath = resourcePackageDir.path;
81
- return _doLoadOverFileFromLocation (resourcePath, p.join (fullPath, "lib" ));
82
- } else {
83
- // maybe we're a snapshot next to a packages/ dir?
84
- return _doLoadFromFileFromPackagesDir (resourcePath);
85
- }
86
- }
87
-
88
- Future <Uint8List > _doLoadOverHttp (final String resourcePath) {
89
- var scriptUri = Platform .script;
90
- var convertedResourcePath = _convertPackageSchemeToPackagesDir (resourcePath);
91
- // strip file name from script uri, append path to resource
92
- var segmentsToResource = scriptUri.pathSegments
93
- .sublist (0 , scriptUri.pathSegments.length - 1 )
94
- ..addAll (p.split (convertedResourcePath));
95
- var fullPath = scriptUri.replace (pathSegments: segmentsToResource);
96
-
97
- return http.readBytes (fullPath);
98
- }
99
-
100
- Future <Uint8List > _doLoadOverFileFromLocation (
101
- final String resourcePath, final String baseDir) {
102
- var convertedPath = _convertPackageSchemeToPackagesDir (resourcePath);
103
- // remove 'packages' and package name
104
- var pathInsideLib = p.split (convertedPath).sublist (2 );
105
- // put the baseDir in front
106
- pathInsideLib.insert (0 , baseDir);
107
- // put it all back together
108
- var fullPath = p.joinAll (pathInsideLib);
109
- return _readFile (resourcePath, fullPath);
110
- }
111
-
112
- /// First, try a packages/ dir next to the entry point (Platform.script).
113
- /// If that doesn't exist, try a packages/ dir inside of the current
114
- /// working directory.
115
- Future <Uint8List > _doLoadFromFileFromPackagesDir (final String resourcePath) {
116
- var convertedPath = _convertPackageSchemeToPackagesDir (resourcePath);
117
- var scriptFile = new File (Platform .script.toFilePath ());
118
- String baseDir = p.dirname (scriptFile.path);
119
-
120
- if (! new Directory (p.join (baseDir, 'packages' )).existsSync ()) {
121
- // try CWD
122
- baseDir = Directory .current.path;
123
- }
124
-
125
- var fullPath = p.join (baseDir, convertedPath);
126
- return _readFile (resourcePath, fullPath);
127
- }
128
-
129
- Future <Uint8List > _readFile (
130
- final String resourcePath, final String fullPath) async {
131
- var file = new File (fullPath);
132
- if (! file.existsSync ()) {
133
- throw new ArgumentError ('$resourcePath does not exist, tried $fullPath ' );
134
- }
135
- var bytes = await file.readAsBytes ();
136
- return new Uint8List .fromList (bytes);
137
- }
138
-
139
- String _convertPackageSchemeToPackagesDir (String resourcePath) {
140
- var withoutScheme = _removePackageScheme (resourcePath);
141
- return p.join ('packages' , withoutScheme);
142
- }
143
-
144
- String _removePackageScheme (final String resourcePath) {
145
- return resourcePath.substring ('package:' .length, resourcePath.length);
146
- }
147
-
148
- /// Tries to determine the app name, which is the same as the directory
149
- /// name when globally installed.
150
- ///
151
- /// Only call this if your app is globally installed.
152
- String _appNameWhenGloballyInstalled () {
153
- var parts = p.split (Platform .script.toFilePath ());
154
- var marker = parts.indexOf ('global_packages' );
155
- if (marker < 0 ) {
156
- throw new StateError (
157
- '${Platform .script .toFilePath ()} does not include global_packages' );
158
- }
159
- return parts[marker + 1 ];
160
- }
31
+ var uri = Uri .parse (path);
161
32
162
- String _packageNameForResource (final String resourcePath) {
163
- var parts = p.split (_convertPackageSchemeToPackagesDir (resourcePath));
164
- // first part is 'packages', second part is the package name
165
- return parts[1 ];
33
+ return await ResourceLoader .defaultLoader.readAsBytes (uri);
166
34
}
0 commit comments