Skip to content

Commit a5d3a97

Browse files
committed
Make BuildCommandFromArguments() reusable
1 parent bd7b7a9 commit a5d3a97

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ConfigurationDoneHandler.cs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using Microsoft.Extensions.Logging;
@@ -13,10 +13,8 @@
1313
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
1414
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
1515
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
16-
using System.Collections.Generic;
1716
using System.Management.Automation;
1817
using System.Management.Automation.Language;
19-
using System.Text;
2018
using System.Threading;
2119
using System.Threading.Tasks;
2220

@@ -140,38 +138,13 @@ await _executionService
140138
{
141139
await _executionService
142140
.ExecutePSCommandAsync(
143-
BuildPSCommandFromArguments(scriptToLaunch, _debugStateService.Arguments),
141+
PSCommandHelpers.BuildCommandFromArguments(scriptToLaunch, _debugStateService.Arguments),
144142
CancellationToken.None,
145143
s_debuggerExecutionOptions)
146144
.ConfigureAwait(false);
147145
}
148146

149147
_debugAdapterServer.SendNotification(EventNames.Terminated);
150148
}
151-
152-
private static PSCommand BuildPSCommandFromArguments(string command, IReadOnlyList<string> arguments)
153-
{
154-
if (arguments is null or { Count: 0 })
155-
{
156-
return new PSCommand().AddCommand(command);
157-
}
158-
159-
// HACK: We use AddScript instead of AddArgument/AddParameter to reuse Powershell parameter binding logic.
160-
// We quote the command parameter so that expressions can still be used in the arguments.
161-
var sb = new StringBuilder()
162-
.Append('&')
163-
.Append('"')
164-
.Append(command)
165-
.Append('"');
166-
167-
foreach (string arg in arguments)
168-
{
169-
sb
170-
.Append(' ')
171-
.Append(ArgumentEscaping.Escape(arg));
172-
}
173-
174-
return new PSCommand().AddScript(sb.ToString());
175-
}
176149
}
177150
}

src/PowerShellEditorServices/Utility/PSCommandExtensions.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq.Expressions;
78
using System.Management.Automation;
@@ -11,11 +12,11 @@
1112

1213
namespace Microsoft.PowerShell.EditorServices.Utility
1314
{
14-
internal static class PSCommandExtensions
15+
internal static class PSCommandHelpers
1516
{
1617
private static readonly Func<CommandInfo, Command> s_commandCtor;
1718

18-
static PSCommandExtensions()
19+
static PSCommandHelpers()
1920
{
2021
var ctor = typeof(Command).GetConstructor(
2122
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
@@ -31,9 +32,14 @@ static PSCommandExtensions()
3132
.Compile();
3233
}
3334

34-
// PowerShell's missing an API for us to AddCommand using a CommandInfo.
35-
// An issue was filed here: https://github.com/PowerShell/PowerShell/issues/12295
36-
// This works around this by creating a `Command` and passing it into PSCommand.AddCommand(Command command)
35+
/// <summary>
36+
/// PowerShell's missing an API for us to AddCommand using a CommandInfo.
37+
/// An issue was filed here: https://github.com/PowerShell/PowerShell/issues/12295
38+
/// This works around this by creating a `Command` and passing it into PSCommand.AddCommand(Command command)
39+
/// </summary>
40+
/// <param name="command"></param>
41+
/// <param name="commandInfo"></param>
42+
/// <returns></returns>
3743
public static PSCommand AddCommand(this PSCommand command, CommandInfo commandInfo)
3844
{
3945
var rsCommand = s_commandCtor(commandInfo);
@@ -81,6 +87,7 @@ public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObjec
8187
/// <summary>
8288
/// Get a representation of the PSCommand, for logging purposes.
8389
/// </summary>
90+
/// <param name="command"></param>
8491
public static string GetInvocationText(this PSCommand command)
8592
{
8693
Command currentCommand = command.Commands[0];
@@ -119,5 +126,31 @@ private static StringBuilder AddCommandText(this StringBuilder sb, Command comma
119126

120127
return sb;
121128
}
129+
130+
public static PSCommand BuildCommandFromArguments(string command, IReadOnlyList<string> arguments)
131+
{
132+
if (arguments is null or { Count: 0 })
133+
{
134+
return new PSCommand().AddCommand(command);
135+
}
136+
137+
// HACK: We use AddScript instead of AddArgument/AddParameter to reuse Powershell parameter binding logic.
138+
// We quote the command parameter so that expressions can still be used in the arguments.
139+
var sb = new StringBuilder()
140+
.Append('&')
141+
.Append(' ')
142+
.Append('"')
143+
.Append(command)
144+
.Append('"');
145+
146+
foreach (string arg in arguments)
147+
{
148+
sb
149+
.Append(' ')
150+
.Append(ArgumentEscaping.Escape(arg));
151+
}
152+
153+
return new PSCommand().AddScript(sb.ToString());
154+
}
122155
}
123156
}

0 commit comments

Comments
 (0)