Skip to content

Commit ed97588

Browse files
committed
Repack tasks extensions to avoid collisions with other analyzers
This is something we got for free from the SponsorLink v2 targets and we missed in the migration to OSMF.
1 parent 38ebe4b commit ed97588

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

src/Directory.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
<ItemGroup>
3636
<None Include="$(MSBuildThisFileDirectory)..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
37+
<ILRepackInclude Include="System.Threading.Tasks.Extensions" />
38+
<ILRepackExclude Include="ThisAssembly.Constants" />
3739
</ItemGroup>
3840

3941
</Project>

src/Directory.targets

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,10 @@
2626
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CSharp', StringComparison.OrdinalIgnoreCase)) And
2727
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.', StringComparison.OrdinalIgnoreCase))"
2828
/>
29-
<!-- Brings in System/Microsoft.IdentityModel, System.Text.Encodings.Web, System.Text.Json -->
30-
<PackCopyLocalAssemblies Include="@(ReferenceCopyLocalAssemblies)" Condition="
31-
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.IdentityModel', StringComparison.OrdinalIgnoreCase)) Or
32-
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.IdentityModel', StringComparison.OrdinalIgnoreCase)) Or
33-
$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.Text', StringComparison.OrdinalIgnoreCase))"
34-
/>
3529
<PackageFile Include="@(PackCopyLocalAssemblies)" PackFolder="$(PackFolder)" />
3630
</ItemGroup>
3731
</Target>
38-
32+
3933
<Target Name="SetLocalVersion" Condition="!$(CI)">
4034
<GetVersion>
4135
<Output TaskParameter="Version" PropertyName="Version" />
@@ -66,4 +60,5 @@
6660
</Task>
6761
</UsingTask>
6862

63+
<Import Project="ILRepack.targets" Condition="'$(IsAnalyzer)' == 'true'" />
6964
</Project>

