Change file system copy folder to copy directory (#7624)

* Change file_system’s copy folder to copy director which takes into account the file system

* Address review comments.
Test with 2 different instances of file systems.
This commit is contained in:
xster
2017-01-25 11:41:48 -08:00
committed by GitHub
parent 9733aab929
commit ee97e5639e
2 changed files with 48 additions and 6 deletions

View File

@@ -29,23 +29,24 @@ void ensureDirectoryExists(String filePath) {
fs.directory(dirPath).createSync(recursive: true);
}
/// Recursively copies a folder from `srcPath` to `destPath`
void copyFolderSync(String srcPath, String destPath) {
Directory srcDir = fs.directory(srcPath);
/// Recursively copies `srcDir` to `destDir`.
///
/// Creates `destDir` if needed.
void copyDirectorySync(Directory srcDir, Directory destDir) {
if (!srcDir.existsSync())
throw new Exception('Source directory "${srcDir.path}" does not exist, nothing to copy');
Directory destDir = fs.directory(destPath);
if (!destDir.existsSync())
destDir.createSync(recursive: true);
srcDir.listSync().forEach((FileSystemEntity entity) {
String newPath = path.join(destDir.path, path.basename(entity.path));
if (entity is File) {
File newFile = fs.file(newPath);
File newFile = destDir.fileSystem.file(newPath);
newFile.writeAsBytesSync(entity.readAsBytesSync());
} else if (entity is Directory) {
copyFolderSync(entity.path, newPath);
copyDirectorySync(
entity, destDir.fileSystem.directory(newPath));
} else {
throw new Exception('${entity.path} is neither File nor Directory');
}