Skip to content

Commit

Permalink
Adding include/exclude to nuspec dependency entries
Browse files Browse the repository at this point in the history
  • Loading branch information
emgarten committed Nov 20, 2015
1 parent 3b4c963 commit 24760a9
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Core/Authoring/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ private static List<ManifestDependency> CreateDependencies(ICollection<PackageDe
select new ManifestDependency
{
Id = dependency.Id.SafeTrim(),
Version = dependency.VersionSpec.ToStringSafe()
Version = dependency.VersionSpec.ToStringSafe(),
Include = dependency.Include,
Exclude = dependency.Exclude
}).ToList();
}

Expand Down
6 changes: 6 additions & 0 deletions src/Core/Authoring/ManifestDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ public class ManifestDependency

[XmlAttribute("version")]
public string Version { get; set; }

[XmlAttribute("include")]
public string Include { get; set; }

[XmlAttribute("exclude")]
public string Exclude { get; set; }
}
}
10 changes: 8 additions & 2 deletions src/Core/Authoring/ManifestMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,16 @@ public List<object> DependencySetsSerialize
return null;
}

if (DependencySets.Any(set => set.TargetFramework != null))
// Write out groups using the post 2.5 format if there are multiple TxMs or if include/exclude are used
if (DependencySets.Any(set => set.TargetFramework != null
||set.Dependencies.Any(dependency => dependency.Exclude != null
|| dependency.Include != null)))
{
return DependencySets.Cast<object>().ToList();
}
else
{
// Legacy flat list format
return DependencySets.SelectMany(set => set.Dependencies).Cast<object>().ToList();
}
}
Expand Down Expand Up @@ -430,7 +434,9 @@ private static PackageDependencySet CreatePackageDependencySet(ManifestDependenc
var dependencies = from d in manifestDependencySet.Dependencies
select new PackageDependency(
d.Id,
String.IsNullOrEmpty(d.Version) ? null : VersionUtility.ParseVersionSpec(d.Version));
String.IsNullOrEmpty(d.Version) ? null : VersionUtility.ParseVersionSpec(d.Version),
d.Include,
d.Exclude);

return new PackageDependencySet(targetFramework, dependencies);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Authoring/ManifestReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ private static List<ManifestDependency> ReadDependencies(XElement containerEleme
select new ManifestDependency
{
Id = idElement.Value.SafeTrim(),
Version = element.GetOptionalAttributeValue("version").SafeTrim()
Version = element.GetOptionalAttributeValue("version").SafeTrim(),
Include = element.GetOptionalAttributeValue("include").SafeTrim(),
Exclude = element.GetOptionalAttributeValue("exclude").SafeTrim(),
}).ToList();
}

Expand Down
15 changes: 12 additions & 3 deletions src/Core/Authoring/PackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void Save(Stream stream)
using (Package package = Package.Open(stream, FileMode.Create))
{
// Validate and write the manifest
WriteManifest(package, DetermineMinimumSchemaVersion(Files));
WriteManifest(package, DetermineMinimumSchemaVersion(Files, DependencySets));

// Write the files to the package
WriteFiles(package);
Expand Down Expand Up @@ -290,9 +290,11 @@ private static string CreatorInfo()
return String.Join(";", creatorInfo);
}

private static int DetermineMinimumSchemaVersion(Collection<IPackageFile> Files)
private static int DetermineMinimumSchemaVersion(
Collection<IPackageFile> Files,
Collection<PackageDependencySet> package)
{
if (HasContentFilesV2(Files))
if (HasContentFilesV2(Files) || HasIncludeExclude(package))
{
// version 5
return ManifestVersionUtility.XdtTransformationVersion;
Expand Down Expand Up @@ -343,6 +345,13 @@ private static bool HasContentFilesV2(ICollection<IPackageFile> contentFiles)
file.Path.StartsWith(Constants.ContentFilesDirectory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase));
}

private static bool HasIncludeExclude(IEnumerable<PackageDependencySet> dependencySets)
{
return dependencySets.Any(dependencyGroup =>
dependencyGroup.Dependencies
.Any(dependency => dependency.Include != null || dependency.Exclude != null));
}

