From 7ecdd99de5b46ee1a38e8bab743d73dfb94c2e61 Mon Sep 17 00:00:00 2001 From: Ionite Date: Wed, 12 Jul 2023 18:24:24 -0400 Subject: [PATCH] Add symlink handling for unix --- StabilityMatrix.Core/Helper/Compat.cs | 7 +++++++ StabilityMatrix.Core/Helper/SharedFolders.cs | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/StabilityMatrix.Core/Helper/Compat.cs b/StabilityMatrix.Core/Helper/Compat.cs index a165f0de..2bd233f1 100644 --- a/StabilityMatrix.Core/Helper/Compat.cs +++ b/StabilityMatrix.Core/Helper/Compat.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; +using System.Runtime.Versioning; using StabilityMatrix.Core.Models.FileInterfaces; namespace StabilityMatrix.Core.Helper; @@ -15,8 +16,14 @@ public static class Compat // OS Platform public static PlatformKind Platform { get; } + + [SupportedOSPlatformGuard("Windows")] public static bool IsWindows => Platform.HasFlag(PlatformKind.Windows); + + [SupportedOSPlatformGuard("Linux")] public static bool IsLinux => Platform.HasFlag(PlatformKind.Linux); + + [SupportedOSPlatformGuard("macOS")] public static bool IsMacOS => Platform.HasFlag(PlatformKind.MacOS); public static bool IsUnix => Platform.HasFlag(PlatformKind.Unix); diff --git a/StabilityMatrix.Core/Helper/SharedFolders.cs b/StabilityMatrix.Core/Helper/SharedFolders.cs index 0edd2464..d8529aa7 100644 --- a/StabilityMatrix.Core/Helper/SharedFolders.cs +++ b/StabilityMatrix.Core/Helper/SharedFolders.cs @@ -20,6 +20,19 @@ public class SharedFolders : ISharedFolders this.settingsManager = settingsManager; this.packageFactory = packageFactory; } + + // Platform redirect for junctions / symlinks + private static void CreateLinkOrJunction(string junctionDir, string targetDir, bool overwrite) + { + if (Compat.IsWindows) + { + Junction.Create(junctionDir, targetDir, overwrite); + } + else + { + Directory.CreateSymbolicLink(junctionDir, targetDir); + } + } public static void SetupLinks(Dictionary definitions, DirectoryPath modelsDirectory, DirectoryPath installDirectory) @@ -55,7 +68,7 @@ public class SharedFolders : ISharedFolders destinationDir.Delete(true); } Logger.Info($"Creating junction link from {sourceDir} to {destinationDir}"); - Junction.Create(destinationDir, sourceDir, true); + CreateLinkOrJunction(destinationDir, sourceDir, true); } } @@ -92,7 +105,7 @@ public class SharedFolders : ISharedFolders Directory.Delete(destination, false); } Logger.Info($"Updating junction link from {source} to {destination}"); - Junction.Create(destination, source, true); + CreateLinkOrJunction(destination, source, true); } }