copyDirectoryToAsync static method

Future<void> copyDirectoryToAsync(
  1. String path,
  2. String pathTarget, {
  3. String? rootPath,
  4. bool showLog = true,
})

目录拷贝

path 源目录路径 pathTarget 目标目录路径 showLog 是否展示消息日志 rootPath 根路径

Implementation

static Future<void> copyDirectoryToAsync(
  String path,
  String pathTarget, {
  String? rootPath,
  bool showLog = true,
}) async {
  if (!await existsAsync(path) || !await isDirectoryAsync(path)) {
    return;
  }
  if (showLog) {
    final samePath = TextUtil.getSameTextInTwoStr(path, pathTarget);
    Logger.info(
      'copy dir',
      '${path.replaceFirst(samePath, '')} -> ${pathTarget.replaceFirst(samePath, '')}',
    );
  }
  Future<void> runCopy(
    String rPath,
    String rPathTarget, {
    bool inShowLog = true,
  }) async {
    final diff = PathUtil.relative(rPath, from: path);
    final mergedPathTarget = PathUtil.join(pathTarget, diff);
    final fileList = await Directory(rPath).list().toList();
    for (final f in fileList) {
      if (await isDirectoryAsync(f.path)) {
        await runCopy(
          f.path,
          pathTarget,
          inShowLog: false,
        );
      } else if (await isFileAsync(f.path)) {
        await copyFileToAsync(
          f.path,
          mergedPathTarget,
          targetIsDirectory: true,
          showLog: false,
        );
      }
    }
  }

  await runCopy(path, pathTarget);
}