-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathfile_utils.dart
More file actions
51 lines (47 loc) · 1.77 KB
/
file_utils.dart
File metadata and controls
51 lines (47 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file/file.dart';
import 'package:path/path.dart' as p;
/// Returns a [File] created by appending all but the last item in [components]
/// to [base] as subdirectories, then appending the last as a file.
///
/// Example:
/// childFileWithSubcomponents(rootDir, ['foo', 'bar', 'baz.txt'])
/// creates a File representing /rootDir/foo/bar/baz.txt.
File childFileWithSubcomponents(Directory base, List<String> components) {
final String basename = components.removeLast();
return childDirectoryWithSubcomponents(base, components).childFile(basename);
}
/// Returns a [Directory] created by appending everything in [components]
/// to [base] as subdirectories.
///
/// Example:
/// childFileWithSubcomponents(rootDir, ['foo', 'bar'])
/// creates a File representing /rootDir/foo/bar/.
Directory childDirectoryWithSubcomponents(
Directory base,
List<String> components,
) {
var dir = base;
for (final directoryName in components) {
dir = dir.childDirectory(directoryName);
}
return dir;
}
/// Returns the relative path from [from] to [entity] using [platformContext]
/// as the path context, but always formatting the result as a POSIX path
/// (using forward slashes).
///
/// This is useful for generating paths that will be used in configuration
/// files or command lines that expect POSIX paths, even when running on a
/// platform that uses a different path separator, or for display.
String relativePosixPath(
FileSystemEntity entity, {
required Directory from,
required p.Context platformContext,
}) => p.posix.joinAll(
platformContext.split(
platformContext.relative(entity.absolute.path, from: from.path),
),
);