summaryrefslogtreecommitdiff
path: root/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/DotNetSolution.cs
blob: 76cb249acfa8dc1f4dd7a86b46c1e9d4eae1c82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using GodotTools.Core;
using System.Collections.Generic;
using System.IO;

namespace GodotTools.ProjectEditor
{
    public class DotNetSolution
    {
        private string directoryPath;
        private readonly Dictionary<string, ProjectInfo> projects = new Dictionary<string, ProjectInfo>();

        public string Name { get; }

        public string DirectoryPath
        {
            get => directoryPath;
            set => directoryPath = value.IsAbsolutePath() ? value : Path.GetFullPath(value);
        }

        public class ProjectInfo
        {
            public string Guid;
            public string PathRelativeToSolution;
            public List<string> Configs = new List<string>();
        }

        public void AddNewProject(string name, ProjectInfo projectInfo)
        {
            projects[name] = projectInfo;
        }

        public bool HasProject(string name)
        {
            return projects.ContainsKey(name);
        }

        public ProjectInfo GetProjectInfo(string name)
        {
            return projects[name];
        }

        public bool RemoveProject(string name)
        {
            return projects.Remove(name);
        }

        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;

            foreach (var pair in projects)
            {
                string name = pair.Key;
                ProjectInfo projectInfo = pair.Value;

                if (!isFirstProject)
                    projectsDecl += "\n";

                projectsDecl += string.Format(ProjectDeclaration,
                    name, projectInfo.PathRelativeToSolution.Replace("/", "\\"), projectInfo.Guid);

                for (int i = 0; i < projectInfo.Configs.Count; i++)
                {
                    string config = projectInfo.Configs[i];

                    if (i != 0 || !isFirstProject)
                    {
                        slnPlatformsCfg += "\n";
                        projPlatformsCfg += "\n";
                    }

                    slnPlatformsCfg += string.Format(SolutionPlatformsConfig, config);
                    projPlatformsCfg += string.Format(ProjectPlatformsConfig, projectInfo.Guid, config);
                }

                isFirstProject = false;
            }

            string solutionPath = Path.Combine(DirectoryPath, Name + ".sln");
            string content = string.Format(SolutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);

            File.WriteAllText(solutionPath, content);
        }

        public DotNetSolution(string name)
        {
            Name = name;
        }

        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
";

        const string ProjectDeclaration =
@"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{{{2}}}""
EndProject";

        const string SolutionPlatformsConfig =
@"	{0}|Any CPU = {0}|Any CPU";

        const string ProjectPlatformsConfig =
@"		{{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU
		{{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU";
    }
}