public virtual void RemoveReference(string name) { NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); try { // Get the reference name without extension string referenceName = Path.GetFileNameWithoutExtension(name); // Remove the reference from the project // NOTE:- Project.Object.References.Item requires Reference.Identity // which is, the Assembly name without path or extension // But, we pass in the assembly file name. And, this works for // almost all the assemblies since Assembly Name is the same as the assembly file name // In case of F#, the input parameter is case-sensitive as well // Hence, an override to THIS function is added to take care of that var reference = EnvDTEProjectUtility.GetReferences(EnvDTEProject).Item(referenceName); if (reference != null) { reference.Remove(); NuGetProjectContext.Log(ProjectManagement.MessageLevel.Debug, Strings.Debug_RemoveReference, name, ProjectName); } } catch (Exception e) { NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, e.Message); } }); }
/// <summary> /// WORKAROUND: /// This override is in place to handle the case-sensitive call to Project.Object.References.Item /// There are certain assemblies where the AssemblyName and Assembly file name do not match in case /// And, this causes a mismatch. For more information, Refer to the RemoveReference of the base class /// </summary> /// <param name="name"></param> public override void RemoveReference(string name) { NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); RemoveReferenceCore(name, EnvDTEProjectUtility.GetReferences(EnvDTEProject)); }); }
public virtual bool ReferenceExists(string name) { try { string referenceName = name; if (ProjectManagement.Constants.AssemblyReferencesExtensions.Contains(Path.GetExtension(name), StringComparer.OrdinalIgnoreCase)) { // Get the reference name without extension referenceName = Path.GetFileNameWithoutExtension(name); } return(EnvDTEProjectUtility.GetReferences(EnvDTEProject).Item(referenceName) != null); } catch { } return(false); }
public virtual bool ReferenceExists(string name) { return(NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); try { string referenceName = name; if (Constants.AssemblyReferencesExtensions.Contains(Path.GetExtension(name), StringComparer.OrdinalIgnoreCase)) { // Get the reference name without extension referenceName = Path.GetFileNameWithoutExtension(name); } return EnvDTEProjectUtility.GetReferences(EnvDTEProject).Item(referenceName) != null; } catch { } return false; })); }
public virtual void AddReference(string referencePath) { if (referencePath == null) { throw new ArgumentNullException(nameof(referencePath)); } var name = Path.GetFileNameWithoutExtension(referencePath); var projectName = string.Empty; var projectFullPath = string.Empty; var assemblyFullPath = string.Empty; var dteProjectFullName = string.Empty; var dteOriginalPath = string.Empty; var resolvedToPackage = false; try { // Perform all DTE operations on the UI thread NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // Read DTE properties from the UI thread projectFullPath = ProjectFullPath; projectName = ProjectName; dteProjectFullName = EnvDTEProject.FullName; // Get the full path to the reference assemblyFullPath = Path.Combine(projectFullPath, referencePath); // Add a reference to the project var references = EnvDTEProjectUtility.GetReferences(EnvDTEProject); dynamic reference = references.Add(assemblyFullPath); if (reference != null) { dteOriginalPath = GetReferencePath(reference); // If path != fullPath, we need to set CopyLocal thru msbuild by setting Private // to true. // This happens if the assembly appears in any of the search paths that VS uses to // locate assembly references. // Most commonly, it happens if this assembly is in the GAC or in the output path. // The path may be null or for some project system it can be "". resolvedToPackage = !string.IsNullOrWhiteSpace(dteOriginalPath) && IsSamePath(dteOriginalPath, assemblyFullPath); if (resolvedToPackage) { // Set reference properties (if needed) TrySetCopyLocal(reference); TrySetSpecificVersion(reference); } } }); if (!resolvedToPackage) { // This should be done off the UI thread // Get the msbuild project for this project var buildProject = EnvDTEProjectUtility.AsMicrosoftBuildEvaluationProject(dteProjectFullName); if (buildProject != null) { // Get the assembly name of the reference we are trying to add var assemblyName = AssemblyName.GetAssemblyName(assemblyFullPath); // Try to find the item for the assembly name var item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences() where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2) select assemblyReferenceNode.Item1).FirstOrDefault(); if (item != null) { // Add the <HintPath> metadata item as a relative path var projectPath = PathUtility.EnsureTrailingSlash(projectFullPath); var relativePath = PathUtility.GetRelativePath(projectPath, referencePath); item.SetMetadataValue("HintPath", relativePath); // Set <Private> to true item.SetMetadataValue("Private", "True"); FileSystemUtility.MakeWritable(dteProjectFullName); // Change to the UI thread to save NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // Save the project after we've modified it. EnvDTEProject.Save(); }); } } else { // The reference cannot be changed by modifying the project file. // This could be a failure, however that could be a breaking // change if there is a non-msbuild project system relying on this // to skip references. // Log a warning to let the user know that their reference may have failed. NuGetProjectContext.Log( ProjectManagement.MessageLevel.Warning, Strings.FailedToAddReference, name); } } } catch (Exception e) { throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, Strings.FailedToAddReference, name), e); } NuGetProjectContext.Log( ProjectManagement.MessageLevel.Debug, $"Added reference '{name}' to project:'{projectName}'. Was the Reference Resolved To Package (resolvedToPackage):'{resolvedToPackage}', " + "where Reference Path from DTE(dteOriginalPath):'{dteOriginalPath}' and Reference Path from package reference(assemblyFullPath):'{assemblyFullPath}'."); }
/// <summary> /// This method should be on the UI thread. The overrides should ensure that /// </summary> protected virtual void AddGacReference(string name) { Debug.Assert(ThreadHelper.CheckAccess()); EnvDTEProjectUtility.GetReferences(EnvDTEProject).Add(name); }
public virtual void AddReference(string referencePath) { if (referencePath == null) { throw new ArgumentNullException("referencePath"); } string name = Path.GetFileNameWithoutExtension(referencePath); try { // Get the full path to the reference string fullPath = Path.Combine(ProjectFullPath, referencePath); string assemblyPath = fullPath; bool usedTempFile = false; // There is a bug in Visual Studio whereby if the fullPath contains a comma, // then calling Project.Object.References.Add() on it will throw a COM exception. // To work around it, we copy the assembly into temp folder and add reference to the copied assembly if (fullPath.Contains(",")) { string tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(fullPath)); File.Copy(fullPath, tempFile, true); assemblyPath = tempFile; usedTempFile = true; } // Add a reference to the project dynamic reference = EnvDTEProjectUtility.GetReferences(EnvDTEProject).Add(assemblyPath); // if we copied the assembly to temp folder earlier, delete it now since we no longer need it. if (usedTempFile) { try { File.Delete(assemblyPath); } catch { // don't care if we fail to delete a temp file } } if (reference != null) { var path = GetReferencePath(reference); // If path != fullPath, we need to set CopyLocal thru msbuild by setting Private // to true. // This happens if the assembly appears in any of the search paths that VS uses to // locate assembly references. // Most commonly, it happens if this assembly is in the GAC or in the output path. if (path != null && !path.Equals(fullPath, StringComparison.OrdinalIgnoreCase)) { // Get the msbuild project for this project MicrosoftBuildEvaluationProject buildProject = EnvDTEProjectUtility.AsMicrosoftBuildEvaluationProject(EnvDTEProject); if (buildProject != null) { // Get the assembly name of the reference we are trying to add AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath); // Try to find the item for the assembly name MicrosoftBuildEvaluationProjectItem item = (from assemblyReferenceNode in buildProject.GetAssemblyReferences() where AssemblyNamesMatch(assemblyName, assemblyReferenceNode.Item2) select assemblyReferenceNode.Item1).FirstOrDefault(); if (item != null) { // Add the <HintPath> metadata item as a relative path item.SetMetadataValue("HintPath", referencePath); // Set <Private> to true item.SetMetadataValue("Private", "True"); // Save the project after we've modified it. FileSystemUtility.MakeWriteable(EnvDTEProject.FullName); EnvDTEProject.Save(); } } } else { TrySetCopyLocal(reference); TrySetSpecificVersion(reference); } } NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_AddReference, name, ProjectName); } catch (Exception e) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Strings.FailedToAddReference, name), e); } }
protected virtual void AddGacReference(string name) { EnvDTEProjectUtility.GetReferences(EnvDTEProject).Add(name); }
/// <summary> /// WORKAROUND: /// This override is in place to handle the case-sensitive call to Project.Object.References.Item /// There are certain assemblies where the AssemblyName and Assembly file name do not match in case /// And, this causes a mismatch. For more information, Refer to the RemoveReference of the base class /// </summary> /// <param name="name"></param> public override void RemoveReference(string name) { RemoveReferenceCore(name, EnvDTEProjectUtility.GetReferences(EnvDTEProject)); }