Skip to content

Send GitCommitId over GetVersionHandler #1965

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
merged 1 commit into from
Dec 6, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ internal class PowerShellVersionDetails
/// </summary>
public Version Version { get; }

/// <summary>
/// Gets the full version string, either the ToString of the Version
/// property or the GitCommitId for open-source PowerShell releases.
/// </summary>
public string VersionString { get; }

/// <summary>
/// Gets the PowerShell edition (generally Desktop or Core).
/// </summary>
Expand All @@ -36,15 +30,12 @@ internal class PowerShellVersionDetails
/// Creates an instance of the PowerShellVersionDetails class.
/// </summary>
/// <param name="version">The version of the PowerShell runtime.</param>
/// <param name="versionString">A string representation of the PowerShell version.</param>
/// <param name="editionString">The string representation of the PowerShell edition.</param>
public PowerShellVersionDetails(
Version version,
string versionString,
string editionString)
{
Version = version;
VersionString = versionString;
Edition = editionString;
}

Expand All @@ -59,7 +50,6 @@ public PowerShellVersionDetails(
public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerShell pwsh)
{
Version powerShellVersion = new(5, 0);
string versionString = null;
string powerShellEdition = "Desktop";

try
Expand Down Expand Up @@ -89,8 +79,6 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
// Expected version string format is 6.0.0-alpha so build a simpler version from that
powerShellVersion = new Version(version.ToString().Split('-')[0]);
}

versionString = psVersionTable["GitCommitId"] is string gitCommitId ? gitCommitId : powerShellVersion.ToString();
}
}
catch (Exception ex)
Expand All @@ -99,7 +87,7 @@ public static PowerShellVersionDetails GetVersionDetails(ILogger logger, PowerSh
"Failed to look up PowerShell version, defaulting to version 5.\r\n\r\n" + ex.ToString());
}

return new PowerShellVersionDetails(powerShellVersion, versionString, powerShellEdition);
return new PowerShellVersionDetails(powerShellVersion, powerShellEdition);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task<PowerShellVersion> Handle(GetVersionParams request, Cancellati
{
Version = VersionUtils.PSVersionString,
Edition = VersionUtils.PSEdition,
DisplayVersion = VersionUtils.PSVersion.ToString(2),
Commit = VersionUtils.GitCommitId,
Architecture = VersionUtils.Architecture
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ internal class GetVersionParams : IRequest<PowerShellVersion> { }
internal record PowerShellVersion
{
public string Version { get; init; }
public string DisplayVersion { get; init; }
public string Edition { get; init; }
public string Commit { get; init; }
public string Architecture { get; init; }
}
}
20 changes: 18 additions & 2 deletions src/PowerShellEditorServices/Utility/VersionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ internal static class VersionUtils
/// </summary>
public static string PSEdition { get; } = PowerShellReflectionUtils.PSEdition;

/// <summary>
/// Gets the GitCommitId of PowerShell being used.
/// </summary>
public static string GitCommitId { get; } = PowerShellReflectionUtils.GitCommitId;

/// <summary>
/// Gets the string of the PSVersion including prerelease tags if it applies.
/// </summary>
Expand Down Expand Up @@ -84,8 +89,12 @@ internal static class PowerShellReflectionUtils
private static readonly PropertyInfo s_psEditionProperty = s_psVersionInfoType
.GetProperty("PSEdition", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

// This property is a string, existing only in PowerShell Core.
private static readonly FieldInfo s_psGitCommitIdProperty = s_psVersionInfoType
.GetField("GitCommitId", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

/// <summary>
/// Get's the Version of PowerShell being used. Note: this will get rid of the SemVer 2.0 suffix because apparently
/// Gets the Version of PowerShell being used. NOTE: this will get rid of the SemVer 2.0 suffix because apparently
/// that property is added as a note property and it is not there when we reflect.
/// </summary>
public static Version PSVersion { get; } = s_psVersionProperty.GetValue(null) as Version;
Expand All @@ -95,12 +104,19 @@ internal static class PowerShellReflectionUtils
/// </summary>
public static string PSEdition { get; } = s_psEditionProperty.GetValue(null) as string;

/// <summary>
/// Gets the GitCommitId or at most x.y.z from the PSVersion, making Windows PowerShell conform to SemVer.
/// </summary>
public static string GitCommitId { get; } = s_psGitCommitIdProperty != null
? s_psGitCommitIdProperty.GetValue(null).ToString()
: PSVersion.ToString(3);

/// <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();
: PSVersion.ToString(3);

public static string GetOSArchitecture()
{
Expand Down