Skip to content

Commit

Permalink
Add WithFilePrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-edmondson committed Aug 21, 2024
1 parent 86a9f32 commit 7b584cf
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 32 deletions.
25 changes: 14 additions & 11 deletions StrongPaths.Test/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
namespace ktsu.io.StrongPaths.Test;

using System.Reflection;

#pragma warning disable CS0219
#pragma warning disable CS1591

Expand Down Expand Up @@ -91,13 +88,6 @@ public void TestAnyAbsolutePath()
[TestMethod]
public void TestAnyDirectoryPath() => _ = (AnyDirectoryPath)FullyQualifiedPath;

[TestMethod]
public void TestAnyFilePath()
{
var path = (AnyFilePath)DotYeet;
_ = Assert.ThrowsException<FormatException>(action: () => _ = (AnyFilePath)Yeet);
}

[TestMethod]
public void TestAnyRelativePath()
{
Expand Down Expand Up @@ -135,6 +125,19 @@ public void TestGetContents()
{
var path = (AnyDirectoryPath)FullyQualifiedPath;
var contents = path.Contents;
Assert.IsTrue(contents.Contains((AbsoluteFilePath)Assembly.GetExecutingAssembly().Location));
var filePath = (AbsoluteDirectoryPath)AppContext.BaseDirectory / (FileName)"ktsu.io.StrongPaths.Test.dll";
Assert.IsTrue(contents.Contains(filePath));
}

[TestMethod]
public void TestWithFilePrefix()
{
var absDirectory = (AbsoluteDirectoryPath)FullyQualifiedPath;
var absFilePath = absDirectory / (FileName)DotYeet;
Assert.IsTrue(absFilePath.WithFilePrefix(Yeet) == absDirectory / (FileName)(Yeet + DotYeet));

var relDirectory = (RelativeDirectoryPath)Yeet;
var relFilePath = relDirectory / (FileName)DotYeet;
Assert.IsTrue(relFilePath.WithFilePrefix(Yeet) == relDirectory / (FileName)(Yeet + DotYeet));
}
}
1 change: 0 additions & 1 deletion StrongPaths.Test/Usings.cs

This file was deleted.

4 changes: 2 additions & 2 deletions StrongPaths/AnyAbsolutePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public record AnyAbsolutePath : StrongPathAbstract<AnyAbsolutePath, IsPath, IsAb
public record AnyAbsolutePath<TDerived> : AnyAbsolutePath

Check warning on line 10 in StrongPaths/AnyAbsolutePath.cs

View workflow job for this annotation

GitHub Actions / build-dotnet-library / ktsu-io/StrongPaths build-dotnet-library

