2
2
// Licensed under the MIT License.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . IO ;
6
7
using System . Linq . Expressions ;
7
8
using System . Management . Automation ;
11
12
12
13
namespace Microsoft . PowerShell . EditorServices . Utility
13
14
{
14
- internal static class PSCommandExtensions
15
+ internal static class PSCommandHelpers
15
16
{
16
17
private static readonly Func < CommandInfo , Command > s_commandCtor ;
17
18
18
- static PSCommandExtensions ( )
19
+ static PSCommandHelpers ( )
19
20
{
20
21
var ctor = typeof ( Command ) . GetConstructor (
21
22
BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ,
@@ -31,9 +32,14 @@ static PSCommandExtensions()
31
32
. Compile ( ) ;
32
33
}
33
34
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>
37
43
public static PSCommand AddCommand ( this PSCommand command , CommandInfo commandInfo )
38
44
{
39
45
var rsCommand = s_commandCtor ( commandInfo ) ;
@@ -81,6 +87,7 @@ public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObjec
81
87
/// <summary>
82
88
/// Get a representation of the PSCommand, for logging purposes.
83
89
/// </summary>
90
+ /// <param name="command"></param>
84
91
public static string GetInvocationText ( this PSCommand command )
85
92
{
86
93
Command currentCommand = command . Commands [ 0 ] ;
@@ -119,5 +126,31 @@ private static StringBuilder AddCommandText(this StringBuilder sb, Command comma
119
126
120
127
return sb ;
121
128
}
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
+ }
122
155
}
123
156
}
0 commit comments