Skip to content

Commit 1816740

Browse files
Hamza Assyadmergify[bot]
authored andcommitted
feat(dotnet): Support for JSII_DEBUG and JSII_RUNTIME (#724)
* Miscellaneous .NET runtime changes - support for JSII_DEBUG and JSII_RUNTIME * Do not overwrite JSII_DEBUG when already passed by tests * JSII_AGENT string enhancement with the assembly file version * Fix integration test * Fixing some indentation - thanks Rider * Refactoring a return statement on NodeProccess.cs
1 parent 810dd4a commit 1816740

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

packages/jsii-dotnet-runtime-test/test/Amazon.JSII.Runtime.IntegrationTests/ComplianceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ public void OptionalAndVariadicArgumentsTest()
928928
[Fact(DisplayName = Prefix + nameof(JsiiAgent))]
929929
public void JsiiAgent()
930930
{
931-
Assert.Equal("DotNet/" + Environment.Version.ToString(), JsiiAgent_.JsiiAgent);
931+
Assert.Equal("DotNet/" + Environment.Version.ToString() + "/.NETStandard,Version=v2.0/1.0.0.0", JsiiAgent_.JsiiAgent);
932932
}
933933

934934
[Fact(DisplayName = Prefix + nameof(ReceiveInstanceOfPrivateClass))]

packages/jsii-dotnet-runtime/src/Amazon.JSII.Runtime/Services/NodeProcess.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Reflection;
5+
using System.Runtime.Versioning;
46
using Microsoft.Extensions.Logging;
57

68
namespace Amazon.JSII.Runtime.Services
@@ -9,25 +11,39 @@ public class NodeProcess : INodeProcess
911
{
1012
readonly Process _process;
1113
readonly ILogger _logger;
14+
private const string JsiiRuntime = "JSII_RUNTIME";
15+
private const string JsiiDebug = "JSII_DEBUG";
16+
private const string JsiiAgent = "JSII_AGENT";
17+
private const string JsiiAgentVersionString = "DotNet/{0}/{1}/{2}";
1218

1319
public NodeProcess(IJsiiRuntimeProvider jsiiRuntimeProvider, ILoggerFactory loggerFactory)
1420
{
1521
loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
1622
_logger = loggerFactory.CreateLogger<NodeProcess>();
1723

24+
var runtimePath = Environment.GetEnvironmentVariable(JsiiRuntime);
25+
if (string.IsNullOrWhiteSpace(runtimePath))
26+
runtimePath = jsiiRuntimeProvider.JsiiRuntimePath;
27+
1828
_process = new Process
1929
{
2030
StartInfo = new ProcessStartInfo
2131
{
2232
FileName = "node",
23-
Arguments = "--max-old-space-size=4096 " + jsiiRuntimeProvider.JsiiRuntimePath,
33+
Arguments = "--max-old-space-size=4096 " + runtimePath,
2434
RedirectStandardInput = true,
2535
RedirectStandardOutput = true,
2636
RedirectStandardError = true
2737
}
2838
};
2939

30-
_process.StartInfo.EnvironmentVariables.Add("JSII_AGENT", "DotNet/" + Environment.Version);
40+
var assemblyVersion = GetAssemblyFileVersion();
41+
_process.StartInfo.EnvironmentVariables.Add(JsiiAgent,
42+
string.Format(JsiiAgentVersionString, Environment.Version, assemblyVersion.Item1, assemblyVersion.Item2));
43+
44+
var debug = Environment.GetEnvironmentVariable(JsiiDebug);
45+
if (!string.IsNullOrWhiteSpace(debug) && !_process.StartInfo.EnvironmentVariables.ContainsKey(JsiiDebug))
46+
_process.StartInfo.EnvironmentVariables.Add(JsiiDebug, debug);
3147

3248
_logger.LogDebug("Starting jsii runtime...");
3349
_logger.LogDebug($"{_process.StartInfo.FileName} {_process.StartInfo.Arguments}");
@@ -48,5 +64,23 @@ void IDisposable.Dispose()
4864
StandardError.Dispose();
4965
_process.Dispose();
5066
}
67+
68+
/// <summary>
69+
/// Gets the target framework attribute value and
70+
/// the assembly file version for the current .NET assembly
71+
/// </summary>
72+
/// <returns>A tuple where Item1 is the target framework
73+
/// ie .NETStandard,Version=v2.0
74+
/// and item2 is the assembly file version (ie 1.0.0.0)</returns>
75+
private Tuple<string, string> GetAssemblyFileVersion()
76+
{
77+
var assembly = typeof(NodeProcess).GetTypeInfo().Assembly;
78+
var assemblyFileVersionAttribute = assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute;
79+
var frameworkAttribute = assembly.GetCustomAttribute(typeof(TargetFrameworkAttribute)) as TargetFrameworkAttribute;
80+
return new Tuple<string, string>(
81+
frameworkAttribute?.FrameworkName ?? "Unknown",
82+
assemblyFileVersionAttribute?.Version ?? "Unknown"
83+
);
84+
}
5185
}
5286
}

0 commit comments

Comments
 (0)