Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 799caf4

Browse files
kzuadalon
authored andcommitted
Implement a better approach than ILMerge for tasks
Instead of ILMerge, use embedded resources for the dependent assemblies, so that on AppDomain.AssembylResolve, we'll fetch those assemblies from the embedded resources. This guarantees that if the right binding redirects are in place (i.e. for nuget, json.net, etc.), we'll "just work" with those newer version, and if they are not and assembly loading fails, we'll use the version we built against. So it's the best of both worlds. The Fody.Costura nuget package will perform both, automatically embed the resources, and while this makes the nuget package a bit larger, it guarantees that it will work in MSBuild 14 as well as 15, even if the tooling for VS only supports VS2017.
1 parent d465a7b commit 799caf4

8 files changed

Lines changed: 9 additions & 50 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Weavers>
2+
<Costura />
3+
</Weavers>

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.Tasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<Reference Include="System.Core" />
1212
</ItemGroup>
1313
<ItemGroup>
14+
<Content Include="FodyWeavers.xml" />
1415
<Content Include="NuGet.Build.Packaging.props">
1516
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1617
<SubType>Designer</SubType>

src/Build/NuGet.Build.Packaging.Tasks/NuGet.Build.Packaging.Tasks.targets

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,42 +118,4 @@ namespace $(RootNamespace)
118118
</ItemGroup>
119119
</Target>
120120

121-
<PropertyGroup>
122-
<ILMerge>$(NuGetPackageRoot)ILRepack\2.0.12\tools\ILRepack.exe</ILMerge>
123-
</PropertyGroup>
124-
125-
<Target Name="ILMerge"
126-
AfterTargets="CoreCompile"
127-
DependsOnTargets="CoreCompile"
128-
Returns="@(MergedAssemblies)"
129-
Condition="'$(Configuration)' == 'Release' And '$(ILMerge)' != 'false'">
130-
<GetReferenceAssemblyPaths BypassFrameworkInstallChecks="False" TargetFrameworkMoniker="$(TargetFrameworkMoniker)">
131-
<Output TaskParameter="FullFrameworkReferenceAssemblyPaths" PropertyName="FullFrameworkReferenceAssemblyPaths" />
132-
</GetReferenceAssemblyPaths>
133-
<ItemGroup>
134-
<MergedAssemblies Include="@(ReferenceCopyLocalPaths)" Condition="$([System.String]::new('%(FileName)').StartsWith('Newtonsoft'))" />
135-
<MergedAssemblies Include="@(ReferenceCopyLocalPaths)" Condition="$([System.String]::new('%(FileName)').StartsWith('NuGet'))" />
136-
<!--<UnmergedAssemblies Include="@(ReferenceCopyLocalPaths)" Exclude="@(MergedAssemblies)" />
137-
<UnmergedAssembliesDir Include="@(UnmergedAssemblies -> '%(RootDir)%(Directory)')" />-->
138-
<ReferenceCopyLocalDirs Include="@(ReferenceCopyLocalPaths -> '%(RootDir)%(Directory)')" />
139-
<ReferenceCopyLocalPaths Remove="@(MergedAssemblies)" />
140-
<LibDir Include="@(ReferenceCopyLocalDirs->Distinct())" />
141-
</ItemGroup>
142-
<PropertyGroup>
143-
<ILMergeArgs Condition="'$(AssemblyOriginatorKeyFile)' != ''">/keyfile:&quot;$(AssemblyOriginatorKeyFile)&quot;</ILMergeArgs>
144-
</PropertyGroup>
145-
<Exec Command="&quot;$(ILMerge)&quot; @(LibDir->'/lib:&quot;%(Identity).&quot;', ' ') $(ILMergeArgs) /targetplatform:&quot;v4,$(FullFrameworkReferenceAssemblyPaths.TrimEnd(\\))&quot; /out:&quot;@(IntermediateAssembly->'%(FullPath)')&quot; &quot;@(IntermediateAssembly->'%(FullPath)')&quot; @(MergedAssemblies->'&quot;%(FullPath)&quot;', ' ')"
146-
WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)"
147-
StandardErrorImportance="high"
148-
StandardOutputImportance="low"
149-
ConsoleToMSBuild="true"
150-
ContinueOnError="true">
151-
<Output TaskParameter="ConsoleOutput" PropertyName="ILMergeOutput"/>
152-
<Output TaskParameter="ExitCode" PropertyName="ExitCode"/>
153-
</Exec>
154-
<Message Importance="high" Text="$(ILMergeOutput)" Condition="'$(ExitCode)' != '0'" />
155-
<Error Text="$(ILMergeOutput)" Condition="'$(ExitCode)' != '0' And '$(ContinueOnError)' != 'true'" />
156-
<Delete Files="@(MergedAssemblies -> '$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" Condition="Exists('$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" />
157-
</Target>
158-
159121
</Project>

src/Build/NuGet.Build.Packaging.Tasks/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"dependencies": {
3+
"Costura.Fody": "1.3.3",
4+
"Fody": "1.29.4",
35
"GitInfo": "1.1.32",
4-
"ILRepack": "2.0.12",
56
"Microsoft.Build": "14.3.0",
67
"Microsoft.Build.Tasks.Core": "14.3.0",
78
"MSBuilder.ThisAssembly.Project": "0.3.1",

src/Build/NuGet.Build.Packaging.Tests/NuGet.Build.Packaging.Tests.targets

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
<!-- Prevents Fody.VerifyTask from running -->
66
<NCrunch>1</NCrunch>
77
</PropertyGroup>
8-
9-
<Target Name="RemoveMergedAssemblies" Condition="'$(Configuration)' == 'Release'" AfterTargets="ResolveNuGetPackageAssets">
10-
<ItemGroup>
11-
<MergedAssemblies Include="@(ReferenceCopyLocalPaths)" Condition="$([System.String]::new('%(FileName)').StartsWith('Newtonsoft'))" />
12-
<MergedAssemblies Include="@(ReferenceCopyLocalPaths)" Condition="$([System.String]::new('%(FileName)').StartsWith('NuGet'))" />
13-
<Reference Remove="@(MergedAssemblies)" />
14-
</ItemGroup>
15-
</Target>
168

179
<Target Name="AfterBuild">
1810
<ItemGroup>

src/Build/msbuild.rsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/p:PackOnBuild=true
2+
/p:PackageOutputPath=..\..\..\out

src/NuGet.Build.Packaging.Shared.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
88
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
99
<RootNamespace>NuGet.Build.Packaging</RootNamespace>
10+
1011
<OutputPath Condition=" '$(OutputPath)' == '' ">bin\$(Configuration)\</OutputPath>
1112
<OutputType>Library</OutputType>
1213
<AppDesignerFolder>Properties</AppDesignerFolder>

src/NuGet.Build.Packaging.Shared.targets

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), NuGet.Restore.targets))\NuGet.Restore.targets"
4-
Condition=" '$(NuGetRestoreImported)' != 'true' " />
5-
63
<Import Project="NuGet.Build.Packaging.props" Condition=" '$(NuGetPackagingPropsImported)' != 'true' " />
74

85
<PropertyGroup Condition=" '$(Language)' == 'C#' ">

0 commit comments

Comments
 (0)