Debian Codegraph @

modules/mono/editor/GodotTools/GodotTools.ProjectEditor/DotNetSolution.cs

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