DotNetSolution.cs 4.2 KB

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