Skip to content

Commit 573bf2a

Browse files
authored
Use custom logic for packaging and remove usage of NuGet assemblies (#158)
1 parent da3ae7c commit 573bf2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2188
-1268
lines changed

Packages.props

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<MicrosoftBuildPackageVersion>17.0.0</MicrosoftBuildPackageVersion>
55
<MicrosoftBuildPackageVersion Condition="'$(TargetFramework)' == 'net5.0'">16.11.0</MicrosoftBuildPackageVersion>
66
<MicrosoftBuildPackageVersion Condition="'$(TargetFramework)' == 'netcoreapp3.1'">16.9.0</MicrosoftBuildPackageVersion>
7-
<NuGetPackageVersion>6.0.0</NuGetPackageVersion>
7+
<NuGetPackageVersion>6.1.0</NuGetPackageVersion>
88
<NuGetPackageVersion Condition="'$(TargetFramework)' == 'net5.0'">5.11.0</NuGetPackageVersion>
99
<NuGetPackageVersion Condition="'$(TargetFramework)' == 'netcoreapp3.1'">5.7.1</NuGetPackageVersion>
1010
</PropertyGroup>
@@ -17,9 +17,8 @@
1717
<PackageReference Update="Microsoft.VisualStudio.SDK.EmbedInteropTypes" Version="15.0.36" />
1818
<PackageReference Update="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="1.16.30" />
1919
<PackageReference Update="NuGet.Frameworks" Version="$(NuGetPackageVersion)" />
20-
<PackageReference Update="NuGet.Packaging" Version="$(NuGetPackageVersion)" />
21-
<PackageReference Update="NuGet.ProjectModel" Version="$(NuGetPackageVersion)" />
2220
<PackageReference Update="Shouldly" Version="4.0.3" />
21+
<PackageReference Update="System.IO.Compression" Version="4.3.0" />
2322
<PackageReference Update="xunit" Version="2.4.1" />
2423
<PackageReference Update="xunit.runner.visualstudio" Version="2.4.3" />
2524
</ItemGroup>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) Jeff Kluge. All rights reserved.
2+
//
3+
// Licensed under the MIT license.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Xml;
9+
using System.Xml.Linq;
10+
using System.Xml.XPath;
11+
12+
namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
13+
{
14+
public class NuspecReader
15+
{
16+
private static readonly XNamespace NuspecNamespace = "http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd";
17+
18+
private readonly XDocument _document;
19+
20+
private readonly XmlNamespaceManager _namespaceManager = new XmlNamespaceManager(new NameTable());
21+
22+
public NuspecReader(string contents)
23+
{
24+
_document = XDocument.Parse(contents);
25+
26+
_namespaceManager.AddNamespace("ns", NuspecNamespace.NamespaceName);
27+
}
28+
29+
public NuspecReader(FileInfo fileInfo)
30+
: this(File.ReadAllText(fileInfo.FullName))
31+
{
32+
}
33+
34+
public string Authors => GetElement("authors");
35+
36+
public IEnumerable<PackageContentFileEntry> ContentFiles => GetContentFiles();
37+
38+
public string Copyright => GetElement("copyright");
39+
40+
public IEnumerable<(string TargetFramework, IEnumerable<PackageDependency> Dependencies)> DependencyGroups
41+
{
42+
get
43+
{
44+
foreach (XElement group in _document.XPathSelectElements("//ns:package/ns:metadata/ns:dependencies/ns:group", _namespaceManager))
45+
{
46+
yield return (group.Attribute("targetFramework")?.Value, GetDependencies(group.Elements()));
47+
}
48+
}
49+
}
50+
51+
public string Description => GetElement("description");
52+
53+
public bool DevelopmentDependency => string.Equals(bool.TrueString, GetElement("developmentDependency"), StringComparison.OrdinalIgnoreCase);
54+
55+
public string Icon => GetElement("icon");
56+
57+
public string IconUrl => GetElement("iconUrl");
58+
59+
public string Id => GetElement("id");
60+
61+
public string Language => GetElement("language");
62+
63+
public string License => GetElement("license");
64+
65+
public string LicenseType => GetAttribute("license", "type");
66+
67+
public string LicenseVersion => GetAttribute("license", "version");
68+
69+
public string Owners => GetElement("owners");
70+
71+
public IEnumerable<string> PackageTypes
72+
{
73+
get
74+
{
75+
foreach (XElement group in _document.XPathSelectElements("//ns:package/ns:metadata/ns:packageTypes/ns:packageType", _namespaceManager))
76+
{
77+
yield return group.Attribute("name")?.Value;
78+
}
79+
}
80+
}
81+
82+
public string ProjectUrl => GetElement("projectUrl");
83+
84+
public string ReleaseNotes => GetElement("releaseNotes");
85+
86+
public string RepositoryBranch => GetAttribute("repository", "branch");
87+
88+
public string RepositoryCommit => GetAttribute("repository", "commit");
89+
90+
public string RepositoryType => GetAttribute("repository", "type");
91+
92+
public string RepositoryUrl => GetAttribute("repository", "url");
93+
94+
public bool RequireLicenseAcceptance => string.Equals(bool.TrueString, GetElement("requireLicenseAcceptance"), StringComparison.OrdinalIgnoreCase);
95+
96+
public bool Serviceable => string.Equals(bool.TrueString, GetElement("serviceable"), StringComparison.OrdinalIgnoreCase);
97+
98+
public string Summary => GetElement("summary");
99+
100+
public string Tags => GetElement("tags");
101+
102+
public string Title => GetElement("title");
103+
104+
public string Version => GetElement("version");
105+
106+
private IEnumerable<PackageContentFileEntry> GetContentFiles()
107+
{
108+
foreach (XElement file in _document.XPathSelectElements("//ns:package/ns:metadata/ns:contentFiles/ns:files", _namespaceManager))
109+
{
110+
yield return new PackageContentFileEntry(
111+
file.Attribute("buildAction")?.Value,
112+
file.Attribute("copyToOutput")?.Value,
113+
file.Attribute("include")?.Value,
114+
file.Attribute("exclude")?.Value,
115+
file.Attribute("flatten")?.Value);
116+
}
117+
}
118+
119+
private IEnumerable<PackageDependency> GetDependencies(IEnumerable<XElement> dependencies)
120+
{
121+
foreach (XElement item in dependencies)
122+
{
123+
yield return new PackageDependency(
124+
item.Attribute("id").Value,
125+
item.Attribute("version").Value,
126+
item.Attribute("exclude").Value);
127+
}
128+
}
129+
130+
private string GetElement(string name)
131+
{
132+
return _document.XPathSelectElement($"//ns:package/ns:metadata/ns:{name}", _namespaceManager)?.Value;
133+
}
134+
135+
private string GetAttribute(string elementName, string attributeName)
136+
{
137+
return _document.XPathSelectElement($"//ns:package/ns:metadata/ns:{elementName}", _namespaceManager)?.Attribute(attributeName)?.Value;
138+
}
139+
}
140+
}

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/PackageFeedTests/PackageFeedFileTests.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
//
33
// Licensed under the MIT license.
44

