From 199a407cd1f3c20048d0f9aedca5ea4e6e89280a Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Mon, 11 Mar 2024 15:38:22 +0000 Subject: [PATCH] tidy up the path in the report. --- uSync.BackOffice/Extensions/PathExtensions.cs | 35 +++++++++++++++++++ .../SyncHandlers/SyncHandlerRoot.cs | 3 +- uSync.Tests/Extensions/PathNameTests.cs | 33 +++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 uSync.BackOffice/Extensions/PathExtensions.cs diff --git a/uSync.BackOffice/Extensions/PathExtensions.cs b/uSync.BackOffice/Extensions/PathExtensions.cs new file mode 100644 index 00000000..9af5d6e9 --- /dev/null +++ b/uSync.BackOffice/Extensions/PathExtensions.cs @@ -0,0 +1,35 @@ +using System; +using System.IO; +using System.Linq; + +namespace uSync.BackOffice.Extensions; + +/// +/// extensions to manipulate folder path strings. +/// +public static class PathExtensions +{ + /// + /// truncate a folder path to only show the last count paths.. + /// + public static string TruncatePath(this string path, int count = 3, bool includeFile = false) + { + if (string.IsNullOrWhiteSpace(path)) return path; + + var result = ""; + + var fullPath = includeFile ? path : Path.GetDirectoryName(path); + if (string.IsNullOrWhiteSpace(fullPath)) return fullPath ?? string.Empty; + + var bits = fullPath.Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]); + + foreach (var item in bits.Reverse().Take(count)) + { + if (Path.IsPathRooted(item)) continue; + + result = $"{Path.DirectorySeparatorChar}{item}{result}"; + } + + return result; + } +} diff --git a/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs b/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs index c4805c84..0ed21ce4 100644 --- a/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs +++ b/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs @@ -17,6 +17,7 @@ using Umbraco.Extensions; using uSync.BackOffice.Configuration; +using uSync.BackOffice.Extensions; using uSync.BackOffice.Models; using uSync.BackOffice.Services; using uSync.BackOffice.SyncHandlers.Models; @@ -570,7 +571,7 @@ private string GetRootFolder(string path) if (path.Contains(folder) is true) return Path.GetFileName(folder.TrimEnd('/')); } - return path; + return path.TruncatePath(3, false); } diff --git a/uSync.Tests/Extensions/PathNameTests.cs b/uSync.Tests/Extensions/PathNameTests.cs index 0687c54b..e5850381 100644 --- a/uSync.Tests/Extensions/PathNameTests.cs +++ b/uSync.Tests/Extensions/PathNameTests.cs @@ -1,5 +1,8 @@ using NUnit.Framework; +using Umbraco.Extensions; + +using uSync.BackOffice.Extensions; using uSync.Core; namespace uSync.Tests.Extensions @@ -27,5 +30,35 @@ public void BadFileNamesAreAppended(string filename, string expected) Assert.AreEqual(expected, value); } + + [TestCase("C:\\Source\\OpenSource\\v13\\uSync\\README.md", "\\OpenSource\\v13\\uSync")] + [TestCase("C:\\Source\\OpenSource\\v13\\README.md", "\\Source\\OpenSource\\v13")] + [TestCase("C:\\Source\\OpenSource\\README.md", "\\Source\\OpenSource")] + [TestCase("C:\\Source\\README.md", "\\Source")] + [TestCase("C:\\Source/OpenSource\\v13\\uSync\\README.md", "\\OpenSource\\v13\\uSync")] + [TestCase("C:\\Source/OpenSource\\v13\\README.md", "\\Source\\OpenSource\\v13")] + [TestCase("C:\\Source/OpenSource/README.md", "\\Source\\OpenSource")] + [TestCase("C:/Source\\README.md", "\\Source")] + [TestCase("README.md", "")] + [TestCase("", "")] + public void TruncatedPathsWithoutFileName(string filename, string expected) + { + var result = filename.TruncatePath(3, false); + + Assert.AreEqual(expected, result); + } + + [TestCase("C:\\Source\\OpenSource\\v13\\uSync\\README.md", "\\v13\\uSync\\README.md")] + [TestCase("C:\\Source\\OpenSource\\v13\\README.md", "\\OpenSource\\v13\\README.md")] + [TestCase("C:\\Source\\OpenSource\\README.md", "\\Source\\OpenSource\\README.md")] + [TestCase("C:\\Source\\README.md", "\\Source\\README.md")] + [TestCase("README.md", "\\README.md")] + [TestCase("", "")] + public void TruncatedPathsWithFileName(string filename, string expected) + { + var result = filename.TruncatePath(3, true); + + Assert.AreEqual(expected, result); + } } }