Skip to content

Commit

Permalink
Add AbsolutePath.Move and Rename extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed May 6, 2023
1 parent c851250 commit 96b148a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
76 changes: 76 additions & 0 deletions source/Nuke.Utilities/IO/AbsolutePath.MoveCopy.cs
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;
}
}
5 changes: 5 additions & 0 deletions source/Nuke.Utilities/IO/AbsolutePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public static implicit operator string([CanBeNull] AbsolutePath path)
/// </summary>
public string NameWithoutExtension => Path.GetFileNameWithoutExtension(_path);

/// <summary>
/// Returns the extension of the file with dot.
/// </summary>
public string Extension => Path.GetExtension(_path);

/// <summary>
/// Returns the parent path (directory).
/// </summary>
Expand Down

0 comments on commit 96b148a

Please sign in to comment.