/// <summary> /// /// </summary> /// <param name="ae_Mode"></param> private void UpdateReferences(ReferenceChangeMode ae_Mode) { // Get the file list and reference list GetScreen(out string[] lsa_FileList, out string[] lsa_ReferenceList); // Spin through the projects we are processing. StringBuilder lsb_Errors = new StringBuilder(); foreach (string ls_FileName in lsa_FileList) { try { // Pull the current csproj into an xdocument XDocument lxd_Document; XNamespace lxn_Namespace; XmlNamespaceManager lxnm_NamespaceMgr; using (XmlReader lxr_Reader = XmlReader.Create(ls_FileName)) { lxd_Document = XDocument.Load(lxr_Reader); lxn_Namespace = lxd_Document.Root.Name.Namespace; lxnm_NamespaceMgr = new XmlNamespaceManager(lxr_Reader.NameTable); lxnm_NamespaceMgr.AddNamespace("prefix", lxn_Namespace.NamespaceName); } // Get the list of nodes that contain the references and extract the Include tags XElement lxe_RefParent = lxd_Document.Root.XPathSelectElement("//prefix:Reference", lxnm_NamespaceMgr).Parent; XElement[] lxea_References = lxd_Document.Root.XPathSelectElements("//prefix:Reference[@Include]", lxnm_NamespaceMgr).ToArray(); // Based on the mode provided, we need to handle the contents of the file differently if (ae_Mode == ReferenceChangeMode.Add) { // Check each reference we were given. If a Reference tag does not already exist for it, add a new one foreach (string ls_Reference in lsa_ReferenceList) { if (!lxea_References.Any(lxe_Reference => lxe_Reference.Attribute("Include").Value.Equals(ls_Reference))) { XElement lxe_NewReference = new XElement(lxn_Namespace + "Reference", new XAttribute("Include", ls_Reference)); lxe_RefParent.Add(lxe_NewReference); } } } else if (ae_Mode == ReferenceChangeMode.Remove) { // Check each Reference node to see if the Include points to one of the provided references and remove it if so foreach (XElement lxe_Reference in lxea_References) { string ls_AttributeValue = lxe_Reference.Attribute("Include").Value; if (lsa_ReferenceList.Any(ls_AttributeValue.StartsWith)) { lxe_Reference.Remove(); } } } else { break; } // Save the doc. We do so twice to fix the formatting lxd_Document.Save(ls_FileName); lxd_Document = XDocument.Load(ls_FileName); lxd_Document.Save(ls_FileName); } catch (Exception ex) { lsb_Errors.AppendLine("[File: " + ls_FileName + "][Error: " + ex.Message + "]"); } } if (lsb_Errors.Length > 0) { throw new Exception(lsb_Errors.ToString()); } }
private static void ChangeReferences(IEnumerable<FileSystemInfo> projectFiles, string projectRootPath, string binariesPath, string targetFrameworkVersion, ReferenceChangeMode sysRuntimeMode, ReferenceChangeMode specificVersionMode, string[] targetProjects) { var duplicates = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); foreach (var singleProject in projectFiles) { var changed = false; var projectFile = new ProjectFile(singleProject.FullName); var relativePath = projectFile.CalculateRelativePath(projectRootPath); var assemblyName = projectFile.GetAssemblyName(); var projectGuid = projectFile.GetProjectId(); var currentDirectory = Path.GetDirectoryName(singleProject.FullName); if (currentDirectory == null) throw new Exception("Unable to resolve project directory"); if (duplicates.ContainsKey(projectGuid)) { throw new Exception( $"Projects has duplicate ids.\n{duplicates[projectGuid]}\n{singleProject.FullName}"); } duplicates.Add(projectGuid, singleProject.FullName); var projectReferences = projectFile.ProjectReferences; foreach (var reference in projectReferences) { var referedProject = Path.GetFullPath(Path.Combine(currentDirectory, reference.Include)); var referedProjectFile = new ProjectFile(referedProject); var referedAssemblyName = referedProjectFile.GetAssemblyName(); var referedOutputPath = referedProjectFile.GetOutputPath(); var referredDirectoryName = Path.GetDirectoryName(referedProject); if (referredDirectoryName == null) throw new Exception("Unable to resolve reffered directory name."); var toPath = Path.GetFullPath(Path.Combine(referredDirectoryName, referedOutputPath)); var outputDir = CalculateRelativePath(projectFile, singleProject.FullName, toPath); var hintPath = $"{outputDir}{referedAssemblyName}.dll"; projectFile.AddReference(reference.Name, hintPath); } var outputPath = relativePath + binariesPath; if (!outputPath.EndsWith("\\")) { outputPath += "\\"; } var isTargetProject = IsTargetProject(assemblyName, targetProjects, projectFile); changed |= projectFile.SetReferencePrivacy(isTargetProject, new[] { "\\packages\\", outputPath }); changed |= projectFile.SetReferenceSpecificVersion(specificVersionMode); changed |= projectFile.RemoveProjectReferences(); if (sysRuntimeMode == ReferenceChangeMode.Add) changed |= projectFile.AddSystemRuntimeReference(); else if (sysRuntimeMode == ReferenceChangeMode.Remove) changed |= projectFile.RemoveSystemRuntimeReference(); if (!string.IsNullOrWhiteSpace(targetFrameworkVersion)) { changed |= projectFile.SetTargetFrameworkVersion(targetFrameworkVersion); } changed |= projectFile.RemoveNuget(); if (changed) { ConsoleLogger.Default.Info($"Replacing {singleProject.FullName}"); projectFile.Save(); } } }