|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | + |
| 8 | +namespace SMA |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Source Code Generator to create partial PSVersionInfo class. |
| 12 | + /// </summary> |
| 13 | + [Generator] |
| 14 | + public class PSVersionInfoGenerator : ISourceGenerator |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Generate output PSVersionInfo.g.cs file. |
| 18 | + /// This allows to directly get ProductVersion and others without reflection. |
| 19 | + /// </summary> |
| 20 | + /// <param name="context">Generator execution context.</param> |
| 21 | + public void Execute(GeneratorExecutionContext context) |
| 22 | + { |
| 23 | + var result = CreatePSVersionInfoPartialClass(context); |
| 24 | + |
| 25 | + // We must use specific file name suffix (*.g.cs,*.g, *.i.cs, *.generated.cs, *.designer.cs) |
| 26 | + // so that Roslyn analyzers skip the file. |
| 27 | + context.AddSource("PSVersionInfo.g.cs", result); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Not used. |
| 32 | + /// </summary> |
| 33 | + /// <param name="context">Generator initialization context.</param> |
| 34 | + public void Initialize(GeneratorInitializationContext context) |
| 35 | + { |
| 36 | + // No initialization required for this one. |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Generate source code for the partial PSVersionInfo class. |
| 41 | + /// </summary> |
| 42 | + /// <param name="context">Generator execution context.</param> |
| 43 | + /// <returns>A string with partial PSVersionInfo class.</returns> |
| 44 | + private static string CreatePSVersionInfoPartialClass(GeneratorExecutionContext context) |
| 45 | + { |
| 46 | + // We must put "<auto-generated" on first line so that Roslyng analyzers skip the file. |
| 47 | + const string SourceTemplate = @"// <auto-generated> |
| 48 | +// This file is auto-generated by PSVersionInfoGenerator. |
| 49 | +// </auto-generated> |
| 50 | +
|
| 51 | +namespace System.Management.Automation |
| 52 | +{{ |
| 53 | + public static partial class PSVersionInfo |
| 54 | + {{ |
| 55 | + // Defined in 'PowerShell.Common.props' as 'ProductVersion' |
| 56 | + // Example: |
| 57 | + // - when built from a commit: ProductVersion = '7.3.0-preview.8 Commits: 29 SHA: 52c6b...' |
| 58 | + // - when built from a preview release tag: ProductVersion = '7.3.0-preview.8 SHA: f1ec9...' |
| 59 | + // - when built from a stable release tag: ProductVersion = '7.3.0 SHA: f1ec9...' |
| 60 | + internal const string ProductVersion = ""{0}""; |
| 61 | +
|
| 62 | + // The git commit id that the build is based off. |
| 63 | + // Defined in 'PowerShell.Common.props' as 'PowerShellVersion' or 'ReleaseTag', |
| 64 | + // depending on whether the '-ReleaseTag' is specified when building. |
| 65 | + // Example: |
| 66 | + // - when built from a commit: GitCommitId = '7.3.0-preview.8-29-g52c6b...' |
| 67 | + // - when built from a preview release tag: GitCommitId = '7.3.0-preview.8' |
| 68 | + // - when built from a stable release tag: GitCommitId = '7.3.0' |
| 69 | + internal const string GitCommitId = ""{1}""; |
| 70 | +
|
| 71 | + // The PowerShell version components. |
| 72 | + // The version string is defined in 'PowerShell.Common.props' as 'PSCoreBuildVersion', |
| 73 | + // but we break it into components to save the overhead of parsing at runtime. |
| 74 | + // Example: |
| 75 | + // - '7.3.0-preview.8' for preview release or private build |
| 76 | + // - '7.3.0' for stable release |
| 77 | + private const int Version_Major = {2}; |
| 78 | + private const int Version_Minor = {3}; |
| 79 | + private const int Version_Patch = {4}; |
| 80 | + private const string Version_Label = ""{5}""; |
| 81 | + }} |
| 82 | +}}"; |
| 83 | + |
| 84 | + context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ProductVersion", out var productVersion); |
| 85 | + context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.PSCoreBuildVersion", out var mainVersion); |
| 86 | + context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.PowerShellVersion", out var gitDescribe); |
| 87 | + context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ReleaseTag", out var releaseTag); |
| 88 | + |
| 89 | + string gitCommitId = string.IsNullOrEmpty(releaseTag) ? gitDescribe : releaseTag; |
| 90 | + if (gitCommitId.StartsWith("v")) |
| 91 | + { |
| 92 | + gitCommitId = gitCommitId.Substring(1); |
| 93 | + } |
| 94 | + |
| 95 | + var result = ParsePSVersion(mainVersion); |
| 96 | + |
| 97 | + return string.Format( |
| 98 | + CultureInfo.InvariantCulture, |
| 99 | + SourceTemplate, |
| 100 | + productVersion, |
| 101 | + gitCommitId, |
| 102 | + result.major, |
| 103 | + result.minor, |
| 104 | + result.patch, |
| 105 | + result.preReleaseLabel); |
| 106 | + } |
| 107 | + |
| 108 | + private static (int major, int minor, int patch, string preReleaseLabel) ParsePSVersion(string mainVersion) |
| 109 | + { |
| 110 | + // We only handle the pre-defined PSVersion format here, e.g. 7.x.x or 7.x.x-preview.x |
| 111 | + int dashIndex = mainVersion.IndexOf('-'); |
| 112 | + bool hasLabel = dashIndex != -1; |
| 113 | + string preReleaseLabel = hasLabel ? mainVersion.Substring(dashIndex + 1) : string.Empty; |
| 114 | + |
| 115 | + if (hasLabel) |
| 116 | + { |
| 117 | + mainVersion = mainVersion.Substring(0, dashIndex); |
| 118 | + } |
| 119 | + |
| 120 | + int majorEnd = mainVersion.IndexOf('.'); |
| 121 | + int minorEnd = mainVersion.LastIndexOf('.'); |
| 122 | + |
| 123 | + int major = int.Parse(mainVersion.Substring(0, majorEnd), NumberStyles.Integer, CultureInfo.InvariantCulture); |
| 124 | + int minor = int.Parse(mainVersion.Substring(majorEnd + 1, minorEnd - majorEnd - 1), NumberStyles.Integer, CultureInfo.InvariantCulture); |
| 125 | + int patch = int.Parse(mainVersion.Substring(minorEnd + 1), NumberStyles.Integer, CultureInfo.InvariantCulture); |
| 126 | + |
| 127 | + return (major, minor, patch, preReleaseLabel); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments