Skip to content

Commit e1db917

Browse files
authored
Merge pull request #24 from kzu/dev
Improvements on transitive package content with relative paths
2 parents 381be5d + fefa14c commit e1db917

File tree

11 files changed

+78
-24
lines changed

11 files changed

+78
-24
lines changed

src/NuGetizer.Tasks/AssignPackagePath.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public override bool Execute()
4646

4747
ITaskItem EnsurePackagePath(ITaskItem file, IDictionary<string, ITaskItem> kindMap)
4848
{
49-
var output = new TaskItem(file);
49+
// Switch to full path items
50+
var output = new TaskItem(
51+
file.GetBoolean("IsFile", true) && file.ItemSpec.IndexOfAny(Path.GetInvalidPathChars()) == -1 && File.Exists(file.GetMetadata("FullPath"))
52+
? file.GetMetadata("FullPath")
53+
: file.ItemSpec);
54+
55+
// Preserve existing metadata.
56+
file.CopyMetadataTo(output);
5057

5158
// Map the pack folder to a target top-level directory.
5259
var packFolder = file.GetMetadata("PackFolder");
@@ -106,7 +113,7 @@ ITaskItem EnsurePackagePath(ITaskItem file, IDictionary<string, ITaskItem> kindM
106113
// If a packaging project is requesting the package path assignment,
107114
// perform it regardless of whether there is a PackageId on the items,
108115
// since they all need to be assigned at this point.
109-
bool isPackaging = false;
116+
var isPackaging = false;
110117
isPackaging = !string.IsNullOrEmpty(IsPackaging) &&
111118
bool.TryParse(IsPackaging, out isPackaging) &&
112119
isPackaging;

src/NuGetizer.Tasks/CreatePackage.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ public Manifest Execute(Stream output)
7373
GeneratePackage(output);
7474

7575
output.Seek(0, SeekOrigin.Begin);
76-
using (var reader = new PackageArchiveReader(output))
77-
{
78-
return reader.GetManifest();
79-
}
76+
using var reader = new PackageArchiveReader(output);
77+
return reader.GetManifest();
8078
}
8179

8280
public Manifest CreateManifest()
@@ -293,7 +291,7 @@ string hash(ITaskItem item)
293291
// Support nugetize CLI by ignoring missing files. When creating the final .nupkg,
294292
// file existence is checked always already
295293
if (!File.Exists(item.GetMetadata("FullPath")))
296-
return item.GetMetadata("FullPath");
294+
return item.GetMetadata("FullPath");
297295

298296
using var file = File.OpenRead(item.GetMetadata("FullPath"));
299297
return string.Concat(md5.Value.ComputeHash(file).Select(x => x.ToString("x2")));

src/NuGetizer.Tasks/Extensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ public static bool TryGetBoolMetadata(this ITaskItem taskItem, string metadataNa
3838

3939
public static bool GetBoolean(this ITaskItem taskItem, string metadataName, bool defaultValue = false)
4040
{
41-
var result = false;
4241
var metadataValue = taskItem.GetMetadata(metadataName);
4342

44-
return bool.TryParse(metadataValue, out result) && result;
43+
return bool.TryParse(metadataValue, out var result) ? result : defaultValue;
4544
}
4645

4746
public static string GetNullableMetadata(this ITaskItem taskItem, string metadataName)

src/NuGetizer.Tasks/NuGetizer.Inference.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Copyright (c) .NET Foundation. All rights reserved.
209209
.NETFramework, the desktop WinFX.targets are imported which don't have the fix, so we need to
210210
do it "the old way" for this particular output group -->
211211
<_SatelliteDllsProjectOutputGroupOutput Include="@(SatelliteDllsProjectOutputGroupOutput)"
212-
FinalOutputPath="'%(FullPath)')" />
212+
FinalOutputPath="%(FullPath)" />
213213

214214
<_InferredProjectOutput Include="@(BuiltProjectOutputGroupOutput -> '%(FinalOutputPath)');
215215
@(BuiltProjectOutputGroupKeyOutput -> '%(FinalOutputPath)');

src/NuGetizer.Tasks/NuGetizer.Shared.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Copyright (c) .NET Foundation. All rights reserved.
263263
<PropertyGroup>
264264
<_NuspecFile>%(NuspecFile.FullPath)</_NuspecFile>
265265
</PropertyGroup>
266-
<CreatePackage Manifest="@(PackageTargetPath)" NuspecFile="$(_NuspecFile)" Contents="@(_PackageContent)"
266+
<CreatePackage Manifest="@(PackageTargetPath)" NuspecFile="$(_NuspecFile)" Contents="@(_PackageContent)"
267267
EmitPackage="$(EmitPackage)" EmitNuspec="$(EmitNuspec)"
268268
TargetPath="@(PackageTargetPath->'%(FullPath)')">
269269
<Output TaskParameter="OutputPackage" ItemName="_PackageTargetPath" />

src/NuGetizer.Tasks/NuGetizer.targets

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ Copyright (c) .NET Foundation. All rights reserved.
7575
<AssignPackagePath Files="@(PackageFile)" KnownFolders="@(PackFolderKind)" IsPackaging="%(PackageFile.IsPackaging)">
7676
<Output TaskParameter="AssignedFiles" ItemName="_PackageContent" />
7777
</AssignPackagePath>
78+
79+
<ItemGroup>
80+
<_PackageContent>
81+
<MSBuildSourceProjectFile Condition="'%(_PackageContent.MSBuildSourceProjectFile)' == ''">$(MSBuildProjectFullPath)</MSBuildSourceProjectFile>
82+
<MSBuildSourceProjectDirectory Condition="'%(_PackageContent.MSBuildSourceProjectDirectory)' == ''">$(MSBuildProjectDirectory)</MSBuildSourceProjectDirectory>
83+
</_PackageContent>
84+
<_PackageContent>
85+
<MSBuildSourceProjectDirectory>$([MSBuild]::EnsureTrailingSlash('%(MSBuildSourceProjectDirectory)'))</MSBuildSourceProjectDirectory>
86+
</_PackageContent>
87+
</ItemGroup>
7888
</Target>
7989

8090
<Target Name="_AddPackageManifest" Condition="'$(IsPackable)' == 'true'">
@@ -133,13 +143,12 @@ Copyright (c) .NET Foundation. All rights reserved.
133143
<_ReferencedPackageContent Remove="@(_PackageContentFromDependency)" />
134144
</ItemGroup>
135145

146+
<ItemGroup>
136147
<!-- Always annotate package contents with the original target framework and moniker -->
137-
<CreateItem Include="@(_ReferencedPackageContent)" PreserveExistingMetadata="true"
138-
Condition="'@(_ReferencedPackageContent)' != ''"
139-
AdditionalMetadata="OriginalTargetFramework=%(_ReferencedPackageContent.TargetFramework);
140-
OriginalTargetFrameworkMoniker=%(_ReferencedPackageContent.TargetFrameworkMoniker)">
141-
<Output TaskParameter="Include" ItemName="_ReferencedPackageContentWithOriginalValues"/>
142-
</CreateItem>
148+
<_ReferencedPackageContentWithOriginalValues Include="@(_ReferencedPackageContent)"
149+
OriginalTargetFramework="%(_ReferencedPackageContent.TargetFramework)"
150+
OriginalTargetFrameworkMoniker="%(_ReferencedPackageContent.TargetFrameworkMoniker)" />
151+
</ItemGroup>
143152

144153
<ItemGroup Condition="'$(IsPackagingProject)' != 'true'">
145154
<!-- Retarget content for the currently building package, if necessary -->

src/NuGetizer.Tests/Builder.NuGetizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public TargetResult(string projectOrSolutionFile, BuildResult result, string tar
171171

172172
public TestOutputLogger Logger { get; private set; }
173173

174-
public string Target { get; private set; }
174+
public string Target { get; internal set; }
175175

176176
public Exception Exception => BuildResult[Target].Exception;
177177

src/NuGetizer.Tests/Scenarios/given_a_packaging_project/b/b.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
<PropertyGroup>
44
<TargetFramework>net46</TargetFramework>
55
<DocumentationFile>$(AssemblyName).xml</DocumentationFile>
6-
</PropertyGroup>
6+
<EnableDefaultItems>true</EnableDefaultItems>
7+
</PropertyGroup>
78
<ItemGroup>
8-
<EmbeddedResource Include="Resources.es-AR.resx" />
9-
<EmbeddedResource Include="Resources.resx" />
109
<ProjectReference Include="..\d.csproj" />
11-
</ItemGroup>
10+
<PackageFile Include="@(None)" PackagePath="docs\%(RelativeDir)%(Filename)%(Extension)" />
11+
<PackageFile Include="as-is.txt" PackFolder="None" IsFile="false" Condition="'$(AddAsIs)' == 'true'" />
12+
</ItemGroup>
1213
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


src/NuGetizer.Tests/given_a_packaging_project.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xunit;
1+
using System.IO;
2+
using Xunit;
23
using Xunit.Abstractions;
34

45
namespace NuGetizer
@@ -135,5 +136,41 @@ public void when_getting_contents_from_packaging_project_then_referenced_outputs
135136
PackagePath = @"lib\net45\c.xml",
136137
}));
137138
}
139+
140+
[Fact]
141+
public void when_getting_contents_then_transitive_content_is_made_full_path()
142+
{
143+
var result = Builder.BuildScenario(nameof(given_a_packaging_project), output: output);
144+
145+
result.AssertSuccess(output);
146+
147+
Assert.Contains(result.Items, item => item.Matches(new
148+
{
149+
Filename = "Readme",
150+
PackFolder = "None",
151+
}) && Path.IsPathRooted(item.ItemSpec));
152+
}
153+
154+
[Fact]
155+
public void when_getting_contents_then_transitive_content_can_opt_out_of_full_path()
156+
{
157+
var result = Builder.BuildScenario(nameof(given_a_packaging_project), properties: new { AddAsIs = "true" }, output: output);
158+
159+
result.AssertSuccess(output);
160+
161+
Assert.Contains(result.Items, item => item.Matches(new
162+
{
163+
Filename = "as-is",
164+
PackFolder = "None",
165+
}) && !Path.IsPathRooted(item.ItemSpec));
166+
}
167+
168+
[Fact]
169+
public void when_packing_then_succeeeds()
170+
{
171+
var result = Builder.BuildScenario(nameof(given_a_packaging_project), target: "Pack", output: output);
172+
173+
result.AssertSuccess(output);
174+
}
138175
}
139176
}

0 commit comments

Comments
 (0)