Skip to content

Commit

Permalink
Merge pull request #123 from leisim/file-dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Mar 14, 2021
2 parents 356cc33 + e41363c commit 693d5be
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@ File('some/path/file.dart').isWithin(dir); // true

## Directory

### .file(String)

References a file within a `Directory`

```dart
Directory androidDir = Directory('flutter-app/android');
File manifestFile = androidDir.file("app/src/main/AndroidManifest.xml");
```

References a directory within a `Directory`

### .directory(String)

```dart
Directory androidDir = Directory('flutter-app/android');
Directory mainSrc = androidDir.directory("app/src/main");
```


### .contains(FileSystemEntity entity, {bool recursive = false})

Checks if a `Directory` contains a `FileSystemEntity`. This can be a `File` or a `Directory`.
Expand Down
54 changes: 54 additions & 0 deletions lib/src/io/directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,57 @@ extension DirectoryContainsSyncExtension on Directory {
(element) => FileSystemEntity.identicalSync(entity.path, element.path));
}
}

extension DirectoryFileExtension on Directory {
/// Returns a [File] within the [Directory]
///
/// ```dart
/// Directory androidDir = Directory('flutter-app/android');
/// File manifestFile = androidDir.file("app/src/main/AndroidManifest.xml");
/// ```
File file(String filePath) {
final sb = StringBuffer(absolute.path);
final path = sb.toString();
if (!path.endsWith(Platform.pathSeparator) &&
!filePath.startsWith(Platform.pathSeparator)) {
// no separator between dir and filePath
sb.write(Platform.pathSeparator);
}
if (path.startsWith(Platform.pathSeparator) &&
filePath.startsWith(Platform.pathSeparator)) {
// joining would cause a double //
final path = filePath.replaceFirst(Platform.pathSeparator, '');
sb.write(path);
} else {
sb.write(filePath);
}
return File(sb.toString());
}
}

extension DirectoryDirectoryExtension on Directory {
/// Returns a directory within the [Directory]
///
/// ```dart
/// Directory androidDir = Directory('flutter-app/android');
/// Directory mainSrc = androidDir.directory("app/src/main");
/// ```
Directory directory(String dirPath) {
final sb = StringBuffer(absolute.path);
final path = sb.toString();
if (!path.endsWith(Platform.pathSeparator) &&
!dirPath.startsWith(Platform.pathSeparator)) {
// no separator between dir and filePath
sb.write(Platform.pathSeparator);
}
if (path.startsWith(Platform.pathSeparator) &&
dirPath.startsWith(Platform.pathSeparator)) {
// joining would cause a double //
final path = dirPath.replaceFirst(Platform.pathSeparator, '');
sb.write(path);
} else {
sb.write(dirPath);
}
return Directory(sb.toString());
}
}
41 changes: 39 additions & 2 deletions test/directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:dartx/dartx_io.dart';
void main() {
group('Directory', () {
test('contains', () async {
final parentParentDir = Directory.current;
final parentParentDir = Directory.systemTemp;
final parentDir = parentParentDir.createTempSync('dartxDirTempDir');
final thisDir = parentDir.createTempSync('dartxDirSubTempDir');

Expand Down Expand Up @@ -37,8 +37,9 @@ void main() {
expect(await parentDir.contains(file), false);
expect(await parentDir.contains(file, recursive: true), true);
});

test('containsSync', () {
final parentParentDir = Directory.current;
final parentParentDir = Directory.systemTemp;
final parentDir = parentParentDir.createTempSync('dartxDirTempDir');
final thisDir = parentDir.createTempSync('dartxDirSubTempDir');

Expand Down Expand Up @@ -69,4 +70,40 @@ void main() {
expect(parentDir.containsSync(file, recursive: true), true);
});
});

group('directory()', () {
test('without / at end', () {
final rootDir = Directory.systemTemp.createTempSync('root');
final level1 = rootDir.directory('level1')..createSync();
final dir = level1.directory('dir');
expect(dir.path, isNot(contains('//')));
});

test('with / at end', () {
final rootDir = Directory.systemTemp.createTempSync('root');
final level1 = rootDir.directory('/level1/')..createSync();
final dir = level1.directory('/dir/');
expect(dir.path, isNot(contains('//')));
});
});

group('file', () {
test('without / at end', () {
final rootDir = Directory.systemTemp.createTempSync('root');
final level1 = rootDir.directory('level1')..createSync();
final level2 = level1.directory('level2')..createSync();
expect(level2.absolute.path, endsWith('/level1/level2'));
final file = level2.file('test.txt');
expect(file.path, endsWith('/level1/level2/test.txt'));
});

test('with / at end', () {
final rootDir = Directory.systemTemp.createTempSync('root');
final level1 = rootDir.directory('level1/')..createSync();
final level2 = level1.directory('level2/')..createSync();
expect(level2.absolute.path, endsWith('/level1/level2/'));
final file = level2.file('/test.txt');
expect(file.path, endsWith('/level1/level2/test.txt'));
});
});
}

0 comments on commit 693d5be

Please sign in to comment.