Skip to content

Commit

Permalink
Merge pull request #115 from Lombiq/issue/HAST-325
Browse files Browse the repository at this point in the history
HAST-325: Exception when running Hastlayer from VS Code
  • Loading branch information
Piedone authored Oct 2, 2023
2 parents 222aa37 + f73329a commit 569cff1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions NuGetTest/Hast.NuGet.Console/Hast.NuGet.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Hast.Algorithms" Version="2.1.1-alpha.0.hast-314" />
<PackageReference Include="Hast.Layer" Version="2.1.1-alpha.0.hast-314" />
<PackageReference Include="Hast.Algorithms" Version="2.1.1-alpha.2.hast-325" />
<PackageReference Include="Hast.Layer" Version="2.1.1-alpha.2.hast-325" />
<PackageReference Include="Hast.Vitis.HardwareFramework" Version="1.2.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class HardwareGenerationConfiguration : IHardwareGenerationConfiguration
/// <inheritdoc/>
public string DeviceName { get; }

/// <inheritdoc/>
public string HardwareGenerationPath { get; set; }

/// <inheritdoc/>
public string HardwareFrameworkPath { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ public interface IHardwareGenerationConfiguration
/// </summary>
string DeviceName { get; }

/// <summary>
/// Gets the file system path that's temporarily set as the working directory when
/// <c>IHastlayer.GenerateHardwareAsync()</c> is executed. If it's left <see langword="null"/> then the current
/// executing assembly's location is used.
/// </summary>
string HardwareGenerationPath { get; }

/// <summary>
/// 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 <see
/// cref="HardwareGenerationPath"/>.
/// </summary>
string HardwareFrameworkPath { get; }

Expand Down
36 changes: 36 additions & 0 deletions src/Hastlayer/Hast.Common/Services/DirectoryScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.IO;

namespace Hast.Common.Services;

/// <summary>
/// Stores the current working directory, moves into a different location and then returns to the original location when
/// the instance is disposed.
/// </summary>
public class DirectoryScope : IDisposable
{
private readonly string _oldPath;
private bool _disposed;

public DirectoryScope(string path)
{
_oldPath = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(string.IsNullOrWhiteSpace(path) ? "." : path.Trim());
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed && Directory.Exists(_oldPath))
{
Directory.SetCurrentDirectory(_oldPath);
}

_disposed = true;
}
}
8 changes: 7 additions & 1 deletion src/Hastlayer/Hast.Layer/Hastlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,16 @@ public static void ConfigureLogging(IServiceCollection services, Action<ILogging
/// </para>
/// </remarks>
/// <param name="configuration">Configuration for Hastlayer.</param>
/// <param name="inAssemblyDirectory">
/// If <see langword="true"/>, the executing assembly's directory is used instead of the current working directory.
/// </param>
/// <returns>A newly created <see cref="Hastlayer"/> instance.</returns>
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;
Expand All @@ -144,6 +148,8 @@ public Task<IHardwareRepresentation> GenerateHardwareAsync(
IEnumerable<string> assemblyPaths,
IHardwareGenerationConfiguration configuration)
{
using var workingDirectory = new DirectoryScope(configuration.HardwareGenerationPath ?? AppDataFolder.AssemblyDirectory);

// Avoid repeated multiple enumerations.
var assembliesPaths = assemblyPaths.AsList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
</ItemGroup>

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

Expand Down

0 comments on commit 569cff1

Please sign in to comment.