diff --git a/Core.Common.Standard/DataAccess/PathHelper.cs b/Core.Common.Standard/DataAccess/PathHelper.cs index ddbc1fd..4bec390 100644 --- a/Core.Common.Standard/DataAccess/PathHelper.cs +++ b/Core.Common.Standard/DataAccess/PathHelper.cs @@ -2,18 +2,22 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Text.RegularExpressions; // ReSharper disable UseFileSystem namespace KY.Core.DataAccess { - public class PathHelper // TODO: to internal + public class PathHelper { - private const string CurrentSymbol = "."; - private const string ParentSymbol = ".."; - private const string DriveSymbol = ":"; - public static readonly Regex AbsolutePathRegex = new Regex(@"^(([A-z]:)|(file:\\\\)|(\\\\))"); + private const string currentSymbol = "."; + private const string parentSymbol = ".."; + private const string driveSymbol = ":"; + + private static readonly Regex absolutePathRegex = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? new Regex(@"^(([A-z]:)|(file:\\\\)|(\\\\))") + : new Regex("^/"); public string Root { get; } public PathHelper(string root = null) @@ -24,7 +28,7 @@ public PathHelper(string root = null) public bool IsAbsolute(string path) { - return AbsolutePathRegex.IsMatch(path); + return absolutePathRegex.IsMatch(path); } public string ToAbsolute(params string[] pathChunks) @@ -47,8 +51,10 @@ public string ToRelative(string path, bool useRelativeChar = true) public string Format(string path) { - path = path?.Replace('/', Path.DirectorySeparatorChar); - if (path != null && path.StartsWith(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar)) + path = path?.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar); + bool isNetworkPath = path?.StartsWith(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar) ?? false; + bool isDrive = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) && (path?.StartsWith(Path.DirectorySeparatorChar.ToString()) ?? false); + if (isNetworkPath || isDrive) { return path.TrimEnd(Path.DirectorySeparatorChar); } @@ -69,7 +75,7 @@ public string Combine(params string[] pathChunks) { return pathChunks.FirstOrDefault(); } - bool isDrive = safePathChunks.First().Contains(DriveSymbol); + bool isDrive = safePathChunks.First().Contains(driveSymbol); List parts = new List(); foreach (string pathChunk in safePathChunks.Select(this.Format)) { @@ -77,16 +83,16 @@ public string Combine(params string[] pathChunks) foreach (string chunk in chunks) { string last = parts.LastOrDefault(); - if (chunk == CurrentSymbol) + if (chunk == currentSymbol) { if (parts.Count == 0) { parts.Add(chunk); } } - else if (chunk == ParentSymbol) + else if (chunk == parentSymbol) { - if (parts.Count == 0 || last == ParentSymbol) + if (parts.Count == 0 || last == parentSymbol) { parts.Add(chunk); } @@ -95,7 +101,7 @@ public string Combine(params string[] pathChunks) parts.Remove(last); } } - else if (parts.Count == 0 ||last == ParentSymbol) + else if (parts.Count == 0 ||last == parentSymbol) { parts.Add(chunk); } @@ -108,26 +114,6 @@ public string Combine(params string[] pathChunks) return string.Join(Path.DirectorySeparatorChar.ToString(), parts); } - //public static string Get(string relative, string absolute) - //{ - // int goToParent = 0; - // while (relative.StartsWith(@"..\")) - // { - // ++goToParent; - // relative = relative.Substring(3); - // } - - // DirectoryInfo info = new DirectoryInfo(absolute); - // for (int i = 0; i < goToParent; ++i) - // { - // if (info.Parent == null) - // break; - - // info = info.Parent; - // } - // return Path.Combine(info.FullName, relative); - //} - public string Parent(string path) { return string.IsNullOrEmpty(path) ? path : Path.GetDirectoryName(path); diff --git a/Core.Common.Standard/KY.Core.Common.Standard.csproj b/Core.Common.Standard/KY.Core.Common.Standard.csproj index ad19d9e..ac11371 100644 --- a/Core.Common.Standard/KY.Core.Common.Standard.csproj +++ b/Core.Common.Standard/KY.Core.Common.Standard.csproj @@ -4,7 +4,7 @@ netstandard2.0 KY.Core KY.Core.Common - 4.16.0 + 4.16.1 KY-Programming KY-Programmingp KY.Core diff --git a/Core.Common.Standard/Logger/ConsoleTarget.cs b/Core.Common.Standard/Logger/ConsoleTarget.cs index 6ca6b5e..4293e09 100644 --- a/Core.Common.Standard/Logger/ConsoleTarget.cs +++ b/Core.Common.Standard/Logger/ConsoleTarget.cs @@ -33,12 +33,12 @@ public override void Write(LogEntry entry) formattedMessage = string.Format(Resources.ConsoleTraceFormat, entry.Timestamp, entry.Message); } - if (entry.Shortable && formattedMessage.Length >= Console.WindowWidth) + if (entry.Shortable && formattedMessage.Length >= Console.WindowWidth && Console.WindowWidth > 0) { formattedMessage = formattedMessage.Substring(0, Console.WindowWidth - 4) + "..."; } - Console.WriteLine(formattedMessage.PadRight(Console.WindowWidth - 1)); + Console.WriteLine(Console.WindowWidth > 0 ? formattedMessage.PadRight(Console.WindowWidth - 1) : formattedMessage); } catch (IOException) { diff --git a/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs b/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs index a6e371f..97d31c3 100644 --- a/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs +++ b/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs @@ -89,8 +89,8 @@ public Assembly Locate(string search, Version defaultVersion = null, bool loadDe { assembly = (location.SearchLocal ? this.TryFind(info, location.Path, info.Path) : null) ?? (location.SearchBin ? this.TryFind(info, location.Path, "bin", info.Path) : null) - ?? (location.SearchBinDebug ? this.TryFindExtended(info, location.Path, "bin", "debug") : null) - ?? (location.SearchBinRelease ? this.TryFindExtended(info, location.Path, "bin", "release") : null); + ?? (location.SearchBinDebug ? this.TryFindExtended(info, location.Path, "bin", "Debug") : null) + ?? (location.SearchBinRelease ? this.TryFindExtended(info, location.Path, "bin", "Release") : null); if (assembly != null) { if (loadDependencies) @@ -179,7 +179,7 @@ private Assembly TryFindExtended(AssemblyInfo info, params string[] chunks) string path = FileSystem.Combine(FileSystem.Combine(chunks)); if (FileSystem.DirectoryExists(path)) { - DirectoryInfo[] directories = FileSystem.GetDirectoryInfos(path, "netcoreapp*").Concat(FileSystem.GetDirectoryInfos(path, "netstandard*")).ToArray(); + DirectoryInfo[] directories = FileSystem.GetDirectoryInfos(path, "net*"); return directories.Select(directory => this.TryFind(info, directory.FullName, info.Name)).FirstOrDefault() ?? this.TryFind(info, path, info.Name); }