Skip to content

Commit 18dd57c

Browse files
committed
Replaced GetAwaiter().GetResult() with SynchronousPowerShellTask
1 parent 8aec52d commit 18dd57c

1 file changed

Lines changed: 56 additions & 35 deletions

File tree

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
using System.Security;
1111
using System.Threading;
1212
using Microsoft.Extensions.Logging;
13-
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
13+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
14+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1415
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1516
using Microsoft.PowerShell.EditorServices.Utility;
1617
using OmniSharp.Extensions.LanguageServer.Protocol;
@@ -84,7 +85,7 @@ internal class WorkspaceService
8485
/// </summary>
8586
public bool FollowSymlinks { get; set; }
8687

87-
private readonly IInternalPowerShellExecutionService executionService;
88+
private readonly PsesInternalHost psesInternalHost;
8889

8990
#endregion
9091

@@ -93,14 +94,14 @@ internal class WorkspaceService
9394
/// <summary>
9495
/// Creates a new instance of the Workspace class.
9596
/// </summary>
96-
public WorkspaceService(ILoggerFactory factory, IInternalPowerShellExecutionService executionService)
97+
public WorkspaceService(ILoggerFactory factory, PsesInternalHost executionService)
9798
{
9899
powerShellVersion = VersionUtils.PSVersion;
99100
logger = factory.CreateLogger<WorkspaceService>();
100101
WorkspaceFolders = new List<WorkspaceFolder>();
101102
ExcludeFilesGlob = new List<string>();
102103
FollowSymlinks = true;
103-
this.executionService = executionService;
104+
this.psesInternalHost = executionService;
104105
}
105106

