Skip to content

Commit

Permalink
Linux/OSX compatibility improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ky-one committed Nov 14, 2020
1 parent cd9b202 commit 6fe910a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
52 changes: 19 additions & 33 deletions Core.Common.Standard/DataAccess/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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);
}
Expand All @@ -69,24 +75,24 @@ public string Combine(params string[] pathChunks)
{
return pathChunks.FirstOrDefault();
}
bool isDrive = safePathChunks.First().Contains(DriveSymbol);
bool isDrive = safePathChunks.First().Contains(driveSymbol);
List<string> parts = new List<string>();
foreach (string pathChunk in safePathChunks.Select(this.Format))
{
string[] chunks = pathChunk.Split(Path.DirectorySeparatorChar);
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);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Core.Common.Standard/KY.Core.Common.Standard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>KY.Core</RootNamespace>
<AssemblyName>KY.Core.Common</AssemblyName>
<Version>4.16.0</Version>
<Version>4.16.1</Version>
<Authors>KY-Programming</Authors>
<Company>KY-Programmingp</Company>
<Product>KY.Core</Product>
Expand Down
4 changes: 2 additions & 2 deletions Core.Common.Standard/Logger/ConsoleTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 3 additions & 3 deletions Core.Common.Standard/Nuget/NugetAssemblyLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 6fe910a

Please sign in to comment.