private bool TryToRetrieveAssemblyInfoOfProjectReference(string solutionfile, List <Project> projects, ProjectRef projref, out string assemblyname, out string outputtype) { XDocument xdoc; XNamespace ns; string fullfilename = Path.Combine( Path.GetDirectoryName(solutionfile), Path.GetDirectoryName(_sln_path), projref.Include); ConsoleHelper.WriteLine($" Loading external project: '{fullfilename}'.", true); try { // Delve greedily and deep into the external project file. xdoc = XDocument.Load(fullfilename); } catch (IOException ex) { ConsoleHelper.ColorWrite(ConsoleColor.Red, $"Couldn't load project: '{fullfilename}': {ex.Message}"); assemblyname = outputtype = null; return(false); } catch (System.Xml.XmlException ex) { ConsoleHelper.ColorWrite(ConsoleColor.Red, $"Couldn't load project: '{fullfilename}': {ex.Message}"); assemblyname = outputtype = null; return(false); } ns = xdoc.Root.Name.Namespace; XElement[] assemblynames = xdoc.Elements(ns + "Project").Elements(ns + "PropertyGroup").Elements(ns + "AssemblyName").ToArray(); if (assemblynames.Length == 0) { ConsoleHelper.ColorWrite(ConsoleColor.Yellow, $"Couldn't load project: '{fullfilename}': Missing AssemblyName."); assemblyname = null; } else { assemblyname = assemblynames.Single().Value; } XElement[] outputtypes = xdoc.Elements(ns + "Project").Elements(ns + "PropertyGroup").Elements(ns + "OutputType").ToArray(); if (outputtypes.Length == 0) { ConsoleHelper.ColorWrite(ConsoleColor.Yellow, $"Couldn't load project: '{fullfilename}': Missing OutputType."); outputtype = null; } else { outputtype = outputtypes.Single().Value; } if (assemblyname != null) { // Caution: External project (its assembly name) may conflict with a loaded project name bool afterthis = false; foreach (Project proj in projects.OrderBy(p => p._sln_path)) { if (proj == this) { afterthis = true; continue; } if (!afterthis) { continue; } if (assemblyname == proj._proj_assemblyname) { ConsoleHelper.ColorWrite(ConsoleColor.Red, $"Error: Projects have identical assembly names: '{assemblyname}': '{fullfilename}' and '{proj._sln_path}'."); throw new Exception("Error"); } } } return(true); }
private AssemblyRef CreateReferenceFromProjectReference(string solutionfile, List <Project> projects, List <string> hintpaths, ProjectRef projref) { // Look for assembly name in external project file. The project file might not exist though. TryToRetrieveAssemblyInfoOfProjectReference(solutionfile, projects, projref, out string assemblyname, out string outputtype); //if (projref.shortinclude != assemblyname) { //ConsoleHelper.ColorWrite(ConsoleColor.Yellow, $"Warning: '{projref.shortinclude}' -> '{assemblyname}'."); } if (assemblyname == null) { // Guess assembly name = proj name assemblyname = projref.Shortinclude; } if (outputtype == null) { // Guess output type = Library outputtype = "Library"; } string ext; switch (outputtype) { case "Library": ext = ".dll"; break; case "WinExe": ext = ".exe"; break; case "Exe": ext = ".exe"; break; case "Database": ext = ".dll"; break; default: throw new Exception($"Unsupported project type: '{assemblyname}' '{outputtype}'."); } // Locate assembly // if we had used OutputFolder from projref project (instead of hintpaths), // debug/release may have caused problems string asspath = LocateAssemblyInHintPaths(solutionfile, hintpaths, assemblyname, ext); return(new AssemblyRef { Include = assemblyname, Shortinclude = assemblyname, Name = assemblyname, Hintpath = asspath, Names = null, Hintpaths = null }); }
private AssemblyRef CreateReferenceFromProjectReference(string solutionfile, List<Project> projects, List<string> hintpaths, ProjectRef projref) { // Look for assembly name in external project file. The project file might not exist though. string assemblyname; string outputtype; TryToRetrieveAssemblyInfoOfProjectReference(solutionfile, projects, projref, out assemblyname, out outputtype); //if (projref.shortinclude != assemblyname) { //ConsoleHelper.ColorWrite(ConsoleColor.Yellow, "Warning: '" + projref.shortinclude + "' -> '" + assemblyname + "'."); } if (assemblyname == null) { // Guess assembly name = proj name assemblyname = projref.shortinclude; } string ext; switch (outputtype) { case "Library": ext = ".dll"; break; case "WinExe": ext = ".exe"; break; case "Exe": ext = ".exe"; break; case "Database": ext = ".dll"; break; default: throw new Exception("Unsupported project type: '" + assemblyname + "' '" + outputtype + "'."); } // Locate assembly // if we had used OutputFolder from projref project (instead of hintpaths), // debug/release may have caused problems string asspath = LocateAssemblyInHintPaths(solutionfile, hintpaths, assemblyname, ext); return new AssemblyRef { include = assemblyname, shortinclude = assemblyname, name = assemblyname, hintpath = asspath, names = null, hintpaths = null }; }
public bool Fix(string solutionfile, List <Project> projects, List <string> hintpaths, string outputpath, bool copylocal, bool removeversion) { ConsoleHelper.WriteLineDeferred($"-=-=- Fixing project: '{_sln_path}' -=-=-"); // ass -> proj foreach (AssemblyRef assref in _references.OrderBy(r => r.Shortinclude)) { bool exists = projects.Any(p => p._proj_assemblyname == assref.Shortinclude); if (exists) { ProjectRef projref = CreateProjectReferenceFromReference(solutionfile, projects, assref); if (assref.Shortinclude == projref.Shortinclude) { ConsoleHelper.WriteLine($" ref -> projref: '{assref.Shortinclude}'.", true); } else { ConsoleHelper.WriteLine($" ref -> projref: '{assref.Shortinclude}' -> '{projref.Shortinclude}'.", true); } _projectReferences.Add(projref); _references.Remove(assref); _modified = true; } } // proj -> ass foreach (ProjectRef projref in _projectReferences.OrderBy(r => r.Shortinclude)) { bool exists = projects.Any(p => p._sln_shortfilename == projref.Shortinclude); if (!exists) { AssemblyRef assref = CreateReferenceFromProjectReference(solutionfile, projects, hintpaths, projref); if (projref.Shortinclude == assref.Shortinclude) { ConsoleHelper.WriteLine($" projref -> ref: '{projref.Shortinclude}'.", true); } else { ConsoleHelper.WriteLine($" projref -> ref: '{projref.Shortinclude}' -> '{assref.Shortinclude}'.", true); } _references.Add(assref); _projectReferences.Remove(projref); _modified = true; } } // Fix hint paths foreach (AssemblyRef assref in _references.OrderBy(r => r.Shortinclude)) { FixHintPath(solutionfile, hintpaths, assref); } if (outputpath != null) { /*// todo: Abs -> Rel? * bool diff = _outputpaths.Any(o => o != outputpath); * if (diff) * { * _outputpaths = new List<string>(); * _outputpaths.Add(outputpath); * _modified = true; * }*/ } if (removeversion) { foreach (AssemblyRef assref in _references.OrderBy(r => r.Shortinclude)) { string shortref = GetShortRef(assref.Include); if (shortref != assref.Include) { ConsoleHelper.WriteLine($" ref: removing version: '{assref.Include}' -> '{shortref}'.", true); assref.Include = shortref; assref.Shortinclude = shortref; _modified = true; } } } ConsoleHelper.WriteLineDeferred(null); return(true); }
private bool TryToRetrieveAssemblyInfoOfProjectReference(string solutionfile, List<Project> projects, ProjectRef projref, out string assemblyname, out string outputtype) { XDocument xdoc; XNamespace ns; string fullfilename = Path.Combine( Path.GetDirectoryName(solutionfile), Path.GetDirectoryName(_sln_path), projref.include); ConsoleHelper.WriteLine(" Loading external project: '" + fullfilename + "'.", true); try { // Delve greedily and deep into the external project file. xdoc = XDocument.Load(fullfilename); } catch (IOException ex) { ConsoleHelper.ColorWrite(ConsoleColor.Red, "Couldn't load project: '" + fullfilename + "': " + ex.Message); assemblyname = outputtype = null; return false; } catch (System.Xml.XmlException ex) { ConsoleHelper.ColorWrite(ConsoleColor.Red, "Couldn't load project: '" + fullfilename + "': " + ex.Message); assemblyname = outputtype = null; return false; } ns = xdoc.Root.Name.Namespace; try { assemblyname = xdoc.Element(ns + "Project").Elements(ns + "PropertyGroup").Elements(ns + "AssemblyName").Single().Value; } catch (System.NullReferenceException) { ConsoleHelper.ColorWrite(ConsoleColor.Red, "Couldn't load project: '" + fullfilename + "': Missing AssemblyName."); assemblyname = outputtype = null; return false; } try { outputtype = xdoc.Element(ns + "Project").Elements(ns + "PropertyGroup").Elements(ns + "OutputType").Single().Value; } catch (System.NullReferenceException) { ConsoleHelper.ColorWrite(ConsoleColor.Red, "Couldn't load project: '" + fullfilename + "': Missing OutputType."); assemblyname = outputtype = null; return false; } // Caution: External project (it's assembly name) may conflict with a loaded project name bool afterthis = false; foreach (Project proj in projects.OrderBy(p => p._sln_path)) { if (proj == this) { afterthis = true; continue; } if (!afterthis) { continue; } if (assemblyname == proj._proj_assemblyname) { ConsoleHelper.ColorWrite(ConsoleColor.Red, "Error: Projects have identical assembly names: '" + assemblyname + "': '" + fullfilename + "' and '" + proj._sln_path + "'."); throw new Exception("Error"); } } return true; }