2019-07-03 07:44:53 +00:00
|
|
|
using GodotTools.Core;
|
2024-01-12 21:47:50 +00:00
|
|
|
using System;
|
2019-07-03 07:44:53 +00:00
|
|
|
using System.Collections.Generic;
|
2024-01-12 21:47:50 +00:00
|
|
|
using System.Globalization;
|
2019-07-03 07:44:53 +00:00
|
|
|
using System.IO;
|
2020-03-11 16:22:12 +00:00
|
|
|
using System.Linq;
|
2020-09-08 18:06:04 +00:00
|
|
|
using System.Text;
|
2020-03-11 16:22:12 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
namespace GodotTools.ProjectEditor
|
|
|
|
{
|
|
|
|
public class DotNetSolution
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
private const string _solutionTemplate =
|
|
|
|
@"Microsoft Visual Studio Solution File, Format Version 12.00
|
|
|
|
# Visual Studio 2012
|
|
|
|
{0}
|
|
|
|
Global
|
|
|
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
|
|
|
{1}
|
|
|
|
EndGlobalSection
|
|
|
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
|
|
|
{2}
|
|
|
|
EndGlobalSection
|
|
|
|
EndGlobal
|
|
|
|
";
|
|
|
|
|
|
|
|
private const string _projectDeclaration =
|
|
|
|
@"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{{{2}}}""
|
|
|
|
EndProject";
|
|
|
|
|
|
|
|
private const string _solutionPlatformsConfig =
|
|
|
|
@" {0}|Any CPU = {0}|Any CPU";
|
|
|
|
|
|
|
|
private const string _projectPlatformsConfig =
|
|
|
|
@" {{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU
|
|
|
|
{{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU";
|
|
|
|
|
|
|
|
private readonly Dictionary<string, ProjectInfo> _projects = new Dictionary<string, ProjectInfo>();
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
public string Name { get; }
|
2024-01-13 04:24:12 +00:00
|
|
|
public string DirectoryPath { get; }
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
public class ProjectInfo
|
|
|
|
{
|
2024-01-13 04:24:12 +00:00
|
|
|
public string Guid { get; }
|
|
|
|
public string PathRelativeToSolution { get; }
|
|
|
|
public List<string> Configs { get; }
|
|
|
|
|
|
|
|
public ProjectInfo(string guid, string pathRelativeToSolution, List<string> configs)
|
|
|
|
{
|
|
|
|
Guid = guid;
|
|
|
|
PathRelativeToSolution = pathRelativeToSolution;
|
|
|
|
Configs = configs;
|
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddNewProject(string name, ProjectInfo projectInfo)
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
_projects[name] = projectInfo;
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasProject(string name)
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
return _projects.ContainsKey(name);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ProjectInfo GetProjectInfo(string name)
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
return _projects[name];
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool RemoveProject(string name)
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
return _projects.Remove(name);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
{
|
|
|
|
if (!Directory.Exists(DirectoryPath))
|
|
|
|
throw new FileNotFoundException("The solution directory does not exist.");
|
|
|
|
|
|
|
|
string projectsDecl = string.Empty;
|
|
|
|
string slnPlatformsCfg = string.Empty;
|
|
|
|
string projPlatformsCfg = string.Empty;
|
|
|
|
|
|
|
|
bool isFirstProject = true;
|
|
|
|
|
2021-07-25 04:16:59 +00:00
|
|
|
foreach (var pair in _projects)
|
2019-07-03 07:44:53 +00:00
|
|
|
{
|
|
|
|
string name = pair.Key;
|
|
|
|
ProjectInfo projectInfo = pair.Value;
|
|
|
|
|
|
|
|
if (!isFirstProject)
|
|
|
|
projectsDecl += "\n";
|
|
|
|
|
2024-01-12 21:47:50 +00:00
|
|
|
projectsDecl += string.Format(CultureInfo.InvariantCulture, _projectDeclaration,
|
|
|
|
name, projectInfo.PathRelativeToSolution.Replace("/", "\\", StringComparison.Ordinal), projectInfo.Guid);
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < projectInfo.Configs.Count; i++)
|
|
|
|
{
|
|
|
|
string config = projectInfo.Configs[i];
|
|
|
|
|
|
|
|
if (i != 0 || !isFirstProject)
|
|
|
|
{
|
|
|
|
slnPlatformsCfg += "\n";
|
|
|
|
projPlatformsCfg += "\n";
|
|
|
|
}
|
|
|
|
|
2024-01-12 21:47:50 +00:00
|
|
|
slnPlatformsCfg += string.Format(CultureInfo.InvariantCulture, _solutionPlatformsConfig, config);
|
|
|
|
projPlatformsCfg += string.Format(CultureInfo.InvariantCulture, _projectPlatformsConfig, projectInfo.Guid, config);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isFirstProject = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string solutionPath = Path.Combine(DirectoryPath, Name + ".sln");
|
2024-01-12 21:47:50 +00:00
|
|
|
string content = string.Format(CultureInfo.InvariantCulture, _solutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2020-09-08 18:06:04 +00:00
|
|
|
File.WriteAllText(solutionPath, content, Encoding.UTF8); // UTF-8 with BOM
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 04:24:12 +00:00
|
|
|
public DotNetSolution(string name, string directoryPath)
|
2019-07-03 07:44:53 +00:00
|
|
|
{
|
|
|
|
Name = name;
|
2024-01-13 04:24:12 +00:00
|
|
|
DirectoryPath = directoryPath.IsAbsolutePath() ? directoryPath : Path.GetFullPath(directoryPath);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 18:01:29 +00:00
|
|
|
public static void MigrateFromOldConfigNames(string slnPath)
|
2020-03-11 16:22:12 +00:00
|
|
|
{
|
|
|
|
if (!File.Exists(slnPath))
|
|
|
|
return;
|
2020-03-14 18:01:29 +00:00
|
|
|
|
2021-07-25 04:16:59 +00:00
|
|
|
string input = File.ReadAllText(slnPath);
|
2020-03-14 18:01:29 +00:00
|
|
|
|
|
|
|
if (!Regex.IsMatch(input, Regex.Escape("Tools|Any CPU")))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// This method renames old configurations in solutions to the new ones.
|
|
|
|
//
|
|
|
|
// This is the order configs appear in the solution and what we want to rename them to:
|
|
|
|
// Debug|Any CPU = Debug|Any CPU -> ExportDebug|Any CPU = ExportDebug|Any CPU
|
|
|
|
// Tools|Any CPU = Tools|Any CPU -> Debug|Any CPU = Debug|Any CPU
|
|
|
|
//
|
|
|
|
// But we want to move Tools (now Debug) to the top, so it's easier to rename like this:
|
|
|
|
// Debug|Any CPU = Debug|Any CPU -> Debug|Any CPU = Debug|Any CPU
|
|
|
|
// Release|Any CPU = Release|Any CPU -> ExportDebug|Any CPU = ExportDebug|Any CPU
|
|
|
|
// Tools|Any CPU = Tools|Any CPU -> ExportRelease|Any CPU = ExportRelease|Any CPU
|
|
|
|
|
2020-03-11 16:22:12 +00:00
|
|
|
var dict = new Dictionary<string, string>
|
|
|
|
{
|
2020-03-14 18:01:29 +00:00
|
|
|
{"Debug|Any CPU", "Debug|Any CPU"},
|
2020-03-11 16:22:12 +00:00
|
|
|
{"Release|Any CPU", "ExportDebug|Any CPU"},
|
|
|
|
{"Tools|Any CPU", "ExportRelease|Any CPU"}
|
|
|
|
};
|
|
|
|
|
2021-07-22 23:56:37 +00:00
|
|
|
var regex = new Regex(string.Join("|", dict.Keys.Select(Regex.Escape)));
|
2021-07-25 04:16:59 +00:00
|
|
|
string result = regex.Replace(input, m => dict[m.Value]);
|
2020-03-11 16:22:12 +00:00
|
|
|
|
|
|
|
if (result != input)
|
2020-04-22 12:39:42 +00:00
|
|
|
{
|
|
|
|
// Save a copy of the solution before replacing it
|
|
|
|
FileUtils.SaveBackupCopy(slnPath);
|
|
|
|
|
2020-03-11 16:22:12 +00:00
|
|
|
File.WriteAllText(slnPath, result);
|
2020-04-22 12:39:42 +00:00
|
|
|
}
|
2020-03-11 16:22:12 +00:00
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
}
|