11using System ;
22using System . Diagnostics ;
33using System . IO ;
4+ using System . Reflection ;
5+ using System . Runtime . Versioning ;
46using Microsoft . Extensions . Logging ;
57
68namespace 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