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:
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user