2
0

DotNetSolution.cs 5.8 KB

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