Skip to content

Commit

Permalink
Merge pull request #5 from kzu/dev
Browse files Browse the repository at this point in the history
Dev > Main
  • Loading branch information
kzu authored Sep 30, 2020
2 parents 15006a6 + 0f09cef commit 3b08183
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/NuGetizer.Tasks/CreatePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public override bool Execute()

OutputPackage = new TaskItem(TargetPath);
Manifest.CopyMetadataTo(OutputPackage);
if (emitSpec)
OutputPackage.SetMetadata("Nuspec", NuspecFile);

return !Log.HasLoggedErrors;
}
Expand Down
5 changes: 4 additions & 1 deletion src/NuGetizer.Tasks/NuGetizer.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ Copyright (c) .NET Foundation. All rights reserved.

<Target Name="Pack" DependsOnTargets="$(PackDependsOn)" Returns="@(_PackageTargetPath)" Condition="'$(IsPackable)' == 'true'">
<MakeDir Directories="$(PackageOutputPath)" Condition="!Exists('$(PackageOutputPath)')" />
<CreatePackage Manifest="@(PackageTargetPath)" NuspecFile="$(NuspecFile)" Contents="@(_PackageContent)"
<ItemGroup>
<NuspecFile Include="$(NuspecFile)" />
</ItemGroup>
<CreatePackage Manifest="@(PackageTargetPath)" NuspecFile="@(NuspecFile -> '%(FullPath)')" Contents="@(_PackageContent)"
EmitPackage="$(EmitPackage)" EmitNuspec="$(EmitNuspec)"
TargetPath="@(PackageTargetPath->'%(FullPath)')">
<Output TaskParameter="OutputPackage" ItemName="_PackageTargetPath" />
Expand Down
10 changes: 4 additions & 6 deletions src/NuGetizer.Tasks/dotnet-nugetize.targets
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ Copyright (c) .NET Foundation. All rights reserved.
<Target Name="CopyFilesToOutputDirectory" />

<Target Name="_WritePackageContents" Condition="'$(dotnet-nugetize)' != ''" AfterTargets="GetPackageContents">
<PropertyGroup>
<_AbsoluteNuspecFile Condition="$([System.IO.Path]::IsPathRooted($(NuspecFile)))">$(NuspecFile)</_AbsoluteNuspecFile>
<_AbsoluteNuspecFile Condition="'$(_NuspecFile)' == ''">$(MSBuildProjectDirectory.TrimEnd('\'))\$(NuspecFile)</_AbsoluteNuspecFile>
<_AbsoluteNuspecFile>$([System.IO.Path]::GetFullPath('$(_AbsoluteNuspecFile)'))</_AbsoluteNuspecFile>
</PropertyGroup>
<ItemGroup>
<PackageMetadata Update="@(PackageMetadata)" Nuspec="$(_AbsoluteNuspecFile)" NuPkg="@(PackageTargetPath)" />
<NuspecFile Include="$(NuspecFile)" />
</ItemGroup>
<ItemGroup>
<PackageMetadata Update="@(PackageMetadata)" Nuspec="@(NuspecFile -> '%(FullPath)')" NuPkg="@(PackageTargetPath)" />
<PackageContent Include="@(_PackageContent)" Exclude="@(PackageMetadata)" />
</ItemGroup>
<WriteItemsToFile Condition="'@(PackageMetadata)' != ''" Overwrite="false" Items="@(PackageMetadata)" ItemName="PackageMetadata" File="$(dotnet-nugetize)" />
Expand Down
8 changes: 7 additions & 1 deletion src/NuGetizer.Tests/Builder.NuGetizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static TargetResult BuildProject(
string projectContent = null,
string target = "GetPackageContents",
ITestOutputHelper output = null,
LoggerVerbosity? verbosity = null)
LoggerVerbosity? verbosity = null,
params (string name, string contents)[] files)
{
using var sha = new SHA1Managed();
var hash = Base62.Encode(Math.Abs(BitConverter.ToInt64(
Expand All @@ -40,6 +41,11 @@ public static TargetResult BuildProject(

doc.Save(Path.Combine(scenarioDir, "library.csproj"));

foreach (var file in files)
{
File.WriteAllText(Path.Combine(scenarioDir, file.name), file.contents);
}

var openLog = OpenBuildLogAttribute.IsActive;
try
{
Expand Down
253 changes: 253 additions & 0 deletions src/NuGetizer.Tests/given_packinference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
using System.Linq;
using System.Xml.Linq;
using Xunit;
using Xunit.Abstractions;

namespace NuGetizer
{
public class given_packinference
{
ITestOutputHelper output;

public given_packinference(ITestOutputHelper output) => this.output = output;

[Fact]
public void when_none_has_PackagePath_then_packs()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Include='Library.props' PackagePath='build\netstandard2.0\%(Filename)%(Extension)' />
</ItemGroup>
</Project>",
"GetPackageContents", output);

result.AssertSuccess(output);
Assert.Contains(result.Items, item => item.Matches(new
{
PackagePath = @"build\netstandard2.0\Library.props",
}));
}

[Fact]
public void when_none_has_Kind_then_packs()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Include='Library.props' Kind='build' />
</ItemGroup>
</Project>",
"GetPackageContents", output);

result.AssertSuccess(output);
Assert.Contains(result.Items, item => item.Matches(new
{
Filename = "Library",
Extension = ".props",
}));
}

[Fact]
public void when_none_has_Kind_FrameworkSpecific_then_packs()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Include='Library.props' Kind='build' FrameworkSpecific='true' />
</ItemGroup>
</Project>",
"GetPackageContents", output);

result.AssertSuccess(output);
Assert.Contains(result.Items, item => item.Matches(new
{
PackagePath = @"build\netstandard2.0\Library.props",
}));
}

[Fact]
public void when_PackContent_false_but_content_has_PackagePath_then_packs()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
<PackContent>false</PackContent>
</PropertyGroup>
<ItemGroup>
<Content Include='Library.props' PackagePath='build\netstandard2.0\%(Filename)%(Extension)' />
</ItemGroup>
</Project>",
"GetPackageContents", output);

result.AssertSuccess(output);
Assert.Contains(result.Items, item => item.Matches(new
{
PackagePath = @"build\netstandard2.0\Library.props",
}));
}

[Fact]
public void when_PackContent_false_but_content_has_Pack_then_packs()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
<PackContent>false</PackContent>
</PropertyGroup>
<ItemGroup>
<Content Include='Library.props' Pack='true' />
</ItemGroup>
</Project>",
"GetPackageContents", output);

