Skip to content

Commit

Permalink
Raise only package assets that are used to build
Browse files Browse the repository at this point in the history
The new task (ResolvePackageAssets) unlike the old ResolvePackageDependencies,
only raises items that that will actually be consumed by the build. The new
items are also much easier to consume in targets and do not require "joins"
to resolve paths, querying via WithMetadataValue, etc.
  • Loading branch information
nguerrera committed Jan 16, 2018
1 parent 85bc209 commit f40056b
Show file tree
Hide file tree
Showing 15 changed files with 581 additions and 380 deletions.
2 changes: 1 addition & 1 deletion src/Tasks/Common/ItemUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static string GetTargetPath(ITaskItem item)

var fileName = Path.GetFileName(sourcePath);

// Get locale subdirectory for satellite assemblies
// Get subdirectory for satellite assemblies / runtime targets
var destinationSubDirectory = item.GetMetadata("DestinationSubDirectory");

if (!string.IsNullOrWhiteSpace(destinationSubDirectory))
Expand Down
21 changes: 21 additions & 0 deletions src/Tasks/Common/MetadataKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,26 @@ internal static class MetadataKeys

// Conflict Resolution
public const string OverridenPackages = "OverridenPackages";

// Package assets
public const string NuGetIsFrameworkReference = "NuGetIsFrameworkReference";
public const string NuGetPackageId = "NuGetPackageId";
public const string NuGetPackageVersion = "NuGetPackageVersion";
public const string NuGetSourceType = "NuGetSourceType";

// References
public const string Private = "Private";
public const string Pack = "Pack";

// Content files
public const string PPOutputPath = "PPOutputPath";
public const string CodeLanguage = "CodeLanguage";
public const string CopyToOutput = "CopyToOutput";
public const string BuildAction = "BuildAction";
public const string OutputPath = "OutputPath";

// Resource assemblies
public const string Culture = "Culture";
public const string DestinationSubDirectory = "DestinationSubDirectory";
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.NET.Build.Tasks
/// set of Packages.
/// </summary>
/// <remarks>
/// Both Items and Packages are expected to have 'PackageName' and 'PackageVersion'
/// Both Items and Packages are expected to have ('PackageName' and 'PackageVersion)' or ('NuGetPackageId' and 'NuGetPackageVersion')
/// metadata properties to use for the matching.
/// </remarks>
public sealed class FindItemsFromPackages : TaskBase
Expand Down
6 changes: 6 additions & 0 deletions src/Tasks/Microsoft.NET.Build.Tasks/ItemUtilities.NuGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public static PackageIdentity GetPackageIdentity(ITaskItem item)
string packageName = item.GetMetadata(MetadataKeys.PackageName);
string packageVersion = item.GetMetadata(MetadataKeys.PackageVersion);

if (string.IsNullOrEmpty(packageName) || string.IsNullOrEmpty(packageVersion))
{
packageName = item.GetMetadata(MetadataKeys.NuGetPackageId);
packageVersion = item.GetMetadata(MetadataKeys.NuGetPackageVersion);
}

if (string.IsNullOrEmpty(packageName) || string.IsNullOrEmpty(packageVersion))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ private void PopulateExistingReferenceItems()
var existingReferenceItemDependencies = new List<ITaskItem>();
foreach (var reference in References)
{
var packageName = reference.GetMetadata("NuGetPackageId");
var packageVersion = reference.GetMetadata("NuGetPackageVersion");
var packageName = reference.GetMetadata(MetadataKeys.NuGetPackageId);
var packageVersion = reference.GetMetadata(MetadataKeys.NuGetPackageVersion);

// This is not a "pre-resolved" assembly; skip it.
if (packageName == null || packageVersion == null)
Expand Down Expand Up @@ -722,4 +722,4 @@ internal static HashSet<string> GetImplicitPackageReferences(string defaultImpli
return implicitPackageReferences;
}
}
}
}
66 changes: 15 additions & 51 deletions src/Tasks/Microsoft.NET.Build.Tasks/ProduceContentAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ namespace Microsoft.NET.Build.Tasks
/// </summary>
public sealed class ProduceContentAssets : TaskBase
{
private const string PPOutputPathKey = "ppOutputPath";
private readonly Dictionary<string, string> _resolvedPaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private const string PPOutputPathKey = "PPOutputPath";
private readonly List<ITaskItem> _contentItems = new List<ITaskItem>();
private readonly List<ITaskItem> _fileWrites = new List<ITaskItem>();
private readonly List<ITaskItem> _copyLocalItems = new List<ITaskItem>();
Expand Down Expand Up @@ -50,14 +49,7 @@ public sealed class ProduceContentAssets : TaskBase
#region Inputs

/// <summary>
/// File Definitions, including metadata for the resolved path.
/// </summary>
[Required]
public ITaskItem[] ContentFileDefinitions { get; set; }

/// <summary>
/// Subset of File Dependencies that are content files, including metadata
/// such as buildAction, ppOutputPath etc.
/// Resolved paths to content file assets with metadata such as BuildAction, PPOutputPath etc.
/// </summary>
[Required]
public ITaskItem[] ContentFileDependencies { get; set; }
Expand Down Expand Up @@ -129,20 +121,6 @@ internal IContentAssetPreprocessor AssetPreprocessor
}

