Skip to content

Commit

Permalink
feat(utilities): add AbsolutePath division operator for range express…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
matkoch committed Dec 15, 2023
1 parent eb63fd3 commit b77724a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/03-common/03-paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var extensionWithDot = IndexFile.Extension;

// Get the parent directory
var parent1 = IndexFile.Parent;
var parent2 = IndexFile / ".."; // gets normalized
var parent2 = IndexFile / ..; // gets normalized
var parent3 = IndexFile / ".."; // gets normalized
// Check if one path contains another
var containsFile = SourceDirectory.Contains(IndexFile);
Expand Down
9 changes: 9 additions & 0 deletions source/Nuke.Utilities.Tests/IO/PathConstructionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void TestParent(string path, string expected)
{
((AbsolutePath) path).Parent.Should().Be((AbsolutePath) expected);
((string) ((AbsolutePath) path).Parent).Should().Be(expected);
((AbsolutePath)path / ..).Should().Be((AbsolutePath)expected);
}

[Theory]
Expand Down Expand Up @@ -214,6 +215,14 @@ public void RelativePath_Specific()
((string) (WinRelativePath) "foo/bar").Should().Be("foo\\bar");
}

[Fact]
public void RelativePath_Parent()
{
((string) ((UnixRelativePath) "foo/bar/foo" / ..)).Should().Be("foo/bar");
((string) ((WinRelativePath) "foo/bar/foo" / ..)).Should().Be("foo\\bar");
((string) ((RelativePath) "foo/bar" / ..)).Should().Be("foo");
}

private static string ParseRelativePath(object[] parts)
{
return parts.Skip(count: 1).Aggregate((RelativePath) (string) parts[0], (rp, p) => rp / (string) p);
Expand Down
10 changes: 10 additions & 0 deletions source/Nuke.Utilities/IO/AbsolutePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ public static implicit operator string([CanBeNull] AbsolutePath path)
? this / ".."
: null;

#if NET6_0_OR_GREATER

public static AbsolutePath operator /(AbsolutePath left, [CanBeNull] Range range)
{
Assert.True(range.Equals(Range.All));
return left.Parent;
}

#endif

public static AbsolutePath operator /(AbsolutePath left, [CanBeNull] string right)
{
return new AbsolutePath(Combine(left.NotNull(), right));
Expand Down
15 changes: 15 additions & 0 deletions source/Nuke.Utilities/IO/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,27 @@ public static implicit operator string([CanBeNull] RelativePath path)
return path?._path;
}

#if NET6_0_OR_GREATER

public static RelativePath operator /(RelativePath left, [CanBeNull] Range range)
{
Assert.True(range.Equals(Range.All));
return left / "..";
}

#endif

public static RelativePath operator /(RelativePath left, [CanBeNull] string right)
{
var separator = left.NotNull()._separator;
return new RelativePath(NormalizePath(Combine(left, (RelativePath) right, separator), separator), separator);
}

public static RelativePath operator +(RelativePath left, [CanBeNull] string right)
{
return new RelativePath(left.ToString() + right);
}

public override string ToString()
{
return _path;
Expand Down

0 comments on commit b77724a

Please sign in to comment.