Skip to content

Commit

Permalink
Adding support for ContentFiles during pack
Browse files Browse the repository at this point in the history
  • Loading branch information
emgarten committed Nov 9, 2015
1 parent c39cb4d commit 8821162
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Core/Authoring/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ public static Manifest Create(IPackageMetadata metadata)
};
}

public static Manifest Create(PackageBuilder packageBuilder)
{
var metadata = (IPackageMetadata)packageBuilder;

return new Manifest
{
Metadata = new ManifestMetadata
{
Id = metadata.Id.SafeTrim(),
Version = metadata.Version.ToStringSafe(),
Title = metadata.Title.SafeTrim(),
Authors = GetCommaSeparatedString(metadata.Authors),
Owners = GetCommaSeparatedString(metadata.Owners) ?? GetCommaSeparatedString(metadata.Authors),
Tags = String.IsNullOrEmpty(metadata.Tags) ? null : metadata.Tags.SafeTrim(),
LicenseUrl = ConvertUrlToStringSafe(metadata.LicenseUrl),
ProjectUrl = ConvertUrlToStringSafe(metadata.ProjectUrl),
IconUrl = ConvertUrlToStringSafe(metadata.IconUrl),
RequireLicenseAcceptance = metadata.RequireLicenseAcceptance,
DevelopmentDependency = metadata.DevelopmentDependency,
Description = metadata.Description.SafeTrim(),
Copyright = metadata.Copyright.SafeTrim(),
Summary = metadata.Summary.SafeTrim(),
ReleaseNotes = metadata.ReleaseNotes.SafeTrim(),
Language = metadata.Language.SafeTrim(),
DependencySets = CreateDependencySets(metadata),
FrameworkAssemblies = CreateFrameworkAssemblies(metadata),
ReferenceSets = CreateReferenceSets(metadata),
MinClientVersionString = metadata.MinClientVersion.ToStringSafe(),
ContentFiles = packageBuilder.ContentFiles.ToList()
},
};
}

private static string ConvertUrlToStringSafe(Uri url)
{
if (url != null)
Expand Down
23 changes: 23 additions & 0 deletions src/Core/Authoring/ManifestContentFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Xml.Serialization;

namespace NuGet
{
[XmlType("files")]
public class ManifestContentFiles
{
[XmlAttribute("include")]
public string Include { get; set; }

[XmlAttribute("exclude")]
public string Exclude { get; set; }

[XmlAttribute("buildAction")]
public string BuildAction { get; set; }

[XmlAttribute("copyToOutput")]
public string CopyToOutput { get; set; }

[XmlAttribute("flatten")]
public string Flatten { get; set; }
}
}
39 changes: 39 additions & 0 deletions src/Core/Authoring/ManifestMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,45 @@ public List<object> ReferenceSetsSerialize
[XmlIgnore]
public List<ManifestReferenceSet> ReferenceSets { get; set; }

[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value", Justification = "The propert setter is not supported.")]
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "It's easier to create a list")]
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is needed for xml serialization")]
[XmlArray("contentFiles", IsNullable = false)]
[XmlArrayItem("files", typeof(ManifestContentFiles))]
public List<object> ContentFilesSerialize
{
get
{
if (ContentFiles == null || ContentFiles.Count == 0)
{
return null;
}
return ContentFiles.Cast<object>().ToList();
}
set
{
// this property is only used for serialization.
throw new InvalidOperationException();
}
}

private List<ManifestContentFiles> _contentFiles;
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists", Justification = "It's easier to create a list")]
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is needed for xml serialization")]
[XmlIgnore]
public List<ManifestContentFiles> ContentFiles
{
get
{
return _contentFiles ?? new List<ManifestContentFiles>();
}

set
{
_contentFiles = value;
}
}

SemanticVersion IPackageName.Version
{
get
Expand Down
29 changes: 29 additions & 0 deletions src/Core/Authoring/ManifestReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,36 @@ private static void ReadMetadataValue(ManifestMetadata manifestMetadata, XElemen
case "references":
manifestMetadata.ReferenceSets = ReadReferenceSets(element);
break;
case "contentFiles":
manifestMetadata.ContentFiles = ReadContentFiles(element);
break;
}
}

private static List<ManifestContentFiles> ReadContentFiles(XElement contentFilesElement)
{
if (!contentFilesElement.HasElements)
{
return new List<ManifestContentFiles>(0);
}

var contentFileSets = (from element in contentFilesElement.ElementsNoNamespace("files")
let includeAttribute = element.Attribute("include")
where includeAttribute != null && !string.IsNullOrEmpty(includeAttribute.Value)
let excludeAttribute = element.Attribute("exclude")
let buildActionAttribute = element.Attribute("buildAction")
let copyToOutputAttribute = element.Attribute("copyToOutput")
let flattenAttribute = element.Attribute("flatten")
select new ManifestContentFiles
{
Include = includeAttribute.Value.SafeTrim(),
Exclude = excludeAttribute?.Value,
BuildAction = buildActionAttribute?.Value,
CopyToOutput = copyToOutputAttribute?.Value,
Flatten = flattenAttribute?.Value
}).ToList();

return contentFileSets;
}

