File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
src/packageSourceGenerator
PackageSourceGeneratorTask Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 79
79
GenerateReferencePackageSource;
80
80
GenerateTextOnlyPackageSource;
81
81
GeneratePackageProject;
82
+ AddSbrpAttribute;
82
83
RewriteNuspec;
83
84
EndGeneratePackageSource;
84
85
InvokePackageDependencies" />
285
286
Importance =" high" />
286
287
</Target >
287
288
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
+
288
310
<!-- Rewrite nuspec and copy to target directory. -->
289
311
<UsingTask TaskName =" RewriteNuspec" AssemblyFile =" $(PackageSourceGeneratorTaskAssembly)" />
290
312
<Target Name =" RewriteNuspec"
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments