diff --git a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAProduceContentsAssetsTask.cs b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAProduceContentsAssetsTask.cs index b22ea5c92acf..b2b2a1bff667 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAProduceContentsAssetsTask.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAProduceContentsAssetsTask.cs @@ -179,12 +179,16 @@ public void ItOutputsCopyLocalItems() var item = copyLocalItems.Where(t => t.ItemSpec.EndsWith(assetWritePath)).First(); item.GetMetadata("TargetPath").Should().Be("samplepp.output.txt"); item.GetMetadata(MetadataKeys.ParentPackage).Should().Be(package); + item.GetMetadata(MetadataKeys.PackageName).Should().Be("LibA"); + item.GetMetadata(MetadataKeys.PackageVersion).Should().Be("1.2.3"); for (int i = 1; i < 3; i++) { item = copyLocalItems.Where(t => t.ItemSpec.EndsWith(contentFiles[i])).First(); item.GetMetadata("TargetPath").Should().Be(Path.Combine("output", contentFiles[i])); item.GetMetadata(MetadataKeys.ParentPackage).Should().Be(package); + item.GetMetadata(MetadataKeys.PackageName).Should().Be("LibA"); + item.GetMetadata(MetadataKeys.PackageVersion).Should().Be("1.2.3"); } // not added to copy diff --git a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAResolvePackageDependenciesTask.cs b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAResolvePackageDependenciesTask.cs index 0cfe35e26c3d..fb29e06bc057 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAResolvePackageDependenciesTask.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenAResolvePackageDependenciesTask.cs @@ -348,6 +348,8 @@ public void ItAssignsFileDefinitionMetadata() fileDefns.Count().Should().Be(1); fileDefns.First().GetMetadata(MetadataKeys.Type).Should().Be(pair.Value); fileDefns.First().GetMetadata(MetadataKeys.Path).Should().Be(pair.Key); + fileDefns.First().GetMetadata(MetadataKeys.PackageName).Should().Be("LibB"); + fileDefns.First().GetMetadata(MetadataKeys.PackageVersion).Should().Be("1.2.3"); fileDefns.First().GetMetadata(MetadataKeys.ResolvedPath) .Should().Be(Path.Combine(_packageRoot, "LibB", "1.2.3", "path", pair.Key.Replace('/', Path.DirectorySeparatorChar))); diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/MetadataKeys.cs b/src/Tasks/Microsoft.NET.Build.Tasks/MetadataKeys.cs index d73a3beb2534..c6c201fcde94 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/MetadataKeys.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/MetadataKeys.cs @@ -12,6 +12,8 @@ public static class MetadataKeys public const string FileGroup = "FileGroup"; public const string Path = "Path"; public const string ResolvedPath = "ResolvedPath"; + public const string PackageName = "PackageName"; + public const string PackageVersion = "PackageVersion"; public const string IsImplicitlyDefined = "IsImplicitlyDefined"; public const string IsTopLevelDependency = "IsTopLevelDependency"; diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ProduceContentAssets.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ProduceContentAssets.cs index 9a5ca5ac4989..6c7db6bbd343 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ProduceContentAssets.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ProduceContentAssets.cs @@ -228,6 +228,13 @@ private void ProduceContentAsset(ITaskItem contentFile) 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]; if (!string.IsNullOrEmpty(ppOutputPath)) { @@ -235,14 +242,9 @@ private void ProduceContentAsset(ITaskItem contentFile) { throw new BuildErrorException(Strings.ContentPreproccessorParameterRequired, nameof(ProduceContentAssets), nameof(ContentPreprocessorOutputDirectory)); } - string [] parts = parentPackage?.Split('/'); - if (parts == null) - { - throw new BuildErrorException(Strings.ContentFileDoesNotContainExpectedParentPackageInformation, contentFile.ItemSpec); - } // We need the preprocessed output, so let's run the preprocessor here - string relativeOutputPath = Path.Combine(parts[0], parts[1], ppOutputPath); + string relativeOutputPath = Path.Combine(packageName, packageVersion, ppOutputPath); if (AssetPreprocessor.Process(resolvedPath, relativeOutputPath, out pathToFinalAsset)) { _fileWrites.Add(new TaskItem(pathToFinalAsset)); @@ -259,6 +261,8 @@ private void ProduceContentAsset(ITaskItem contentFile) var item = new TaskItem(pathToFinalAsset); item.SetMetadata("TargetPath", outputPath); item.SetMetadata(MetadataKeys.ParentPackage, parentPackage); + item.SetMetadata(MetadataKeys.PackageName, packageName); + item.SetMetadata(MetadataKeys.PackageVersion, packageVersion); _copyLocalItems.Add(item); } @@ -274,6 +278,8 @@ private void ProduceContentAsset(ITaskItem contentFile) { var item = new TaskItem(pathToFinalAsset); item.SetMetadata(MetadataKeys.ParentPackage, parentPackage); + item.SetMetadata(MetadataKeys.PackageName, packageName); + item.SetMetadata(MetadataKeys.PackageVersion, 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); diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs index 02f9426ec6da..790c41573742 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs @@ -184,11 +184,13 @@ private void GetPackageAndFileDefinitions() TaskItem item; foreach (var package in LockFile.Libraries) { - string packageId = $"{package.Name}/{package.Version.ToNormalizedString()}"; + var packageName = package.Name; + var packageVersion = package.Version.ToNormalizedString(); + string packageId = $"{packageName}/{packageVersion}"; item = new TaskItem(packageId); - item.SetMetadata(MetadataKeys.Name, package.Name); + item.SetMetadata(MetadataKeys.Name, packageName); item.SetMetadata(MetadataKeys.Type, package.Type); - item.SetMetadata(MetadataKeys.Version, package.Version.ToNormalizedString()); + item.SetMetadata(MetadataKeys.Version, packageVersion); item.SetMetadata(MetadataKeys.Path, package.Path ?? string.Empty); @@ -207,6 +209,8 @@ private void GetPackageAndFileDefinitions() var fileKey = $"{packageId}/{file}"; var fileItem = new TaskItem(fileKey); fileItem.SetMetadata(MetadataKeys.Path, file); + fileItem.SetMetadata(MetadataKeys.PackageName, packageName); + fileItem.SetMetadata(MetadataKeys.PackageVersion, packageVersion); string resolvedPath = ResolveFilePath(file, resolvedPackagePath); fileItem.SetMetadata(MetadataKeys.ResolvedPath, resolvedPath ?? string.Empty); @@ -388,6 +392,8 @@ private void GetFileDependencies(LockFileTargetLibrary package, string targetNam { // NOTE: the path provided for framework assemblies is the name of the framework reference item.SetMetadata("FrameworkAssembly", filePath); + item.SetMetadata(MetadataKeys.PackageName, package.Name); + item.SetMetadata(MetadataKeys.PackageVersion, package.Version.ToNormalizedString()); } foreach (var property in properties) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.PackageDependencyResolution.targets b/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.PackageDependencyResolution.targets index 9c6df32606c9..9cbb1fd32123 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.PackageDependencyResolution.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.PackageDependencyResolution.targets @@ -331,10 +331,12 @@ Copyright (c) .NET Foundation. All rights reserved. <__CompileFileDefinitions Include="@(FileDefinitions)" Exclude="@(_CompileFileItems)" /> <_CompileFileDefinitions Include="@(FileDefinitions)" Exclude="@(__CompileFileDefinitions)" /> - + false false Package + %(PackageName) + %(PackageVersion) @@ -347,10 +349,12 @@ Copyright (c) .NET Foundation. All rights reserved. <_FrameworkAssemblies Include="@(_TFMOnlyFileDependencies->WithMetadataValue('FileGroup', 'FrameworkAssembly'))" /> - + false true Package + %(PackageName) + %(PackageVersion) @@ -462,10 +466,22 @@ Copyright (c) .NET Foundation. All rights reserved. - + <_AllCopyLocalItems Include="@(_NativeCopyLocalItems);@(_RuntimeCopyLocalItems);@(_ResourceCopyLocalItems)" /> + + + false + false + Package + %(PackageName) + %(PackageVersion) + + + false false Package + %(PackageName) + %(PackageVersion)