Skip to content

Commit 88b8727

Browse files
committed
Generate SBRP attribute with PackageSourceGenerator
1 parent ba2ae03 commit 88b8727

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/packageSourceGenerator/PackageSourceGenerator.proj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
GenerateReferencePackageSource;
8080
GenerateTextOnlyPackageSource;
8181
GeneratePackageProject;
82+
AddSbrpAttribute;
8283
RewriteNuspec;
8384
EndGeneratePackageSource;
8485
InvokePackageDependencies" />
@@ -285,6 +286,22 @@
285286
Importance="high" />
286287
</Target>
287288

289+
<!-- Add SBRP attribute to CSharp file and copy to target directory. -->
290+
<UsingTask TaskName="AddSbrpAttribute" AssemblyFile="$(PackageSourceGeneratorTaskAssembly)" />
291+
<Target Name="AddSbrpAttribute"
292+
Condition="'@(PackageCompileItem)' != '' or '$(PackageType)' == 'text'">
293+
294+
<ItemGroup>
295+
<PackageCSharpFiles Include="$(PackageTargetDirectory)**/*.cs" />
296+
</ItemGroup>
297+
298+
<AddSbrpAttribute CSharpPaths="@(PackageCSharpFiles)" />
299+
300+
<Message Text="$(MSBuildProjectName) -> @(PackageCSharpFiles)"
301+
Importance="high" />
302+
303+
</Target>
304+
288305
<!-- Rewrite nuspec and copy to target directory. -->
289306
<UsingTask TaskName="RewriteNuspec" AssemblyFile="$(PackageSourceGeneratorTaskAssembly)" />
290307
<Target Name="RewriteNuspec"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)