Skip to content

Grab prerelease if it's available from PSCurrentVersion #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public async Task StartAsync()
_additionalModules))
.ConfigureLogging(builder => builder
.AddSerilog(Log.Logger)
.AddLanguageServer(LogLevel.Trace)
.SetMinimumLevel(LogLevel.Trace))
.AddDefaultLoggingProvider()
.WithHandler<WorkspaceSymbolsHandler>()
.WithHandler<TextDocumentHandler>()
.WithHandler<GetVersionHandler>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public ReferencesCodeLensProvider(WorkspaceService workspaceService, SymbolsServ
_workspaceService = workspaceService;
_symbolsService = symbolsService;
// TODO: Pull this from components
_symbolProvider = new ScriptDocumentSymbolProvider(
VersionUtils.PSVersion);
_symbolProvider = new ScriptDocumentSymbolProvider();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Task<PowerShellVersion> Handle(GetVersionParams request, CancellationToke

return Task.FromResult(new PowerShellVersion
{
Version = VersionUtils.PSVersion.ToString(),
Version = VersionUtils.PSVersionString,
Edition = VersionUtils.PSEdition,
DisplayVersion = VersionUtils.PSVersion.ToString(2),
Architecture = architecture.ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
/// </summary>
public class ScriptDocumentSymbolProvider : IDocumentSymbolProvider
{
private Version powerShellVersion;

/// <summary>
/// Creates an instance of the ScriptDocumentSymbolProvider to
/// target the specified PowerShell version.
/// </summary>
/// <param name="powerShellVersion">The target PowerShell version.</param>
public ScriptDocumentSymbolProvider(Version powerShellVersion)
{
this.powerShellVersion = powerShellVersion;
}

IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
ScriptFile scriptFile)
{
Expand All @@ -37,10 +25,7 @@ IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
(scriptFile.FilePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase) ||
scriptFile.FilePath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase)))
{
return
FindSymbolsInDocument(
scriptFile.ScriptAst,
this.powerShellVersion);
return FindSymbolsInDocument(scriptFile.ScriptAst);
}

return Enumerable.Empty<SymbolReference>();
Expand All @@ -52,7 +37,7 @@ IEnumerable<SymbolReference> IDocumentSymbolProvider.ProvideDocumentSymbols(
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
/// <param name="powerShellVersion">The PowerShell version the Ast was generated from</param>
/// <returns>A collection of SymbolReference objects</returns>
static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst, Version powerShellVersion)
static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst)
{
IEnumerable<SymbolReference> symbolReferences = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public SymbolsService(
_workspaceService = workspaceService;
_documentSymbolProviders = new IDocumentSymbolProvider[]
{
new ScriptDocumentSymbolProvider(VersionUtils.PSVersion),
new ScriptDocumentSymbolProvider(),
new PsdDocumentSymbolProvider(),
new PesterDocumentSymbolProvider()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public DocumentSymbolHandler(ILoggerFactory factory, ConfigurationService config
_workspaceService = workspaceService;
_providers = new IDocumentSymbolProvider[]
{
new ScriptDocumentSymbolProvider(
VersionUtils.PSVersion),
new ScriptDocumentSymbolProvider(),
new PsdDocumentSymbolProvider(),
new PesterDocumentSymbolProvider()
};
Expand Down
24 changes: 22 additions & 2 deletions src/PowerShellEditorServices/Utility/VersionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ internal static class VersionUtils
public static bool IsNetCore { get; } = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.Ordinal);

/// <summary>
/// Get's the Version of PowerShell being used.
/// Gets the Version of PowerShell being used.
/// </summary>
public static Version PSVersion { get; } = PowerShellReflectionUtils.PSVersion;

/// <summary>
/// Get's the Edition of PowerShell being used.
/// Gets the Edition of PowerShell being used.
/// </summary>
public static string PSEdition { get; } = PowerShellReflectionUtils.PSEdition;

/// <summary>
/// Gets the string of the PSVersion including prerelease tags if it applies.
/// </summary>
public static string PSVersionString { get; } = PowerShellReflectionUtils.PSVersionString;

/// <summary>
/// True if we are running in Windows PowerShell, false otherwise.
/// </summary>
Expand All @@ -49,8 +54,16 @@ internal static class PowerShellReflectionUtils
{

private static readonly Type s_psVersionInfoType = typeof(System.Management.Automation.Runspaces.Runspace).Assembly.GetType("System.Management.Automation.PSVersionInfo");

// This property is a Version type in PowerShell. It's existed since 5.1, but it was only made public in 6.2.
private static readonly PropertyInfo s_psVersionProperty = s_psVersionInfoType
.GetProperty("PSVersion", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

// This property is a SemanticVersion in PowerShell that contains the prerelease tag as well.
// It was added in 6.2 so we can't depend on it for anything before.
private static readonly PropertyInfo s_psCurrentVersionProperty = s_psVersionInfoType
.GetProperty("PSCurrentVersion", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

private static readonly PropertyInfo s_psEditionProperty = s_psVersionInfoType
.GetProperty("PSEdition", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

Expand All @@ -64,5 +77,12 @@ internal static class PowerShellReflectionUtils
/// Get's the Edition of PowerShell being used.
/// </summary>
public static string PSEdition { get; } = s_psEditionProperty.GetValue(null) as string;

/// <summary>
/// Gets the stringified version of PowerShell including prerelease tags if it applies.
/// </summary>
public static string PSVersionString { get; } = s_psCurrentVersionProperty != null
? s_psCurrentVersionProperty.GetValue(null).ToString()
: s_psVersionProperty.GetValue(null).ToString();
}
}