Skip to content

Commit d5f5937

Browse files
committed
Use pkg/resource to load assets
Closes #1089
1 parent 2d943ca commit d5f5937

File tree

3 files changed

+34
-157
lines changed

3 files changed

+34
-157
lines changed

lib/src/html/resource_loader.dart

Lines changed: 3 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ library dartdoc.resource_loader;
1212

1313
import 'dart:async' show Future;
1414
import 'dart:convert' show UTF8;
15-
import 'dart:io' show Platform, File, Directory;
16-
import 'dart:typed_data' show Uint8List;
1715

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';
2117

2218
/// Loads a `package:` resource as a String.
2319
Future<String> loadAsString(String path) async {
@@ -32,135 +28,7 @@ Future<List<int>> loadAsBytes(String path) async {
3228
throw new ArgumentError('path must begin with package:');
3329
}
3430

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);
16132

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);
16634
}

pubspec.lock

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ packages:
44
analyzer:
55
description: analyzer
66
source: hosted
7-
version: "0.27.1+1"
7+
version: "0.27.1+2"
88
ansicolor:
99
description: ansicolor
1010
source: hosted
1111
version: "0.0.9"
1212
args:
1313
description: args
1414
source: hosted
15-
version: "0.13.2"
15+
version: "0.13.3+1"
1616
async:
1717
description: async
1818
source: hosted
19-
version: "1.5.0"
19+
version: "1.8.0"
2020
barback:
2121
description: barback
2222
source: hosted
@@ -32,7 +32,7 @@ packages:
3232
collection:
3333
description: collection
3434
source: hosted
35-
version: "1.2.0"
35+
version: "1.4.0"
3636
contrast:
3737
description: contrast
3838
source: hosted
@@ -52,15 +52,15 @@ packages:
5252
github:
5353
description: github
5454
source: hosted
55-
version: "2.3.1"
55+
version: "2.3.1+1"
5656
glob:
5757
description: glob
5858
source: hosted
59-
version: "1.0.5"
59+
version: "1.1.0"
6060
grinder:
6161
description: grinder
6262
source: hosted
63-
version: "0.8.0+1"
63+
version: "0.8.0+2"
6464
html:
6565
description: html
6666
source: hosted
@@ -76,7 +76,7 @@ packages:
7676
http_parser:
7777
description: http_parser
7878
source: hosted
79-
version: "2.0.0"
79+
version: "2.1.0"
8080
librato:
8181
description: librato
8282
source: hosted
@@ -116,7 +116,7 @@ packages:
116116
petitparser:
117117
description: petitparser
118118
source: hosted
119-
version: "1.5.0"
119+
version: "1.5.1"
120120
plugin:
121121
description: plugin
122122
source: hosted
@@ -125,10 +125,6 @@ packages:
125125
description: pool
126126
source: hosted
127127
version: "1.2.1"
128-
pub_cache:
129-
description: pub_cache
130-
source: hosted
131-
version: "0.1.0"
132128
pub_package_data:
133129
description: pub_package_data
134130
source: hosted
@@ -141,10 +137,14 @@ packages:
141137
description: quiver
142138
source: hosted
143139
version: "0.21.4"
140+
resource:
141+
description: resource
142+
source: hosted
143+
version: "1.1.0"
144144
shelf:
145145
description: shelf
146146
source: hosted
147-
version: "0.6.4+3"
147+
version: "0.6.5"
148148
shelf_static:
149149
description: shelf_static
150150
source: hosted
@@ -168,19 +168,27 @@ packages:
168168
stack_trace:
169169
description: stack_trace
170170
source: hosted
171-
version: "1.5.0"
171+
version: "1.6.0"
172+
stream_channel:
173+
description: stream_channel
174+
source: hosted
175+
version: "1.0.1"
172176
string_scanner:
173177
description: string_scanner
174178
source: hosted
175-
version: "0.1.4"
179+
version: "0.1.4+1"
176180
supports_color:
177181
description: supports_color
178182
source: hosted
179183
version: "0.1.1"
180184
test:
181185
description: test
182186
source: hosted
183-
version: "0.12.6+2"
187+
version: "0.12.8"
188+
typed_data:
189+
description: typed_data
190+
source: hosted
191+
version: "1.1.1"
184192
unscripted:
185193
description: unscripted
186194
source: hosted
@@ -204,8 +212,9 @@ packages:
204212
xml:
205213
description: xml
206214
source: hosted
207-
version: "2.4.0"
215+
version: "2.4.1"
208216
yaml:
209217
description: yaml
210218
source: hosted
211-
version: "2.1.7"
219+
version: "2.1.8"
220+
sdk: ">=1.14.0 <1.16.0"

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ author: Dart Team <[email protected]>
55
description: A documentation generator for Dart.
66
homepage: https://github.com/dart-lang/dartdoc
77
environment:
8-
sdk: '>=1.12.0 <2.0.0'
8+
sdk: '>=1.14.0 <2.0.0'
99
dependencies:
1010
analyzer: '>=0.27.0 <0.28.0'
1111
args: ^0.13.0
1212
cli_util: ^0.0.1
1313
collection: ^1.2.0
1414
html: ^0.12.1
15-
http: ^0.11.0
1615
logging: '>=0.9.0 <0.12.0'
1716
markdown: ^0.8.0
1817
mustache4dart: ^1.0.9
1918
path: ^1.3.0
20-
pub_cache: ^0.1.0
2119
quiver: '>=0.18.0 <0.22.0'
20+
resource: ^1.1.0
2221
stack_trace: ^1.4.2
2322
yaml: ^2.1.0
2423
dev_dependencies:
2524
den_api: ^0.1.0
2625
grinder: ^0.8.0
26+
http: ^0.11.0
2727
librato: ^0.0.3
2828
pub_semver: ^1.0.0
2929
test: ^0.12.0

0 commit comments

Comments
 (0)