-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathPsesStdioProcess.cs
127 lines (104 loc) · 4.39 KB
/
PsesStdioProcess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace PowerShellEditorServices.Test.E2E
{
/// <summary>
/// A <see cref="ServerProcess"/> is responsible for launching or attaching to a language server, providing access to its input and output streams, and tracking its lifetime.
/// </summary>
public class PsesStdioProcess : StdioServerProcess
{
protected static readonly string s_binDir =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#region private static or constants members
private static readonly string s_bundledModulePath = new FileInfo(Path.Combine(
s_binDir,
"..", "..", "..", "..", "..",
"module")).FullName;
private static readonly string s_sessionDetailsPath = Path.Combine(
s_binDir,
$"pses_test_sessiondetails_{Path.GetRandomFileName()}");
private static readonly string s_logPath = Path.Combine(
Environment.GetEnvironmentVariable("BUILD_ARTIFACTSTAGINGDIRECTORY") ?? s_binDir,
$"pses_test_logs_{Path.GetRandomFileName()}");
private const string s_logLevel = "Diagnostic";
private static readonly string[] s_featureFlags = { "PSReadLine" };
private const string s_hostName = "TestHost";
private const string s_hostProfileId = "TestHost";
private const string s_hostVersion = "1.0.0";
private static readonly string[] s_additionalModules = { "PowerShellEditorServices.VSCode" };
#endregion
#region public static properties
// NOTE: Just hard-code this to "powershell" when testing with the code lens.
public static string PwshExe { get; } = Environment.GetEnvironmentVariable("PWSH_EXE_NAME") ?? "pwsh";
public static bool IsWindowsPowerShell { get; } = PwshExe.Contains("powershell");
public static bool RunningInConstrainedLanguageMode { get; } =
Environment.GetEnvironmentVariable("__PSLockdownPolicy", EnvironmentVariableTarget.Machine) != null;
#endregion
#region ctor
public PsesStdioProcess(ILoggerFactory loggerFactory, bool isDebugAdapter) : base(loggerFactory, GeneratePsesStartInfo(isDebugAdapter))
{
}
#endregion
#region helper private methods
private static ProcessStartInfo GeneratePsesStartInfo(bool isDebugAdapter)
{
ProcessStartInfo processStartInfo = new()
{
FileName = PwshExe
};
foreach (string arg in GeneratePsesArguments(isDebugAdapter))
{
processStartInfo.ArgumentList.Add(arg);
}
return processStartInfo;
}
private static string[] GeneratePsesArguments(bool isDebugAdapter)
{
List<string> args = new()
{
"&",
SingleQuoteEscape(Path.Combine(s_bundledModulePath, "PowerShellEditorServices", "Start-EditorServices.ps1")),
"-LogPath",
SingleQuoteEscape(s_logPath),
"-LogLevel",
s_logLevel,
"-SessionDetailsPath",
SingleQuoteEscape(s_sessionDetailsPath),
"-FeatureFlags",
string.Join(',', s_featureFlags),
"-HostName",
s_hostName,
"-HostProfileId",
s_hostProfileId,
"-HostVersion",
s_hostVersion,
"-AdditionalModules",
string.Join(',', s_additionalModules),
"-BundledModulesPath",
SingleQuoteEscape(s_bundledModulePath),
"-Stdio"
};
if (isDebugAdapter)
{
args.Add("-DebugServiceOnly");
}
string base64Str = Convert.ToBase64String(
System.Text.Encoding.Unicode.GetBytes(string.Join(' ', args)));
return new string[]
{
"-NoLogo",
"-NoProfile",
"-EncodedCommand",
base64Str
};
}
private static string SingleQuoteEscape(string str) => $"'{str.Replace("'", "''")}'";
#endregion
}
}