Skip to content

Commit 7bfcda9

Browse files
committed
Update generator to add sbrp attribute
1 parent ba2ae03 commit 7bfcda9

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/packageSourceGenerator/PackageSourceGenerator.proj

Lines changed: 22 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,27 @@
285286
Importance="high" />
286287
</Target>
287288

289+
<!-- Add SBRP attribute to CSharp file and copy to target directory. -->
290+
<ItemGroup>
291+
<PackageCSharpFiles Include="$(PackageTargetDirectory)**/*.cs" />
292+
</ItemGroup>
293+
294+
<UsingTask TaskName="AddSbrpAttribute" AssemblyFile="$(PackageSourceGeneratorTaskAssembly)" />
295+
<Target Name="AddSbrpAttribute"
296+
Condition="'@(PackageCompileItem)' != '' or '$(PackageType)' == 'text'">
297+
298+
<PropertyGroup>
299+
<PackageCSharpTargetPath>%(PackageCSharpFiles.Identity)</PackageCSharpTargetPath>
300+
</PropertyGroup>
301+
302+
<AddSbrpAttribute CSharpPath="$(PackageCSharpTargetPath)"
303+
TargetPath="$(PackageCSharpTargetPath)" />
304+
305+
<Message Text="$(MSBuildProjectName) -> $(PackageProjectTargetPath)"
306+
Importance="high" />
307+
308+
</Target>
309+
288310
<!-- Rewrite nuspec and copy to target directory. -->
289311
<UsingTask TaskName="RewriteNuspec" AssemblyFile="$(PackageSourceGeneratorTaskAssembly)" />
290312
<Target Name="RewriteNuspec"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.Text.RegularExpressions;
7+
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Utilities;
9+
10+
namespace Microsoft.DotNet.SourceBuild.Tasks
11+
{
12+
public partial class AddSbrpAttribute : Task
13+
{
14+
15+
[Required]
16+
public string? CSharpPath { get; set; }
17+
18+
[Required]
19+
public string? TargetPath { get; set; }
20+
21+
public override bool Execute()
22+
{
23+
string specContent = File.ReadAllText(CSharpPath!);
24+
25+
Match versionMatch = GetVersionRegex().Match(specContent);
26+
27+
if (versionMatch.Success)
28+
{
29+
string version = versionMatch.Groups["version"].Value;
30+
31+
if (!GetSbrpAttributeRegex().IsMatch(specContent))
32+
{
33+
string sbrpAttribute = $"[assembly: AssemblyInformationalVersion(\"{version} originated from source-build-reference-packages\")]";
34+
35+
specContent = GetVersionRegex().Replace(specContent, $"{versionMatch.Value}{Environment.NewLine}{sbrpAttribute}");
36+
37+
File.WriteAllText(TargetPath!, specContent);
38+
39+
}
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
[GeneratedRegex(@"\[assembly:\s+System\.Reflection\.AssemblyVersionAttribute\(""(?<version>[0-9.]+)""\)\]")]
46+
private static partial Regex GetVersionRegex();
47+
48+
[GeneratedRegex(@"\[assembly:\s+AssemblyInformationalVersion\(""(?<version>.+ originated from source-build-reference-packages)""\)\]")]
49+
private static partial Regex GetSbrpAttributeRegex();
50+
}
51+
}

0 commit comments

Comments
 (0)