Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dotnet): Support for JSII_DEBUG and JSII_RUNTIME #724

Merged
merged 10 commits into from
Aug 26, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ public void OptionalAndVariadicArgumentsTest()
[Fact(DisplayName = Prefix + nameof(JsiiAgent))]
public void JsiiAgent()
{
Assert.Equal("DotNet/" + Environment.Version.ToString(), JsiiAgent_.JsiiAgent);
Assert.Equal("DotNet/" + Environment.Version.ToString() + "/.NETStandard,Version=v2.0/1.0.0.0", JsiiAgent_.JsiiAgent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it take for this to break? Does it need manual action (source change somewhere else)? Or can it change just by getting updated toolkit?

Copy link
Contributor Author

@assyadh assyadh Aug 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An update to the framework that your app is targeting.

There is no 'clean' way to get the running .NET Core version:

https://weblog.west-wind.com/posts/2018/Apr/12/Getting-the-NET-Core-Runtime-Version-in-a-Running-Application

I could just test that it StartsWith("DotNet")?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, were we not going to change to core 2.1 target?

Standard is great, except it makes it look like we support framework - and I don't even really want to give developers that option.

}

[Fact(DisplayName = Prefix + nameof(ReceiveInstanceOfPrivateClass))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Extensions.Logging;

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

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

var runtimePath = Environment.GetEnvironmentVariable(JsiiRuntime);
if (string.IsNullOrWhiteSpace(runtimePath))
runtimePath = jsiiRuntimeProvider.JsiiRuntimePath;

_process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "node",
Arguments = "--max-old-space-size=4096 " + jsiiRuntimeProvider.JsiiRuntimePath,
Arguments = "--max-old-space-size=4096 " + runtimePath,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};

_process.StartInfo.EnvironmentVariables.Add("JSII_AGENT", "DotNet/" + Environment.Version);
var assemblyVersion = GetAssemblyFileVersion();
_process.StartInfo.EnvironmentVariables.Add(JsiiAgent,
string.Format(JsiiAgentVersionString, Environment.Version, assemblyVersion.Item1, assemblyVersion.Item2));

var debug = Environment.GetEnvironmentVariable(JsiiDebug);
if (!string.IsNullOrWhiteSpace(debug) && !_process.StartInfo.EnvironmentVariables.ContainsKey(JsiiDebug))
_process.StartInfo.EnvironmentVariables.Add(JsiiDebug, debug);

_logger.LogDebug("Starting jsii runtime...");
_logger.LogDebug($"{_process.StartInfo.FileName} {_process.StartInfo.Arguments}");
Expand All @@ -48,5 +64,23 @@ void IDisposable.Dispose()
StandardError.Dispose();
_process.Dispose();
}

/// <summary>
/// Gets the target framework attribute value and
/// the assembly file version for the current .NET assembly
/// </summary>
/// <returns>A tuple where Item1 is the target framework
/// ie .NETStandard,Version=v2.0
/// and item2 is the assembly file version (ie 1.0.0.0)</returns>
private Tuple<string, string> GetAssemblyFileVersion()
{
var assembly = typeof(NodeProcess).GetTypeInfo().Assembly;
var assemblyFileVersionAttribute = assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute;
var frameworkAttribute = assembly.GetCustomAttribute(typeof(TargetFrameworkAttribute)) as TargetFrameworkAttribute;
return new Tuple<string, string>(
frameworkAttribute?.FrameworkName ?? "Unknown",
assemblyFileVersionAttribute?.Version ?? "Unknown"
);
}
}
}