This record has 7 parents which is greater than 5 authorized. (https://rules.sonarsource.com/csharp/RSPEC-110)
where TDerived : AnyAbsolutePath<TDerived>
{
public static explicit operator AnyAbsolutePath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: value);
public static explicit operator AnyAbsolutePath<TDerived>(string? value) => FromString<TDerived>(value: value);
public static explicit operator AnyAbsolutePath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: MakeCanonical(value));
public static explicit operator AnyAbsolutePath<TDerived>(string? value) => FromString<TDerived>(value: MakeCanonical(value));
}
4 changes: 2 additions & 2 deletions StrongPaths/AnyDirectoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public static Collection<AnyAbsolutePath> GetContents(AnyDirectoryPath directory
public record AnyDirectoryPath<TDerived> : AnyDirectoryPath
where TDerived : AnyDirectoryPath<TDerived>
{
public static explicit operator AnyDirectoryPath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: value);
public static explicit operator AnyDirectoryPath<TDerived>(string? value) => FromString<TDerived>(value: value);
public static explicit operator AnyDirectoryPath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: MakeCanonical(value));
public static explicit operator AnyDirectoryPath<TDerived>(string? value) => FromString<TDerived>(value: MakeCanonical(value));
}
4 changes: 2 additions & 2 deletions StrongPaths/AnyFileName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public record AnyFileName : StrongPathAbstract<AnyFileName, IsPath, IsFileName>;
public record AnyFileName<TDerived> : AnyFileName
where TDerived : AnyFileName<TDerived>
{
public static explicit operator AnyFileName<TDerived>(char[]? value) => FromCharArray<TDerived>(value: value);
public static explicit operator AnyFileName<TDerived>(string? value) => FromString<TDerived>(value: value);
public static explicit operator AnyFileName<TDerived>(char[]? value) => FromCharArray<TDerived>(value: MakeCanonical(value));
public static explicit operator AnyFileName<TDerived>(string? value) => FromString<TDerived>(value: MakeCanonical(value));
}
4 changes: 2 additions & 2 deletions StrongPaths/AnyFilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public FileExtension FileExtension
public record AnyFilePath<TDerived> : AnyFilePath
where TDerived : AnyFilePath<TDerived>
{
public static explicit operator AnyFilePath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: value);
public static explicit operator AnyFilePath<TDerived>(string? value) => FromString<TDerived>(value: value);
public static explicit operator AnyFilePath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: MakeCanonical(value));
public static explicit operator AnyFilePath<TDerived>(string? value) => FromString<TDerived>(value: MakeCanonical(value));
}
4 changes: 2 additions & 2 deletions StrongPaths/AnyRelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public static TDest Make<TDest>(AnyStrongPath from, AnyStrongPath to)
public record AnyRelativePath<TDerived> : AnyRelativePath
where TDerived : AnyRelativePath<TDerived>
{
public static explicit operator AnyRelativePath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: value);
public static explicit operator AnyRelativePath<TDerived>(string? value) => FromString<TDerived>(value: value);
public static explicit operator AnyRelativePath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: MakeCanonical(value));
public static explicit operator AnyRelativePath<TDerived>(string? value) => FromString<TDerived>(value: MakeCanonical(value));
}
16 changes: 14 additions & 2 deletions StrongPaths/AnyStrongPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
namespace ktsu.io.StrongPaths;

using System.Diagnostics.CodeAnalysis;
using ktsu.io.Extensions;
using StrongStrings;

public abstract record AnyStrongPath : StrongStringAbstract<AnyStrongPath, IsPath>
{
public bool Exists => IsDirectory || IsFile;
public bool IsDirectory => Directory.Exists(path: WeakString);
public bool IsFile => File.Exists(path: WeakString);
public static string MakeCanonical(string? input)
{
ArgumentNullException.ThrowIfNull(input);
return input.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).RemoveSuffix(new string([Path.DirectorySeparatorChar]));
}

public static char[]? MakeCanonical(char[]? input)
{
ArgumentNullException.ThrowIfNull(input);
return MakeCanonical(new string(input)).ToCharArray();
}
}

[SuppressMessage(category: "Usage", checkId: "CA2225:Operator overloads have named alternates", Justification = "The base class already has these")]
public abstract record AnyStrongPath<TDerived> : AnyStrongPath
where TDerived : AnyStrongPath<TDerived>
{
public static explicit operator AnyStrongPath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: value);
public static explicit operator AnyStrongPath<TDerived>(string? value) => FromString<TDerived>(value: value);
public static explicit operator AnyStrongPath<TDerived>(char[]? value) => FromCharArray<TDerived>(value: MakeCanonical(value));
public static explicit operator AnyStrongPath<TDerived>(string? value) => FromString<TDerived>(value: MakeCanonical(value));
}
25 changes: 19 additions & 6 deletions StrongPaths/CommonPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace ktsu.io.StrongPaths;

using System.Collections.ObjectModel;
using ktsu.io.Extensions;

