|
3 | 3 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
4 | 4 | //
|
5 | 5 |
|
6 |
| -using Microsoft.Extensions.Logging; |
7 | 6 | using System;
|
8 |
| -using System.Collections; |
9 |
| -using System.Management.Automation.Runspaces; |
| 7 | +using Microsoft.PowerShell.EditorServices.Utility; |
10 | 8 |
|
11 | 9 | namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
|
12 | 10 | {
|
@@ -89,76 +87,34 @@ public PowerShellVersionDetails(
|
89 | 87 | /// <summary>
|
90 | 88 | /// Gets the PowerShell version details for the given runspace.
|
91 | 89 | /// </summary>
|
92 |
| - /// <param name="runspace">The runspace for which version details will be gathered.</param> |
93 |
| - /// <param name="logger">An ILogger implementation used for writing log messages.</param> |
94 | 90 | /// <returns>A new PowerShellVersionDetails instance.</returns>
|
95 |
| - public static PowerShellVersionDetails GetVersionDetails(Runspace runspace, ILogger logger) |
| 91 | + public static PowerShellVersionDetails GetVersionDetails() |
96 | 92 | {
|
97 |
| - Version powerShellVersion = new Version(5, 0); |
98 |
| - string versionString = null; |
99 |
| - string powerShellEdition = "Desktop"; |
| 93 | + var arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); |
100 | 94 | var architecture = PowerShellProcessArchitecture.Unknown;
|
101 |
| - |
102 |
| - try |
| 95 | + if (arch != null) |
103 | 96 | {
|
104 |
| - var psVersionTable = PowerShellContextService.ExecuteScriptAndGetItem<Hashtable>("$PSVersionTable", runspace); |
105 |
| - if (psVersionTable != null) |
| 97 | + if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) |
| 98 | + { |
| 99 | + architecture = PowerShellProcessArchitecture.X64; |
| 100 | + } |
| 101 | + else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) |
106 | 102 | {
|
107 |
| - var edition = psVersionTable["PSEdition"] as string; |
108 |
| - if (edition != null) |
109 |
| - { |
110 |
| - powerShellEdition = edition; |
111 |
| - } |
112 |
| - |
113 |
| - // The PSVersion value will either be of Version or SemanticVersion. |
114 |
| - // In the former case, take the value directly. In the latter case, |
115 |
| - // generate a Version from its string representation. |
116 |
| - var version = psVersionTable["PSVersion"]; |
117 |
| - if (version is Version) |
118 |
| - { |
119 |
| - powerShellVersion = (Version)version; |
120 |
| - } |
121 |
| - else if (version != null) |
122 |
| - { |
123 |
| - // Expected version string format is 6.0.0-alpha so build a simpler version from that |
124 |
| - powerShellVersion = new Version(version.ToString().Split('-')[0]); |
125 |
| - } |
126 |
| - |
127 |
| - var gitCommitId = psVersionTable["GitCommitId"] as string; |
128 |
| - if (gitCommitId != null) |
129 |
| - { |
130 |
| - versionString = gitCommitId; |
131 |
| - } |
132 |
| - else |
133 |
| - { |
134 |
| - versionString = powerShellVersion.ToString(); |
135 |
| - } |
136 |
| - |
137 |
| - var arch = PowerShellContextService.ExecuteScriptAndGetItem<string>("$env:PROCESSOR_ARCHITECTURE", runspace); |
138 |
| - if (arch != null) |
139 |
| - { |
140 |
| - if (string.Equals(arch, "AMD64", StringComparison.CurrentCultureIgnoreCase)) |
141 |
| - { |
142 |
| - architecture = PowerShellProcessArchitecture.X64; |
143 |
| - } |
144 |
| - else if (string.Equals(arch, "x86", StringComparison.CurrentCultureIgnoreCase)) |
145 |
| - { |
146 |
| - architecture = PowerShellProcessArchitecture.X86; |
147 |
| - } |
148 |
| - } |
| 103 | + architecture = PowerShellProcessArchitecture.X86; |
149 | 104 | }
|
150 | 105 | }
|
151 |
| - catch (Exception ex) |
| 106 | + |
| 107 | + if (!VersionUtils.IsWindows) |
152 | 108 | {
|
153 |
| - logger.LogWarning( |
154 |
| - "Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString()); |
| 109 | + architecture = PowerShellProcessArchitecture.X64; |
155 | 110 | }
|
156 | 111 |
|
157 | 112 | return new PowerShellVersionDetails(
|
158 |
| - powerShellVersion, |
159 |
| - versionString, |
160 |
| - powerShellEdition, |
161 |
| - architecture); |
| 113 | + VersionUtils.PSVersion, |
| 114 | + VersionUtils.PSVersionString, |
| 115 | + VersionUtils.PSEdition, |
| 116 | + architecture |
| 117 | + ); |
162 | 118 | }
|
163 | 119 |
|
164 | 120 | #endregion
|
|
0 commit comments