From 082e587f15c26ae6a9694390b08dc56c2c5ce1c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 2 Oct 2023 00:00:52 +0200 Subject: [PATCH 1/5] Add HardwareGenerationPath property. --- .../Configuration/HardwareGenerationConfiguration.cs | 3 +++ .../Configuration/IHardwareGenerationConfiguration.cs | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Hastlayer/Hast.Common/Configuration/HardwareGenerationConfiguration.cs b/src/Hastlayer/Hast.Common/Configuration/HardwareGenerationConfiguration.cs index 6af17f70..c835bc00 100644 --- a/src/Hastlayer/Hast.Common/Configuration/HardwareGenerationConfiguration.cs +++ b/src/Hastlayer/Hast.Common/Configuration/HardwareGenerationConfiguration.cs @@ -55,6 +55,9 @@ public class HardwareGenerationConfiguration : IHardwareGenerationConfiguration /// public string DeviceName { get; } + /// + public string HardwareGenerationPath { get; set; } + /// public string HardwareFrameworkPath { get; } diff --git a/src/Hastlayer/Hast.Common/Configuration/IHardwareGenerationConfiguration.cs b/src/Hastlayer/Hast.Common/Configuration/IHardwareGenerationConfiguration.cs index 0d93fc72..ded67038 100644 --- a/src/Hastlayer/Hast.Common/Configuration/IHardwareGenerationConfiguration.cs +++ b/src/Hastlayer/Hast.Common/Configuration/IHardwareGenerationConfiguration.cs @@ -61,10 +61,18 @@ public interface IHardwareGenerationConfiguration /// string DeviceName { get; } + /// + /// Gets the file system path that's temporarily set as the working directory when + /// IHastlayer.GenerateHardwareAsync() is executed. If it's left then the current + /// executing assembly's location is used. + /// + string HardwareGenerationPath { get; } + /// /// Gets the file system path here where the hardware framework is located. The file describing the hardware to be /// generated will be saved there as well as anything else necessary, and that framework will be used to implement - /// the hardware and configure the device. + /// the hardware and configure the device. If it's a relative path, it will be resolved inside the . /// string HardwareFrameworkPath { get; } From bc6b6662ba94dec41ba13c2f8ada615de3c9291c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 2 Oct 2023 00:20:04 +0200 Subject: [PATCH 2/5] Set workingDirectory during hardware generation. --- .../Hast.Common/Services/DirectoryScope.cs | 36 +++++++++++++++++++ src/Hastlayer/Hast.Layer/Hastlayer.cs | 2 ++ 2 files changed, 38 insertions(+) create mode 100644 src/Hastlayer/Hast.Common/Services/DirectoryScope.cs diff --git a/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs b/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs new file mode 100644 index 00000000..ff748702 --- /dev/null +++ b/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; + +namespace Hast.Common.Services; + +/// +/// Stores the current working directory, moves into a different location and then returns to the original location when +/// the instance is disposed. +/// +public class DirectoryScope : IDisposable +{ + private readonly string _oldPath; + private bool _disposed; + + public DirectoryScope(string path) + { + _oldPath = Environment.CurrentDirectory; + Environment.CurrentDirectory = path; + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!_disposed && Directory.Exists(_oldPath)) + { + Environment.CurrentDirectory = _oldPath; + } + + _disposed = true; + } +} diff --git a/src/Hastlayer/Hast.Layer/Hastlayer.cs b/src/Hastlayer/Hast.Layer/Hastlayer.cs index d621a05f..864a6b65 100644 --- a/src/Hastlayer/Hast.Layer/Hastlayer.cs +++ b/src/Hastlayer/Hast.Layer/Hastlayer.cs @@ -144,6 +144,8 @@ public Task GenerateHardwareAsync( IEnumerable assemblyPaths, IHardwareGenerationConfiguration configuration) { + using var workingDirectory = new DirectoryScope(configuration.HardwareGenerationPath ?? AppDataFolder.AssemblyDirectory); + // Avoid repeated multiple enumerations. var assembliesPaths = assemblyPaths.AsList(); From f9234c2df1d5f2d12da62c5515d5d6d4e8d26b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 2 Oct 2023 01:01:27 +0200 Subject: [PATCH 3/5] Update nuget version. --- NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj | 4 ++-- .../Hast.Samples.SampleAssembly.csproj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj b/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj index 060aea24..80198c69 100644 --- a/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj +++ b/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj b/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj index 3dd3be9d..3c92f00f 100644 --- a/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj +++ b/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj @@ -12,8 +12,8 @@ - - + + From 2f2fb57f6379c8176bc56049febdb5f208c5c7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 2 Oct 2023 01:01:49 +0200 Subject: [PATCH 4/5] Also change directory during Hastlayer.Create(). --- src/Hastlayer/Hast.Common/Services/DirectoryScope.cs | 4 ++-- src/Hastlayer/Hast.Layer/Hastlayer.cs | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs b/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs index ff748702..28afd10c 100644 --- a/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs +++ b/src/Hastlayer/Hast.Common/Services/DirectoryScope.cs @@ -15,7 +15,7 @@ public class DirectoryScope : IDisposable public DirectoryScope(string path) { _oldPath = Environment.CurrentDirectory; - Environment.CurrentDirectory = path; + Directory.SetCurrentDirectory(string.IsNullOrWhiteSpace(path) ? "." : path.Trim()); } public void Dispose() @@ -28,7 +28,7 @@ protected virtual void Dispose(bool disposing) { if (!_disposed && Directory.Exists(_oldPath)) { - Environment.CurrentDirectory = _oldPath; + Directory.SetCurrentDirectory(_oldPath); } _disposed = true; diff --git a/src/Hastlayer/Hast.Layer/Hastlayer.cs b/src/Hastlayer/Hast.Layer/Hastlayer.cs index 864a6b65..1883067d 100644 --- a/src/Hastlayer/Hast.Layer/Hastlayer.cs +++ b/src/Hastlayer/Hast.Layer/Hastlayer.cs @@ -119,12 +119,16 @@ public static void ConfigureLogging(IServiceCollection services, Action /// /// Configuration for Hastlayer. + /// + /// If , the executing assembly's directory is used instead of the current working directory. + /// /// A newly created instance. - public static Hastlayer Create(IHastlayerConfiguration configuration) + public static Hastlayer Create(IHastlayerConfiguration configuration, bool inAssemblyDirectory = true) { Argument.ThrowIfNull(configuration, nameof(configuration)); Argument.ThrowIfNull(configuration.Extensions, nameof(configuration.Extensions)); + using var workingDirectory = new DirectoryScope(inAssemblyDirectory ? AppDataFolder.AssemblyDirectory : "."); var hastlayer = new Hastlayer(configuration); hastlayer.LoadHost(); return hastlayer; From f73329a31d2c797722944a5a9ac9d188b485b32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 2 Oct 2023 02:19:17 +0200 Subject: [PATCH 5/5] update package versions. --- NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj | 4 ++-- .../Hast.Samples.SampleAssembly.csproj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj b/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj index 80198c69..aca8bd51 100644 --- a/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj +++ b/NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj b/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj index 3c92f00f..d0a00130 100644 --- a/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj +++ b/src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj @@ -12,8 +12,8 @@ - - + +