DotNetSolution.cs 3.5 KB

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