5-
using NuGet.Frameworks;
6-
using NuGet.Packaging;
7-
using NuGet.Packaging.Core;
85
using Shouldly;
96
using System;
107
using System.IO;
@@ -22,14 +19,14 @@ public void ContentFileTextCustom()
2219

2320
PackageFeed.Create(FeedRootPath)
2421
.Package("PackageA", "1.0.0", out Package packageA)
25-
.ContentFileText("file.cs", "A1CF42B9F20B4155B6B70753784615B5", "netstandard2.0", "Compile", copyToOutput: false, flatten: true, language)
22+
.ContentFileText("file.cs", "A1CF42B9F20B4155B6B70753784615B5", "netstandard2.0", "Compile", copyToOutput: false, flatten: true, language)
2623
.Save();
2724

2825
VerifyContentFile(
2926
packageA,
3027
"file.cs",
3128
"A1CF42B9F20B4155B6B70753784615B5",
32-
FrameworkConstants.CommonFrameworks.NetStandard20,
29+
"netstandard2.0",
3330
expectedBuildAction: "Compile",
3431
expectedCopyToOutput: false,
3532
expectedFlatten: true,
@@ -48,7 +45,7 @@ public void ContentFileTextDefault()
4845
packageA,
4946
"file.txt",
5047
"F1BE6E0E408141459C9728FBE0CD5751",
51-
FrameworkConstants.CommonFrameworks.Net45,
48+
"net45",
5249
expectedBuildAction: "Content",
5350
expectedCopyToOutput: true);
5451
}
@@ -58,20 +55,22 @@ public void FileCustom()
5855
{
5956
string fileName = $"{Guid.NewGuid():N}.txt";
6057

61-
FileInfo fileInfo = new FileInfo(Path.Combine(TestRootPath, fileName));
58+
FileInfo sourceFileInfo = new FileInfo(Path.Combine(TestRootPath, fileName));
6259

63-
File.WriteAllText(fileInfo.FullName, "585B55DD5AC54A10B841B3D9A00129D8");
60+
string relativePath = Path.Combine("tools", "net46", fileName);
61+
62+
File.WriteAllText(sourceFileInfo.FullName, "585B55DD5AC54A10B841B3D9A00129D8");
6463

6564
PackageFeed.Create(FeedRootPath)
6665
.Package("PackageA", "1.0.0", out Package packageA)
67-
.FileCustom(Path.Combine("tools", "net46", fileName), fileInfo)
66+
.FileCustom(relativePath, sourceFileInfo)
6867
.Save();
6968

70-
GetFileContents(packageA.FullPath, $"tools/net46/{fileName}")
69+
GetFileContents(packageA.FullPath, relativePath)
7170
.ShouldBe("585B55DD5AC54A10B841B3D9A00129D8");
7271

73-
GetNuspecReader(packageA)
74-
.GetDependencyGroups().Select(i => i.TargetFramework.GetShortFolderName()).ToList()
72+
GetNuspec(packageA)
73+
.DependencyGroups.Select(i => i.TargetFramework).ToList()
7574
.ShouldContain("net46");
7675
}
7776

