private string GetProjectWithoutDependancies(List <string> ARemainingProjects, Dictionary <string, TDetailsOfDll> AProjects) { string SecondChoice = null; foreach (string project in ARemainingProjects) { TDetailsOfDll details = AProjects[project]; bool unmetDependancy = false; foreach (string dependency in details.ReferencedDlls) { if (ARemainingProjects.Contains(dependency)) { unmetDependancy = true; break; } } if (!unmetDependancy) { if (details.OutputType.ToLower() != "library") { // put executables last. as long as there is a library, put the library first SecondChoice = project; } else { return(project); } } } return(SecondChoice); }
/// <summary> /// now reduce the namespace names to dll names /// </summary> private Dictionary <string, TDetailsOfDll> UsingNamespaceMapToDll( Dictionary <string, string> NamespaceMap, Dictionary <string, string> Namespace3rdPartyMap, Dictionary <string, TDetailsOfDll> UsingNamespaces, bool AShowWarnings) { Dictionary <string, TDetailsOfDll> result = new Dictionary <string, TDetailsOfDll>(); foreach (string key in UsingNamespaces.Keys) { if (IgnoreNamespace(key)) { continue; } TDetailsOfDll DetailsOfDll = new TDetailsOfDll(); DetailsOfDll.OutputType = UsingNamespaces[key].OutputType; DetailsOfDll.OutputName = UsingNamespaces[key].OutputName; result.Add(key, DetailsOfDll); foreach (string usingNamespace in UsingNamespaces[key].UsedNamespaces) { if (!NamespaceMap.ContainsKey(usingNamespace)) { // check the 3rd party namespace map to find the correct DLL bool found = false; foreach (string namespace3rdParty in Namespace3rdPartyMap.Keys) { if ((namespace3rdParty == usingNamespace) || (namespace3rdParty.EndsWith("*") && usingNamespace.StartsWith(namespace3rdParty.Replace("*", "")))) { NamespaceMap.Add(usingNamespace, Namespace3rdPartyMap[namespace3rdParty]); found = true; break; } } if (!found && AShowWarnings) { Console.WriteLine("Warning: we do not know in which dll the namespace " + usingNamespace + " is defined"); } } if (NamespaceMap.ContainsKey(usingNamespace)) { string dllname = NamespaceMap[usingNamespace]; if ((dllname != key) && !DetailsOfDll.ReferencedDlls.Contains(dllname) && (DetailsOfDll.OutputName != dllname)) { DetailsOfDll.ReferencedDlls.Add(dllname); } } } } return(result); }
private void ReadMap(string filename, out Dictionary <string, TDetailsOfDll> map, out Dictionary <string, string> mapOutputNameToPath) { if (!File.Exists(filename)) { throw new Exception("Cannot find file " + filename + ". Please first run nant generateNamespaceMap!"); } StreamReader sr = new StreamReader(filename); map = new Dictionary <string, TDetailsOfDll>(); mapOutputNameToPath = new Dictionary <string, string>(); TDetailsOfDll currentDll = null; while (!sr.EndOfStream) { string line = sr.ReadLine(); char firstChar = line[0]; if (firstChar == ' ') { currentDll.ReferencedDlls.Add(line.Substring(2)); } else if (firstChar != '#') { currentDll = new TDetailsOfDll(); string[] LineDetails = line.Split(new char[] { ',' }); currentDll.OutputType = LineDetails[1]; if ((LineDetails.Length > 2) && (LineDetails[2].Length > 0)) { currentDll.OutputName = LineDetails[2]; mapOutputNameToPath.Add(currentDll.OutputName, LineDetails[0]); } else { mapOutputNameToPath.Add(LineDetails[0], LineDetails[0]); } map.Add(LineDetails[0], currentDll); } } sr.Close(); }
/// <summary> /// do a topological sorts of the projects by their dependencies. /// </summary> private List <string> SortProjectsByDependencies(Dictionary <string, TDetailsOfDll> AProjects) { List <string> Result = new List <string>(); List <string> ProjectNames = new List <string>(AProjects.Keys); while (ProjectNames.Count > 0) { string next = GetProjectWithoutDependancies(ProjectNames, AProjects); if (next == null) { List <string> ProjectsNotPartOfCyclicDep = new List <string>(); int CountProjectsChanged = 0; do { CountProjectsChanged = ProjectsNotPartOfCyclicDep.Count; foreach (string file in ProjectNames) { if (ProjectsNotPartOfCyclicDep.Contains(file)) { continue; } // if this file is not in the list of another file, ignore it bool isReferenced = false; foreach (string file2 in ProjectNames) { if (ProjectsNotPartOfCyclicDep.Contains(file2)) { continue; } TDetailsOfDll details2 = AProjects[file2]; foreach (string dependency in details2.ReferencedDlls) { if (dependency == file) { isReferenced = true; } } } if (!isReferenced) { ProjectsNotPartOfCyclicDep.Add(file); } } } while (ProjectsNotPartOfCyclicDep.Count != CountProjectsChanged); string problemFiles = string.Empty; foreach (string file in ProjectNames) { if (ProjectsNotPartOfCyclicDep.Contains(file)) { continue; } Console.WriteLine(file); TDetailsOfDll details = AProjects[file]; foreach (string dependency in details.ReferencedDlls) { if (ProjectNames.Contains(dependency)) { Console.WriteLine(" => " + dependency); } } if (problemFiles.Length > 0) { problemFiles += " and "; } problemFiles += file; } throw new Exception("There is a cyclic dependancy for projects " + problemFiles); } Result.Add(next); ProjectNames.Remove(next); } return(Result); }
private string ParseCSFile(Dictionary <string, string> NamespaceMap, Dictionary <string, TDetailsOfDll> UsingNamespaces, string filename) { StreamReader sr = new StreamReader(filename); string DllName = GetProjectNameFromCSFile(filename, FCodeRootDir); TDetailsOfDll DetailsOfDll; if (UsingNamespaces.ContainsKey(DllName)) { DetailsOfDll = UsingNamespaces[DllName]; } else { DetailsOfDll = new TDetailsOfDll(); DetailsOfDll.UsedNamespaces.Add("System"); DetailsOfDll.UsedNamespaces.Add("System.Xml"); UsingNamespaces.Add(DllName, DetailsOfDll); } try { string LastNamespace = string.Empty; bool ReferencesWinForms = false; while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Trim().StartsWith("namespace ")) { string Namespace = line.Substring("namespace".Length).Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); if (Namespace != string.Empty) { LastNamespace = Namespace; if (!NamespaceMap.ContainsKey(Namespace)) { NamespaceMap.Add(Namespace, DllName); } else { if (NamespaceMap[Namespace] != DllName) { throw new Exception( string.Format( "Error: GenerateNamespaceMap: a namespace cannot exist in two different directories! {0} exists in {1} and {2}", Namespace, NamespaceMap[Namespace], DllName)); } } } } else if (line.Trim().StartsWith("using ") || line.Trim().StartsWith("// generateNamespaceMap-Link-Extra-DLL ")) { string Namespace = line.Substring("using".Length); int indexComment = Namespace.IndexOf("//"); if (line.Trim().StartsWith("// generateNamespaceMap-Link-Extra-DLL ")) { Namespace = line.Substring("// generateNamespaceMap-Link-Extra-DLL ".Length); indexComment = Namespace.IndexOf("//"); } if (indexComment != -1) { // eg. // Implicit reference Namespace = Namespace.Substring(0, indexComment); } Namespace = Namespace.Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); if ((Namespace != string.Empty) && !Namespace.Contains(" ")) { if (Namespace == "System.Windows.Forms") { ReferencesWinForms = true; } if (!FCompilingForStandalone && Namespace.StartsWith("Ict.Petra.Server") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Shared")) { Console.WriteLine( "Warning: we must not reference a Server namespace (" + Namespace + ") from the shared directory in " + filename); } if (!DetailsOfDll.UsedNamespaces.Contains(Namespace)) { if (Namespace.StartsWith("Microsoft.Win32") && Path.GetDirectoryName(filename).EndsWith("RuntimeHost")) { // we can ignore special Microsoft features on this project because it is for Windows developers only Console.WriteLine( "Info: ignoring Microsoft.Win32 reference when building namespace map in " + filename); } else { DetailsOfDll.UsedNamespaces.Add(Namespace); } } } } else if (line.Contains("static void Main(") || line.Contains("static int Main(")) { if (ReferencesWinForms) { DetailsOfDll.OutputType = "winexe"; } else { DetailsOfDll.OutputType = "exe"; } DetailsOfDll.OutputName = LastNamespace; } } } finally { sr.Close(); } return(string.Empty); }
/// <summary> /// now reduce the namespace names to dll names /// </summary> private Dictionary <string, TDetailsOfDll>UsingNamespaceMapToDll( Dictionary <string, string>NamespaceMap, Dictionary <string, string>Namespace3rdPartyMap, Dictionary <string, TDetailsOfDll>UsingNamespaces, bool AShowWarnings) { Dictionary <string, TDetailsOfDll>result = new Dictionary <string, TDetailsOfDll>(); foreach (string key in UsingNamespaces.Keys) { if (IgnoreNamespace(key)) { continue; } TDetailsOfDll DetailsOfDll = new TDetailsOfDll(); DetailsOfDll.OutputType = UsingNamespaces[key].OutputType; DetailsOfDll.OutputName = UsingNamespaces[key].OutputName; result.Add(key, DetailsOfDll); foreach (string usingNamespace in UsingNamespaces[key].UsedNamespaces) { if (!NamespaceMap.ContainsKey(usingNamespace)) { // check the 3rd party namespace map to find the correct DLL bool found = false; foreach (string namespace3rdParty in Namespace3rdPartyMap.Keys) { if ((namespace3rdParty == usingNamespace) || (namespace3rdParty.EndsWith("*") && usingNamespace.StartsWith(namespace3rdParty.Replace("*", "")))) { NamespaceMap.Add(usingNamespace, Namespace3rdPartyMap[namespace3rdParty]); found = true; break; } } if (!found && AShowWarnings) { Console.WriteLine("Warning: we do not know in which dll the namespace " + usingNamespace + " is defined"); } } if (NamespaceMap.ContainsKey(usingNamespace)) { string dllname = NamespaceMap[usingNamespace]; if ((dllname != key) && !DetailsOfDll.ReferencedDlls.Contains(dllname) && (DetailsOfDll.OutputName != dllname)) { DetailsOfDll.ReferencedDlls.Add(dllname); } } } } return result; }
private string ParseCSFile(Dictionary <string, string>NamespaceMap, Dictionary <string, TDetailsOfDll>UsingNamespaces, string filename) { StreamReader sr = new StreamReader(filename); string DllName = GetProjectNameFromCSFile(filename, FCodeRootDir); TDetailsOfDll DetailsOfDll; if (UsingNamespaces.ContainsKey(DllName)) { DetailsOfDll = UsingNamespaces[DllName]; } else { DetailsOfDll = new TDetailsOfDll(); DetailsOfDll.UsedNamespaces.Add("System"); DetailsOfDll.UsedNamespaces.Add("System.Xml"); UsingNamespaces.Add(DllName, DetailsOfDll); } try { string LastNamespace = string.Empty; bool ReferencesWinForms = false; while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Trim().StartsWith("namespace ")) { string Namespace = line.Substring("namespace".Length).Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); if (Namespace != string.Empty) { LastNamespace = Namespace; if (!NamespaceMap.ContainsKey(Namespace)) { NamespaceMap.Add(Namespace, DllName); } else { if (NamespaceMap[Namespace] != DllName) { throw new Exception( string.Format( "Error: GenerateNamespaceMap: a namespace cannot exist in two different directories! {0} exists in {1} and {2}", Namespace, NamespaceMap[Namespace], DllName)); } } } } else if (line.Trim().StartsWith("using ")) { string Namespace = line.Substring("using".Length); int indexComment = Namespace.IndexOf("//"); if (indexComment != -1) { // eg. // Implicit reference Namespace = Namespace.Substring(0, indexComment); } Namespace = Namespace.Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); if ((Namespace != string.Empty) && !Namespace.Contains(" ")) { if (Namespace == "System.Windows.Forms") { ReferencesWinForms = true; } if (Namespace.StartsWith("System.Web") && !Path.GetDirectoryName(filename).EndsWith("WebService")) { Console.WriteLine( "Warning: we should not reference System.Web since that is not part of the client profile of .net 4.0! in " + filename); } if (Namespace.StartsWith("System.Runtime.Caching") && !Path.GetDirectoryName(filename).EndsWith("WebService")) { Console.WriteLine( "Warning: we should not reference System.Runtime.Caching since that is not part of the client profile of .net 4.0! in " + filename); } if (Namespace.StartsWith("Ict.Petra.Server") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Client")) { Console.WriteLine( "Warning: we must not reference a Server namespace (" + Namespace + ") from the client side in " + filename); } if (Namespace.StartsWith("Ict.Petra.Server") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Shared")) { Console.WriteLine( "Warning: we must not reference a Server namespace (" + Namespace + ") from the shared directory in " + filename); } if (Namespace.StartsWith("Ict.Petra.Client") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Shared")) { Console.WriteLine( "Warning: we must not reference a Client namespace (" + Namespace + ") from the shared directory in " + filename); } if (Namespace.StartsWith("Ict.Petra.Client") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Server")) { Console.WriteLine( "Warning: we must not reference a Client namespace (" + Namespace + ") from the server directory in " + filename); } if (!DetailsOfDll.UsedNamespaces.Contains(Namespace)) { DetailsOfDll.UsedNamespaces.Add(Namespace); } } } else if (line.Trim().StartsWith("// generateNamespaceMap-Link-Extra-DLL ")) { string Namespace = line.Substring("// generateNamespaceMap-Link-Extra-DLL ".Length); int indexComment = Namespace.IndexOf("//"); if (indexComment != -1) { // eg. // Implicit reference Namespace = Namespace.Substring(0, indexComment); } Namespace = Namespace.Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); //Console.WriteLine("Encountered 'generateNamespaceMap-Link-Extra-DLL ': Namespace = " +Namespace); if ((Namespace != string.Empty) && !Namespace.Contains(" ")) { if (!NamespaceMap.ContainsKey(Namespace)) { NamespaceMap.Add(Namespace, Namespace + ".dll"); } if (!DetailsOfDll.UsedNamespaces.Contains(Namespace)) { //Console.WriteLine("Added 'Extra DLL': " + Namespace); DetailsOfDll.UsedNamespaces.Add(Namespace); } } } else if (line.Contains("static void Main(") || line.Contains("static int Main(")) { if (ReferencesWinForms) { DetailsOfDll.OutputType = "winexe"; } else { DetailsOfDll.OutputType = "exe"; } DetailsOfDll.OutputName = LastNamespace; } } } finally { sr.Close(); } return string.Empty; }
private string ParseCSFile(Dictionary <string, string> NamespaceMap, Dictionary <string, TDetailsOfDll> UsingNamespaces, string filename) { StreamReader sr = new StreamReader(filename); string DllName = GetProjectNameFromCSFile(filename, FCodeRootDir); TDetailsOfDll DetailsOfDll; if (UsingNamespaces.ContainsKey(DllName)) { DetailsOfDll = UsingNamespaces[DllName]; } else { DetailsOfDll = new TDetailsOfDll(); DetailsOfDll.UsedNamespaces.Add("System"); DetailsOfDll.UsedNamespaces.Add("System.Xml"); UsingNamespaces.Add(DllName, DetailsOfDll); } try { string LastNamespace = string.Empty; bool ReferencesWinForms = false; while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Trim().StartsWith("namespace ")) { string Namespace = line.Substring("namespace".Length).Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); if (Namespace != string.Empty) { LastNamespace = Namespace; if (!NamespaceMap.ContainsKey(Namespace)) { NamespaceMap.Add(Namespace, DllName); } else { if (NamespaceMap[Namespace] != DllName) { throw new Exception( string.Format( "Error: GenerateNamespaceMap: a namespace cannot exist in two different directories! {0} exists in {1} and {2}", Namespace, NamespaceMap[Namespace], DllName)); } } } } else if (line.Trim().StartsWith("using ")) { string Namespace = line.Substring("using".Length); int indexComment = Namespace.IndexOf("//"); if (indexComment != -1) { // eg. // Implicit reference Namespace = Namespace.Substring(0, indexComment); } Namespace = Namespace.Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); if ((Namespace != string.Empty) && !Namespace.Contains(" ")) { if (Namespace == "System.Windows.Forms") { ReferencesWinForms = true; } if (Namespace.StartsWith("System.Web") && !Path.GetDirectoryName(filename).EndsWith("WebService")) { Console.WriteLine( "Warning: we should not reference System.Web since that is not part of the client profile of .net 4.0! in " + filename); } if (Namespace.StartsWith("System.Runtime.Caching") && !Path.GetDirectoryName(filename).EndsWith("WebService")) { Console.WriteLine( "Warning: we should not reference System.Runtime.Caching since that is not part of the client profile of .net 4.0! in " + filename); } if (Namespace.StartsWith("Ict.Petra.Server") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Client")) { Console.WriteLine( "Warning: we must not reference a Server namespace (" + Namespace + ") from the client side in " + filename); } if (Namespace.StartsWith("Ict.Petra.Server") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Shared")) { Console.WriteLine( "Warning: we must not reference a Server namespace (" + Namespace + ") from the shared directory in " + filename); } if (Namespace.StartsWith("Ict.Petra.Client") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Shared")) { Console.WriteLine( "Warning: we must not reference a Client namespace (" + Namespace + ") from the shared directory in " + filename); } if (Namespace.StartsWith("Ict.Petra.Client") && Path.GetDirectoryName(filename).Replace("\\", "/").Contains("ICT/Petra/Server")) { Console.WriteLine( "Warning: we must not reference a Client namespace (" + Namespace + ") from the server directory in " + filename); } if (!DetailsOfDll.UsedNamespaces.Contains(Namespace)) { DetailsOfDll.UsedNamespaces.Add(Namespace); } } } else if (line.Trim().StartsWith("// generateNamespaceMap-Link-Extra-DLL ")) { string Namespace = line.Substring("// generateNamespaceMap-Link-Extra-DLL ".Length); int indexComment = Namespace.IndexOf("//"); if (indexComment != -1) { // eg. // Implicit reference Namespace = Namespace.Substring(0, indexComment); } Namespace = Namespace.Trim(new char[] { ' ', '\t', '\n', '\r', ';' }); //Console.WriteLine("Encountered 'generateNamespaceMap-Link-Extra-DLL ': Namespace = " +Namespace); if ((Namespace != string.Empty) && !Namespace.Contains(" ")) { if (!NamespaceMap.ContainsKey(Namespace)) { NamespaceMap.Add(Namespace, Namespace + ".dll"); } if (!DetailsOfDll.UsedNamespaces.Contains(Namespace)) { //Console.WriteLine("Added 'Extra DLL': " + Namespace); DetailsOfDll.UsedNamespaces.Add(Namespace); } } } else if (line.Contains("static void Main(") || line.Contains("static int Main(")) { if (ReferencesWinForms) { DetailsOfDll.OutputType = "winexe"; } else { DetailsOfDll.OutputType = "exe"; } DetailsOfDll.OutputName = LastNamespace; } } } finally { sr.Close(); } return(string.Empty); }
private void ReadMap(string filename, out Dictionary <string, TDetailsOfDll>map, out Dictionary <string, string>mapOutputNameToPath) { if (!File.Exists(filename)) { throw new Exception("Cannot find file " + filename + ". Please first run nant generateNamespaceMap!"); } StreamReader sr = new StreamReader(filename); map = new Dictionary <string, TDetailsOfDll>(); mapOutputNameToPath = new Dictionary <string, string>(); TDetailsOfDll currentDll = null; while (!sr.EndOfStream) { string line = sr.ReadLine(); char firstChar = line[0]; if (firstChar == ' ') { currentDll.ReferencedDlls.Add(line.Substring(2)); } else if (firstChar != '#') { currentDll = new TDetailsOfDll(); string[] LineDetails = line.Split(new char[] { ',' }); currentDll.OutputType = LineDetails[1]; if ((LineDetails.Length > 2) && (LineDetails[2].Length > 0)) { currentDll.OutputName = LineDetails[2]; mapOutputNameToPath.Add(currentDll.OutputName, LineDetails[0]); } else { mapOutputNameToPath.Add(LineDetails[0], LineDetails[0]); } map.Add(LineDetails[0], currentDll); } } sr.Close(); }