result.AssertSuccess(output);
Assert.Contains(result.Items, item => item.Matches(new
{
Filename = "Library",
Extension = ".props",
}));
}

[Fact]
public void when_PackContent_false_but_content_has_Kind_then_packs()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
<PackContent>false</PackContent>
</PropertyGroup>
<ItemGroup>
<Content Include='Library.props' Kind='build' />
</ItemGroup>
</Project>",
"GetPackageContents", output);

result.AssertSuccess(output);
Assert.Contains(result.Items, item => item.Matches(new
{
Filename = "Library",
Extension = ".props",
}));
}

[Fact]
public void when_content_has_buildaction_then_spec_has_attribute()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<EmitNuSpec>true</EmitNuSpec>
<EmitPackage>false</EmitPackage>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include='Library.props' BuildAction='None' />
</ItemGroup>
</Project>",
"Pack", output);

result.AssertSuccess(output);

Assert.Single(result.Items);
Assert.NotEmpty(result.Items[0].GetMetadata("Nuspec"));

var nuspec = XDocument.Load(result.Items[0].GetMetadata("Nuspec"));

// Nuspec should contain:
// <files include="any\netstandard2.0\Library.props" buildAction="None" />
Assert.Equal("None", nuspec
.Descendants("{http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd}files")
.Select(x => x.Attribute("buildAction")?.Value)
.FirstOrDefault());
}

[Fact]
public void when_content_has_copytooutput_then_spec_has_attribute()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<EmitNuSpec>true</EmitNuSpec>
<EmitPackage>false</EmitPackage>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include='Library.props' CopyToOutput='true' />
</ItemGroup>
</Project>",
"Pack", output);

result.AssertSuccess(output);

Assert.Single(result.Items);
Assert.NotEmpty(result.Items[0].GetMetadata("Nuspec"));

var nuspec = XDocument.Load(result.Items[0].GetMetadata("Nuspec"));

// Nuspec should contain:
// <files include="any\netstandard2.0\Library.props" copyToOutput="true" />
Assert.Equal("true", nuspec
.Descendants("{http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd}files")
.Select(x => x.Attribute("copyToOutput")?.Value)
.FirstOrDefault());
}

[Fact]
public void when_content_has_copytooutput_flatten_then_spec_has_attributes()
{
var result = Builder.BuildProject(@"
<Project Sdk='Microsoft.NET.Sdk'>
<PropertyGroup>
<PackageId>Library</PackageId>
<EmitNuSpec>true</EmitNuSpec>
<EmitPackage>false</EmitPackage>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include='Library.props' CopyToOutput='true' Flatten='true' />
</ItemGroup>
</Project>",
"Pack", output);

result.AssertSuccess(output);

Assert.Single(result.Items);
Assert.NotEmpty(result.Items[0].GetMetadata("Nuspec"));

var nuspec = XDocument.Load(result.Items[0].GetMetadata("Nuspec"));

// Nuspec should contain:
// <files include="any\netstandard2.0\Library.props" copyToOutput="true" flatten="true" />
Assert.Equal("true", nuspec
.Descendants("{http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd}files")
.Select(x => x.Attribute("copyToOutput")?.Value)
.FirstOrDefault());

Assert.Equal("true", nuspec
.Descendants("{http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd}files")
.Select(x => x.Attribute("flatten")?.Value)
.FirstOrDefault());
}
}
}
Loading

0 comments on commit 3b08183

Please sign in to comment.