Skip to content

Commit 922b127

Browse files
Joel BennettJoel Bennett
Joel Bennett
authored and
Joel Bennett
committed
Fix running in runspaces without a host
+semver:feature
1 parent e9eda3c commit 922b127

File tree

1 file changed

+61
-68
lines changed

1 file changed

+61
-68
lines changed

HostIOInterceptor.cs

+61-68
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ReSharper disable UnusedMember.Global
1+
// ReSharper disable UnusedMember.Global
22
// ReSharper disable UnusedMethodReturnValue.Global
33

44
namespace PSLogging
@@ -39,7 +39,7 @@ private HostIOInterceptor()
3939
#endregion
4040

4141
#region Properties
42-
42+
4343
public bool Paused
4444
{
4545
get { return paused; }
@@ -152,9 +152,9 @@ public override Dictionary<string, PSObject> Prompt(string caption,
152152
string message,
153153
Collection<FieldDescription> descriptions)
154154
{
155-
if (externalUI == null)
155+
if (externalUI != null)
156156
{
157-
throw new InvalidOperationException();
157+
throw new InvalidOperationException("Unable to prompt user in headless session");
158158
}
159159

160160
Dictionary<string, PSObject> result = externalUI.Prompt(caption, message, descriptions);
@@ -169,9 +169,9 @@ public override int PromptForChoice(string caption,
169169
Collection<ChoiceDescription> choices,
170170
int defaultChoice)
171171
{
172-
if (externalUI == null)
172+
if (externalUI != null)
173173
{
174-
throw new InvalidOperationException();
174+
throw new InvalidOperationException("Unable to prompt user for choice in headless session");
175175
}
176176

177177
int result = externalUI.PromptForChoice(caption, message, choices, defaultChoice);
@@ -186,11 +186,11 @@ public override PSCredential PromptForCredential(string caption,
186186
string userName,
187187
string targetName)
188188
{
189-
if (externalUI == null)
189+
if (externalUI != null)
190190
{
191-
throw new InvalidOperationException();
191+
throw new InvalidOperationException("Unable to prompt user for credential in headless session");
192192
}
193-
193+
194194
PSCredential result = externalUI.PromptForCredential(caption, message, userName, targetName);
195195

196196
SendToSubscribers(s => s.CredentialPrompt(result));
@@ -205,9 +205,9 @@ public override PSCredential PromptForCredential(string caption,
205205
PSCredentialTypes allowedCredentialTypes,
206206
PSCredentialUIOptions options)
207207
{
208-
if (externalUI == null)
208+
if (externalUI != null)
209209
{
210-
throw new InvalidOperationException();
210+
throw new InvalidOperationException("Unable to prompt user for credential in headless session");
211211
}
212212

213213
PSCredential result = externalUI.PromptForCredential(caption,
@@ -224,9 +224,9 @@ public override PSCredential PromptForCredential(string caption,
224224

225225
public override string ReadLine()
226226
{
227-
if (externalUI == null)
227+
if (externalUI != null)
228228
{
229-
throw new InvalidOperationException();
229+
throw new InvalidOperationException("Unable to ReadLine from host in headless session");
230230
}
231231

232232
string result = externalUI.ReadLine();
@@ -238,11 +238,12 @@ public override string ReadLine()
238238

239239
public override SecureString ReadLineAsSecureString()
240240
{
241-
if (externalUI == null)
241+
if (externalUI != null)
242242
{
243-
throw new InvalidOperationException();
243+
throw new InvalidOperationException("Unable to ReadLineAsSecureString from host in headless session");
244244
}
245245

246+
246247
return externalUI.ReadLineAsSecureString();
247248
}
248249

@@ -271,13 +272,11 @@ public void RemoveSubscriber(IHostIOSubscriber subscriber)
271272

272273
public override void Write(string value)
273274
{
274-
if (externalUI == null)
275+
if (externalUI != null)
275276
{
276-
throw new InvalidOperationException();
277+
externalUI.Write(value);
277278
}
278279

279-
externalUI.Write(value);
280-
281280
if (!paused)
282281
{
283282
writeCache.Append(value);
@@ -286,13 +285,11 @@ public override void Write(string value)
286285

287286
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
288287
{
289-
if (externalUI == null)
288+
if (externalUI != null)
290289
{
291-
throw new InvalidOperationException();
290+
externalUI.Write(foregroundColor, backgroundColor, value);
292291
}
293292

294-
externalUI.Write(foregroundColor, backgroundColor, value);
295-
296293
if (!paused)
297294
{
298295
writeCache.Append(value);
@@ -301,44 +298,40 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background
301298

302299
public override void WriteDebugLine(string message)
303300
{
304-
if (externalUI == null)
305-
{
306-
throw new InvalidOperationException();
307-
}
308-
309301
string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
310302
foreach (string line in lines)
311303
{
312304
string temp = line;
313305
SendToSubscribers(s => s.WriteDebug(temp.TrimEnd() + "\r\n"));
314306
}
315307

316-
externalUI.WriteDebugLine(message);
308+
if (externalUI != null)
309+
{
310+
externalUI.WriteDebugLine(message);
311+
}
317312
}
318313

319314
public override void WriteErrorLine(string message)
320315
{
321-
if (externalUI == null)
322-
{
323-
throw new InvalidOperationException();
324-
}
325-
326316
string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
327317
foreach (string line in lines)
328318
{
329319
string temp = line;
330320
SendToSubscribers(s => s.WriteError(temp.TrimEnd() + "\r\n"));
331321
}
332322

333-
externalUI.WriteErrorLine(message);
323+
if (externalUI != null)
324+
{
325+
externalUI.WriteErrorLine(message);
326+
}
334327
}
335328

336329
public override void WriteLine()
337330
{
338-
if (externalUI == null)
339-
{
340-
throw new InvalidOperationException();
341-
}
331+
// if (externalUI == null)
332+
// {
333+
// throw new InvalidOperationException();
334+
// }
342335

343336
string[] lines = writeCache.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
344337
foreach (string line in lines)
@@ -348,15 +341,17 @@ public override void WriteLine()
348341
}
349342

350343
writeCache.Length = 0;
351-
externalUI.WriteLine();
344+
if (externalUI != null) {
345+
externalUI.WriteLine();
346+
}
352347
}
353348

354349
public override void WriteLine(string value)
355350
{
356-
if (externalUI == null)
357-
{
358-
throw new InvalidOperationException();
359-
}
351+
// if (externalUI == null)
352+
// {
353+
// throw new InvalidOperationException();
354+
// }
360355

361356
string[] lines = (writeCache + value).Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
362357
foreach (string line in lines)
@@ -366,15 +361,17 @@ public override void WriteLine(string value)
366361
}
367362

368363
writeCache.Length = 0;
369-
externalUI.WriteLine(value);
364+
if (externalUI != null) {
365+
externalUI.WriteLine(value);
366+
}
370367
}
371368

372369
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
373370
{
374-
if (externalUI == null)
375-
{
376-
throw new InvalidOperationException();
377-
}
371+
// if (externalUI == null)
372+
// {
373+
// throw new InvalidOperationException();
374+
// }
378375

379376
string[] lines = (writeCache + value).Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
380377
foreach (string line in lines)
@@ -384,53 +381,49 @@ public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgr
384381
}
385382

386383
writeCache.Length = 0;
387-
externalUI.WriteLine(foregroundColor, backgroundColor, value);
384+
if (externalUI != null){
385+
externalUI.WriteLine(foregroundColor, backgroundColor, value);
386+
}
388387
}
389388

390389
public override void WriteProgress(long sourceId, ProgressRecord record)
391390
{
392-
if (externalUI == null)
393-
{
394-
throw new InvalidOperationException();
395-
}
396-
397391
SendToSubscribers(s => s.WriteProgress(sourceId, record));
398392

399-
externalUI.WriteProgress(sourceId, record);
393+
if (externalUI != null)
394+
{
395+
externalUI.WriteProgress(sourceId, record);
396+
}
400397
}
401398

402399
public override void WriteVerboseLine(string message)
403400
{
404-
if (externalUI == null)
405-
{
406-
throw new InvalidOperationException();
407-
}
408-
409401
string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
410402
foreach (string line in lines)
411403
{
412404
string temp = line;
413405
SendToSubscribers(s => s.WriteVerbose(temp.TrimEnd() + "\r\n"));
414406
}
415407

416-
externalUI.WriteVerboseLine(message);
408+
if (externalUI != null)
409+
{
410+
externalUI.WriteVerboseLine(message);
411+
}
417412
}
418413

419414
public override void WriteWarningLine(string message)
420415
{
421-
if (externalUI == null)
422-
{
423-
throw new InvalidOperationException();
424-
}
425-
426416
string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
427417
foreach (string line in lines)
428418
{
429419
string temp = line;
430420
SendToSubscribers(s => s.WriteWarning(temp.TrimEnd() + "\r\n"));
431421
}
432422

433-
externalUI.WriteWarningLine(message);
423+
if (externalUI != null)
424+
{
425+
externalUI.WriteWarningLine(message);
426+
}
434427
}
435428

436429
#endregion

0 commit comments

Comments
 (0)