protected internal virtual void SetHintPathAndPrivateValue(ProjectInstance instance) { // Private means local copy; we want to know if it is already set to not override the default string privateValue = this.ItemNode.GetMetadata(ProjectFileConstants.Private); ThreadHelper.ThrowIfNotOnUIThread(); // Get the list of items which require HintPath IEnumerable <ProjectItemInstance> references = MSBuildProjectInstance.GetItems(instance, MsBuildGeneratedItemType.ReferenceCopyLocalPaths); // Remove the HintPath, we will re-add it below if it is needed if (!String.IsNullOrEmpty(this.assemblyPath)) { this.ItemNode.SetMetadata(ProjectFileConstants.HintPath, null); } // Now loop through the generated References to find the corresponding one foreach (ProjectItemInstance reference in references) { string fileName = Path.GetFileNameWithoutExtension(MSBuildItem.GetEvaluatedInclude(reference)); if (String.Compare(fileName, this.assemblyName.Name, StringComparison.OrdinalIgnoreCase) == 0) { // We found it, now set some properties based on this. string hintPath = MSBuildItem.GetMetadataValue(reference, ProjectFileConstants.HintPath); if (!String.IsNullOrEmpty(hintPath)) { if (Path.IsPathRooted(hintPath)) { hintPath = PackageUtilities.GetPathDistance(this.ProjectMgr.BaseURI.Uri, new Uri(hintPath)); } this.ItemNode.SetMetadata(ProjectFileConstants.HintPath, hintPath); // If this is not already set, we default to true if (String.IsNullOrEmpty(privateValue)) { this.ItemNode.SetMetadata(ProjectFileConstants.Private, true.ToString()); } } break; } } }
/// <summary> /// Adds references to this container from a MSBuild project. /// </summary> public void LoadReferencesFromBuildProject(MSBuild.Project buildProject) { XSharpProjectPackage.Instance.UIThread.MustBeCalledFromUIThread(); List <ReferenceNode> duplicatedNode = new List <ReferenceNode>(); BuildResult buildResult = this.ProjectMgr.Build(MsBuildTarget.ResolveAssemblyReferences); var children = new List <ReferenceNode>(); foreach (string referenceType in SupportedReferenceTypes) { bool isAssemblyReference = referenceType == ProjectFileConstants.Reference; if (isAssemblyReference && !buildResult.IsSuccessful) { continue; } foreach (var item in MSBuildProject.GetItems(buildProject, referenceType)) { ProjectElement element = new ProjectElement(this.ProjectMgr, item, false); ReferenceNode node = CreateReferenceNode(referenceType, element); if (node != null) { // Make sure that we do not want to add the item twice to the ui hierarchy // We are using here the UI representation of the Node namely the Caption to find that out, in order to // avoid different representation problems. // Example :<Reference Include="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> // <Reference Include="EnvDTE80" /> bool found = false; for (HierarchyNode n = this.FirstChild; n != null && !found; n = n.NextSibling) { if (String.Compare(n.Caption, node.Caption, StringComparison.OrdinalIgnoreCase) == 0) { found = true; break; } } if (!found) { this.AddChild(node); children.Add(node); } else { duplicatedNode.Add(node); } } } } // Now manage duplicates if (duplicatedNode.Count > 0) { // Make a backup first string original = buildProject.FullPath; string backupName = Path.ChangeExtension(original, ".backup"); if (Utilities.DeleteFileSafe(backupName)) { File.Copy(original, backupName); } foreach (ReferenceNode node in duplicatedNode) { //this.RemoveChild( node ); node.Remove(false); } buildProject.Save(original); } var references = buildResult.ProjectInstance.GetItems(ProjectFileConstants.ReferencePath); //var references = MSBuildProjectInstance.GetItems(buildResult.ProjectInstance, ProjectFileConstants.ReferencePath); foreach (var reference in references) { string fullName = MSBuildItem.GetEvaluatedInclude(reference); string name = Path.GetFileNameWithoutExtension(fullName); foreach (var child in children) { if (child is XSharpAssemblyReferenceNode && child.Caption == name) { var xChild = child as XSharpAssemblyReferenceNode; xChild.AssemblyPath = fullName; xChild.SetHintPathAndPrivateValue(buildResult.ProjectInstance, reference); } } } }