| // Copyright 2024, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. 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'; |
| |
| /// Extends [Directory] with functions to resolve relative paths and move |
| /// sub-directories. |
| extension UsefulDirectory on Directory { |
| /// Resolves [relativePath] to a directory relative to [this]. |
| /// |
| /// Returns the resulting directory. |
| Directory resolve(String relativePath) { |
| return fileSystem.directory(uri.resolve(relativePath)); |
| } |
| |
| /// Resolves a [relativePath] to a file relative to [this]. |
| /// |
| /// Returns the resulting file. |
| File resolveFile(String relativePath) { |
| return fileSystem.file(uri.resolve(relativePath)); |
| } |
| |
| /// Moves a sub-directory [from] to path [to]. |
| /// |
| /// Both [from] and [to] must be relative paths. |
| /// The [to] directory's parent directory is created if it doesn't exist |
| /// already. |
| /// |
| /// Throws if the parent of [to] can't be created or [from] can't be moved. |
| /// |
| /// Returns the resolved [to] directory. |
| Future<Directory> move(String from, String to) async { |
| final toDir = resolve(to); |
| await toDir.parent.create(recursive: true); |
| final fromDir = resolve(from); |
| return await fromDir.rename(toDir.path); |
| } |
| } |