Skip to content

Commit

Permalink
Adding properties to reference and dependency groups and property con…
Browse files Browse the repository at this point in the history
…straints to dependencies. Fixes 4265. Fixes 4266.
  • Loading branch information
jeffhandley committed Aug 19, 2014
1 parent 284ed43 commit 14f07e6
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 25 deletions.
8 changes: 7 additions & 1 deletion src/Core/Authoring/ManifestDependency.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using NuGet.Resources;

Expand All @@ -13,5 +14,10 @@ public class ManifestDependency

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

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is required by XML serializer")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "This is required by XML serializer.")]
[XmlElement("propertyConstraints")]
public List<ManifestProperty> PropertyConstraints { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Core/Authoring/ManifestDependencySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public class ManifestDependencySet
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "This is required by XML serializer.")]
[XmlElement("dependency")]
public List<ManifestDependency> Dependencies { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is required by XML serializer")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "This is required by XML serializer.")]
[XmlElement("property")]
public List<ManifestProperty> Properties { get; set; }
}
}
13 changes: 8 additions & 5 deletions src/Core/Authoring/ManifestMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ IEnumerable<PackageDependencySet> IPackageMetadata.DependencySets
var dependencySets = DependencySets.Select(CreatePackageDependencySet);

// group the dependency sets with the same target framework together.
var dependencySetGroups = dependencySets.GroupBy(set => set.TargetFramework);
var groupedDependencySets = dependencySetGroups.Select(group => new PackageDependencySet(group.Key, group.SelectMany(g => g.Dependencies)))
var dependencySetGroups = dependencySets.GroupBy(set => new { set.TargetFramework, set.Properties });
var groupedDependencySets = dependencySetGroups.Select(group => new PackageDependencySet(group.Key.TargetFramework, group.SelectMany(g => g.Dependencies), group.Key.Properties))
.ToList();
// move the group with the null target framework (if any) to the front just for nicer display in UI
int nullTargetFrameworkIndex = groupedDependencySets.FindIndex(set => set.TargetFramework == null);
Expand All @@ -331,8 +331,8 @@ ICollection<PackageReferenceSet> IPackageMetadata.PackageAssemblyReferences

var referenceSets = ReferenceSets.Select(r => new PackageReferenceSet(r));

var referenceSetGroups = referenceSets.GroupBy(set => set.TargetFramework);
var groupedReferenceSets = referenceSetGroups.Select(group => new PackageReferenceSet(group.Key, group.SelectMany(g => g.References)))
var referenceSetGroups = referenceSets.GroupBy(set => new { set.TargetFramework, set.Properties });
var groupedReferenceSets = referenceSetGroups.Select(group => new PackageReferenceSet(group.Key.TargetFramework, group.SelectMany(g => g.References), group.Key.Properties))
.ToList();

int nullTargetFrameworkIndex = groupedReferenceSets.FindIndex(set => set.TargetFramework == null);
Expand Down Expand Up @@ -435,7 +435,10 @@ private static PackageDependencySet CreatePackageDependencySet(ManifestDependenc
d.Id,
String.IsNullOrEmpty(d.Version) ? null : VersionUtility.ParseVersionSpec(d.Version));

return new PackageDependencySet(targetFramework, dependencies);
var properties = from p in manifestDependencySet.Properties
select new PackageProperty(p.Name, p.Value);

return new PackageDependencySet(targetFramework, dependencies, properties);
}
}
}
13 changes: 8 additions & 5 deletions src/Core/Authoring/ManifestReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ private static void ReadMetadataValue(ManifestMetadata manifestMetadata, XElemen
}
}

