From 25a292ecfa9872813772f47d6c5cf4b3ce6d27ea Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 4 Oct 2016 11:56:48 -0700 Subject: [PATCH] Add DebugHelper to make debugging easier --- .../Internal/DebugHelper.cs | 25 +++++++++++++++++++ .../Program.cs | 4 +++ .../ResolveTagHelpersDispatchCommand.cs | 13 ++++++++++ .../Program.cs | 4 +++ .../Infrastructure/ToolTestFixture.cs | 2 +- 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNetCore.Razor.Design/Internal/DebugHelper.cs diff --git a/src/Microsoft.AspNetCore.Razor.Design/Internal/DebugHelper.cs b/src/Microsoft.AspNetCore.Razor.Design/Internal/DebugHelper.cs new file mode 100644 index 0000000..5f42dbe --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Design/Internal/DebugHelper.cs @@ -0,0 +1,25 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +#if DEBUG +using System; +using System.Diagnostics; +using System.Linq; + +namespace Microsoft.AspNetCore.Razor.Design.Internal +{ + public class DebugHelper + { + public static void HandleDebugSwitch(ref string[] args) + { + if (args.Length > 0 && string.Equals("--debug", args[0], StringComparison.OrdinalIgnoreCase)) + { + args = args.Skip(1).ToArray(); + Console.Error.WriteLine("Waiting for debugger to attach. Press ENTER to continue"); + Console.Error.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}"); + Console.ReadLine(); + } + } + } +} +#endif \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Razor.Design/Program.cs b/src/Microsoft.AspNetCore.Razor.Design/Program.cs index 3106339..3a2e194 100644 --- a/src/Microsoft.AspNetCore.Razor.Design/Program.cs +++ b/src/Microsoft.AspNetCore.Razor.Design/Program.cs @@ -5,6 +5,7 @@ using System; using System.Globalization; using System.Linq; using System.Reflection; +using Microsoft.AspNetCore.Razor.Design.Internal; namespace Microsoft.AspNetCore.Razor.Design { @@ -14,6 +15,9 @@ namespace Microsoft.AspNetCore.Razor.Design public static int Main(string[] args) { +#if DEBUG + DebugHelper.HandleDebugSwitch(ref args); +#endif var app = new RazorToolingApplication(ProgramType); EnsureValidDispatchRecipient(ref args); diff --git a/src/Microsoft.AspNetCore.Razor.Tools/Internal/ResolveTagHelpersDispatchCommand.cs b/src/Microsoft.AspNetCore.Razor.Tools/Internal/ResolveTagHelpersDispatchCommand.cs index 74a7eb6..16a6eab 100644 --- a/src/Microsoft.AspNetCore.Razor.Tools/Internal/ResolveTagHelpersDispatchCommand.cs +++ b/src/Microsoft.AspNetCore.Razor.Tools/Internal/ResolveTagHelpersDispatchCommand.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -86,6 +87,14 @@ namespace Microsoft.AspNetCore.Razor.Tools.Internal dispatchArgs.Add(ProtocolOption.Value()); } +#if DEBUG + var commandLineArgs = Environment.GetCommandLineArgs(); + if (commandLineArgs.Length > 1 && commandLineArgs[1] == "--debug") + { + dispatchArgs.Insert(0, commandLineArgs[1]); + } +#endif + var toolName = typeof(Design.Program).GetTypeInfo().Assembly.GetName().Name; var dispatchCommand = DotnetToolDispatcher.CreateDispatchCommand( dispatchArgs, @@ -99,7 +108,11 @@ namespace Microsoft.AspNetCore.Razor.Tools.Internal using (var errorWriter = new StringWriter()) { var commandExitCode = dispatchCommand +#if DEBUG + .ForwardStdErr(Console.Error) +#else .ForwardStdErr(errorWriter) +#endif .ForwardStdOut() .Execute() .ExitCode; diff --git a/src/Microsoft.AspNetCore.Razor.Tools/Program.cs b/src/Microsoft.AspNetCore.Razor.Tools/Program.cs index 57461ba..06811e1 100644 --- a/src/Microsoft.AspNetCore.Razor.Tools/Program.cs +++ b/src/Microsoft.AspNetCore.Razor.Tools/Program.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Razor.Design; +using Microsoft.AspNetCore.Razor.Design.Internal; using Microsoft.AspNetCore.Razor.Tools.Internal; namespace Microsoft.AspNetCore.Razor.Tools @@ -10,6 +11,9 @@ namespace Microsoft.AspNetCore.Razor.Tools { public static int Main(string[] args) { +#if DEBUG + DebugHelper.HandleDebugSwitch(ref args); +#endif var app = new RazorToolingApplication(typeof(Program)); ResolveTagHelpersCommandBase.Register(app); diff --git a/test/Microsoft.AspNetCore.Razor.Tools.Test/Infrastructure/ToolTestFixture.cs b/test/Microsoft.AspNetCore.Razor.Tools.Test/Infrastructure/ToolTestFixture.cs index 0ecd03a..cb832d7 100644 --- a/test/Microsoft.AspNetCore.Razor.Tools.Test/Infrastructure/ToolTestFixture.cs +++ b/test/Microsoft.AspNetCore.Razor.Tools.Test/Infrastructure/ToolTestFixture.cs @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Razor.Tools.Test.Infrastructure var outputWriter = new StringWriter(); var exitCode = command - .ForwardStdErr(outputWriter) + .ForwardStdErr(Console.Error) .ForwardStdOut(outputWriter) .WorkingDirectory(projectDirectory) .Execute()