Skip to content

Commit

Permalink
Pass args to TS generator tool using response file (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Nov 11, 2023
1 parent 248214d commit 1ceb048
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 103 deletions.
87 changes: 87 additions & 0 deletions src/NodeApi.DotNetHost/DebugHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.Threading;

namespace Microsoft.JavaScript.NodeApi;

// Only checking the environment variable for debugging.
#pragma warning disable RS1035 // The symbol 'Environment' is banned for use by analyzers.

internal class DebugHelper
{
[Conditional("DEBUG")]
public static void AttachDebugger(string environmentVariableName)
{
string? debugValue = Environment.GetEnvironmentVariable(environmentVariableName);
if (string.Equals(debugValue, "VS", StringComparison.OrdinalIgnoreCase))
{
// Launch the Visual Studio debugger.
Debugger.Launch();
}
else if (!string.IsNullOrEmpty(debugValue))
{
Process currentProcess = Process.GetCurrentProcess();
string processName = currentProcess.ProcessName;
int processId = currentProcess.Id;
Console.WriteLine("###################### DEBUG ######################");

int waitSeconds = 20;
string waitingMessage = string.Empty;
if (Console.IsOutputRedirected)
{
Console.WriteLine(
$"Process \"{processName}\" ({processId}) is " +
$"waiting {waitSeconds} seconds for debugger.");
}
else
{
Console.WriteLine(
$"Process \"{processName}\" ({processId}) is waiting for debugger.");
waitingMessage = "Press any key to continue without debugging... ";
Console.Write(waitingMessage + $"({waitSeconds})");
}

Stopwatch stopwatch = Stopwatch.StartNew();
int remainingSeconds = waitSeconds;
while (!Debugger.IsAttached)
{
if (!Console.IsOutputRedirected && Console.KeyAvailable)
{
Console.ReadKey(true);
Console.WriteLine();
return;
}
else if (stopwatch.Elapsed > TimeSpan.FromSeconds(waitSeconds))
{
Console.WriteLine(
$"Debugger did not attach after {waitSeconds} seconds. Continuing.");
return;
}

Thread.Sleep(100);

if (remainingSeconds > waitSeconds - (int)stopwatch.Elapsed.TotalSeconds)
{
remainingSeconds = waitSeconds - (int)stopwatch.Elapsed.TotalSeconds;

if (!Console.IsOutputRedirected)
{
Console.CursorLeft = waitingMessage.Length;
Console.Write($"({remainingSeconds:D2})");
}
}
}

if (!Console.IsOutputRedirected)
{
Console.CursorLeft = waitingMessage.Length;
Console.WriteLine(" ");
}

Debugger.Break();
}
}
}
69 changes: 1 addition & 68 deletions src/NodeApi.DotNetHost/ManagedHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.JavaScript.NodeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtime;
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
Expand Down Expand Up @@ -210,7 +209,7 @@ public static napi_value InitializeModule(napi_env env, napi_value exports)
Trace($" .NET Runtime version: {Environment.Version}");
#endif

AttachDebugger();
DebugHelper.AttachDebugger("DEBUG_NODE_API_RUNTIME");

JSRuntime runtime = new NodejsRuntime();

Expand Down Expand Up @@ -638,70 +637,4 @@ public override void TraceEvent(
params object?[]? args)
=> WriteLine(string.Format(format ?? string.Empty, args ?? []));
}

[Conditional("DEBUG")]
private static void AttachDebugger()
{
string? debugValue = Environment.GetEnvironmentVariable("DEBUG_NODE_API_RUNTIME");
if (string.Equals(debugValue, "VS", StringComparison.OrdinalIgnoreCase))
{
// Launch the Visual Studio debugger.
Debugger.Launch();
}
else if (!string.IsNullOrEmpty(debugValue))
{
Process currentProcess = Process.GetCurrentProcess();
string processName = currentProcess.ProcessName;
int processId = currentProcess.Id;
Console.WriteLine("###################### DEBUG ######################");
Console.WriteLine($"Process \"{processName}\" ({processId}) is waiting for debugger.");

int waitSeconds = 20;
string waitingMessage = "Press any key to continue without debugging... ";

if (!Console.IsOutputRedirected)
{
Console.Write(waitingMessage + $"({waitSeconds})");
}

Stopwatch stopwatch = Stopwatch.StartNew();
int remainingSeconds = waitSeconds;
while (!Debugger.IsAttached)
{
if (Console.KeyAvailable)
{
Console.ReadKey(true);
Console.WriteLine();
return;
}
else if (stopwatch.Elapsed > TimeSpan.FromSeconds(waitSeconds))
{
Console.WriteLine(
$"Debugger did not attach after {waitSeconds} seconds. Continuing.");
return;
}

Thread.Sleep(100);

if (remainingSeconds > waitSeconds - (int)stopwatch.Elapsed.TotalSeconds)
{
remainingSeconds = waitSeconds - (int)stopwatch.Elapsed.TotalSeconds;

if (!Console.IsOutputRedirected)
{
Console.CursorLeft = waitingMessage.Length;
Console.Write($"({remainingSeconds:D2})");
}
}
}

if (!Console.IsOutputRedirected)
{
Console.CursorLeft = waitingMessage.Length;
Console.WriteLine(" ");
}

Debugger.Break();
}
}
}
12 changes: 2 additions & 10 deletions src/NodeApi.Generator/ModuleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,10 @@ public class ModuleGenerator : SourceGenerator, ISourceGenerator

