DotNetSolution.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 readonly Dictionary<string, ProjectInfo> _projects = new Dictionary<string, ProjectInfo>();
  33. public string Name { get; }
  34. public string DirectoryPath { get; }
  35. public class ProjectInfo
  36. {
  37. public string Guid { get; }
  38. public string PathRelativeToSolution { get; }
  39. public List<string> Configs { get; }
  40. public ProjectInfo(string guid, string pathRelativeToSolution, List<string> configs)
  41. {
  42. Guid = guid;
  43. PathRelativeToSolution = pathRelativeToSolution;
  44. Configs = configs;
  45. }
  46. }
  47. public void AddNewProject(string name, ProjectInfo projectInfo)
  48. {
  49. _projects[name] = projectInfo;
  50. }
  51. public bool HasProject(string name)
  52. {
  53. return _projects.ContainsKey(name);
  54. }
  55. public ProjectInfo GetProjectInfo(string name)
  56. {
  57. return _projects[name];
  58. }
  59. public bool RemoveProject(string name)
  60. {
  61. return _projects.Remove(name);
  62. }
  63. public void Save()
  64. {
  65. if (!Directory.Exists(DirectoryPath))
  66. throw new FileNotFoundException("The solution directory does not exist.");
  67. string projectsDecl = string.Empty;
  68. string slnPlatformsCfg = string.Empty;
  69. string projPlatformsCfg = string.Empty;
  70. bool isFirstProject = true;
  71. foreach (var pair in _projects)
  72. {
  73. string name = pair.Key;
  74. ProjectInfo projectInfo = pair.Value;
  75. if (!isFirstProject)
  76. projectsDecl += "\n";
  77. projectsDecl += string.Format(_projectDeclaration,
  78. name, projectInfo.PathRelativeToSolution.Replace("/", "\\"), projectInfo.Guid);
  79. for (int i = 0; i < projectInfo.Configs.Count; i++)
  80. {
  81. string config = projectInfo.Configs[i];
  82. if (i != 0 || !isFirstProject)
  83. {
  84. slnPlatformsCfg += "\n";
  85. projPlatformsCfg += "\n";
  86. }
  87. slnPlatformsCfg += string.Format(_solutionPlatformsConfig, config);
  88. projPlatformsCfg += string.Format(_projectPlatformsConfig, projectInfo.Guid, config);
  89. }
  90. isFirstProject = false;
  91. }
  92. string solutionPath = Path.Combine(DirectoryPath, Name + ".sln");
  93. string content = string.Format(_solutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);
  94. File.WriteAllText(solutionPath, content, Encoding.UTF8); // UTF-8 with BOM
  95. }
  96. public DotNetSolution(string name, string directoryPath)
  97. {
  98. Name = name;
  99. DirectoryPath = directoryPath.IsAbsolutePath() ? directoryPath : Path.GetFullPath(directoryPath);
  100. }
  101. public static void MigrateFromOldConfigNames(string slnPath)
  102. {
  103. if (!File.Exists(slnPath))
  104. return;
  105. string input = File.ReadAllText(slnPath);
  106. if (!Regex.IsMatch(input, Regex.Escape("Tools|Any CPU")))
  107. return;
  108. // This method renames old configurations in solutions to the new ones.
  109. //
  110. // This is the order configs appear in the solution and what we want to rename them to:
  111. // Debug|Any CPU = Debug|Any CPU -> ExportDebug|Any CPU = ExportDebug|Any CPU
  112. // Tools|Any CPU = Tools|Any CPU -> Debug|Any CPU = Debug|Any CPU
  113. //
  114. // But we want to move Tools (now Debug) to the top, so it's easier to rename like this:
  115. // Debug|Any CPU = Debug|Any CPU -> Debug|Any CPU = Debug|Any CPU
  116. // Release|Any CPU = Release|Any CPU -> ExportDebug|Any CPU = ExportDebug|Any CPU
  117. // Tools|Any CPU = Tools|Any CPU -> ExportRelease|Any CPU = ExportRelease|Any CPU
  118. var dict = new Dictionary<string, string>
  119. {
  120. {"Debug|Any CPU", "Debug|Any CPU"},
  121. {"Release|Any CPU", "ExportDebug|Any CPU"},
  122. {"Tools|Any CPU", "ExportRelease|Any CPU"}
  123. };
  124. var regex = new Regex(string.Join("|", dict.Keys.Select(Regex.Escape)));
  125. string result = regex.Replace(input, m => dict[m.Value]);
  126. if (result != input)
  127. {
  128. // Save a copy of the solution before replacing it
  129. FileUtils.SaveBackupCopy(slnPath);
  130. File.WriteAllText(slnPath, result);
  131. }
  132. }
  133. }
  134. }