Skip to content

Commit 6e77537

Browse files
iSazonovdaxian-dbw
authored andcommitted
Get 'PSVersion' and 'GitCommitId' from the 'ProductVersion' attribute of assembly (PowerShell#4863)
1 parent f83e238 commit 6e77537

File tree

8 files changed

+82
-43
lines changed

8 files changed

+82
-43
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ dotnet-uninstall-debian-packages.sh
4343
*.pkg
4444
*.nupkg
4545

46-
# ignore the version file as it is generated at build time
47-
powershell.version
48-
4946
# ignore the telemetry semaphore file
5047
DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY
5148

PowerShell.Common.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<PSCoreAdditionalCommits>$([System.Text.RegularExpressions.Regex]::Match($(PowerShellVersion), $(RegexGitVersion)).Groups[2].Value)</PSCoreAdditionalCommits>
3232
<PSCoreCommitSHA>$([System.Text.RegularExpressions.Regex]::Match($(PowerShellVersion), $(RegexGitVersion)).Groups[3].Value)</PSCoreCommitSHA>
3333

34+
<!--
35+
Caution! 'PSVersion' and 'GitCommitId' from 'PSVersionInfo.cs' depend on the format of this version string.
36+
-->
3437
<PSCoreFormattedVersion Condition = "'$(ReleaseTag)' != '' or '$(PSCoreAdditionalCommits)' == '0'">$(PSCoreBuildVersion) SHA: $(PSCoreCommitSHA)</PSCoreFormattedVersion>
3538
<PSCoreFormattedVersion Condition = "'$(PSCoreFormattedVersion)' == ''">$(PSCoreBuildVersion) Commits: $(PSCoreAdditionalCommits) SHA: $(PSCoreCommitSHA)</PSCoreFormattedVersion>
3639

build.psm1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,6 @@ function Start-PSBuild {
393393
}
394394
}
395395

396-
# save git commit id to file for PowerShell to include in PSVersionTable
397-
$gitCommitId = $ReleaseTag
398-
if (-not $gitCommitId) {
399-
# if ReleaseTag is not specified, use 'git describe' to get the commit id
400-
$gitCommitId = Get-PSCommitId -WarningAction SilentlyContinue
401-
}
402-
$gitCommitId > "$psscriptroot/powershell.version"
403-
404396
# create the telemetry flag file
405397
$null = new-item -force -type file "$psscriptroot/DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY"
406398

src/System.Management.Automation/engine/PSVersionInfo.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class PSVersionInfo
4545
private static Version s_psV4Version = new Version(4, 0);
4646
private static Version s_psV5Version = new Version(5, 0);
4747
private static Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE);
48-
private static SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, "beta");
48+
private static SemanticVersion s_psV6Version;
4949

5050
/// <summary>
5151
/// A constant to track current PowerShell Edition
@@ -57,9 +57,38 @@ static PSVersionInfo()
5757
{
5858
s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase);
5959

60+
string assemblyPath = typeof(PSVersionInfo).Assembly.Location;
61+
string productVersion = FileVersionInfo.GetVersionInfo(assemblyPath).ProductVersion;
62+
63+
// Get 'GitCommitId' and 'PSVersion' from the 'productVersion' assembly attribute.
64+
//
65+
// The strings can be one of the following format examples:
66+
// when powershell is built from a commit:
67+
// productVersion = '6.0.0-beta.7 Commits: 29 SHA: 52c6b...' convert to GitCommitId = 'v6.0.0-beta.7-29-g52c6b...'
68+
// PSVersion = '6.0.0-beta.7'
69+
// when powershell is built from a release tag:
70+
// productVersion = '6.0.0-beta.7 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0-beta.7'
71+
// PSVersion = '6.0.0-beta.7'
72+
// when powershell is built from a release tag for RTM:
73+
// productVersion = '6.0.0 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0'
74+
// PSVersion = '6.0.0'
75+
string rawGitCommitId;
76+
string mainVersion = productVersion.Substring(0, productVersion.IndexOf(' '));
77+
78+
if (productVersion.Contains(" Commits: "))
79+
{
80+
rawGitCommitId = "v" + productVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g");
81+
}
82+
else
83+
{
84+
rawGitCommitId = "v" + mainVersion;
85+
}
86+
87+
s_psV6Version = new SemanticVersion(mainVersion);
88+
6089
s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV6Version;
6190
s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue;
62-
s_psVersionTable[PSGitCommitIdName] = GetCommitInfo();
91+
s_psVersionTable[PSGitCommitIdName] = rawGitCommitId;
6392
s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version };
6493
s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion);
6594
s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion;
@@ -81,20 +110,6 @@ internal static Hashtable GetPSVersionTableForDownLevel()
81110
return result;
82111
}
83112