public void Initialize(GeneratorInitializationContext context)
{
#if DEBUG
#pragma warning disable RS1035 // The symbol 'Environment' is banned for use by analyzers.
// Note source generators are not covered by normal debugging,
// Note source generators cannot be directly launched in a debugger,
// because the generator runs at build time, not at application run-time.
// Set the environment variable to trigger debugging at build time.

if (Environment.GetEnvironmentVariable("DEBUG_NODE_API_GENERATOR") != null)
{
System.Diagnostics.Debugger.Launch();
}
#pragma warning restore RS1035
#endif
DebugHelper.AttachDebugger("DEBUG_NODE_API_GENERATOR");
}

public void Execute(GeneratorExecutionContext context)
Expand Down
4 changes: 4 additions & 0 deletions src/NodeApi.Generator/NodeApi.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<SelfContained>false</SelfContained>
</PropertyGroup>

<ItemGroup>
<Compile Include="../NodeApi.DotNetHost/DebugHelper.cs" Link="DebugHelper.cs" />
</ItemGroup>

<ItemGroup>
<!-- Package the generator and dependencies in the analyzer directory of the nuget package -->
<!-- Use the .NET 6 targeted assembly as the analyzer for broader compatibility. (There's no difference in functionality.) -->
Expand Down
30 changes: 28 additions & 2 deletions src/NodeApi.Generator/NodeApi.Generator.targets
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@
Outputs="$(TargetDir)$(NodeApiTypeDefinitionsFileName)"
Condition=" '$(GenerateNodeApiTypeDefinitions)' == 'true' AND Exists('$(TargetPath)') "
>
<Exec Command="dotnet &quot;$(NodeApiGeneratorAssemblyPath)&quot; --assembly &quot;$(TargetPath)&quot; --packs &quot;@(TargetingPack)&quot; --reference &quot;@(ReferencePathWithRefAssemblies)&quot; --typedefs &quot;$(TargetDir)$(NodeApiTypeDefinitionsFileName)&quot; $(NodeApiTypeDefinitionsGeneratorOptions)"
<PropertyGroup>
<NodeApiGeneratorResponseFile>$(IntermediateOutputPath)$(NodeApiGeneratorAssemblyName).rsp</NodeApiGeneratorResponseFile>
<_NodeApiGeneratorTargetingPacks>@(TargetingPack, '%3B')</_NodeApiGeneratorTargetingPacks>
<_NodeApiGeneratorAssemblyReferences>@(ReferencePathWithRefAssemblies, '%3B')</_NodeApiGeneratorAssemblyReferences>
</PropertyGroup>

<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--assembly &quot;$(TargetPath)&quot;" Overwrite="true" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--packs &quot;$(_NodeApiGeneratorTargetingPacks)&quot;" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--reference &quot;$(_NodeApiGeneratorAssemblyReferences)&quot;" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--typedefs &quot;$(TargetDir)$(NodeApiTypeDefinitionsFileName)&quot;" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="$(NodeApiTypeDefinitionsGeneratorOptions)" />

<!-- Run the generator using args from the response file. Note the '@' indicates the response file NOT an MSBuild item-list. -->
<Exec Command="dotnet &quot;$(NodeApiGeneratorAssemblyPath)&quot; &quot;@$(NodeApiGeneratorResponseFile)&quot;"
ConsoleToMSBuild="true" />
</Target>

Expand Down Expand Up @@ -101,7 +114,20 @@
<_NodeApiAllTypeDefs Include="@(NodeApiSystemReferenceAssembly->'$(TargetDir)%(Identity).d.ts')" />
</ItemGroup>

<Exec Command="dotnet &quot;$(NodeApiGeneratorAssemblyPath)&quot; $(_SuppressTSGenerationWarnings) --assemblies &quot;@(_NodeApiAllReferenceAssemblies)&quot; --packs &quot;@(TargetingPack)&quot; --typedefs &quot;@(_NodeApiAllTypeDefs)&quot; $(NodeApiTypeDefinitionsGeneratorOptions)"
<PropertyGroup>
<NodeApiGeneratorResponseFile>$(IntermediateOutputPath)$(NodeApiGeneratorAssemblyName).rsp</NodeApiGeneratorResponseFile>
<_NodeApiGeneratorTargetingPacks>@(TargetingPack, '%3B')</_NodeApiGeneratorTargetingPacks>
<_NodeApiGeneratorAssemblyReferences>@(_NodeApiAllReferenceAssemblies, '%3B')</_NodeApiGeneratorAssemblyReferences>
<_NodeApiGeneratorTypeDefs>@(_NodeApiAllTypeDefs, '%3B')</_NodeApiGeneratorTypeDefs>
</PropertyGroup>

<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--assemblies &quot;$(_NodeApiGeneratorAssemblyReferences)&quot;" Overwrite="true" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--packs &quot;$(_NodeApiGeneratorTargetingPacks)&quot;" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--typedefs &quot;$(_NodeApiGeneratorTypeDefs)&quot;" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="$(NodeApiTypeDefinitionsGeneratorOptions)" />

<!-- Run the generator using args from the response file. Note the '@' indicates the response file NOT an MSBuild item-list. -->
<Exec Command="dotnet &quot;$(NodeApiGeneratorAssemblyPath)&quot; &quot;@$(NodeApiGeneratorResponseFile)&quot;"
ConsoleToMSBuild="true" />
</Target>

Expand Down
Loading

0 comments on commit 1ceb048

Please sign in to comment.