Skip to content

Commit eb8baf7

Browse files
authored
move io utils to their own library, release 1.9.1 (flutter#74)
1 parent 79f1d2a commit eb8baf7

14 files changed

+190
-158
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ dart_task:
77
- dartfmt
88
- dartanalyzer: --fatal-warnings .
99

10+
matrix:
11+
include:
12+
- dart: dev
13+
script: pub run build_runner test -- -p chrome
14+
1015
# Only building master means that we don't run two builds for each pull request.
1116
branches:
1217
only: [master]

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.9.1
2+
3+
- Remove accidental transitive import of `dart:io` from entrypoints that are
4+
supposed to be cross-platform compatible.
5+
16
## 1.9.0
27

38
- Based on new JSON file format with more content.

lib/src/discovery.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import "errors.dart";
1111
import "package_config_impl.dart";
1212
import "package_config_json.dart";
1313
import "packages_file.dart" as packages_file;
14-
import "util.dart" show defaultLoader, pathJoin;
14+
import "util_io.dart" show defaultLoader, pathJoin;
1515

1616
final Uri packageConfigJsonPath = Uri(path: ".dart_tool/package_config.json");
1717
final Uri dotPackagesPath = Uri(path: ".packages");

lib/src/package_config_io.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import "package_config_impl.dart";
1414
import "package_config_json.dart";
1515
import "packages_file.dart" as packages_file;
1616
import "util.dart";
17+
import "util_io.dart";
1718

1819
/// Reads a package configuration file.
1920
///

lib/src/packages_io_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import "dart:io" show Directory;
1212

1313
import "packages_impl.dart";
1414

15-
import "util.dart";
15+
import "util_io.dart";
1616

1717
/// A [Packages] implementation based on a local directory.
1818
class FilePackagesDirectoryPackages extends PackagesBase {

lib/src/util.dart

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
/// Utility methods used by more than one library in the package.
66
library package_config.util;
77

8-
import 'dart:io';
9-
import 'dart:typed_data';
10-
118
import "errors.dart";
129

1310
// All ASCII characters that are valid in a package name, with space
@@ -215,102 +212,6 @@ Uri relativizeUri(Uri uri, Uri /*?*/ baseUri) {
215212
}
216213
}
217214

218-
Future<Uint8List> defaultLoader(Uri uri) async {
219-
if (uri.isScheme("file")) {
220-
var file = File.fromUri(uri);
221-
try {
222-
return file.readAsBytes();
223-
} catch (_) {
224-
return null;
225-
}
226-
}
227-
if (uri.isScheme("http") || uri.isScheme("https")) {
228-
return _httpGet(uri);
229-
}
230-
throw UnsupportedError("Default URI unsupported scheme: $uri");
231-
}
232-
233-
Future<Uint8List /*?*/ > _httpGet(Uri uri) async {
234-
assert(uri.isScheme("http") || uri.isScheme("https"));
235-
var client = HttpClient();
236-
var request = await client.getUrl(uri);
237-
var response = await request.close();
238-
if (response.statusCode != HttpStatus.ok) {
239-
return null;
240-
}
241-
var splitContent = await response.toList();
242-
var totalLength = 0;
243-
if (splitContent.length == 1) {
244-
var part = splitContent[0];
245-
if (part is Uint8List) {
246-
return part;
247-
}
248-
}
249-
for (var list in splitContent) {
250-
totalLength += list.length;
251-
}
252-
var result = Uint8List(totalLength);
253-
var offset = 0;
254-
for (Uint8List contentPart in splitContent) {
255-
result.setRange(offset, offset + contentPart.length, contentPart);
256-
offset += contentPart.length;
257-
}
258-
return result;
259-
}
260-
261-
/// The file name of a path.
262-
///
263-
/// The file name is everything after the last occurrence of
264-
/// [Platform.pathSeparator], or the entire string if no
265-
/// path separator occurs in the string.
266-
String fileName(String path) {
267-
var separator = Platform.pathSeparator;
268-
var lastSeparator = path.lastIndexOf(separator);
269-
if (lastSeparator < 0) return path;
270-
return path.substring(lastSeparator + separator.length);
271-
}
272-
273-
/// The directory name of a path.
274-
///
275-
/// The directory name is everything before the last occurrence of
276-
/// [Platform.pathSeparator], or the empty string if no
277-
/// path separator occurs in the string.
278-
String dirName(String path) {
279-
var separator = Platform.pathSeparator;
280-
var lastSeparator = path.lastIndexOf(separator);
281-
if (lastSeparator < 0) return "";
282-
return path.substring(0, lastSeparator);
283-
}
284-
285-
/// Join path parts with the [Platform.pathSeparator].
286-
///
287-
/// If a part ends with a path separator, then no extra separator is
288-
/// inserted.
289-
String pathJoin(String part1, String part2, [String part3]) {
290-
var separator = Platform.pathSeparator;
291-
var separator1 = part1.endsWith(separator) ? "" : separator;
292-
if (part3 == null) {
293-
return "$part1$separator1$part2";
294-
}
295-
var separator2 = part2.endsWith(separator) ? "" : separator;
296-
return "$part1$separator1$part2$separator2$part3";
297-
}
298-
299-
/// Join an unknown number of path parts with [Platform.pathSeparator].
300-
///
301-
/// If a part ends with a path separator, then no extra separator is
302-
/// inserted.
303-
String pathJoinAll(Iterable<String> parts) {
304-
var buffer = StringBuffer();
305-
var separator = "";
306-
for (var part in parts) {
307-
buffer..write(separator)..write(part);
308-
separator =
309-
part.endsWith(Platform.pathSeparator) ? "" : Platform.pathSeparator;
310-
}
311-
return buffer.toString();
312-
}
313-
314215
// Character constants used by this package.
315216
/// "Line feed" control character.
316217
const int $lf = 0x0a;

lib/src/util_io.dart

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// Utility methods requiring dart:io and used by more than one library in the
6+
/// package.
7+
library package_config.util_io;
8+
9+
import 'dart:io';
10+
import 'dart:typed_data';
11+
12+
Future<Uint8List> defaultLoader(Uri uri) async {
13+
if (uri.isScheme("file")) {
14+
var file = File.fromUri(uri);
15+
try {
16+
return file.readAsBytes();
17+
} catch (_) {
18+
return null;
19+
}
20+
}
21+
if (uri.isScheme("http") || uri.isScheme("https")) {
22+
return _httpGet(uri);
23+
}
24+
throw UnsupportedError("Default URI unsupported scheme: $uri");
25+
}
26+
27+
Future<Uint8List /*?*/ > _httpGet(Uri uri) async {
28+
assert(uri.isScheme("http") || uri.isScheme("https"));
29+
var client = HttpClient();
30+
var request = await client.getUrl(uri);
31+
var response = await request.close();
32+
if (response.statusCode != HttpStatus.ok) {
33+
return null;
34+
}
35+
var splitContent = await response.toList();
36+
var totalLength = 0;
37+
if (splitContent.length == 1) {
38+
var part = splitContent[0];
39+
if (part is Uint8List) {
40+
return part;
41+
}
42+
}
43+
for (var list in splitContent) {
44+
totalLength += list.length;
45+
}
46+
var result = Uint8List(totalLength);
47+
var offset = 0;
48+
for (Uint8List contentPart in splitContent) {
49+
result.setRange(offset, offset + contentPart.length, contentPart);
50+
offset += contentPart.length;
51+
}
52+
return result;
53+
}
54+
55+
/// The file name of a path.
56+
///
57+
/// The file name is everything after the last occurrence of
58+
/// [Platform.pathSeparator], or the entire string if no
59+
/// path separator occurs in the string.
60+
String fileName(String path) {
61+
var separator = Platform.pathSeparator;
62+
var lastSeparator = path.lastIndexOf(separator);
63+
if (lastSeparator < 0) return path;
64+
return path.substring(lastSeparator + separator.length);
65+
}
66+
67+
/// The directory name of a path.
68+
///
69+
/// The directory name is everything before the last occurrence of
70+
/// [Platform.pathSeparator], or the empty string if no
71+
/// path separator occurs in the string.
72+
String dirName(String path) {
73+
var separator = Platform.pathSeparator;
74+
var lastSeparator = path.lastIndexOf(separator);
75+
if (lastSeparator < 0) return "";
76+
return path.substring(0, lastSeparator);
77+
}
78+
79+
/// Join path parts with the [Platform.pathSeparator].
80+
///
81+
/// If a part ends with a path separator, then no extra separator is
82+
/// inserted.
83+
String pathJoin(String part1, String part2, [String part3]) {
84+
var separator = Platform.pathSeparator;
85+
var separator1 = part1.endsWith(separator) ? "" : separator;
86+
if (part3 == null) {
87+
return "$part1$separator1$part2";
88+
}
89+
var separator2 = part2.endsWith(separator) ? "" : separator;
90+
return "$part1$separator1$part2$separator2$part3";
91+
}
92+
93+
/// Join an unknown number of path parts with [Platform.pathSeparator].
94+
///
95+
/// If a part ends with a path separator, then no extra separator is
96+
/// inserted.
97+
String pathJoinAll(Iterable<String> parts) {
98+
var buffer = StringBuffer();
99+
var separator = "";
100+
for (var part in parts) {
101+
buffer..write(separator)..write(part);
102+
separator =
103+
part.endsWith(Platform.pathSeparator) ? "" : Platform.pathSeparator;
104+
}
105+
return buffer.toString();
106+
}

pubspec.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: package_config
2-
version: 1.9.0
2+
version: 1.9.1
33
description: Support for working with Package Configuration files.
44
homepage: https://github.com/dart-lang/package_config
55

@@ -14,3 +14,6 @@ dev_dependencies:
1414
test: ^1.6.4
1515
matcher: ^0.12.5
1616
pedantic: 1.8.0
17+
build_runner: ^1.0.0
18+
build_web_compilers: ^2.0.0
19+
build_test: ^0.10.0

test/discovery_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
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+
@TestOn('vm')
56
library package_config.discovery_test;
67

78
import "dart:io";
89
import "package:test/test.dart";
910
import "package:package_config/package_config.dart";
1011

1112
import "src/util.dart";
13+
import "src/util_io.dart";
1214

1315
const packagesFile = """
1416
# A comment

test/discovery_uri_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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+
@TestOn('vm')
56
library package_config.discovery_test;
67

78
import "package:test/test.dart";

0 commit comments

Comments
 (0)