protected override void ExecuteCore()
{
MapContentFileDefinitions();
ProcessContentFileInputs();
}

private void MapContentFileDefinitions()
{
foreach (var item in ContentFileDefinitions ?? Enumerable.Empty<ITaskItem>())
{
_resolvedPaths.Add(item.ItemSpec, item.GetMetadata(MetadataKeys.ResolvedPath));
}
}

private void ProcessContentFileInputs()
{
var preprocessorValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Expand Down Expand Up @@ -185,7 +163,7 @@ private void ProcessContentFileInputs()
else
{
string projectLanguage = NuGetUtils.GetLockFileLanguageName(ProjectLanguage);
if (grouping.Any(t => t.GetMetadata("codeLanguage") == projectLanguage))
if (grouping.Any(t => t.GetMetadata("CodeLanguage") == projectLanguage))
{
codeLanguageToSelect = projectLanguage;
}
Expand Down Expand Up @@ -218,23 +196,11 @@ private bool IsPreprocessorFile(ITaskItem contentFile) =>

private void ProduceContentAsset(ITaskItem contentFile)
{
string resolvedPath;
if (!_resolvedPaths.TryGetValue(contentFile.ItemSpec, out resolvedPath))
{
Log.LogWarning(Strings.UnableToFindResolvedPath, contentFile.ItemSpec);
return;
}

string resolvedPath = contentFile.ItemSpec;
string pathToFinalAsset = resolvedPath;
string ppOutputPath = contentFile.GetMetadata(PPOutputPathKey);
string parentPackage = contentFile.GetMetadata(MetadataKeys.ParentPackage);
string[] parts = parentPackage?.Split('/');
if (parts == null || parts.Length != 2)
{
throw new BuildErrorException(Strings.ContentFileDoesNotContainExpectedParentPackageInformation, contentFile.ItemSpec);
}
string packageName = parts[0];
string packageVersion = parts[1];
string packageName = contentFile.GetMetadata(MetadataKeys.NuGetPackageId);
string packageVersion = contentFile.GetMetadata(MetadataKeys.NuGetPackageVersion);

if (!string.IsNullOrEmpty(ppOutputPath))
{
Expand All @@ -251,18 +217,17 @@ private void ProduceContentAsset(ITaskItem contentFile)
}
}

if (contentFile.GetBooleanMetadata("copyToOutput") == true)
if (contentFile.GetBooleanMetadata("CopyToOutput") == true)
{
string outputPath = contentFile.GetMetadata("outputPath");
string outputPath = contentFile.GetMetadata("OutputPath");
outputPath = string.IsNullOrEmpty(outputPath) ? ppOutputPath : outputPath;

if (!string.IsNullOrEmpty(outputPath))
{
var item = new TaskItem(pathToFinalAsset);
item.SetMetadata("TargetPath", outputPath);
item.SetMetadata(MetadataKeys.ParentPackage, parentPackage);
item.SetMetadata(MetadataKeys.PackageName, packageName);
item.SetMetadata(MetadataKeys.PackageVersion, packageVersion);
item.SetMetadata(MetadataKeys.NuGetPackageId, packageName);
item.SetMetadata(MetadataKeys.NuGetPackageVersion, packageVersion);

_copyLocalItems.Add(item);
}
Expand All @@ -273,13 +238,12 @@ private void ProduceContentAsset(ITaskItem contentFile)
}

// TODO if build action is none do we even need to write the processed file above?
string buildAction = contentFile.GetMetadata("buildAction");
string buildAction = contentFile.GetMetadata("BuildAction");
if (!string.Equals(buildAction, "none", StringComparison.OrdinalIgnoreCase))
{
var item = new TaskItem(pathToFinalAsset);
item.SetMetadata(MetadataKeys.ParentPackage, parentPackage);
item.SetMetadata(MetadataKeys.PackageName, packageName);
item.SetMetadata(MetadataKeys.PackageVersion, packageVersion);
item.SetMetadata(MetadataKeys.NuGetPackageId, packageName);
item.SetMetadata(MetadataKeys.NuGetPackageVersion, packageVersion);

// We'll put additional metadata on the item so we can convert it back to the real item group in our targets
item.SetMetadata("ProcessedItemType", buildAction);
Expand All @@ -288,11 +252,11 @@ private void ProduceContentAsset(ITaskItem contentFile)
// If this is XAML, the build targets expect Link metadata to construct the relative path
if (string.Equals(buildAction, "Page", StringComparison.OrdinalIgnoreCase))
{
item.SetMetadata("Link", Path.Combine("NuGet", parentPackage, Path.GetFileName(resolvedPath)));
item.SetMetadata("Link", Path.Combine("NuGet", packageName, packageVersion, Path.GetFileName(resolvedPath)));
}

_contentItems.Add(item);
}
}
}
}
}
Loading

0 comments on commit f40056b

Please sign in to comment.