106107
#endregion
@@ -185,6 +186,11 @@ public bool TryGetFile(Uri fileUri, out ScriptFile scriptFile) =>
185186
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
186187
public bool TryGetFile(DocumentUri documentUri, out ScriptFile scriptFile)
187188
{
189+
if (ScriptFile.IsUntitledPath(documentUri.ToString()))
190+
{
191+
scriptFile = null;
192+
return false;
193+
}
188194
try
189195
{
190196
scriptFile = GetFile(documentUri);
@@ -338,22 +344,29 @@ public IEnumerable<string> EnumeratePSFiles(
338344
int maxDepth,
339345
bool ignoreReparsePoints)
340346
{
341-
PSCommand psCommand = new();
342-
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Get-ChildItem")
343-
.AddParameter("Path", WorkspacePaths)
344-
.AddParameter("File")
345-
.AddParameter("Recurse")
346-
.AddParameter("ErrorAction", "SilentlyContinue")
347-
.AddParameter("Force")
348-
.AddParameter("Include", includeGlobs.Concat(VersionUtils.IsNetCore ? s_psFileExtensionsCoreFramework : s_psFileExtensionsFullFramework))
349-
.AddParameter("Exclude", excludeGlobs)
350-
.AddParameter("Depth", maxDepth)
351-
.AddParameter("FollowSymlink", !ignoreReparsePoints)
347+
IEnumerable<string> results = new SynchronousPowerShellTask<string>(
348+
logger,
349+
psesInternalHost,
350+
new PSCommand()
351+
.AddCommand(@"Microsoft.PowerShell.Management\Get-ChildItem")
352+
.AddParameter("LiteralPath", WorkspacePaths)
353+
.AddParameter("Recurse")
354+
.AddParameter("ErrorAction", "SilentlyContinue")
355+
.AddParameter("Force")
356+
.AddParameter("Include", includeGlobs.Concat(VersionUtils.IsNetCore ? s_psFileExtensionsCoreFramework : s_psFileExtensionsFullFramework))
357+
.AddParameter("Exclude", excludeGlobs)
358+
.AddParameter("Depth", maxDepth)
359+
.AddParameter("FollowSymlink", !ignoreReparsePoints)
360+
.AddCommand("Where-Object")
361+
.AddParameter("Property", "PSIsContainer")
362+
.AddParameter("EQ")
363+
.AddParameter("Value", false)
352364
.AddCommand("Select-Object")
353-
.AddParameter("ExpandObject", "FullName");
354-
IEnumerable<string> results = executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
365+
.AddParameter("ExpandObject", "FullName"),
366+
null,
367+
CancellationToken.None)
368+
.ExecuteAndGetResult(CancellationToken.None);
355369
return results;
356-
357370
}
358371

359372
#endregion
@@ -374,7 +387,7 @@ internal string ReadFileContents(DocumentUri uri)
374387
{
375388
string PSProvider = uri.Authority;
376389
string path = uri.Path.TrimStart('/');
377-
pspath = $"{PSProvider.Replace("-", "\\")}::{path}";
390+
pspath = $"{PSProvider}::{path}";
378391
}
379392
/*
380393
* Authority = ""
@@ -397,22 +410,25 @@ internal string ReadFileContents(DocumentUri uri)
397410
* Result "FileSystem://c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
398411
399412
*/
400-
psCommand.AddCommand("Get-Content")
401-
.AddParameter("LiteralPath", pspath)
402-
.AddParameter("Raw", true)
403-
.AddParameter("ErrorAction", ActionPreference.Stop);
413+
IEnumerable<string> result;
404414
try
405415
{
406-
IEnumerable<string> result = executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None, new PowerShell.Execution.PowerShellExecutionOptions()
407-
{
408-
ThrowOnError = true
409-
}).ConfigureAwait(false).GetAwaiter().GetResult();
410-
return result.FirstOrDefault();
416+
result = new SynchronousPowerShellTask<string>(
417+
logger,
418+
psesInternalHost,
419+
psCommand.AddCommand("Get-Content")
420+
.AddParameter("LiteralPath", pspath)
421+
.AddParameter("ErrorAction", ActionPreference.Stop),
422+
new PowerShellExecutionOptions()
423+
{
424+
ThrowOnError = true
425+
}, CancellationToken.None).ExecuteAndGetResult(CancellationToken.None);
411426
}
412427
catch (ActionPreferenceStopException ex) when (ex.ErrorRecord.CategoryInfo.Category == ErrorCategory.ObjectNotFound && ex.ErrorRecord.TargetObject is string[] missingFiles && missingFiles.Count() == 1)
413428
{
414429
throw new FileNotFoundException(ex.ErrorRecord.ToString(), missingFiles.First(), ex.ErrorRecord.Exception);
415430
}
431+
return string.Join(Environment.NewLine, result);
416432
}
417433

418434
internal string ResolveWorkspacePath(string path) => ResolveRelativeScriptPath(InitialWorkingDirectory, path);
@@ -439,13 +455,18 @@ internal string ResolveRelativeScriptPath(string baseFilePath, string relativePa
439455
Path.Combine(
440456
baseFilePath,
441457
relativePath));
442-
443-
PSCommand psCommand = new();
444-
psCommand.AddCommand("Resolve-Path")
445-
.AddParameter("Relative", true)
446-
.AddParameter("Path", relativePath)
447-
.AddParameter("RelativeBasePath", baseFilePath);
448-
IEnumerable<string> result = executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
458+
IEnumerable<string> result;
459+
result = new SynchronousPowerShellTask<string>(
460+
logger,
461+
psesInternalHost,
462+
new PSCommand()
463+
.AddCommand("Resolve-Path")
464+
.AddParameter("Relative", true)
465+
.AddParameter("Path", relativePath)
466+
.AddParameter("RelativeBasePath", baseFilePath),
467+
new(),
468+
CancellationToken.None)
469+
.ExecuteAndGetResult(CancellationToken.None);
449470
combinedPath = result.FirstOrDefault();
450471
}
451472
catch (NotSupportedException e)

0 commit comments

Comments
 (0)