DotNetSolution.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using GodotTools.Core;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace GodotTools.ProjectEditor
  8. {
  9. public class DotNetSolution
  10. {
  11. private string directoryPath;
  12. private readonly Dictionary<string, ProjectInfo> projects = new Dictionary<string, ProjectInfo>();
  13. public string Name { get; }
  14. public string DirectoryPath
  15. {
  16. get => directoryPath;
  17. set => directoryPath = value.IsAbsolutePath() ? value : Path.GetFullPath(value);
  18. }
  19. public class ProjectInfo
  20. {
  21. public string Guid;
  22. public string PathRelativeToSolution;
  23. public List<string> Configs = new List<string>();
  24. }
  25. public void AddNewProject(string name, ProjectInfo projectInfo)
  26. {
  27. projects[name] = projectInfo;
  28. }
  29. public bool HasProject(string name)
  30. {
  31. return projects.ContainsKey(name);
  32. }
  33. public ProjectInfo GetProjectInfo(string name)
  34. {
  35. return projects[name];
  36. }
  37. public bool RemoveProject(string name)
  38. {
  39. return projects.Remove(name);
  40. }
  41. public void Save()
  42. {
  43. if (!Directory.Exists(DirectoryPath))
  44. throw new FileNotFoundException("The solution directory does not exist.");
  45. string projectsDecl = string.Empty;
  46. string slnPlatformsCfg = string.Empty;
  47. string projPlatformsCfg = string.Empty;
  48. bool isFirstProject = true;
  49. foreach (var pair in projects)
  50. {
  51. string name = pair.Key;
  52. ProjectInfo projectInfo = pair.Value;
  53. if (!isFirstProject)
  54. projectsDecl += "\n";
  55. projectsDecl += string.Format(ProjectDeclaration,
  56. name, projectInfo.PathRelativeToSolution.Replace("/", "\\"), projectInfo.Guid);
  57. for (int i = 0; i < projectInfo.Configs.Count; i++)
  58. {
  59. string config = projectInfo.Configs[i];
  60. if (i != 0 || !isFirstProject)
  61. {
  62. slnPlatformsCfg += "\n";
  63. projPlatformsCfg += "\n";
  64. }
  65. slnPlatformsCfg += string.Format(SolutionPlatformsConfig, config);
  66. projPlatformsCfg += string.Format(ProjectPlatformsConfig, projectInfo.Guid, config);
  67. }
  68. isFirstProject = false;
  69. }
  70. string solutionPath = Path.Combine(DirectoryPath, Name + ".sln");
  71. string content = string.Format(SolutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);
  72. File.WriteAllText(solutionPath, content, Encoding.UTF8); // UTF-8 with BOM
  73. }
  74. public DotNetSolution(string name)
  75. {
  76. Name = name;
  77. }
  78. const string SolutionTemplate =
  79. @"Microsoft Visual Studio Solution File, Format Version 12.00
  80. # Visual Studio 2012
  81. {0}
  82. Global
  83. GlobalSection(SolutionConfigurationPlatforms) = preSolution
  84. {1}
  85. EndGlobalSection
  86. GlobalSection(ProjectConfigurationPlatforms) = postSolution
  87. {2}
  88. EndGlobalSection
  89. EndGlobal
  90. ";
  91. const string ProjectDeclaration =
  92. @"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{{{2}}}""
  93. EndProject";
  94. const string SolutionPlatformsConfig =
  95. @" {0}|Any CPU = {0}|Any CPU";
  96. const string ProjectPlatformsConfig =
  97. @" {{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU
  98. {{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU";
  99. public static void MigrateFromOldConfigNames(string slnPath)
  100. {
  101. if (!File.Exists(slnPath))
  102. return;
  103. var input = File.ReadAllText(slnPath);
  104. if (!Regex.IsMatch(input, Regex.Escape("Tools|Any CPU")))
  105. return;
  106. // This method renames old configurations in solutions to the new ones.
  107. //
  108. // This is the order configs appear in the solution and what we want to rename them to:
  109. // Debug|Any CPU = Debug|Any CPU -> ExportDebug|Any CPU = ExportDebug|Any CPU
  110. // Tools|Any CPU = Tools|Any CPU -> Debug|Any CPU = Debug|Any CPU
  111. //
  112. // But we want to move Tools (now Debug) to the top, so it's easier to rename like this:
  113. // Debug|Any CPU = Debug|Any CPU -> Debug|Any CPU = Debug|Any CPU
  114. // Release|Any CPU = Release|Any CPU -> ExportDebug|Any CPU = ExportDebug|Any CPU
  115. // Tools|Any CPU = Tools|Any CPU -> ExportRelease|Any CPU = ExportRelease|Any CPU
  116. var dict = new Dictionary<string, string>
  117. {
  118. {"Debug|Any CPU", "Debug|Any CPU"},
  119. {"Release|Any CPU", "ExportDebug|Any CPU"},
  120. {"Tools|Any CPU", "ExportRelease|Any CPU"}
  121. };
  122. var regex = new Regex(string.Join("|",dict.Keys.Select(Regex.Escape)));
  123. var result = regex.Replace(input,m => dict[m.Value]);
  124. if (result != input)
  125. {
  126. // Save a copy of the solution before replacing it
  127. FileUtils.SaveBackupCopy(slnPath);
  128. File.WriteAllText(slnPath, result);
  129. }
  130. }
  131. }
  132. }