private static List<ManifestProperty> ReadProperties(XElement propertiesElement)
private static List<ManifestProperty> ReadProperties(XElement propertiesElement, string propertyElementName = "property")
{
if (!propertiesElement.HasElements)
{
return new List<ManifestProperty>(0);
}

return (from element in propertiesElement.ElementsNoNamespace("property")
return (from element in propertiesElement.ElementsNoNamespace(propertyElementName)
let nameAttribute = element.Attribute("name")
let valueAttribute = element.Attribute("value")
where nameAttribute != null && !String.IsNullOrEmpty(nameAttribute.Value)
Expand Down Expand Up @@ -195,7 +195,8 @@ private static List<ManifestReferenceSet> ReadReferenceSets(XElement referencesE
select new ManifestReferenceSet
{
TargetFramework = element.GetOptionalAttributeValue("targetFramework").SafeTrim(),
References = ReadReference(element, throwIfEmpty: true)
References = ReadReference(element, throwIfEmpty: true),
Properties = ReadProperties(element)
}).ToList();
}
}
Expand Down Expand Up @@ -265,7 +266,8 @@ private static List<ManifestDependencySet> ReadDependencySets(XElement dependenc
select new ManifestDependencySet
{
TargetFramework = element.GetOptionalAttributeValue("targetFramework").SafeTrim(),
Dependencies = ReadDependencies(element)
Dependencies = ReadDependencies(element),
Properties = ReadProperties(element)
}).ToList();
}
}
Expand All @@ -281,7 +283,8 @@ private static List<ManifestDependency> ReadDependencies(XElement containerEleme
select new ManifestDependency
{
Id = idElement.Value.SafeTrim(),
Version = element.GetOptionalAttributeValue("version").SafeTrim()
Version = element.GetOptionalAttributeValue("version").SafeTrim(),
PropertyConstraints = ReadProperties(element, "propertyConstraint")
}).ToList();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Core/Authoring/ManifestReferenceSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class ManifestReferenceSet : IValidatableObject
[XmlElement("reference")]
public List<ManifestReference> References { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is required by XML serializer")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "This is required by XML serializer.")]
[XmlElement("property")]
public List<ManifestProperty> Properties { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (References != null)
Expand Down
17 changes: 17 additions & 0 deletions src/Core/Authoring/PackageDependencySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ public class PackageDependencySet : IFrameworkTargetable
{
private readonly FrameworkName _targetFramework;
private readonly ReadOnlyCollection<PackageDependency> _dependencies;
private readonly ReadOnlyCollection<PackageProperty> _properties;

public PackageDependencySet(FrameworkName targetFramework, IEnumerable<PackageDependency> dependencies)
: this(targetFramework, dependencies, properties: null)
{
}

public PackageDependencySet(FrameworkName targetFramework, IEnumerable<PackageDependency> dependencies, IEnumerable<PackageProperty> properties)
{
if (dependencies == null)
{
Expand All @@ -20,6 +26,9 @@ public PackageDependencySet(FrameworkName targetFramework, IEnumerable<PackageDe

_targetFramework = targetFramework;
_dependencies = new ReadOnlyCollection<PackageDependency>(dependencies.ToList());

// Properties are optional - so we'll create an empty list by default
_properties = new ReadOnlyCollection<PackageProperty>(properties != null ? properties.ToList() : new List<PackageProperty>(0));
}

public FrameworkName TargetFramework
Expand All @@ -30,6 +39,14 @@ public FrameworkName TargetFramework
}
}

public IEnumerable<PackageProperty> Properties
{
get
{
return _properties;
}
}

public ICollection<PackageDependency> Dependencies
{
get
Expand Down
18 changes: 18 additions & 0 deletions src/Core/Authoring/PackageReferenceSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ public class PackageReferenceSet : IFrameworkTargetable
{
private readonly FrameworkName _targetFramework;
private readonly ICollection<string> _references;
private readonly ReadOnlyCollection<PackageProperty> _properties;

public PackageReferenceSet(FrameworkName targetFramework, IEnumerable<string> references)
: this(targetFramework, references, properties: null)
{
}

public PackageReferenceSet(FrameworkName targetFramework, IEnumerable<string> references, IEnumerable<PackageProperty> properties)
{
if (references == null)
{
Expand All @@ -20,6 +26,9 @@ public PackageReferenceSet(FrameworkName targetFramework, IEnumerable<string> re

_targetFramework = targetFramework;
_references = new ReadOnlyCollection<string>(references.ToList());

// Properties are optional - so we'll create an empty list by default
_properties = new ReadOnlyCollection<PackageProperty>(properties != null ? properties.ToList() : new List<PackageProperty>(0));
}

public PackageReferenceSet(ManifestReferenceSet manifestReferenceSet)
Expand All @@ -35,6 +44,7 @@ public PackageReferenceSet(ManifestReferenceSet manifestReferenceSet)
}

_references = new ReadOnlyHashSet<string>(manifestReferenceSet.References.Select(r => r.File), StringComparer.OrdinalIgnoreCase);
_properties = new ReadOnlyCollection<PackageProperty>(manifestReferenceSet.Properties.Select(p => new PackageProperty(p.Name, p.Value)).ToList());
}

public ICollection<string> References
Expand All @@ -45,6 +55,14 @@ public ICollection<string> References
}
}

public IEnumerable<PackageProperty> Properties
{
get
{
return _properties;
}
}

public FrameworkName TargetFramework
{
get { return _targetFramework; }
Expand Down
25 changes: 14 additions & 11 deletions src/Core/Authoring/nuspec.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
xmlns:mstns="{0}"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="property">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="value" type="xs:string" use="required" />
</xs:complexType>

<xs:complexType name="dependency">
<xs:sequence>
<xs:element name="propertyConstraint" minOccurs="0" maxOccurs="unbounded" type="property" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="version" type="xs:string" use="optional" />
</xs:complexType>

<xs:complexType name="dependencyGroup">
<xs:sequence>
<xs:element name="dependency" minOccurs="0" maxOccurs="unbounded" type="dependency">
</xs:element>
<xs:element name="dependency" minOccurs="0" maxOccurs="unbounded" type="dependency" />
<xs:element name="property" minOccurs="0" maxOccurs="unbounded" type="property" />
</xs:sequence>
<xs:attribute name="targetFramework" type="xs:string" use="optional" />
</xs:complexType>
Expand All @@ -25,8 +33,8 @@

<xs:complexType name="referenceGroup">
<xs:sequence>
<xs:element name="reference" minOccurs="1" maxOccurs="unbounded" type="reference">
</xs:element>
<xs:element name="reference" minOccurs="1" maxOccurs="unbounded" type="reference" />
<xs:element name="property" minOccurs="0" maxOccurs="unbounded" type="property" />
</xs:sequence>
<xs:attribute name="targetFramework" type="xs:string" use="optional" />
</xs:complexType>
Expand Down Expand Up @@ -91,12 +99,7 @@
<xs:element name="properties" maxOccurs="1" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="property" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="property" minOccurs="0" maxOccurs="unbounded" type="property" />
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down
17 changes: 15 additions & 2 deletions src/Core/Packages/PackageDependency.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
using System;
using System.Collections.Generic;

namespace NuGet
{
public class PackageDependency
{
public PackageDependency(string id)
: this(id, versionSpec: null)
: this(id, versionSpec: null, propertyConstraints: null)
{
}

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

public PackageDependency(string id, IVersionSpec versionSpec, IEnumerable<PackageProperty> propertyConstraints)
{
if (String.IsNullOrEmpty(id))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "id");
}
Id = id;
VersionSpec = versionSpec;
PropertyConstraints = propertyConstraints;
}

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

public IEnumerable<PackageProperty> PropertyConstraints
{
get;
private set;
}

public override string ToString()
{
if (VersionSpec == null)
Expand Down
2 changes: 1 addition & 1 deletion test/Test.Utility/PackageUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static IPackage CreatePackage(string id,
{
var dependencySets = new List<PackageDependencySet>
{
new PackageDependencySet(null, dependencies ?? Enumerable.Empty<PackageDependency>())
new PackageDependencySet(null, dependencies ?? Enumerable.Empty<PackageDependency>(), Enumerable.Empty<PackageProperty>())
};

return CreatePackage(id,
Expand Down

0 comments on commit 14f07e6

Please sign in to comment.