-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSponsorLink.Analyzer.targets
238 lines (217 loc) · 14.9 KB
/
SponsorLink.Analyzer.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<Project>
<!-- For inclusion in analyzer projects that want to integrate SponsorLink -->
<!-- Import using the following property: -->
<!--<CustomAfterMicrosoftCSharpTargets>$(MSBuildThisFileDirectory)..\SponsorLink\SponsorLink.Analyzer.targets</CustomAfterMicrosoftCSharpTargets>-->
<PropertyGroup>
<ShowSponsorLinkInProject Condition="$(ShowSponsorLinkInProject) == '' and '$(TargetFramework)' == 'netstandard2.0'">true</ShowSponsorLinkInProject>
<!-- This ensures we expose only the main assembly in the Dependencies > Analyzers node -->
<MergeAnalyzerAssemblies Condition="'$(MergeAnalyzerAssemblies)' == '' and '$(Configuration)' == 'Release'">true</MergeAnalyzerAssemblies>
<MergeAnalyzerAssemblies Condition="'$(MergeAnalyzerAssemblies)' == ''">false</MergeAnalyzerAssemblies>
<!-- Whether we merge files or not, dependencies will need to be copy-local -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Make Resources visible to intellisense -->
<CoreCompileDependsOn>CoreResGen;$(CoreCompileDependsOn)</CoreCompileDependsOn>
<!-- Default funding product the Product, which already part of ThisAssembly -->
<FundingProduct Condition="'$(FundingProduct)' == ''">$(Product)</FundingProduct>
<FundingPackageId Condition="'$(FundingPackageId)' == ''">$(PackageId)</FundingPackageId>
<!-- Default prefix is the joined upper-case letters in the product name (i.e. for ThisAssembly, TA) -->
<FundingPrefix Condition="'$(FundingPrefix)' == ''">$([System.Text.RegularExpressions.Regex]::Replace("$(FundingProduct)", "[^A-Z]", ""))</FundingPrefix>
<!-- Default grace days for an expired sponsor manifest or unknown status -->
<FundingGrace Condition="'$(FundingGrace)' == ''">15</FundingGrace>
<!-- Url to use as the helpUrl in diagnostics, to learn more about SL -->
<FundingHelpUrl Condition="'$(FundingHelpUrl)' == ''">https://github.com/devlooped#sponsorlink</FundingHelpUrl>
<!-- Strong-naming analyzers is both unnecessary and additionally unsupported by ILRepack when run as a dotnet tool -->
<SignAssembly Condition="'$(MergeAnalyzerAssemblies)' == 'true'">false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)SponsorLink/*.cs"
Exclude="$(MSBuildThisFileDirectory)SponsorLink/bin/**;$(MSBuildThisFileDirectory)SponsorLink/obj/**"
Source="SponsorLink"/>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)SponsorLink/*.resx"
Exclude="$(MSBuildThisFileDirectory)SponsorLink/bin/**;$(MSBuildThisFileDirectory)SponsorLink/obj/**"
Source="SponsorLink"
Type="Resx"
ManifestResourceName="Devlooped.Sponsors.%(Filename)"/>
<EmbeddedResource Update="$(MSBuildThisFileDirectory)SponsorLink/Resources.resx"
StronglyTypedManifestPrefix="Devlooped.Sponsors"
StronglyTypedClassName="%(Filename)"
StronglyTypedNamespace="Devlooped.Sponsors"
StronglyTypedLanguage="$(Language)" />
<None Include="$(MSBuildThisFileDirectory)SponsorLink/buildTransitive/*.*"
Source="SponsorLink"
PackagePath="buildTransitive/%(Filename)%(Extension)"/>
</ItemGroup>
<ItemGroup Condition="$(ShowSponsorLinkInProject) == 'true'">
<Compile Update="@(Compile -> WithMetadataValue('Source', 'SponsorLink'))">
<Link>SponsorLink\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
<EmbeddedResource Update="@(EmbeddedResource -> WithMetadataValue('Source', 'SponsorLink'))">
<Link>SponsorLink\%(RecursiveDir)%(Filename)%(Extension)</Link>
</EmbeddedResource>
<Content Update="@(Content -> WithMetadataValue('Source', 'SponsorLink'))">
<Link>SponsorLink\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Content>
<None Update="@(None -> WithMetadataValue('Source', 'SponsorLink'))">
<Link>SponsorLink\%(PackagePath)</Link>
</None>
</ItemGroup>
<ItemGroup Condition="$(ShowSponsorLinkInProject) != 'true'">
<Compile Update="@(Compile -> WithMetadataValue('Source', 'SponsorLink'))">
<Visible>false</Visible>
</Compile>
<EmbeddedResource Update="@(EmbeddedResource -> WithMetadataValue('Source', 'SponsorLink'))">
<Visible>false</Visible>
</EmbeddedResource>
<Content Update="@(Content -> WithMetadataValue('Source', 'SponsorLink'))">
<Visible>false</Visible>
</Content>
<None Update="@(None -> WithMetadataValue('Source', 'SponsorLink'))">
<Visible>false</Visible>
</None>
</ItemGroup>
<PropertyGroup>
<PackMergedAssemblies>true</PackMergedAssemblies>
<PackMergedAssemblies Condition="'$(MergeAnalyzerAssemblies)' == 'true'">false</PackMergedAssemblies>
</PropertyGroup>
<ItemGroup Condition="'$(ManagePackageVersionsCentrally)' == 'true'">
<PackageReference Include="Humanizer.Core" VersionOverride="2.14.1" PrivateAssets="all" Pack="$(PackMergedAssemblies)" />
<PackageReference Include="Humanizer.Core.es" VersionOverride="2.14.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" VersionOverride="8.2.1" PrivateAssets="all" Pack="$(PackMergedAssemblies)" />
<PackageReference Include="ILRepack" Version="2.0.35" VersionOverride="all" PrivateAssets="all" Pack="false" />
</ItemGroup>
<ItemGroup Condition="'$(ManagePackageVersionsCentrally)' != 'true'">
<PackageReference Include="Humanizer.Core" Version="2.14.1" PrivateAssets="all" Pack="$(PackMergedAssemblies)" />
<PackageReference Include="Humanizer.Core.es" Version="2.14.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.1" PrivateAssets="all" Pack="$(PackMergedAssemblies)" />
<PackageReference Include="ILRepack" Version="2.0.35" PrivateAssets="all" Pack="false" />
</ItemGroup>
<Target Name="EmitFunding" BeforeTargets="CompileDesignTime;CoreCompile" Inputs="$(MSBuildAllProjects)" Outputs="$(IntermediateOutputPath)SponsorLink.g.cs">
<Warning Condition="'$(FundingPackageId)' == ''" Code="SL001"
Text="Could not determine value of FundingPackageId (defaulted to PackageId). Defaulting it to FundingProduct ('$(FundingProduct)'). Make sure this matches the containing package id, or set an explicit value." />
<PropertyGroup>
<!-- Default to Product, which is most common for single-package products (i.e. Moq) -->
<FundingPackageId Condition="'$(FundingPackageId)' == ''">$(FundingProduct)</FundingPackageId>
</PropertyGroup>
<ItemGroup Condition="'@(FundingAnalyzerPackageId)' == ''">
<FundingAnalyzerPackageId Include="$(FundingPackageId)" />
</ItemGroup>
<ItemGroup>
<_FundingAnalyzerPackageId Include="@(FundingAnalyzerPackageId -> '"%(Identity)"')" />
</ItemGroup>
<PropertyGroup>
<_FundingPackageIds>@(_FundingAnalyzerPackageId, ',')</_FundingPackageIds>
</PropertyGroup>
<PropertyGroup>
<SponsorLinkPartial>using System.Collections.Generic%3B
namespace Devlooped.Sponsors%3B
partial class SponsorLink
{
public partial class Funding
{
public static HashSet<string> PackageIds { get%3B } = [$(_FundingPackageIds)]%3B
public const string Product = "$(FundingProduct)"%3B
public const string Prefix = "$(FundingPrefix)"%3B
public const string HelpUrl = "$(FundingHelpUrl)"%3B
public const int Grace = $(FundingGrace)%3B
}
}
</SponsorLinkPartial>
</PropertyGroup>
<WriteLinesToFile File="$(IntermediateOutputPath)SponsorLink.g.cs" Lines="$(SponsorLinkPartial)" WriteOnlyWhenDifferent="true" Overwrite="true" />
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)SponsorLink.g.cs" />
</ItemGroup>
</Target>
<Target Name="EnsureILRepack" BeforeTargets="ILRepack" Condition="'$(MergeAnalyzerAssemblies)' == 'true'">
<Exec Command="ilrepack --version" StandardErrorImportance="high" StandardOutputImportance="low" ConsoleToMSBuild="true" IgnoreExitCode="true" ContinueOnError="true">
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" />
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
</Exec>
<Message Importance="high" Text="Using installed dotnet-ilrepack v$(ILRepackOutput)" Condition="$(ExitCode) == '0'" />
<Exec Command="dotnet tool install -g dotnet-ilrepack"
Condition="$(ExitCode) != '0'" />
<Exec Command="ilrepack --version" Condition="$(ExitCode) != '0'" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackInstalledOutput" />
</Exec>
<Message Importance="high" Text="Installed dotnet-ilrepack v$(ILRepackInstalledOutput)" Condition="$(ExitCode) != '0'" />
</Target>
<Target Name="ILRepack" AfterTargets="CoreCompile" BeforeTargets="CopyFilesToOutputDirectory"
Inputs="@(IntermediateAssembly -> '%(FullPath)')"
Outputs="$(IntermediateOutputPath)ilrepack.txt"
Returns="@(MergedAssemblies)"
Condition="Exists(@(IntermediateAssembly -> '%(FullPath)')) And '$(MergeAnalyzerAssemblies)' == 'true'">
<ItemGroup>
<ReferenceCopyLocalAssemblies Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)' == '.dll'
And !$([MSBuild]::ValueOrDefault('%(FileName)', '').EndsWith('.resources', StringComparison.OrdinalIgnoreCase))" />
<MergedAssemblies Include="@(ReferenceCopyLocalAssemblies)" Condition="
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CodeAnalysis', StringComparison.OrdinalIgnoreCase)) And
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CSharp', StringComparison.OrdinalIgnoreCase)) And
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.', StringComparison.OrdinalIgnoreCase))"
/>
<!-- Brings in System/Microsoft.IdentityModel, System.Text.Encodings.Web, System.Text.Json, Humanizer -->
<MergedAssemblies Include="@(ReferenceCopyLocalAssemblies)" Condition="
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.IdentityModel', StringComparison.OrdinalIgnoreCase)) Or
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.IdentityModel', StringComparison.OrdinalIgnoreCase)) Or
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.Text', StringComparison.OrdinalIgnoreCase)) Or
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Humanizer', StringComparison.OrdinalIgnoreCase))"
/>
</ItemGroup>
<ItemGroup>
<ReferenceCopyLocalDirs Include="@(ReferenceCopyLocalPaths -> '%(RootDir)%(Directory)')" />
<ReferenceCopyLocalPaths Remove="@(MergedAssemblies)" />
<LibDir Include="@(ReferenceCopyLocalDirs -> Distinct())" />
</ItemGroup>
<PropertyGroup>
<AbsoluteAssemblyOriginatorKeyFile Condition="'$(SignAssembly)' == 'true' and '$(AssemblyOriginatorKeyFile)' != ''">$([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','$(AssemblyOriginatorKeyFile)'))))</AbsoluteAssemblyOriginatorKeyFile>
<ILRepackArgs Condition="'$(AbsoluteAssemblyOriginatorKeyFile)' != ''">/keyfile:"$(AbsoluteAssemblyOriginatorKeyFile)" /delaysign</ILRepackArgs>
<ILRepackArgs>$(ILRepackArgs) /internalize</ILRepackArgs>
<ILRepackArgs>$(ILRepackArgs) /union</ILRepackArgs>
<!-- This is needed to merge types with identical names into one, wich happens with IFluentInterface in Merq and Merq.Core (Xamarin.Messaging dependencies) -->
<ILRepackArgs>$(ILRepackArgs) @(LibDir -> '/lib:"%(Identity)."', ' ')</ILRepackArgs>
<ILRepackArgs>$(ILRepackArgs) /out:"@(IntermediateAssembly -> '%(FullPath)')"</ILRepackArgs>
<ILRepackArgs>$(ILRepackArgs) "@(IntermediateAssembly -> '%(FullPath)')"</ILRepackArgs>
<ILRepackArgs>$(ILRepackArgs) @(MergedAssemblies -> '"%(FullPath)"', ' ')</ILRepackArgs>
<!--<ILRepackArgs>$(ILRepackArgs) "/lib:$(NetstandardDirectory)"</ILRepackArgs> -->
<!-- This is needed for ilrepack to find netstandard.dll, which is referenced by the System.Text.Json assembly -->
</PropertyGroup>
<Exec Command='ilrepack $(ILRepackArgs)' WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" StandardErrorImportance="high" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="low" ConsoleToMSBuild="true" ContinueOnError="true">
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" />
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
</Exec>
<Message Importance="high" Text="$(ILRepackOutput)" Condition="'$(ExitCode)' != '0'" />
<Delete Files="$(IntermediateOutputPath)ilrepack.txt" Condition="'$(ExitCode)' != '0'" />
<Touch AlwaysCreate="true" Files="$(IntermediateOutputPath)ilrepack.txt" Condition="'$(ExitCode)' == '0'" />
<Error Text="$(ILRepackOutput)" Condition="'$(ExitCode)' != '0' And '$(ContinueOnError)' != 'true'" />
<ItemGroup>
<MergedAssembliesToRemove Include="@(MergedAssemblies)" />
<MergedAssembliesToRemove Remove="@(ReferenceToPreserve)" />
</ItemGroup>
<Delete Files="@(MergedAssembliesToRemove -> '$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" Condition="Exists('$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" />
</Target>
<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
<Exec Command="curl --silent --output $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk https://sponsorlink.devlooped.com/jwk" />
</Target>
<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
<PropertyGroup>
<!-- Read public key we validate manifests against -->
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk'))</DevloopedJwk>
</PropertyGroup>
<ItemGroup>
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk)" />
</ItemGroup>
</Target>
<!-- Make sure dependencies are available in the package when not merging (i.e. local dev) -->
<Target Name="PackCopyLocalLockFileAssemblies" Condition="'$(MergeAnalyzerAssemblies)' != 'true'"
BeforeTargets="GetPackageContents" DependsOnTargets="ReferenceCopyLocalPathsOutputGroup">
<ItemGroup>
<PackageFile Include="@(ReferenceCopyLocalPathsOutputGroupOutput)" Condition="
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CodeAnalysis', StringComparison.OrdinalIgnoreCase)) And
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CSharp', StringComparison.OrdinalIgnoreCase))"
PackFolder="$(PackFolder)" />
</ItemGroup>
</Target>
<PropertyGroup>
<!-- Can be used in analyzer projects to detect double imports -->
<SponsorLinkImported>true</SponsorLinkImported>
</PropertyGroup>
</Project>