private static bool HasXdtTransformFile(ICollection<IPackageFile> contentFiles)
{
return contentFiles.Any(file =>
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Authoring/nuspec.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<xs:complexType name="dependency">
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="version" type="xs:string" use="optional" />
<xs:attribute name="include" type="xs:string" use="optional" />
<xs:attribute name="exclude" type="xs:string" use="optional" />
</xs:complexType>

<xs:complexType name="dependencyGroup">
Expand Down
19 changes: 19 additions & 0 deletions src/Core/Packages/PackageDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ public PackageDependency(string id)
}

public PackageDependency(string id, IVersionSpec versionSpec)
: this(id, versionSpec, include: null, exclude: null)
{
}

public PackageDependency(string id, IVersionSpec versionSpec, string include, string exclude)
{
if (String.IsNullOrEmpty(id))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "id");
}
Id = id;
VersionSpec = versionSpec;
Include = include;
Exclude = exclude;
}

public string Id
Expand All @@ -31,6 +38,18 @@ public IVersionSpec VersionSpec
private set;
}

public string Include
{
get;
private set;
}

public string Exclude
{
get;
private set;
}

public override string ToString()
{
if (VersionSpec == null)
Expand Down
115 changes: 115 additions & 0 deletions test/Core.Test/PackageBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,121 @@ namespace NuGet.Test
{
public class PackageBuilderTest
{
[Fact]
public void CreatePackageWithNuspecIncludeExcludeAnyGroup()
{
// Arrange
PackageBuilder builder = new PackageBuilder()
{
Id = "A",
Version = new SemanticVersion("1.0"),
Description = "Descriptions",
};
builder.Authors.Add("JaneDoe");

var dependencies = new List<PackageDependency>();
dependencies.Add(new PackageDependency("packageB", VersionUtility.ParseVersionSpec("1.0.0"), null, "z"));
dependencies.Add(new PackageDependency(
"packageC",
VersionUtility.ParseVersionSpec("1.0.0"),
"a,b,c",
"b,c"));

var set = new PackageDependencySet(null, dependencies);
builder.DependencySets.Add(set);

using (var ms = new MemoryStream())
{
builder.Save(ms);

ms.Seek(0, SeekOrigin.Begin);

var manifestStream = GetManifestStream(ms);

var result = manifestStream.ReadToEnd();

// Assert
Assert.Equal(@"<?xml version=""1.0""?>
<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
<metadata>
<id>A</id>
<version>1.0</version>
<authors>JaneDoe</authors>
<owners>JaneDoe</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Descriptions</description>
<dependencies>
<group>
<dependency id=""packageB"" version=""1.0.0"" exclude=""z"" />
<dependency id=""packageC"" version=""1.0.0"" include=""a,b,c"" exclude=""b,c"" />
</group>
</dependencies>
</metadata>
</package>".Replace("\r\n", "\n"), result.Replace("\r\n", "\n"));
}
}

[Fact]
public void CreatePackageWithNuspecIncludeExclude()
{
// Arrange
PackageBuilder builder = new PackageBuilder()
{
Id = "A",
Version = new SemanticVersion("1.0"),
Description = "Descriptions",
};
builder.Authors.Add("JaneDoe");

var dependencies45 = new List<PackageDependency>();
dependencies45.Add(new PackageDependency("packageB", VersionUtility.ParseVersionSpec("1.0.0"), null, "z"));

var dependencies46 = new List<PackageDependency>();
dependencies46.Add(new PackageDependency(
"packageC",
VersionUtility.ParseVersionSpec("1.0.0"),
"a,b,c",
"b,c"));

var net45 = new PackageDependencySet(new FrameworkName(".NETFramework", new Version(4, 5)), dependencies45);
builder.DependencySets.Add(net45);

var net46 = new PackageDependencySet(new FrameworkName(".NETFramework", new Version(4, 6)), dependencies46);
builder.DependencySets.Add(net46);

using (var ms = new MemoryStream())
{
builder.Save(ms);

ms.Seek(0, SeekOrigin.Begin);

var manifestStream = GetManifestStream(ms);

var result = manifestStream.ReadToEnd();

// Assert
Assert.Equal(@"<?xml version=""1.0""?>
<package xmlns=""http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd"">
<metadata>
<id>A</id>
<version>1.0</version>
<authors>JaneDoe</authors>
<owners>JaneDoe</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Descriptions</description>
<dependencies>
<group targetFramework="".NETFramework4.5"">
<dependency id=""packageB"" version=""1.0.0"" exclude=""z"" />
</group>
<group targetFramework="".NETFramework4.6"">
<dependency id=""packageC"" version=""1.0.0"" include=""a,b,c"" exclude=""b,c"" />
</group>
</dependencies>
</metadata>
</package>".Replace("\r\n", "\n"), result.Replace("\r\n", "\n"));
}
}

[Fact]
public void CreatePackageWithNuspecContentV2()
{
Expand Down

0 comments on commit 24760a9

Please sign in to comment.