From 0f3463223d127b36401a8e72025e130bbec30703 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 15 Jun 2021 17:32:34 +0500 Subject: [PATCH 01/13] Use Source Code Generator for PSVersionInfo class --- PowerShell.Common.props | 7 ++ .../PSVersionInfoGenerator.cs | 75 +++++++++++++++++++ .../PSVersionInfoGenerator.csproj | 17 +++++ .../PSVersionInfoGenerator/README.md | 1 + .../System.Management.Automation.csproj | 19 +++++ .../engine/PSVersionInfo.cs | 37 +++------ 6 files changed, 131 insertions(+), 25 deletions(-) create mode 100644 src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs create mode 100644 src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj create mode 100644 src/SourceGenerators/PSVersionInfoGenerator/README.md diff --git a/PowerShell.Common.props b/PowerShell.Common.props index cf5c11c9ed2..27684a3580c 100644 --- a/PowerShell.Common.props +++ b/PowerShell.Common.props @@ -104,6 +104,13 @@ + + + + + + + @@ -33,11 +37,26 @@ + + + true + gen\SourceGeneratedFiles + + $(DefineConstants);CORECLR + + + diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 99740a44d13..fb2177babae 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Globalization; -using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -27,7 +26,7 @@ namespace System.Management.Automation /// The above statement retrieves the PowerShell edition. /// /// - public static class PSVersionInfo + public static partial class PSVersionInfo { internal const string PSVersionTableName = "PSVersionTable"; internal const string PSRemotingProtocolVersionName = "PSRemotingProtocolVersion"; @@ -42,6 +41,13 @@ public static class PSVersionInfo private static readonly PSVersionHashTable s_psVersionTable; + /* + // The property and fields are generated by PSVersionInfoGenerator source generator. + internal static string ProductVersion { get; } = "..."; + private static readonly string _rawGitCommitId = "..."; + private static readonly string _mainVersion = "..."; + */ + /// /// A constant to track current PowerShell Version. /// @@ -78,12 +84,7 @@ static PSVersionInfo() { s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase); - Assembly currentAssembly = typeof(PSVersionInfo).Assembly; - ProductVersion = currentAssembly.GetCustomAttribute().InformationalVersion; - - // Get 'GitCommitId' and 'PSVersion' from the 'productVersion' assembly attribute. - // - // The strings can be one of the following format examples: + // 'GitCommitId' and 'PSVersion' can be one of the following format examples: // when powershell is built from a commit: // productVersion = '6.0.0-beta.7 Commits: 29 SHA: 52c6b...' convert to GitCommitId = 'v6.0.0-beta.7-29-g52c6b...' // PSVersion = '6.0.0-beta.7' @@ -93,25 +94,13 @@ static PSVersionInfo() // when powershell is built from a release tag for RTM: // productVersion = '6.0.0 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0' // PSVersion = '6.0.0' - string rawGitCommitId; - string mainVersion = ProductVersion.Substring(0, ProductVersion.IndexOf(' ')); - - if (ProductVersion.Contains(" Commits: ")) - { - rawGitCommitId = ProductVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g"); - } - else - { - rawGitCommitId = mainVersion; - } - - s_psSemVersion = new SemanticVersion(mainVersion); + s_psSemVersion = new SemanticVersion(_mainVersion); s_psVersion = (Version)s_psSemVersion; s_psVersionTable[PSVersionInfo.PSVersionName] = s_psSemVersion; s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue; - s_psVersionTable[PSGitCommitIdName] = rawGitCommitId; - s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV61Version, s_psV62Version, s_psV7Version, s_psV71Version, s_psV72Version, s_psVersion }; + s_psVersionTable[PSGitCommitIdName] = _rawGitCommitId; + s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV61Version, s_psV62Version, s_psV7Version, s_psV71Version, s_psVersion }; s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion); s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion; s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion(); @@ -183,8 +172,6 @@ public static Version PSVersion } } - internal static string ProductVersion { get; } - internal static string GitCommitId { get From 94b1adc17d1f8c155a1a139910b4f4b24e2ddcb1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 18 Jun 2021 20:09:21 +0500 Subject: [PATCH 02/13] Fix CodeFactor issues --- .../PSVersionInfoGenerator/PSVersionInfoGenerator.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs b/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs index ef7785bc41a..0a01b251146 100644 --- a/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs +++ b/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs @@ -18,6 +18,7 @@ public class PSVersionInfoGenerator : ISourceGenerator /// Generate output PSVersionInfo.generated.cs file. /// This allows to directly get ProductVersion and others without reflection. /// + /// Generator execution context. public void Execute(GeneratorExecutionContext context) { var result = CreatePSVersionInfoPartialClass(context); @@ -30,6 +31,7 @@ public void Execute(GeneratorExecutionContext context) /// /// Not used. /// + /// Generator initialization context. public void Initialize(GeneratorInitializationContext context) { // No initialization required for this one. @@ -38,6 +40,8 @@ public void Initialize(GeneratorInitializationContext context) /// /// Create a string with partial PSVersionInfo class. /// + /// Generator execution context. + /// A string with partial PSVersionInfo class. private static string CreatePSVersionInfoPartialClass(GeneratorExecutionContext context) { // We must put " Date: Fri, 18 Jun 2021 20:14:10 +0500 Subject: [PATCH 03/13] Remove starting 'v' from _rawGitCommitId --- .../PSVersionInfoGenerator/PSVersionInfoGenerator.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs b/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs index 0a01b251146..8473a9e4582 100644 --- a/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs +++ b/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs @@ -70,10 +70,15 @@ public static partial class PSVersionInfo }}"; context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ProductVersion", out var productVersion); - context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.PowerShellVersion", out var powerShellVersion); + context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.PowerShellVersion", out var rawGitCommitId); context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.PSCoreBuildVersion", out var mainVersion); - return string.Format(CultureInfo.InvariantCulture, SourceTemplate, productVersion, powerShellVersion, mainVersion); + if (rawGitCommitId.StartsWith("v")) + { + rawGitCommitId = rawGitCommitId.Substring(1); + } + + return string.Format(CultureInfo.InvariantCulture, SourceTemplate, productVersion, rawGitCommitId, mainVersion); } } } From 18dd3241cd391658a81df5eb5ed3c0f9a991b2f6 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 18 Jun 2021 20:27:24 +0500 Subject: [PATCH 04/13] Fix spelling --- .spelling | 1 + 1 file changed, 1 insertion(+) diff --git a/.spelling b/.spelling index 35c76464e8d..ffd4b8f7e5e 100644 --- a/.spelling +++ b/.spelling @@ -758,6 +758,7 @@ pssessionconfiguration pssnapinloadunload pssnapins psversion +PSVersionInfoGenerator psversiontable PSWindowsPowerShellCompatibility PublishReadyToRun From 01503398332a703f6158b27a21beded9d753c856 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 18 Jun 2021 22:55:12 +0500 Subject: [PATCH 05/13] Fix typo --- .../PSVersionInfoGenerator/PSVersionInfoGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs b/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs index 8473a9e4582..aa589e694fa 100644 --- a/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs +++ b/src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs @@ -24,7 +24,7 @@ public void Execute(GeneratorExecutionContext context) var result = CreatePSVersionInfoPartialClass(context); // We must use specific file name suffix (*.g.cs,*.g, *.i.cs, *.generated.cs, *.designer.cs) - // so that Roslyng analizers skip the file. + // so that Roslyn analyzers skip the file. context.AddSource("PSVersionInfo.generated.cs", result); } From c08c0f1bb6a274ce71571a53023d5be5ee9af1d1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 19 Jun 2021 23:57:34 +0500 Subject: [PATCH 06/13] Fix comment --- src/System.Management.Automation/engine/PSVersionInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index fb2177babae..548f05f06ce 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -42,7 +42,7 @@ public static partial class PSVersionInfo private static readonly PSVersionHashTable s_psVersionTable; /* - // The property and fields are generated by PSVersionInfoGenerator source generator. + The following property and fields are generated by PSVersionInfoGenerator source generator. internal static string ProductVersion { get; } = "..."; private static readonly string _rawGitCommitId = "..."; private static readonly string _mainVersion = "..."; From c6b34bb5197abc8074b6be408e182306fda299ba Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 25 Jun 2021 14:07:14 +0500 Subject: [PATCH 07/13] Use ReleaseTag property --- PowerShell.Common.props | 1 + .../PSVersionInfoGenerator/PSVersionInfoGenerator.cs | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/PowerShell.Common.props b/PowerShell.Common.props index 27684a3580c..0a78f26b68c 100644 --- a/PowerShell.Common.props +++ b/PowerShell.Common.props @@ -109,6 +109,7 @@ + - - - - - - - + netstandard2.0 + 9.0 + true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/src/System.Management.Automation/System.Management.Automation.csproj b/src/System.Management.Automation/System.Management.Automation.csproj index d4699b47d78..6020ebc3389 100644 --- a/src/System.Management.Automation/System.Management.Automation.csproj +++ b/src/System.Management.Automation/System.Management.Automation.csproj @@ -1,4 +1,4 @@ - + PowerShell's System.Management.Automation project @@ -6,15 +6,29 @@ System.Management.Automation - - - + + + true + gen\SourceGenerated + - + + + + + + + + + + + + @@ -37,24 +51,14 @@ - - - true - gen\SourceGeneratedFiles - - $(DefineConstants);CORECLR - + + + diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 0705beb56b8..daaa728daea 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -42,10 +42,15 @@ public static partial class PSVersionInfo private static readonly PSVersionHashTable s_psVersionTable; /* - The following property and fields are generated by PSVersionInfoGenerator source generator. - internal static string ProductVersion { get; } = "..."; - private static readonly string _rawGitCommitId = "..."; - private static readonly string _mainVersion = "..."; + The following constants are generated by the source generator 'PSVersionInfoGenerator': + + internal const string ProductVersion; + internal const string GitCommitId; + + private const int Version_Major + private const int Version_Minor; + private const int Version_Patch; + private const string Version_Label; */ /// @@ -84,22 +89,14 @@ static PSVersionInfo() { s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase); - // 'GitCommitId' and 'PSVersion' can be one of the following format examples: - // when powershell is built from a commit: - // productVersion = '6.0.0-beta.7 Commits: 29 SHA: 52c6b...' convert to GitCommitId = 'v6.0.0-beta.7-29-g52c6b...' - // PSVersion = '6.0.0-beta.7' - // when powershell is built from a release tag: - // productVersion = '6.0.0-beta.7 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0-beta.7' - // PSVersion = '6.0.0-beta.7' - // when powershell is built from a release tag for RTM: - // productVersion = '6.0.0 SHA: f1ec9...' convert to GitCommitId = 'v6.0.0' - // PSVersion = '6.0.0' - s_psSemVersion = new SemanticVersion(_mainVersion); + s_psSemVersion = Version_Label == string.Empty + ? new SemanticVersion(Version_Major, Version_Minor, Version_Patch) + : new SemanticVersion(Version_Major, Version_Minor, Version_Patch, Version_Label, buildLabel: null); s_psVersion = (Version)s_psSemVersion; s_psVersionTable[PSVersionInfo.PSVersionName] = s_psSemVersion; s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue; - s_psVersionTable[PSGitCommitIdName] = _rawGitCommitId; + s_psVersionTable[PSGitCommitIdName] = GitCommitId; s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV61Version, s_psV62Version, s_psV7Version, s_psV71Version, s_psV72Version, s_psVersion }; s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion); s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion; @@ -172,14 +169,6 @@ public static Version PSVersion } } - internal static string GitCommitId - { - get - { - return (string)s_psVersionTable[PSGitCommitIdName]; - } - } - internal static Version[] PSCompatibleVersions { get From 7a89b2efd4551757b4c1eecc400ad346b3166634 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 6 Oct 2022 10:49:42 +0500 Subject: [PATCH 10/13] Turn off EmitCompilerGeneratedFiles - Turn off EmitCompilerGeneratedFiles - Remove first blank line in generated code --- .../PSVersionInfoGenerator/PSVersionInfoGenerator.cs | 3 +-- .../System.Management.Automation.csproj | 8 -------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs b/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs index 4674b74fd39..bbda7eee0dc 100644 --- a/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs +++ b/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs @@ -44,8 +44,7 @@ public void Initialize(GeneratorInitializationContext context) private static string CreatePSVersionInfoPartialClass(GeneratorExecutionContext context) { // We must put " + const string SourceTemplate = @"// // This file is auto-generated by PSVersionInfoGenerator. // diff --git a/src/System.Management.Automation/System.Management.Automation.csproj b/src/System.Management.Automation/System.Management.Automation.csproj index 6020ebc3389..27cd25ab629 100644 --- a/src/System.Management.Automation/System.Management.Automation.csproj +++ b/src/System.Management.Automation/System.Management.Automation.csproj @@ -6,12 +6,6 @@ System.Management.Automation - - - true - gen\SourceGenerated - - @@ -58,8 +52,6 @@ - - From 44a795ecc3a4f60c9506e8452efe22e97037a5fa Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 6 Oct 2022 10:08:26 -0700 Subject: [PATCH 11/13] Rename the function to be 'ParsePSVersion' --- .../PSVersionInfoGenerator/PSVersionInfoGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs b/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs index bbda7eee0dc..6f5b47cca01 100644 --- a/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs +++ b/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs @@ -92,7 +92,7 @@ public static partial class PSVersionInfo gitCommitId = gitCommitId.Substring(1); } - var result = FastParsePSVersion(mainVersion); + var result = ParsePSVersion(mainVersion); return string.Format( CultureInfo.InvariantCulture, @@ -105,7 +105,7 @@ public static partial class PSVersionInfo result.preReleaseLabel); } - private static (int major, int minor, int patch, string preReleaseLabel) FastParsePSVersion(string mainVersion) + private static (int major, int minor, int patch, string preReleaseLabel) ParsePSVersion(string mainVersion) { // We only handle the pre-defined PSVersion format here, e.g. 7.x.x or 7.x.x-preview.x int dashIndex = mainVersion.IndexOf('-'); From 77ad8909c7068441ed6a96f6ff89b3b2580f011c Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 6 Oct 2022 12:13:46 -0700 Subject: [PATCH 12/13] Make generated files always visible to VSCode and Visual Studio and not affect re-building --- PowerShell.Common.props | 3 +++ .../System.Management.Automation.csproj | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/PowerShell.Common.props b/PowerShell.Common.props index 53ba4d1a57a..2d5e078f269 100644 --- a/PowerShell.Common.props +++ b/PowerShell.Common.props @@ -94,6 +94,9 @@ + + + + true + gen\SourceGenerated + + From 1e26d0c7ccd13915e0192b0e4d2d4a8621ed9300 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 7 Oct 2022 14:04:14 -0700 Subject: [PATCH 13/13] Address comments --- PowerShell.Common.props | 14 ++++++++++++++ .../PSVersionInfoGenerator.csproj | 7 ++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/PowerShell.Common.props b/PowerShell.Common.props index 38745597007..8f784ede515 100644 --- a/PowerShell.Common.props +++ b/PowerShell.Common.props @@ -73,6 +73,16 @@ $(PSCoreBuildVersion) SHA: $(PSCoreCommitSHA) $(PSCoreBuildVersion) Commits: $(PSCoreAdditionalCommits) SHA: $(PSCoreCommitSHA) + + netstandard2.0 - 9.0 + 10.0 true - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - +