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

Commit 986e31f

Browse files
committed
Enable emitting the nuspec for troubleshooting purposes
Setting the $(EmitNuspec) will cause the creation of the nuspec in the default location of `$(OutputPath)\$(PackageId).nuspec`, unless overriden by an (SDK Pack compatible) `NuspecFile` property.
1 parent a804f32 commit 986e31f

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/Build/NuGet.Build.Packaging.Tasks/CreatePackage.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class CreatePackage : Task
2424
[Required]
2525
public string TargetPath { get; set; }
2626

27+
public string NuspecFile { get; set; }
28+
2729
[Output]
2830
public ITaskItem OutputPackage { get; set; }
2931

@@ -112,7 +114,7 @@ where item.GetMetadata(MetadataName.Kind) == PackageItemKind.Dependency &&
112114
Version = VersionRange.Parse(item.GetMetadata(MetadataName.Version)),
113115
TargetFramework = item.GetNuGetTargetFramework()
114116
};
115-
117+
116118
manifest.Metadata.DependencyGroups = (from dependency in dependencies
117119
group dependency by dependency.TargetFramework into dependenciesByFramework
118120
select new PackageDependencyGroup
@@ -190,6 +192,15 @@ void BuildPackage(Stream output)
190192
new PhysicalPackageFile { SourcePath = file.Source, TargetPath = file.Target }));
191193

192194
builder.Save(output);
195+
196+
if (!string.IsNullOrEmpty(NuspecFile))
197+
{
198+
Directory.CreateDirectory(Path.GetDirectoryName(NuspecFile));
199+
using (var stream = File.Create(NuspecFile))
200+
{
201+
manifest.Save(stream, true);
202+
}
203+
}
193204
}
194205

195206
static VersionRange AggregateVersions(VersionRange aggregate, VersionRange next)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Copyright (c) .NET Foundation. All rights reserved.
3535

3636
<!-- This file signals that the Build should actually do a Pack. Supports the IDE solution-wide Build command for authoring projects. -->
3737
<PackOnBuildFile>$(MSBuildProjectDirectory)\.packonbuild</PackOnBuildFile>
38+
39+
<!-- Whether to emit the nuspec that's used to create the final package -->
40+
<EmitNuSpec Condition="'$(EmitNuSpec)' == ''">false</EmitNuSpec>
41+
<NuspecFile Condition="'$(NuspecFile)' == ''">$(OutputPath)\$(PackageId).nuspec</NuspecFile>
3842
</PropertyGroup>
3943

4044
<Import Project="NuGet.Build.Packaging.Inference.targets" Condition="'$(InferPackageContents)' != 'false'" />
@@ -364,7 +368,10 @@ Copyright (c) .NET Foundation. All rights reserved.
364368
-->
365369
<Target Name="Pack" DependsOnTargets="$(PackDependsOn)" Returns="@(PackageTargetPath)" Condition="'$(IsPackable)' == 'true'">
366370
<MakeDir Directories="$(PackageOutputPath)" Condition="!Exists('$(PackageOutputPath)')" />
367-
<CreatePackage Manifest="@(PackageTargetPath)" Contents="@(_PackageContent)" TargetPath="@(PackageTargetPath->'%(FullPath)')">
371+
<PropertyGroup>
372+
<NuspecFile Condition="'$(EmitNuspec)' == 'true' And '$(NuspecFile)' ==''">$(OutputPath)\$(PackageId).nuspec</NuspecFile>
373+
</PropertyGroup>
374+
<CreatePackage Manifest="@(PackageTargetPath)" NuspecFile="$(NuspecFile)" Contents="@(_PackageContent)" TargetPath="@(PackageTargetPath->'%(FullPath)')">
368375
<Output TaskParameter="OutputPackage" ItemName="_PackageTargetPath" />
369376
<Output TaskParameter="OutputPackage" ItemName="FileWrites" />
370377
</CreatePackage>

src/Build/NuGet.Build.Packaging.Tests/CreatePackageTests.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,18 @@ public void when_creating_package_with_empty_dependency_groups_then_succeeds()
189189
}),
190190
};
191191

192+
task.NuspecFile = Path.GetTempFileName();
193+
createPackage = Debugger.IsAttached;
194+
192195
var manifest = ExecuteTask();
193196

197+
Process.Start("notepad.exe", task.NuspecFile);
198+
194199
Assert.NotNull(manifest);
195200
Assert.Equal(4, manifest.Metadata.DependencyGroups.Count());
196201
Assert.All(manifest.Metadata.DependencyGroups, d => Assert.Empty(d.Packages));
197-
198-
//Assert.Equal(NuGetFramework.Parse(".NETFramework,Version=v4.5"), manifest.Metadata.DependencyGroups.First().TargetFramework);
199-
//Assert.Equal(1, manifest.Metadata.DependencyGroups.First().Packages.Count());
200-
//Assert.Equal("Newtonsoft.Json", manifest.Metadata.DependencyGroups.First().Packages.First().Id);
201-
202-
//// We get a version range actually for the specified dependency, like [1.0.0,)
203-
//Assert.Equal("8.0.0", manifest.Metadata.DependencyGroups.First().Packages.First().VersionRange.MinVersion.ToString());
204202
}
205203

206-
207204
[Fact]
208205
public void when_creating_package_with_development_dependency_then_does_not_generate_dependency_group()
209206
{

0 commit comments

Comments
 (0)