@@ -92,16 +91,18 @@ public void FileCustomDoesNotExist()
9291
[Fact]
9392
public void FileText()
9493
{
94+
string relativePath = Path.Combine("something", "nothing.txt");
95+
9596
PackageFeed.Create(FeedRootPath)
9697
.Package("PackageA", "1.0.0", out Package packageA)
97-
.FileText(@"something\nothing.txt", "607779BADE3645F8A288543213BFE948")
98+
.FileText(relativePath, "607779BADE3645F8A288543213BFE948")
9899
.Save();
99100

100-
GetFileContents(packageA.FullPath, "something/nothing.txt")
101+
GetFileContents(packageA.FullPath, relativePath)
101102
.ShouldBe("607779BADE3645F8A288543213BFE948");
102103

103-
GetNuspecReader(packageA)
104-
.GetDependencyGroups()
104+
GetNuspec(packageA)
105+
.DependencyGroups
105106
.ShouldBeEmpty();
106107
}
107108

@@ -115,13 +116,13 @@ public void FileTextThrowsIfNoPackageAdded()
115116
});
116117
}
117118

118-
private void VerifyContentFile(Package package, string relativePath, string expectedContents, NuGetFramework expectedTargetFramework, string expectedExclude = null, string expectedBuildAction = null, bool expectedCopyToOutput = false, bool expectedFlatten = false, string expectedLanguage = "any")
119+
private void VerifyContentFile(Package package, string relativePath, string expectedContents, string expectedTargetFramework, string expectedExclude = null, string expectedBuildAction = null, bool expectedCopyToOutput = false, bool expectedFlatten = false, string expectedLanguage = "any")
119120
{
120-
GetFileContents(package.FullPath, $"contentFiles/{expectedLanguage}/{expectedTargetFramework.GetShortFolderName()}/{relativePath}").ShouldBe(expectedContents);
121+
GetFileContents(package.FullPath, Path.Combine("contentFiles", expectedLanguage, expectedTargetFramework, relativePath)).ShouldBe(expectedContents);
121122

122-
NuspecReader nuspecReader = GetNuspecReader(package);
123+
NuspecReader nuspecReader = GetNuspec(package);
123124

124-
ContentFilesEntry file = nuspecReader.GetContentFiles().ShouldHaveSingleItem();
125+
PackageContentFileEntry file = nuspecReader.ContentFiles.ShouldHaveSingleItem();
125126

126127
file.BuildAction.ShouldBe(expectedBuildAction, StringCompareShould.IgnoreCase);
127128

@@ -134,7 +135,7 @@ private void VerifyContentFile(Package package, string relativePath, string expe
134135
file.CopyToOutput.ShouldNotBeNull().ShouldBeFalse();
135136
}
136137

137-
file.Include.ShouldBe(Path.Combine(expectedLanguage, expectedTargetFramework.GetShortFolderName(), relativePath));
138+
file.Include.ShouldBe(Path.Combine(expectedLanguage, expectedTargetFramework, relativePath));
138139

139140
if (expectedExclude == null)
140141
{

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/PackageFeedTests/PackageFeedLibraryTests.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
//
33
// Licensed under the MIT license.
44

5-
using NuGet.Frameworks;
6-
using NuGet.Packaging;
75
using Shouldly;
6+
using System.IO;
87
using System.Reflection;
98
using Xunit;
109

@@ -20,7 +19,7 @@ public void LibraryCustom()
2019
.Library("net45", "CustomFile.dll", "Custom.Namespace", "CustomClass", "2.3.4.5")
2120
.Save();
2221

23-
ValidateAssembly(packageA, @"lib/net45/CustomFile.dll", "CustomFile, Version=2.3.4.5, Culture=neutral, PublicKeyToken=null", "Custom.Namespace.CustomClass");
22+
ValidateAssembly(packageA, Path.Combine("lib", "net45", "CustomFile.dll"), "CustomFile, Version=2.3.4.5, Culture=neutral, PublicKeyToken=null", "Custom.Namespace.CustomClass");
2423
}
2524

2625
[Theory]
@@ -30,10 +29,10 @@ public void LibraryDefault(string packageName)
3029
{
3130
PackageFeed.Create(FeedRootPath)
3231
.Package(packageName, "1.0.0", out Package packageA)
33-
.Library(FrameworkConstants.CommonFrameworks.Net45.GetShortFolderName())
32+
.Library("net45")
3433
.Save();
3534

36-
ValidateAssembly(packageA, $@"lib/net45/{packageName}.dll", $"{packageName}, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", $"{packageName}.{packageName.Replace(".", "_")}_Class");
35+
ValidateAssembly(packageA, Path.Combine("lib", "net45", $"{packageName}.dll"), $"{packageName}, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", $"{packageName}.{packageName.Replace(".", "_")}_Class");
3736
}
3837

3938
[Fact]
@@ -42,7 +41,7 @@ public void LibraryThrowsIfNoPackageAdded()
4241
ShouldThrowExceptionIfNoPackageAdded(() =>
4342
{
4443
PackageFeed.Create(FeedRootPath)
45-
.Library(FrameworkConstants.CommonFrameworks.Net45.GetShortFolderName());
44+
.Library("net45");
4645
});
4746
}
4847