private static List<ManifestReferenceSet> ReadReferenceSets(XElement referencesElement)
Expand Down
24 changes: 24 additions & 0 deletions src/Core/Authoring/PackageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private PackageBuilder(bool includeEmptyDirectories)
Files = new Collection<IPackageFile>();
DependencySets = new Collection<PackageDependencySet>();
FrameworkReferences = new Collection<FrameworkAssemblyReference>();
ContentFiles = new Collection<ManifestContentFiles>();
PackageAssemblyReferences = new Collection<PackageReferenceSet>();
Authors = new HashSet<string>();
Owners = new HashSet<string>();
Expand Down Expand Up @@ -172,6 +173,15 @@ public Collection<FrameworkAssemblyReference> FrameworkReferences
private set;
}

/// <summary>
/// ContentFiles section from the manifest for content v2
/// </summary>
public Collection<ManifestContentFiles> ContentFiles
{
get;
private set;
}

public ICollection<PackageReferenceSet> PackageAssemblyReferences
{
get;
Expand Down Expand Up @@ -282,6 +292,12 @@ private static string CreatorInfo()

private static int DetermineMinimumSchemaVersion(Collection<IPackageFile> Files)
{
if (HasContentFilesV2(Files))
{
// version 5
return ManifestVersionUtility.XdtTransformationVersion;
}

if (HasXdtTransformFile(Files))
{
// version 5
Expand Down Expand Up @@ -320,6 +336,13 @@ private static bool RequiresV4TargetFrameworkSchema(ICollection<IPackageFile> fi
return hasEmptyLibFolder;
}

private static bool HasContentFilesV2(ICollection<IPackageFile> contentFiles)
{
return contentFiles.Any(file =>
file.Path != null &&
file.Path.StartsWith(Constants.ContentFilesDirectory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase));
}

private static bool HasXdtTransformFile(ICollection<IPackageFile> contentFiles)
{
return contentFiles.Any(file =>
Expand Down Expand Up @@ -411,6 +434,7 @@ public void Populate(ManifestMetadata manifestMetadata)
Language = metadata.Language;
Copyright = metadata.Copyright;
MinClientVersion = metadata.MinClientVersion;
ContentFiles = new Collection<ManifestContentFiles>(manifestMetadata.ContentFiles);

if (metadata.Tags != null)
{
Expand Down
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Authoring\EmptyFrameworkFolderFile.cs" />
<Compile Include="Authoring\IPackageBuilder.cs" />
<Compile Include="Authoring\IPropertyProvider.cs" />
<Compile Include="Authoring\ManifestContentFiles.cs" />
<Compile Include="Authoring\ManifestDependencySet.cs" />
<Compile Include="Authoring\ManifestFrameworkAssembly.cs" />
<Compile Include="Authoring\ManifestDependency.cs" />
Expand Down
61 changes: 61 additions & 0 deletions test/Core.Test/PackageBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
Expand All @@ -16,6 +17,66 @@ namespace NuGet.Test
{
public class PackageBuilderTest
{
[Fact]
public void CreatePackageWithNuspecContentV2()
{
System.Diagnostics.Debugger.Launch();

// Arrange
PackageBuilder builder = new PackageBuilder()
{
Id = "A",
Version = new SemanticVersion("1.0"),
Description = "Descriptions",
};
builder.Authors.Add("JaneDoe");
builder.Files.Add(CreatePackageFile("contentFiles\\any\\any\\config\\config.xml"));
builder.Files.Add(CreatePackageFile("contentFiles\\cs\\net45\\code.cs.pp"));

builder.ContentFiles.Add(new ManifestContentFiles()
{
Include = "**/*",
BuildAction = "Compile"
});

builder.ContentFiles.Add(new ManifestContentFiles()
{
Include = "**/*",
Exclude = "**/*.cs",
BuildAction = "None",
Flatten = "true",
CopyToOutput = "true"
});

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>
<contentFiles>
<files include=""**/*"" buildAction=""Compile"" />
<files include=""**/*"" exclude=""**/*.cs"" buildAction=""None"" copyToOutput=""true"" flatten=""true"" />
</contentFiles>
</metadata>
</package>".Replace("\r\n", "\n"), result.Replace("\r\n", "\n"));
}
}

[Fact]
public void OwnersFallsBackToAuthorsIfNoneSpecified()
{
Expand Down

0 comments on commit 8821162

Please sign in to comment.