public sealed record class AbsoluteDirectoryPath : AbsolutePathAbstract<AbsoluteDirectoryPath, IsDirectory>
{
public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, FileName right) => (AbsoluteFilePath)$"{left}/{right}";
public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, RelativeFilePath right) => (AbsoluteFilePath)$"{left}/{right}";
public static AbsoluteDirectoryPath operator /(AbsoluteDirectoryPath left, RelativeDirectoryPath right) => (AbsoluteDirectoryPath)$"{left}/{right}";
public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, FileName right) => (AbsoluteFilePath)MakeCanonical($"{left}/{right}");
public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, RelativeFilePath right) => (AbsoluteFilePath)MakeCanonical($"{left}/{right}");
public static AbsoluteDirectoryPath operator /(AbsoluteDirectoryPath left, RelativeDirectoryPath right) => (AbsoluteDirectoryPath)MakeCanonical($"{left}/{right}");
public RelativeDirectoryPath RelativeTo(AnyStrongPath other) => AnyRelativePath.Make<RelativeDirectoryPath>(from: other, to: this);
public Collection<AnyAbsolutePath> Contents => AnyDirectoryPath.GetContents((AnyDirectoryPath)WeakString);
public AbsoluteDirectoryPath Parent => (AbsoluteDirectoryPath)Path.GetDirectoryName(WeakString.Trim(Path.DirectorySeparatorChar).Trim(Path.AltDirectorySeparatorChar));
Expand Down Expand Up @@ -37,13 +38,19 @@ public FileExtension FileExtension

public FileName FileName => (FileName)Path.GetFileName(WeakString);
public AbsoluteDirectoryPath DirectoryPath => (AbsoluteDirectoryPath)Path.GetDirectoryName(WeakString);

public AbsoluteFilePath WithFilePrefix(string filePrefix)
{
string directory = WeakString.RemoveSuffix(FileName);
return (AbsoluteFilePath)$"{directory}{filePrefix}{FileName}";
}
}

public sealed record class RelativeDirectoryPath : RelativePathAbstract<RelativeDirectoryPath, IsDirectory>
{
public static RelativeFilePath operator /(RelativeDirectoryPath left, FileName right) => (RelativeFilePath)$"{left}/{right}";
public static RelativeFilePath operator /(RelativeDirectoryPath left, RelativeFilePath right) => (RelativeFilePath)$"{left}/{right}";
public static RelativeDirectoryPath operator /(RelativeDirectoryPath left, RelativeDirectoryPath right) => (RelativeDirectoryPath)$"{left}/{right}";
public static RelativeFilePath operator /(RelativeDirectoryPath left, FileName right) => (RelativeFilePath)MakeCanonical($"{left}/{right}");
public static RelativeFilePath operator /(RelativeDirectoryPath left, RelativeFilePath right) => (RelativeFilePath)MakeCanonical($"{left}/{right}");
public static RelativeDirectoryPath operator /(RelativeDirectoryPath left, RelativeDirectoryPath right) => (RelativeDirectoryPath)MakeCanonical($"{left}/{right}");
public RelativeDirectoryPath RelativeTo(AnyStrongPath other) => Make<RelativeDirectoryPath>(from: other, to: this);
public Collection<AnyAbsolutePath> Contents => AnyDirectoryPath.GetContents((AnyDirectoryPath)WeakString);
public RelativeDirectoryPath Parent => (RelativeDirectoryPath)Path.GetDirectoryName(WeakString.Trim(Path.DirectorySeparatorChar).Trim(Path.AltDirectorySeparatorChar));
Expand Down Expand Up @@ -73,4 +80,10 @@ public FileExtension FileExtension

public FileName FileName => (FileName)Path.GetFileName(WeakString);
public RelativeDirectoryPath DirectoryPath => (RelativeDirectoryPath)Path.GetDirectoryName(WeakString);

public RelativeFilePath WithFilePrefix(string filePrefix)
{
string directory = WeakString.RemoveSuffix(FileName);
return (RelativeFilePath)$"{directory}{filePrefix}{FileName}";
}
}
3 changes: 2 additions & 1 deletion StrongPaths/StrongPaths.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="ktsu.io.StrongStrings" Version="1.0.1" />
<PackageReference Include="ktsu.io.Extensions" Version="1.0.7" />
<PackageReference Include="ktsu.io.StrongStrings" Version="1.1.0" />
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.8
1.1.0

0 comments on commit 7b584cf

Please sign in to comment.