Skip to content

Commit 6fe910a

Browse files
committed
Linux/OSX compatibility improved
1 parent cd9b202 commit 6fe910a

File tree

4 files changed

+25
-39
lines changed

4 files changed

+25
-39
lines changed

Core.Common.Standard/DataAccess/PathHelper.cs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56
using System.Text.RegularExpressions;
67

78
// ReSharper disable UseFileSystem
89

910
namespace KY.Core.DataAccess
1011
{
11-
public class PathHelper // TODO: to internal
12+
public class PathHelper
1213
{
13-
private const string CurrentSymbol = ".";
14-
private const string ParentSymbol = "..";
15-
private const string DriveSymbol = ":";
16-
public static readonly Regex AbsolutePathRegex = new Regex(@"^(([A-z]:)|(file:\\\\)|(\\\\))");
14+
private const string currentSymbol = ".";
15+
private const string parentSymbol = "..";
16+
private const string driveSymbol = ":";
17+
18+
private static readonly Regex absolutePathRegex = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
19+
? new Regex(@"^(([A-z]:)|(file:\\\\)|(\\\\))")
20+
: new Regex("^/");
1721
public string Root { get; }
1822

1923
public PathHelper(string root = null)
@@ -24,7 +28,7 @@ public PathHelper(string root = null)
2428

2529
public bool IsAbsolute(string path)
2630
{
27-
return AbsolutePathRegex.IsMatch(path);
31+
return absolutePathRegex.IsMatch(path);
2832
}
2933

3034
public string ToAbsolute(params string[] pathChunks)
@@ -47,8 +51,10 @@ public string ToRelative(string path, bool useRelativeChar = true)
4751

4852
public string Format(string path)
4953
{
50-
path = path?.Replace('/', Path.DirectorySeparatorChar);
51-
if (path != null && path.StartsWith(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar))
54+
path = path?.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
55+
bool isNetworkPath = path?.StartsWith(Path.DirectorySeparatorChar.ToString() + Path.DirectorySeparatorChar) ?? false;
56+
bool isDrive = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) && (path?.StartsWith(Path.DirectorySeparatorChar.ToString()) ?? false);
57+
if (isNetworkPath || isDrive)
5258
{
5359
return path.TrimEnd(Path.DirectorySeparatorChar);
5460
}
@@ -69,24 +75,24 @@ public string Combine(params string[] pathChunks)
6975
{
7076
return pathChunks.FirstOrDefault();
7177
}
72-
bool isDrive = safePathChunks.First().Contains(DriveSymbol);
78+
bool isDrive = safePathChunks.First().Contains(driveSymbol);
7379
List<string> parts = new List<string>();
7480
foreach (string pathChunk in safePathChunks.Select(this.Format))
7581
{
7682
string[] chunks = pathChunk.Split(Path.DirectorySeparatorChar);
7783
foreach (string chunk in chunks)
7884
{
7985
string last = parts.LastOrDefault();
80-
if (chunk == CurrentSymbol)
86+
if (chunk == currentSymbol)
8187
{
8288
if (parts.Count == 0)
8389
{
8490
parts.Add(chunk);
8591
}
8692
}
87-
else if (chunk == ParentSymbol)
93+
else if (chunk == parentSymbol)
8894
{
89-
if (parts.Count == 0 || last == ParentSymbol)
95+
if (parts.Count == 0 || last == parentSymbol)
9096
{
9197
parts.Add(chunk);
9298
}
@@ -95,7 +101,7 @@ public string Combine(params string[] pathChunks)
95101
parts.Remove(last);
96102
}
97103
}
98-
else if (parts.Count == 0 ||last == ParentSymbol)
104+
else if (parts.Count == 0 ||last == parentSymbol)
99105
{
100106
parts.Add(chunk);
101107
}
@@ -108,26 +114,6 @@ public string Combine(params string[] pathChunks)
108114
return string.Join(Path.DirectorySeparatorChar.ToString(), parts);
109115
}
110116

111-
//public static string Get(string relative, string absolute)
112-
//{
113-
// int goToParent = 0;
114-
// while (relative.StartsWith(@"..\"))
115-
// {
116-
// ++goToParent;
117-
// relative = relative.Substring(3);
118-
// }
119-
120-
// DirectoryInfo info = new DirectoryInfo(absolute);
121-
// for (int i = 0; i < goToParent; ++i)
122-
// {
123-
// if (info.Parent == null)
124-
// break;
125-
126-
// info = info.Parent;
127-
// }
128-
// return Path.Combine(info.FullName, relative);
129-
//}
130-
131117
public string Parent(string path)
132118
{
133119
return string.IsNullOrEmpty(path) ? path : Path.GetDirectoryName(path);

Core.Common.Standard/KY.Core.Common.Standard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>KY.Core</RootNamespace>
66
<AssemblyName>KY.Core.Common</AssemblyName>
7-
<Version>4.16.0</Version>
7+
<Version>4.16.1</Version>
88
<Authors>KY-Programming</Authors>
99
<Company>KY-Programmingp</Company>
1010
<Product>KY.Core</Product>

Core.Common.Standard/Logger/ConsoleTarget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public override void Write(LogEntry entry)
3333
formattedMessage = string.Format(Resources.ConsoleTraceFormat, entry.Timestamp, entry.Message);
3434
}
3535

36-
if (entry.Shortable && formattedMessage.Length >= Console.WindowWidth)
36+
if (entry.Shortable && formattedMessage.Length >= Console.WindowWidth && Console.WindowWidth > 0)
3737
{
3838
formattedMessage = formattedMessage.Substring(0, Console.WindowWidth - 4) + "...";
3939
}
4040

41-
Console.WriteLine(formattedMessage.PadRight(Console.WindowWidth - 1));
41+
Console.WriteLine(Console.WindowWidth > 0 ? formattedMessage.PadRight(Console.WindowWidth - 1) : formattedMessage);
4242
}
4343
catch (IOException)
4444
{

Core.Common.Standard/Nuget/NugetAssemblyLocator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public Assembly Locate(string search, Version defaultVersion = null, bool loadDe
8989
{
9090
assembly = (location.SearchLocal ? this.TryFind(info, location.Path, info.Path) : null)
9191
?? (location.SearchBin ? this.TryFind(info, location.Path, "bin", info.Path) : null)
92-
?? (location.SearchBinDebug ? this.TryFindExtended(info, location.Path, "bin", "debug") : null)
93-
?? (location.SearchBinRelease ? this.TryFindExtended(info, location.Path, "bin", "release") : null);
92+
?? (location.SearchBinDebug ? this.TryFindExtended(info, location.Path, "bin", "Debug") : null)
93+
?? (location.SearchBinRelease ? this.TryFindExtended(info, location.Path, "bin", "Release") : null);
9494
if (assembly != null)
9595
{
9696
if (loadDependencies)
@@ -179,7 +179,7 @@ private Assembly TryFindExtended(AssemblyInfo info, params string[] chunks)
179179
string path = FileSystem.Combine(FileSystem.Combine(chunks));
180180
if (FileSystem.DirectoryExists(path))
181181
{
182-
DirectoryInfo[] directories = FileSystem.GetDirectoryInfos(path, "netcoreapp*").Concat(FileSystem.GetDirectoryInfos(path, "netstandard*")).ToArray();
182+
DirectoryInfo[] directories = FileSystem.GetDirectoryInfos(path, "net*");
183183
return directories.Select(directory => this.TryFind(info, directory.FullName, info.Name)).FirstOrDefault()
184184
?? this.TryFind(info, path, info.Name);
185185
}

0 commit comments

Comments
 (0)