Skip to content

Commit 569cff1

Browse files
authored
Merge pull request #115 from Lombiq/issue/HAST-325
HAST-325: Exception when running Hastlayer from VS Code
2 parents 222aa37 + f73329a commit 569cff1

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Hast.Algorithms" Version="2.1.1-alpha.0.hast-314" />
12-
<PackageReference Include="Hast.Layer" Version="2.1.1-alpha.0.hast-314" />
11+
<PackageReference Include="Hast.Algorithms" Version="2.1.1-alpha.2.hast-325" />
12+
<PackageReference Include="Hast.Layer" Version="2.1.1-alpha.2.hast-325" />
1313
<PackageReference Include="Hast.Vitis.HardwareFramework" Version="1.2.0" />
1414
</ItemGroup>
1515

src/Hastlayer/Hast.Common/Configuration/HardwareGenerationConfiguration.cs

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class HardwareGenerationConfiguration : IHardwareGenerationConfiguration
5555
/// <inheritdoc/>
5656
public string DeviceName { get; }
5757

58+
/// <inheritdoc/>
59+
public string HardwareGenerationPath { get; set; }
60+
5861
/// <inheritdoc/>
5962
public string HardwareFrameworkPath { get; }
6063

src/Hastlayer/Hast.Common/Configuration/IHardwareGenerationConfiguration.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,18 @@ public interface IHardwareGenerationConfiguration
6161
/// </summary>
6262
string DeviceName { get; }
6363

64+
/// <summary>
65+
/// Gets the file system path that's temporarily set as the working directory when
66+
/// <c>IHastlayer.GenerateHardwareAsync()</c> is executed. If it's left <see langword="null"/> then the current
67+
/// executing assembly's location is used.
68+
/// </summary>
69+
string HardwareGenerationPath { get; }
70+
6471
/// <summary>
6572
/// Gets the file system path here where the hardware framework is located. The file describing the hardware to be
6673
/// generated will be saved there as well as anything else necessary, and that framework will be used to implement
67-
/// the hardware and configure the device.
74+
/// the hardware and configure the device. If it's a relative path, it will be resolved inside the <see
75+
/// cref="HardwareGenerationPath"/>.
6876
/// </summary>
6977
string HardwareFrameworkPath { get; }
7078

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Hast.Common.Services;
5+
6+
/// <summary>
7+
/// Stores the current working directory, moves into a different location and then returns to the original location when
8+
/// the instance is disposed.
9+
/// </summary>
10+
public class DirectoryScope : IDisposable
11+
{
12+
private readonly string _oldPath;
13+
private bool _disposed;
14+
15+
public DirectoryScope(string path)
16+
{
17+
_oldPath = Environment.CurrentDirectory;
18+
Directory.SetCurrentDirectory(string.IsNullOrWhiteSpace(path) ? "." : path.Trim());
19+
}
20+
21+
public void Dispose()
22+
{
23+
Dispose(disposing: true);
24+
GC.SuppressFinalize(this);
25+
}
26+
27+
protected virtual void Dispose(bool disposing)
28+
{
29+
if (!_disposed && Directory.Exists(_oldPath))
30+
{
31+
Directory.SetCurrentDirectory(_oldPath);
32+
}
33+
34+
_disposed = true;
35+
}
36+
}

src/Hastlayer/Hast.Layer/Hastlayer.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,16 @@ public static void ConfigureLogging(IServiceCollection services, Action<ILogging
119119
/// </para>
120120
/// </remarks>
121121
/// <param name="configuration">Configuration for Hastlayer.</param>
122+
/// <param name="inAssemblyDirectory">
123+
/// If <see langword="true"/>, the executing assembly's directory is used instead of the current working directory.
124+
/// </param>
122125
/// <returns>A newly created <see cref="Hastlayer"/> instance.</returns>
123-
public static Hastlayer Create(IHastlayerConfiguration configuration)
126+
public static Hastlayer Create(IHastlayerConfiguration configuration, bool inAssemblyDirectory = true)
124127
{
125128
Argument.ThrowIfNull(configuration, nameof(configuration));
126129
Argument.ThrowIfNull(configuration.Extensions, nameof(configuration.Extensions));
127130

131+
using var workingDirectory = new DirectoryScope(inAssemblyDirectory ? AppDataFolder.AssemblyDirectory : ".");
128132
var hastlayer = new Hastlayer(configuration);
129133
hastlayer.LoadHost();
130134
return hastlayer;
@@ -144,6 +148,8 @@ public Task<IHardwareRepresentation> GenerateHardwareAsync(
144148
IEnumerable<string> assemblyPaths,
145149
IHardwareGenerationConfiguration configuration)
146150
{
151+
using var workingDirectory = new DirectoryScope(configuration.HardwareGenerationPath ?? AppDataFolder.AssemblyDirectory);
152+
147153
// Avoid repeated multiple enumerations.
148154
var assembliesPaths = assemblyPaths.AsList();
149155

src/Samples/Hast.Samples.SampleAssembly/Hast.Samples.SampleAssembly.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
</ItemGroup>
1313

1414
<ItemGroup Condition="$(SolutionName) == 'Hastlayer.SDK.NuGet'">
15-
<PackageReference Include="Hast.Algorithms" Version="2.1.1-alpha.0.hast-314" />
16-
<PackageReference Include="Hast.Layer" Version="2.1.1-alpha.0.hast-314" />
15+
<PackageReference Include="Hast.Algorithms" Version="2.1.1-alpha.2.hast-325" />
16+
<PackageReference Include="Hast.Layer" Version="2.1.1-alpha.2.hast-325" />
1717
<PackageReference Include="Lombiq.Arithmetics" Version="0.0.1-alpha.3.hast-175" />
1818
</ItemGroup>
1919

0 commit comments

Comments
 (0)