Skip to content

Commit d93395a

Browse files
Copilotkzu
andcommitted
Add support for packing reference assemblies
Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
1 parent 0c48c33 commit d93395a

3 files changed

Lines changed: 155 additions & 0 deletions

File tree

src/NuGetizer.Tasks/NuGetizer.Inference.targets

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Copyright (c) .NET Foundation. All rights reserved.
3636
<!-- Whether to include @(DebugSymbolsProjectOutputGroupOutput) items in the package -->
3737
<PackSymbols Condition="'$(PackSymbols)' == '' and '$(PackBuildOutput)' == 'true'">true</PackSymbols>
3838

39+
<!-- Whether to include the reference assembly ($(TargetRefPath)) in the package when ProduceReferenceAssembly=true -->
40+
<PackRefAssembly Condition="'$(PackRefAssembly)' == '' and '$(PackBuildOutput)' == 'true' and '$(ProduceReferenceAssembly)' == 'true'">true</PackRefAssembly>
41+
3942
<!-- Whether to include framework references (%(ReferencePath.ResolvedFrom)' == '{TargetFrameworkDirectory}') in the package -->
4043
<PackFrameworkReferences Condition="'$(PackFrameworkReferences)' == '' and '$(PackDependencies)' == 'false'">false</PackFrameworkReferences>
4144
<!-- Only default to true if the project isn't a nuget packaging project itself and its primary output is lib. -->
@@ -402,6 +405,14 @@ Copyright (c) .NET Foundation. All rights reserved.
402405
<_InferredPackageFile Include="@(_InferredProjectOutput -> Distinct())" />
403406
</ItemGroup>
404407

408+
<ItemGroup Label="Reference Assembly Inference" Condition="'$(PackRefAssembly)' == 'true' and '$(PackAsPublish)' != 'true' and '$(TargetRefPath)' != ''">
409+
<!-- Include the reference assembly in the ref folder when ProduceReferenceAssembly=true -->
410+
<_InferredPackageFile Include="$(TargetRefPath)">
411+
<PackFolder>Ref</PackFolder>
412+
<FrameworkSpecific>true</FrameworkSpecific>
413+
</_InferredPackageFile>
414+
</ItemGroup>
415+
405416
<ItemGroup Label="SatelliteDll Resources from Dependencies" Condition="'$(PackBuildOutput)' == 'true' and '$(PackAsPublish)' != 'true'">
406417
<!-- Include transitive resources if their NuGetPackageId has PrivateAssets=all and Pack!=false only. -->
407418
<_InferredPackageFile Include="@(ResourceCopyLocalItems)"

src/NuGetizer.Tasks/NuGetizer.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Copyright (c) .NET Foundation. All rights reserved.
134134
</PackFolderKind>
135135
<PackFolderKind Include="Ref">
136136
<PackageFolder>ref</PackageFolder>
137+
<FrameworkSpecific>true</FrameworkSpecific>
137138
</PackFolderKind>
138139
<PackFolderKind Include="Analyzer;Analyzers">
139140
<PackageFolder>analyzers</PackageFolder>

src/NuGetizer.Tests/InlineProjectTests.cs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,5 +1097,148 @@ public void when_dependency_is_development_dependency_then_can_explicitly_pack_i
10971097
Assert.Equal("ThisAssembly.Constants", manifest.Metadata.DependencyGroups.First().Packages.First().Id);
10981098
}
10991099
}
1100+
1101+
[Fact]
1102+
public void when_produce_reference_assembly_then_includes_ref_assembly()
1103+
{
1104+
var result = Builder.BuildProject(
1105+
"""
1106+
<Project Sdk="Microsoft.NET.Sdk">
1107+
<PropertyGroup>
1108+
<TargetFramework>net8.0</TargetFramework>
1109+
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
1110+
</PropertyGroup>
1111+
</Project>
1112+
""", output: output);
1113+
1114+
result.AssertSuccess(output);
1115+
1116+
// Should have the main assembly in lib
1117+
Assert.Contains(result.Items, item => item.Matches(new
1118+
{
1119+
Extension = ".dll",
1120+
PackFolder = PackFolderKind.Lib
1121+
}));
1122+
1123+
// Should also have the reference assembly in ref
1124+
Assert.Contains(result.Items, item => item.Matches(new
1125+
{
1126+
Extension = ".dll",
1127+
PackFolder = PackFolderKind.Ref
1128+
}));
1129+
}
1130+
1131+
[Fact]
1132+
public void when_produce_reference_assembly_false_then_does_not_include_ref_assembly()
1133+
{
1134+
var result = Builder.BuildProject(
1135+
"""
1136+
<Project Sdk="Microsoft.NET.Sdk">
1137+
<PropertyGroup>
1138+
<TargetFramework>net8.0</TargetFramework>
1139+
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
1140+
</PropertyGroup>
1141+
</Project>
1142+
""", output: output);
1143+
1144+
result.AssertSuccess(output);
1145+
1146+
// Should have the main assembly in lib
1147+
Assert.Contains(result.Items, item => item.Matches(new
1148+
{
1149+
Extension = ".dll",
1150+
PackFolder = PackFolderKind.Lib
1151+
}));
1152+
1153+
// Should NOT have a reference assembly in ref
1154+
Assert.DoesNotContain(result.Items, item => item.Matches(new
1155+
{
1156+
PackFolder = PackFolderKind.Ref
1157+
}));
1158+
}
1159+
1160+
[Fact]
1161+
public void when_produce_reference_assembly_but_pack_build_output_false_then_does_not_include_ref_assembly()
1162+
{
1163+
var result = Builder.BuildProject(
1164+
"""
1165+
<Project Sdk="Microsoft.NET.Sdk">
1166+
<PropertyGroup>
1167+
<TargetFramework>net8.0</TargetFramework>
1168+
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
1169+
<PackBuildOutput>false</PackBuildOutput>
1170+
</PropertyGroup>
1171+
</Project>
1172+
""", output: output);
1173+
1174+
result.AssertSuccess(output);
1175+
1176+
// Should NOT have the main assembly in lib
1177+
Assert.DoesNotContain(result.Items, item => item.Matches(new
1178+
{
1179+
Extension = ".dll",
1180+
PackFolder = PackFolderKind.Lib
1181+
}));
1182+
1183+
// Should NOT have a reference assembly in ref either
1184+
Assert.DoesNotContain(result.Items, item => item.Matches(new
1185+
{
1186+
PackFolder = PackFolderKind.Ref
1187+
}));
1188+
}
1189+
1190+
[Fact]
1191+
public void when_produce_reference_assembly_can_opt_out_with_pack_ref_assembly_false()
1192+
{
1193+
var result = Builder.BuildProject(
1194+
"""
1195+
<Project Sdk="Microsoft.NET.Sdk">
1196+
<PropertyGroup>
1197+
<TargetFramework>net8.0</TargetFramework>
1198+
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
1199+
<PackRefAssembly>false</PackRefAssembly>
1200+
</PropertyGroup>
1201+
</Project>
1202+
""", output: output);
1203+
1204+
result.AssertSuccess(output);
1205+
1206+
// Should have the main assembly in lib
1207+
Assert.Contains(result.Items, item => item.Matches(new
1208+
{
1209+
Extension = ".dll",
1210+
PackFolder = PackFolderKind.Lib
1211+
}));
1212+
1213+
// Should NOT have a reference assembly in ref
1214+
Assert.DoesNotContain(result.Items, item => item.Matches(new
1215+
{
1216+
PackFolder = PackFolderKind.Ref
1217+
}));
1218+
}
1219+
1220+
[Fact]
1221+
public void when_produce_reference_assembly_then_ref_assembly_is_framework_specific()
1222+
{
1223+
var result = Builder.BuildProject(
1224+
"""
1225+
<Project Sdk="Microsoft.NET.Sdk">
1226+
<PropertyGroup>
1227+
<TargetFramework>net8.0</TargetFramework>
1228+
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
1229+
<IsPackable>true</IsPackable>
1230+
</PropertyGroup>
1231+
</Project>
1232+
""", output: output);
1233+
1234+
result.AssertSuccess(output);
1235+
1236+
// The ref assembly should have a package path that includes the TFM
1237+
Assert.Contains(result.Items, item => item.Matches(new
1238+
{
1239+
PackFolder = PackFolderKind.Ref,
1240+
PackagePath = "ref/net8.0/scenario.dll"
1241+
}));
1242+
}
11001243
}
11011244
}

0 commit comments

Comments
 (0)