DotNetSolution.cs 5.4 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 const string _solutionTemplate =
  12. @"Microsoft Visual Studio Solution File, Format Version 12.00
  13. # Visual Studio 2012
  14. {0}
  15. Global
  16. GlobalSection(SolutionConfigurationPlatforms) = preSolution
  17. {1}
  18. EndGlobalSection
  19. GlobalSection(ProjectConfigurationPlatforms) = postSolution
  20. {2}
  21. EndGlobalSection
  22. EndGlobal
  23. ";
  24. private const string _projectDeclaration =
  25. @"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{{{2}}}""
  26. EndProject";
  27. private const string _solutionPlatformsConfig =
  28. @" {0}|Any CPU = {0}|Any CPU";
  29. private const string _projectPlatformsConfig =
  30. @" {{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU
  31. {{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU";
  32. private string _directoryPath;
  33. private readonly Dictionary<string, ProjectInfo> _projects = new Dictionary<string, ProjectInfo>();
  34. public string Name { get; }
  35. public string DirectoryPath
  36. {
  37. get => _directoryPath;
  38. set => _directoryPath = value.IsAbsolutePath() ? value : Path.GetFullPath(value);
  39. }
  40. public class ProjectInfo
  41. {
  42. public string Guid;
  43. public string PathRelativeToSolution;
  44. public List<string> Configs = new List<string>();
  45. }
  46. public void AddNewProject(string name, ProjectInfo projectInfo)
  47. {
  48. _projects[name] = projectInfo;
  49. }
  50. public bool HasProject(string name)
  51. {
  52. return _projects.ContainsKey(name);
  53. }
  54. public ProjectInfo GetProjectInfo(string name)
  55. {
  56. return _projects[name];
  57. }
  58. public bool RemoveProject(string name)
  59. {
  60. return _projects.Remove(name);
  61. }
  62. public void Save()
  63. {
  64. if (!Directory.Exists(DirectoryPath))
  65. throw new FileNotFoundException("The solution directory does not exist.");
  66. string projectsDecl = string.Empty;
  67. string slnPlatformsCfg = string.Empty;
  68. string projPlatformsCfg = string.Empty;
  69. bool isFirstProject = true;
  70. foreach (var pair in _projects)
  71. {
  72. string name = pair.Key;
  73. ProjectInfo projectInfo = pair.Value;
  74. if (!isFirstProject)
  75. projectsDecl += "\n";
  76. projectsDecl += string.Format(_projectDeclaration,
  77. name, projectInfo.PathRelativeToSolution.Replace("/", "\\"), projectInfo.Guid);
  78. for (int i = 0; i < projectInfo.Configs.Count; i++)
  79. {
  80. string config = projectInfo.Configs[i];
  81. if (i != 0 || !isFirstProject)
  82. {
  83. slnPlatformsCfg += "\n";
  84. projPlatformsCfg += "\n";
  85. }
  86. slnPlatformsCfg += string.Format(_solutionPlatformsConfig, config);
  87. projPlatformsCfg += string.Format(_projectPlatformsConfig, projectInfo.Guid, config);
  88. }
  89. isFirstProject = false;
  90. }
  91. string solutionPath = Path.Combine(DirectoryPath, Name + ".sln");
  92. string content = string.Format(_solutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);
  93. File.WriteAllText(solutionPath, content, Encoding.UTF8); // UTF-8 with BOM
  94. }
  95. public DotNetSolution(string name)
  96. {
  97. Name = name;
  98. }
  99. public static void MigrateFromOldConfigNames(string slnPath)
  100. {
  101. if (!File.Exists(slnPath))
  102. return;
  103. string 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. string 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. }