84-
// Get the commit id from the powershell.version file. If the powershell.version file doesn't exist, use the string "N/A"
85-
internal static string GetCommitInfo()
86-
{
87-
try
88-
{
89-
string assemblyPath = IO.Path.GetDirectoryName(typeof(PSVersionInfo).GetTypeInfo().Assembly.Location);
90-
return (IO.File.ReadAllLines(IO.Path.Combine(assemblyPath, "powershell.version"))[0]);
91-
}
92-
catch (Exception e)
93-
{
94-
return e.Message;
95-
}
96-
}
97-
98113
#region Private helper methods
99114

100115
// Gets the current WSMan stack version from the registry.
@@ -826,4 +841,4 @@ internal Exception GetVersionParseException()
826841
}
827842
}
828843
}
829-
}
844+
}

src/System.Management.Automation/engine/remoting/commands/CustomShellCommands.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function Register-PSSessionConfiguration
132132
$pluginWsmanRunAsUserPath = [System.IO.Path]::Combine(""WSMan:\localhost\Plugin"", ""$pluginName"", ""RunAsUser"")
133133
set-item -WarningAction SilentlyContinue $pluginWsmanRunAsUserPath $runAsCredential -confirm:$false
134134
}} catch {{
135-
135+
136136
remove-item (Join-Path WSMan:\localhost\Plugin ""$pluginName"") -recurse -force
137137
write-error $_
138138
# Do not add anymore clean up code after Write-Error, because if EA=Stop is set by user
@@ -1561,10 +1561,9 @@ internal static string GetRunAsVirtualAccountGroupsString(string[] groups)
15611561
internal static string GetWinrmPluginShellName()
15621562
{
15631563
// PowerShell Core uses a versioned directory to hold the plugin
1564-
Hashtable versionTable = PSVersionInfo.GetPSVersionTable();
15651564
// TODO: This should be PSVersionInfo.PSVersionName once we get
15661565
// closer to release. Right now it doesn't support alpha versions.
1567-
return System.String.Concat("PowerShell.", (string)versionTable["GitCommitId"]);
1566+
return System.String.Concat("PowerShell.", PSVersionInfo.GitCommitId);
15681567
}
15691568

15701569
/// <summary>
@@ -1574,10 +1573,9 @@ internal static string GetWinrmPluginShellName()
15741573
internal static string GetWinrmPluginDllPath()
15751574
{
15761575
// PowerShell Core uses its versioned directory instead of system32
1577-
Hashtable versionTable = PSVersionInfo.GetPSVersionTable();
15781576
// TODO: This should be PSVersionInfo.PSVersionName once we get
15791577
// closer to release. Right now it doesn't support alpha versions.
1580-
string pluginDllDirectory = System.IO.Path.Combine("%windir%\\system32\\PowerShell", (string)versionTable["GitCommitId"]);
1578+
string pluginDllDirectory = System.IO.Path.Combine("%windir%\\system32\\PowerShell", PSVersionInfo.GitCommitId);
15811579
return System.IO.Path.Combine(pluginDllDirectory, RemotingConstants.PSPluginDLLName);
15821580
}
15831581

@@ -2555,7 +2553,7 @@ function Unregister-PSSessionConfiguration
25552553
return
25562554
}}
25572555
}}
2558-
2556+
25592557
$shellsFound++
25602558
25612559
$shouldProcessTargetString = $targetTemplate -f $_.Name
@@ -2779,12 +2777,12 @@ function ExtractPluginProperties([string]$pluginDir, $objectToWriteTo)
27792777
}}
27802778
27812779
Get-Details $pluginDir $h
2782-
2780+
27832781
# Workflow is not supported in PowerShell Core. Attempting to load the
27842782
# assembly results in a FileNotFoundException.
27852783
if (![System.Management.Automation.Platform]::IsCoreCLR -AND
27862784
$h[""AssemblyName""] -eq ""Microsoft.PowerShell.Workflow.ServiceCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"") {{
2787-
2785+
27882786
$serviceCore = [Reflection.Assembly]::Load(""Microsoft.Powershell.Workflow.ServiceCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"")
27892787
27902788
if ($null -ne $serviceCore) {{
@@ -4967,7 +4965,7 @@ function Enable-PSRemoting
49674965
}}
49684966
}}
49694967
}}
4970-
4968+
49714969
# remove the 'network deny all' tag
49724970
Get-PSSessionConfiguration -Force:$Force | ForEach-Object {{
49734971
$sddl = $null

src/powershell-unix/powershell-unix.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1717
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
1818
</Content>
19-
<Content Include="..\..\license_thirdparty_proprietary.txt;..\..\powershell.version;..\..\DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY">
19+
<Content Include="..\..\license_thirdparty_proprietary.txt;..\..\DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY">
2020
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2121
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
2222
</Content>

src/powershell-win-core/powershell-win-core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2020
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
2121
</Content>
22-
<Content Include="..\..\license_thirdparty_proprietary.txt;..\..\powershell.version;..\..\DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY">
22+
<Content Include="..\..\license_thirdparty_proprietary.txt;..\..\DELETE_ME_TO_DISABLE_CONSOLEHOST_TELEMETRY">
2323
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2424
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
2525
</Content>

test/powershell/Host/PSVersionTable.Tests.ps1

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
Describe "PSVersionTable" -Tags "CI" {
2+
3+
BeforeAll {
4+
$sma = Get-Item (Join-Path $PSHome "System.Management.Automation.dll")
5+
$formattedVersion = $sma.VersionInfo.ProductVersion
6+
7+
$mainVersionPattern = "(\d+\.\d+\.\d+)(-.+)?"
8+
$fullVersionPattern = "^v(\d+\.\d+\.\d+)-(.+)-(\d+)-g(.+)$"
9+
10+
$expectedPSVersion = ($formattedVersion -split " ")[0]
11+
$expectedVersionPattern = "^$mainVersionPattern$"
12+
13+
if ($formattedVersion.Contains(" Commits: "))
14+
{
15+
$rawGitCommitId = "v" + $formattedVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g")
16+
$expectedGitCommitIdPattern = $fullVersionPattern
17+
$unexpectectGitCommitIdPattern = "qwerty"
18+
} else {
19+
$rawGitCommitId = "v" + ($formattedVersion -split " SHA: ")[0]
20+
$expectedGitCommitIdPattern = "^v$mainVersionPattern$"
21+
$unexpectectGitCommitIdPattern = $fullVersionPattern
22+
}
23+
}
24+
225
It "Should have version table entries" {
326
$PSVersionTable.Count | Should Be 9
427
}
@@ -15,20 +38,31 @@ Describe "PSVersionTable" -Tags "CI" {
1538
$PSVersionTable.ContainsKey("OS") | Should Be True
1639

1740
}
18-
It "GitCommitId property should not contain an error" {
19-
$PSVersionTable.GitCommitId | Should not match "powershell.version"
41+
42+
It "PSVersion property" {
43+
$PSVersionTable.PSVersion | Should BeOfType "System.Management.Automation.SemanticVersion"
44+
$PSVersionTable.PSVersion | Should BeExactly $expectedPSVersion
45+
$PSVersionTable.PSVersion | Should Match $expectedVersionPattern
46+
$PSVersionTable.PSVersion.Major | Should Be 6
47+
}
48+
49+
It "GitCommitId property" {
50+
$PSVersionTable.GitCommitId | Should BeOfType "System.String"
51+
$PSVersionTable.GitCommitId | Should Match $expectedGitCommitIdPattern
52+
$PSVersionTable.GitCommitId | Should Not Match $unexpectectGitCommitIdPattern
53+
$PSVersionTable.GitCommitId | Should BeExactly $rawGitCommitId
2054
}
2155

2256
It "Should have the correct platform info" {
2357
$platform = [String][System.Environment]::OSVersion.Platform
24-
[String]$PSVersionTable["Platform"] | Should Be $platform
58+
[String]$PSVersionTable["Platform"] | Should Be $platform
2559
}
2660

2761
It "Should have the correct OS info" {
2862
if ($IsCoreCLR)
2963
{
3064
$OSDescription = [String][System.Runtime.InteropServices.RuntimeInformation]::OSDescription
31-
[String]$PSVersionTable["OS"] | Should Be $OSDescription
65+
[String]$PSVersionTable["OS"] | Should Be $OSDescription
3266
}
3367
else
3468
{

0 commit comments

Comments
 (0)