public void CopyMetadataFromITaskItem() { TaskItem from = new TaskItem(); from.ItemSpec = "Monkey.txt"; from.SetMetadata("Dog", "Bingo"); from.SetMetadata("Cat", "Morris"); from.SetMetadata("Bird", "Big"); TaskItem to = new TaskItem(); to.ItemSpec = "Bonobo.txt"; to.SetMetadata("Sponge", "Bob"); to.SetMetadata("Dog", "Harriet"); to.SetMetadata("Cat", "Mike"); from.CopyMetadataTo(to); Assert.AreEqual("Bonobo.txt", to.ItemSpec); // ItemSpec is never overwritten Assert.AreEqual("Bob", to.GetMetadata("Sponge")); // Metadata not in source are preserverd. Assert.AreEqual("Harriet", to.GetMetadata("Dog")); // Metadata present on destination are not overwritten. Assert.AreEqual("Mike", to.GetMetadata("Cat")); Assert.AreEqual("Big", to.GetMetadata("Bird")); }
/// <summary> /// Set metadata on the items which will be output from RAR. /// </summary> private ITaskItem SetItemMetadata(ArrayList relatedItems, ArrayList satelliteItems, ArrayList serializationAssemblyItems, ArrayList scatterItems, string fusionName, Reference reference, AssemblyNameExtension assemblyName, FileExists fileExists) { // Set up the main item. ITaskItem referenceItem = new TaskItem(); referenceItem.ItemSpec = reference.FullPath; referenceItem.SetMetadata(ItemMetadataNames.resolvedFrom, reference.ResolvedSearchPath); // Set the CopyLocal metadata. if (reference.IsCopyLocal) { referenceItem.SetMetadata(ItemMetadataNames.copyLocal, "true"); } else { referenceItem.SetMetadata(ItemMetadataNames.copyLocal, "false"); } // Set the FusionName metadata. referenceItem.SetMetadata(ItemMetadataNames.fusionName, fusionName); // Set the Redist name metadata. if (!String.IsNullOrEmpty(reference.RedistName)) { referenceItem.SetMetadata(ItemMetadataNames.redist, reference.RedistName); } if (Reference.IsFrameworkFile(reference.FullPath, _frameworkPaths) || (_installedAssemblies != null && _installedAssemblies.FrameworkAssemblyEntryInRedist(assemblyName))) { if (!IsAssemblyRemovedFromDotNetFramework(assemblyName, reference.FullPath, _frameworkPaths, _installedAssemblies)) { referenceItem.SetMetadata(ItemMetadataNames.frameworkFile, "true"); } } if (!String.IsNullOrEmpty(reference.ImageRuntime)) { referenceItem.SetMetadata(ItemMetadataNames.imageRuntime, reference.ImageRuntime); } if (reference.IsWinMDFile) { referenceItem.SetMetadata(ItemMetadataNames.winMDFile, "true"); // The ImplementationAssembly is only set if the implementation file exits on disk if (reference.ImplementationAssembly != null) { if (VerifyArchitectureOfImplementationDll(reference.ImplementationAssembly, reference.FullPath)) { referenceItem.SetMetadata(ItemMetadataNames.winmdImplmentationFile, Path.GetFileName(reference.ImplementationAssembly)); // Add the implementation item as a related file ITaskItem item = new TaskItem(reference.ImplementationAssembly); // Clone metadata. referenceItem.CopyMetadataTo(item); // Related files don't have a fusion name. item.SetMetadata(ItemMetadataNames.fusionName, ""); RemoveNonForwardableMetadata(item); // Add the related item. relatedItems.Add(item); } } if (reference.IsManagedWinMDFile) { referenceItem.SetMetadata(ItemMetadataNames.winMDFileType, "Managed"); } else { referenceItem.SetMetadata(ItemMetadataNames.winMDFileType, "Native"); } } // Set the IsRedistRoot metadata if (reference.IsRedistRoot == true) { referenceItem.SetMetadata(ItemMetadataNames.isRedistRoot, "true"); } else if (reference.IsRedistRoot == false) { referenceItem.SetMetadata(ItemMetadataNames.isRedistRoot, "false"); } else { // This happens when the redist root is "null". This means there // was no IsRedistRoot flag in the Redist XML (or there was no // redist XML at all for this item). } // If there was a primary source item, then forward metadata from it. // It's important that the metadata from the primary source item // win over the same metadata from other source items, so that's // why we put this first. (CopyMetadataTo will never override an // already existing metadata.) For example, if this reference actually // came directly from an item declared in the project file, we'd // want to use the metadata from it, not some other random item in // the project file that happened to have this reference as a dependency. if (reference.PrimarySourceItem != null) { reference.PrimarySourceItem.CopyMetadataTo(referenceItem); } else { bool hasImplementationFile = referenceItem.GetMetadata(ItemMetadataNames.winmdImplmentationFile).Length > 0; bool hasImageRuntime = referenceItem.GetMetadata(ItemMetadataNames.imageRuntime).Length > 0; bool hasWinMDFile = referenceItem.GetMetadata(ItemMetadataNames.winMDFile).Length > 0; // If there were non-primary source items, then forward metadata from them. ICollection sourceItems = reference.GetSourceItems(); foreach (ITaskItem sourceItem in sourceItems) { sourceItem.CopyMetadataTo(referenceItem); } // If the item originally did not have the implementation file metadata then we do not want to get it from the set of primary source items // since the implementation file is something specific to the source item and not supposed to be propigated. if (!hasImplementationFile) { referenceItem.RemoveMetadata(ItemMetadataNames.winmdImplmentationFile); } // If the item originally did not have the ImageRuntime metadata then we do not want to get it from the set of primary source items // since the ImageRuntime is something specific to the source item and not supposed to be propigated. if (!hasImageRuntime) { referenceItem.RemoveMetadata(ItemMetadataNames.imageRuntime); } // If the item originally did not have the WinMDFile metadata then we do not want to get it from the set of primary source items // since the WinMDFile is something specific to the source item and not supposed to be propigated if (!hasWinMDFile) { referenceItem.RemoveMetadata(ItemMetadataNames.winMDFile); } } if (reference.ReferenceVersion != null) { referenceItem.SetMetadata(ItemMetadataNames.version, reference.ReferenceVersion.ToString()); } else { referenceItem.SetMetadata(ItemMetadataNames.version, String.Empty); } // Now clone all properties onto the related files. foreach (string relatedFileExtension in reference.GetRelatedFileExtensions()) { ITaskItem item = new TaskItem(reference.FullPathWithoutExtension + relatedFileExtension); // Clone metadata. referenceItem.CopyMetadataTo(item); // Related files don't have a fusion name. item.SetMetadata(ItemMetadataNames.fusionName, ""); RemoveNonForwardableMetadata(item); // Add the related item. relatedItems.Add(item); } // Set up the satellites. foreach (string satelliteFile in reference.GetSatelliteFiles()) { ITaskItem item = new TaskItem(Path.Combine(reference.DirectoryName, satelliteFile)); // Clone metadata. referenceItem.CopyMetadataTo(item); // Set the destination directory. item.SetMetadata(ItemMetadataNames.destinationSubDirectory, FileUtilities.EnsureTrailingSlash(Path.GetDirectoryName(satelliteFile))); // Satellite files don't have a fusion name. item.SetMetadata(ItemMetadataNames.fusionName, ""); RemoveNonForwardableMetadata(item); // Add the satellite item. satelliteItems.Add(item); } // Set up the serialization assemblies foreach (string serializationAssemblyFile in reference.GetSerializationAssemblyFiles()) { ITaskItem item = new TaskItem(Path.Combine(reference.DirectoryName, serializationAssemblyFile)); // Clone metadata. referenceItem.CopyMetadataTo(item); // serialization assemblies files don't have a fusion name. item.SetMetadata(ItemMetadataNames.fusionName, ""); RemoveNonForwardableMetadata(item); // Add the serialization assembly item. serializationAssemblyItems.Add(item); } // Set up the scatter files. foreach (string scatterFile in reference.GetScatterFiles()) { ITaskItem item = new TaskItem(Path.Combine(reference.DirectoryName, scatterFile)); // Clone metadata. referenceItem.CopyMetadataTo(item); // We don't have a fusion name for scatter files. item.SetMetadata(ItemMetadataNames.fusionName, ""); RemoveNonForwardableMetadata(item); // Add the satellite item. scatterItems.Add(item); } // As long as the item has not come from somewhere else say it came from rar (p2p's can come from somewhere else). if (referenceItem.GetMetadata(ItemMetadataNames.msbuildReferenceSourceTarget).Length == 0) { referenceItem.SetMetadata(ItemMetadataNames.msbuildReferenceSourceTarget, "ResolveAssemblyReference"); } if (referenceItem.GetMetadata(ItemMetadataNames.msbuildReferenceSourceTarget).Equals("ProjectReference")) { if (reference.PrimarySourceItem != null) { referenceItem.SetMetadata(ItemMetadataNames.projectReferenceOriginalItemSpec, reference.PrimarySourceItem.GetMetadata("OriginalItemSpec")); } } return referenceItem; }
public void TestCopyMetadataTo () { item1 = new TaskItem ("itemSpec"); item2 = new TaskItem ("itemSpec"); item1.SetMetadata ("A", "1"); item1.SetMetadata ("B", "1"); item1.SetMetadata ("C", "1"); item2.SetMetadata ("B", "2"); item1.CopyMetadataTo (item2); Assert.AreEqual ("1", item2.GetMetadata ("A"), "1"); Assert.AreEqual ("2", item2.GetMetadata ("B"), "2"); Assert.AreEqual ("1", item2.GetMetadata ("C"), "3"); }
private static ITaskItem SetItemMetadata(ArrayList relatedItems, ArrayList satelliteItems, ArrayList serializationAssemblyItems, ArrayList scatterItems, string fusionName, Reference reference) { ITaskItem destinationItem = new TaskItem { ItemSpec = reference.FullPath }; destinationItem.SetMetadata("ResolvedFrom", reference.ResolvedSearchPath); if (reference.IsCopyLocal) { destinationItem.SetMetadata("CopyLocal", "true"); } else { destinationItem.SetMetadata("CopyLocal", "false"); } destinationItem.SetMetadata("FusionName", fusionName); if (!string.IsNullOrEmpty(reference.RedistName)) { destinationItem.SetMetadata("Redist", reference.RedistName); } if (reference.IsRedistRoot == true) { destinationItem.SetMetadata("IsRedistRoot", "true"); } else if (reference.IsRedistRoot == false) { destinationItem.SetMetadata("IsRedistRoot", "false"); } if (reference.PrimarySourceItem != null) { reference.PrimarySourceItem.CopyMetadataTo(destinationItem); } else { foreach (ITaskItem item2 in reference.GetSourceItems()) { item2.CopyMetadataTo(destinationItem); } } if (reference.ReferenceVersion != null) { destinationItem.SetMetadata("Version", reference.ReferenceVersion.ToString()); } else { destinationItem.SetMetadata("Version", string.Empty); } foreach (string str in reference.GetRelatedFileExtensions()) { ITaskItem item3 = new TaskItem(reference.FullPathWithoutExtension + str); destinationItem.CopyMetadataTo(item3); item3.SetMetadata("FusionName", ""); relatedItems.Add(item3); } foreach (string str2 in reference.GetSatelliteFiles()) { ITaskItem item4 = new TaskItem(Path.Combine(reference.DirectoryName, str2)); destinationItem.CopyMetadataTo(item4); item4.SetMetadata("DestinationSubDirectory", Microsoft.Build.Shared.FileUtilities.EnsureTrailingSlash(Path.GetDirectoryName(str2))); item4.SetMetadata("FusionName", ""); satelliteItems.Add(item4); } foreach (string str3 in reference.GetSerializationAssemblyFiles()) { ITaskItem item5 = new TaskItem(Path.Combine(reference.DirectoryName, str3)); destinationItem.CopyMetadataTo(item5); item5.SetMetadata("FusionName", ""); serializationAssemblyItems.Add(item5); } foreach (string str4 in reference.GetScatterFiles()) { ITaskItem item6 = new TaskItem(Path.Combine(reference.DirectoryName, str4)); destinationItem.CopyMetadataTo(item6); item6.SetMetadata("FusionName", ""); scatterItems.Add(item6); } return destinationItem; }