src/ILRepack.targets

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<Project>
2+
<!--
3+
ILRepack.targets provides MSBuild integration for ILRepack, allowing merging of assemblies into a single output.
4+
5+
Extension Points:
6+
- Property 'ILRepack': Set to 'true' to enable ILRepack (default: true in Release, false otherwise).
7+
- Item 'ILRepackInclude': list of assembly filenames to explicitly include in merging.
8+
- Item 'ILRepackExclude': list of assembly filenames to exclude from merging.
9+
- Item 'ILRepackPreserve': Assemblies that should not be deleted after merging, even if merged.
10+
-->
11+
12+
<PropertyGroup>
13+
<ILRepack Condition="'$(ILRepack)' == '' and '$(Configuration)' == 'Release'">true</ILRepack>
14+
<ILRepack Condition="'$(ILRepack)' == ''">false</ILRepack>
15+
<!-- We need to turn on copy-local for ILRepack to find deps -->
16+
<CopyLocalLockFileAssemblies Condition="'$(CopyLocalLockFileAssemblies)' == '' and '$(ILRepack)' == 'true'">true</CopyLocalLockFileAssemblies>
17+
</PropertyGroup>
18+
19+
<Target Name="EnsureILRepack" BeforeTargets="ILRepack" Condition="'$(ILRepack)' == 'true'">
20+
<Exec Command="ilrepack --version" StandardErrorImportance="high" StandardOutputImportance="low" ConsoleToMSBuild="true" IgnoreExitCode="true" ContinueOnError="true">
21+
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" />
22+
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
23+
</Exec>
24+
<Message Importance="high" Text="Using installed dotnet-ilrepack v$(ILRepackOutput)" Condition="$(ExitCode) == '0'" />
25+
<Exec Command="dotnet tool install -g dotnet-ilrepack"
26+
Condition="$(ExitCode) != '0'" />
27+
<Exec Command="ilrepack --version" Condition="$(ExitCode) != '0'" ConsoleToMSBuild="true">
28+
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackInstalledOutput" />
29+
</Exec>
30+
<Message Importance="high" Text="Installed dotnet-ilrepack v$(ILRepackInstalledOutput)" Condition="$(ExitCode) != '0'" />
31+
</Target>
32+
33+
<Target Name="ILRepack" AfterTargets="CoreCompile" BeforeTargets="CopyFilesToOutputDirectory"
34+
Inputs="@(IntermediateAssembly -&gt; '%(FullPath)')"
35+
Outputs="$(IntermediateOutputPath)ilrepack.txt"
36+
Returns="@(MergedAssemblies)"
37+
Condition="Exists(@(IntermediateAssembly -&gt; '%(FullPath)')) And '$(ILRepack)' == 'true'">
38+
<PropertyGroup>
39+
<ILRepackInclude>;@(ILRepackInclude, ';');</ILRepackInclude>
40+
<ILRepackExclude>;@(ILRepackExclude, ';');</ILRepackExclude>
41+
</PropertyGroup>
42+
<ItemGroup>
43+
<ReferenceCopyLocalAssemblies Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)' == '.dll'
44+
And !$([MSBuild]::ValueOrDefault('%(FileName)', '').EndsWith('.resources', StringComparison.OrdinalIgnoreCase))" />
45+
46+
<!-- Default to never mergingmerging analyzer, C# or system assemblies -->
47+
<MergedAssemblies Include="@(ReferenceCopyLocalAssemblies)" Condition="
48+
!$(ILRepackExclude.Contains(';%(FileName);')) And
49+
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CodeAnalysis', StringComparison.OrdinalIgnoreCase)) And
50+
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CSharp', StringComparison.OrdinalIgnoreCase)) And
51+
!$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.', StringComparison.OrdinalIgnoreCase))"
52+
/>
53+
<!-- Brings in explicitly opted-in assemblies -->
54+
<MergedAssemblies Include="@(ReferenceCopyLocalAssemblies)" Condition="$(ILRepackInclude.Contains(';%(FileName);'))" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ReferenceCopyLocalDirs Include="@(ReferenceCopyLocalPaths -&gt; '%(RootDir)%(Directory)')" />
58+
<ReferenceCopyLocalPaths Remove="@(MergedAssemblies)" />
59+
<LibDir Include="@(ReferenceCopyLocalDirs -&gt; Distinct())" />
60+
</ItemGroup>
61+
<PropertyGroup>
62+
<AbsoluteAssemblyOriginatorKeyFile Condition="'$(SignAssembly)' == 'true' and '$(AssemblyOriginatorKeyFile)' != ''">$([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','$(AssemblyOriginatorKeyFile)'))))</AbsoluteAssemblyOriginatorKeyFile>
63+
<ILRepackArgs Condition="'$(AbsoluteAssemblyOriginatorKeyFile)' != ''">/keyfile:"$(AbsoluteAssemblyOriginatorKeyFile)" /delaysign</ILRepackArgs>
64+
<ILRepackArgs>$(ILRepackArgs) /internalize</ILRepackArgs>
65+
<ILRepackArgs>$(ILRepackArgs) /union</ILRepackArgs>
66+
<!-- This is needed to merge types with identical names into one, wich happens with IFluentInterface in Merq and Merq.Core (Xamarin.Messaging dependencies) -->
67+
<ILRepackArgs>$(ILRepackArgs) @(LibDir -&gt; '/lib:"%(Identity)."', ' ')</ILRepackArgs>
68+
<ILRepackArgs>$(ILRepackArgs) /out:"@(IntermediateAssembly -&gt; '%(FullPath)')"</ILRepackArgs>
69+
<ILRepackArgs>$(ILRepackArgs) "@(IntermediateAssembly -&gt; '%(FullPath)')"</ILRepackArgs>
70+
<ILRepackArgs>$(ILRepackArgs) @(MergedAssemblies -&gt; '"%(FullPath)"', ' ')</ILRepackArgs>
71+
<!--<ILRepackArgs>$(ILRepackArgs) "/lib:$(NetstandardDirectory)"</ILRepackArgs> -->
72+
<!-- This is needed for ilrepack to find netstandard.dll, which is referenced by the System.Text.Json assembly -->
73+
</PropertyGroup>
74+
<Exec Command='ilrepack $(ILRepackArgs)' WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" StandardErrorImportance="high" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="low" ConsoleToMSBuild="true" ContinueOnError="true">
75+
<Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" />
76+
<Output TaskParameter="ExitCode" PropertyName="ExitCode" />
77+
</Exec>
78+
<Message Importance="high" Text="$(ILRepackOutput)" Condition="'$(ExitCode)' != '0'" />
79+
<Delete Files="$(IntermediateOutputPath)ilrepack.txt" Condition="'$(ExitCode)' != '0'" />
80+
<Touch AlwaysCreate="true" Files="$(IntermediateOutputPath)ilrepack.txt" Condition="'$(ExitCode)' == '0'" />
81+
<Error Text="$(ILRepackOutput)" Condition="'$(ExitCode)' != '0' And '$(ContinueOnError)' != 'true'" />
82+
<ItemGroup>
83+
<MergedAssembliesToRemove Include="@(MergedAssemblies)" />
84+
<MergedAssembliesToRemove Remove="@(ILRepackPreserve)" />
85+
</ItemGroup>
86+
<Delete Files="@(MergedAssembliesToRemove -&gt; '$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" Condition="Exists('$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" />
87+
</Target>
88+
89+
</Project>

src/ThisAssembly.Tests/ThisAssembly.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2828
<WarningsAsErrors>true</WarningsAsErrors>
2929
<RuntimeIdentifiers>win-x64;linux-x86</RuntimeIdentifiers>
30+
<ILRepack>false</ILRepack>
3031
</PropertyGroup>
3132

3233
<Import Project="..\*\ThisAssembly*.props" />

0 commit comments

Comments
 (0)