@@ -51,8 +50,8 @@ public void MultipleTargetFrameworks()
5150
{
5251
string[] targetFrameworks =
5352
{
54-
FrameworkConstants.CommonFrameworks.Net45.GetShortFolderName(),
55-
FrameworkConstants.CommonFrameworks.Net46.GetShortFolderName(),
53+
"net45",
54+
"net46",
5655
};
5756

5857
PackageFeed.Create(FeedRootPath)
@@ -68,8 +67,8 @@ public void MultipleTargetFrameworks()
6867

6968
foreach (string targetFramework in targetFrameworks)
7069
{
71-
ValidateAssembly(packageA, $@"lib/{targetFramework}/PackageA.dll", "PackageA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "PackageA.PackageA_Class");
72-
ValidateAssembly(packageA, $@"ref/{targetFramework}/PackageA.dll", "PackageA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "PackageA.PackageA_Class");
70+
ValidateAssembly(packageA, Path.Combine("lib", targetFramework, "PackageA.dll"), "PackageA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "PackageA.PackageA_Class");
71+
ValidateAssembly(packageA, Path.Combine("ref", targetFramework, "PackageA.dll"), "PackageA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "PackageA.PackageA_Class");
7372
}
7473
}
7574

@@ -81,18 +80,18 @@ public void ReferenceAssemblyCustom()
8180
.ReferenceAssembly("net45", "CustomFile.dll", "Custom.Namespace", "CustomClass", "2.3.4.5")
8281
.Save();
8382

84-
ValidateAssembly(packageA, @"ref/net45/CustomFile.dll", "CustomFile, Version=2.3.4.5, Culture=neutral, PublicKeyToken=null", "Custom.Namespace.CustomClass");
83+
ValidateAssembly(packageA, Path.Combine("ref", "net45", "CustomFile.dll"), "CustomFile, Version=2.3.4.5, Culture=neutral, PublicKeyToken=null", "Custom.Namespace.CustomClass");
8584
}
8685

8786
[Fact]
8887
public void ReferenceAssemblyDefault()
8988
{
9089
PackageFeed.Create(FeedRootPath)
9190
.Package("PackageA", "1.0.0", out Package packageA)
92-
.ReferenceAssembly(FrameworkConstants.CommonFrameworks.Net45.GetShortFolderName())
91+
.ReferenceAssembly("net45")
9392
.Save();
9493

95-
ValidateAssembly(packageA, @"ref/net45/PackageA.dll", "PackageA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "PackageA.PackageA_Class");
94+
ValidateAssembly(packageA, Path.Combine("ref", "net45", "PackageA.dll"), "PackageA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "PackageA.PackageA_Class");
9695
}
9796

9897
[Fact]
@@ -101,7 +100,7 @@ public void ReferenceAssemblyThrowsIfNoPackageAdded()
101100
ShouldThrowExceptionIfNoPackageAdded(() =>
102101
{
103102
PackageFeed.Create(FeedRootPath)
104-
.ReferenceAssembly(FrameworkConstants.CommonFrameworks.Net45.GetShortFolderName());
103+
.ReferenceAssembly("net45");
105104
});
106105
}
107106

0 commit comments

Comments
 (0)