-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AbsolutePath.Move and Rename extensions
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 Maintainers of NUKE. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.IO; | ||
|
||
namespace Nuke.Common.IO; | ||
|
||
partial class AbsolutePathExtensions | ||
{ | ||
/// <summary> | ||
/// Renames the file or directory. | ||
/// </summary> | ||
public static AbsolutePath Rename(this AbsolutePath path, string newName) | ||
{ | ||
return path.Move(path.Parent / newName); | ||
} | ||
|
||
/// <summary> | ||
/// Renames the file or directory. | ||
/// </summary> | ||
public static AbsolutePath Rename(this AbsolutePath path, Func<AbsolutePath, string> newName) | ||
{ | ||
return path.Rename(newName.Invoke(path)); | ||
} | ||
|
||
/// <summary> | ||
/// Renames the file without changing the extension. | ||
/// </summary> | ||
public static AbsolutePath RenameWithoutExtension(this AbsolutePath path, string newName) | ||
{ | ||
Assert.True(path.FileExists()); | ||
return path.Move(path.Parent / newName + path.Extension); | ||
} | ||
|
||
/// <summary> | ||
/// Renames the file without changing the extension. | ||
/// </summary> | ||
public static AbsolutePath RenameWithoutExtension(this AbsolutePath path, Func<AbsolutePath, string> newName) | ||
{ | ||
return path.RenameWithoutExtension(newName.Invoke(path)); | ||
} | ||
|
||
/// <summary> | ||
/// Moves the file or directory to another directory. | ||
/// </summary> | ||
public static AbsolutePath MoveToDirectory(this AbsolutePath path, AbsolutePath directory) | ||
{ | ||
Assert.True(directory.Exists()); | ||
return path.Move(directory / path.Name); | ||
} | ||
|
||
/// <summary> | ||
/// Moves the file or directory. | ||
/// </summary> | ||
public static AbsolutePath Move(this AbsolutePath path, Func<AbsolutePath, AbsolutePath> newPath) | ||
{ | ||
return path.Move(newPath.Invoke(path)); | ||
} | ||
|
||
/// <summary> | ||
/// Moves the file or directory. | ||
/// </summary> | ||
public static AbsolutePath Move(this AbsolutePath path, AbsolutePath newPath) | ||
{ | ||
Assert.True(path.DirectoryExists() || path.FileExists()); | ||
|
||
if (path.DirectoryExists()) | ||
Directory.Move(path, newPath); | ||
else if (path.FileExists()) | ||
File.Move(path, newPath); | ||
|
||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters