|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | +using Microsoft.Build.Framework; |
| 9 | +using Microsoft.Build.Utilities; |
| 10 | + |
| 11 | +namespace Microsoft.DotNet.SourceBuild.Tasks |
| 12 | +{ |
| 13 | + public partial class AddSbrpAttribute : Task |
| 14 | + { |
| 15 | + |
| 16 | + [Required] |
| 17 | + public string[] CSharpPaths { get; set; } |
| 18 | + |
| 19 | + public override bool Execute() |
| 20 | + { |
| 21 | + foreach (string path in CSharpPaths) |
| 22 | + { |
| 23 | + if (AddSbrpAttributeToSpec(path)) |
| 24 | + { |
| 25 | + Log.LogMessage(MessageImportance.Low, $"Added SBRP attribute to {path}"); |
| 26 | + } |
| 27 | + } |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + private bool AddSbrpAttributeToSpec(string CSharpPath) { |
| 32 | + |
| 33 | + string specContent = File.ReadAllText(CSharpPath!); |
| 34 | + |
| 35 | + Match versionMatch = GetVersionRegex().Match(specContent); |
| 36 | + |
| 37 | + if (versionMatch.Success) |
| 38 | + { |
| 39 | + string version = versionMatch.Groups["version"].Value; |
| 40 | + |
| 41 | + if (!GetSbrpAttributeRegex().IsMatch(specContent)) |
| 42 | + { |
| 43 | + string sbrpAttribute = $"[assembly: System.Reflection.AssemblyInformationalVersion(\"{version} originated from source-build-reference-packages\")]"; |
| 44 | + |
| 45 | + specContent = GetVersionRegex().Replace(specContent, $"{versionMatch.Value}{Environment.NewLine}{sbrpAttribute}"); |
| 46 | + |
| 47 | + File.WriteAllText(CSharpPath!, specContent); |
| 48 | + |
| 49 | + return true; |
| 50 | + } |
| 51 | + } |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + [GeneratedRegex(@"\[assembly:\s+System\.Reflection\.AssemblyVersionAttribute\(""(?<version>[0-9.]+)""\)\]")] |
| 56 | + private static partial Regex GetVersionRegex(); |
| 57 | + |
| 58 | + [GeneratedRegex(@"\[assembly:\s+System\.Reflection\.AssemblyInformationalVersion\(""(?<version>.+ originated from source-build-reference-packages)""\)\]")] |
| 59 | + private static partial Regex GetSbrpAttributeRegex(); |
| 60 | + } |
| 61 | +} |
0 commit comments