aboutsummaryrefslogtreecommitdiffstatsci
path: root/CrowIDE/src/Solution.cs
blob: 888bf684c4f2ce9f81425760526f5ddad4fd4f65 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Solution.cs
//
//code taken in project https://sourceforge.net/projects/syncproj/
// no licence info was included, I took the liberty to modify it.
// Author:
//		tarmopikaro
//      2018 Jean-Philippe Bruyère
//MIT-licenced

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace CrowIDE{
	public class SolutionProject {
		public string ProjectHostGuid;
		public string ProjectName;
		public string RelativePath;
		public string ProjectGuid;
	}
	/// <summary>
/// .sln loaded into class.
/// </summary>
	public class Solution
	{
	    /// <summary>
	    /// Solution name
	    /// </summary>
	    public String name;

		public string DisplayName {
			get { return name; }
		}

	    /// <summary>
	    /// File path from where solution was loaded.
	    /// </summary>
	    [XmlIgnore]
	    public String path;

	    /// <summary>
	    /// Solution name for debugger.
	    /// </summary>
	    [ExcludeFromCodeCoverage]
	    public override string ToString()
	    {
	        return "Solution, name = " + name;
	    }

	    /// <summary>
	    /// Gets solution path
	    /// </summary>
	    /// <returns></returns>
	    public String SolutionFolder
	    {
			get { return Path.GetDirectoryName (path); }
	    }

		public IList<Project> Projects {
			get {
				List<Project> tmp = new List<Project> ();
				foreach (SolutionProject p in projects) {
					string pp = Path.Combine (SolutionFolder, p.RelativePath.Replace('\\','/'));
					tmp.Add (new Project (pp, this));
				}
				return tmp;
			}
		}

	    double slnVer;                                      // 11.00 - vs2010, 12.00 - vs2015

	    /// <summary>
	    /// Visual studio version information used for generation, for example 2010, 2012, 2015 and so on...
	    /// </summary>
	    public int fileFormatVersion;

	    /// <summary>
	    /// null for old visual studio's
	    /// </summary>
	    public String VisualStudioVersion;
	    
	    /// <summary>
	    /// null for old visual studio's
	    /// </summary>
	    public String MinimumVisualStudioVersion;

	    /// <summary>
	    /// List of project included into solution.
	    /// </summary>
	    public List<SolutionProject> projects = new List<SolutionProject>();

	    /// <summary>
	    /// List of configuration list, in form "{Configuration}|{Platform}", for example "Release|Win32".
	    /// To extract individual platforms / configuration list, use following functions.
	    /// </summary>
	    public List<String> configurations = new List<string>();

	    /// <summary>
	    /// Extracts platfroms supported by solution
	    /// </summary>
	    public IEnumerable<String> getPlatforms()
	    {
	        return configurations.Select(x => x.Split('|')[1]).Distinct();
	    }

	    /// <summary>
	    /// Extracts configuration names supported by solution
	    /// </summary>
	    public IEnumerable<String> getConfigurations()
	    {
	        return configurations.Select(x => x.Split('|')[0]).Distinct();
	    }


	    /// <summary>
	    /// Creates new solution.
	    /// </summary>
	    public Solution() { }

	    /// <summary>
	    /// Loads visual studio .sln solution
	    /// </summary>
	    /// <exception cref="System.IO.FileNotFoundException">The file specified in path was not found.</exception>
	    static public Solution LoadSolution(string path)
	    {
	        Solution s = new Solution();
	        s.path = path;

	        String slnTxt = File.ReadAllText(path);
	        //
	        //  Extra line feed is used by Visual studio, cmake does not generate extra line feed.
	        //
	        s.slnVer = Double.Parse(Regex.Match(slnTxt, "[\r\n]?Microsoft Visual Studio Solution File, Format Version ([0-9.]+)", RegexOptions.Multiline).Groups[1].Value, CultureInfo.InvariantCulture);

	        int vsNumber = Int32.Parse(Regex.Match(slnTxt, "^\\# Visual Studio (Express )?([0-9]+)", RegexOptions.Multiline).Groups[2].Value);
	        if (vsNumber > 2000)
	            s.fileFormatVersion = vsNumber;
	        else
	            s.fileFormatVersion = vsNumber - 14 + 2015;     // Visual Studio 14 => vs2015, formula might not be applicable for future vs versions.

	        foreach (String line in new String[] { "VisualStudioVersion", "MinimumVisualStudioVersion" })
	        {
	            var m = Regex.Match(slnTxt, "^" + line + " = ([0-9.]+)", RegexOptions.Multiline);
	            String v = null;
	            if (m.Success)
	                v = m.Groups[1].Value;

	            s.GetType().GetField(line).SetValue(s, v);
	        }

	        Regex reProjects = new Regex(
	            "Project\\(\"(?<ProjectHostGuid>{[A-F0-9-]+})\"\\) = \"(?<ProjectName>.*?)\", \"(?<RelativePath>.*?)\", \"(?<ProjectGuid>{[A-F0-9-]+})\"[\r\n]*(?<dependencies>.*?)EndProject[\r\n]+",
	            RegexOptions.Singleline);


	        reProjects.Replace(slnTxt, new MatchEvaluator(m =>
	        {
	            SolutionProject p = new SolutionProject();

	            foreach (String g in reProjects.GetGroupNames())
	            {
	                if (g == "0")   //"0" - RegEx special kind of group
	                    continue;

	                //
	                // ProjectHostGuid, ProjectName, RelativePath, ProjectGuid fields/properties are set here.
	                //
	                String v = m.Groups[g].ToString();
	                if (g != "dependencies")
	                {
	                    FieldInfo fi = p.GetType().GetField(g);
	                    if (fi != null)
	                    {
	                        fi.SetValue(p, v);
	                    }
	                    else
	                    {
	                        p.GetType().GetProperty(g).SetValue(p, v);
	                    }
	                    continue;
	                }

	                if (v == "")    // No dependencies set
	                    continue;

	                String depsv = new Regex("ProjectSection\\(ProjectDependencies\\)[^\r\n]*?[\r\n]+" + "(.*?)" + "EndProjectSection", RegexOptions.Singleline).Match(v).Groups[1].Value;

	                //
	                // key is always equal to it's value.
	                // http://stackoverflow.com/questions/5629981/question-about-visual-studio-sln-file-format
	                //
	                var ProjectDependencies = new Regex("\\s*?({[A-F0-9-]+}) = ({[A-F0-9-]+})[\r\n]+", RegexOptions.Multiline).Matches(depsv).Cast<Match>().Select(x => x.Groups[1].Value).ToList();
	            } //foreach
	            s.projects.Add(p);
	            return "";
	        }
	        )
	        );

	        new Regex("GlobalSection\\(SolutionConfigurationPlatforms\\).*?[\r\n]+(.*?)EndGlobalSection[\r\n]+", RegexOptions.Singleline).Replace(slnTxt, new MatchEvaluator(m2 =>
	        {
	            s.configurations = new Regex("\\s*(.*)\\s+=").Matches(m2.Groups[1].ToString()).Cast<Match>().Select(x => x.Groups[1].Value).ToList();
	            return "";
	        }
	        ));

	        new Regex("GlobalSection\\(ProjectConfigurationPlatforms\\).*?[\r\n]+(.*?)EndGlobalSection[\r\n]+", RegexOptions.Singleline).Replace(slnTxt, new MatchEvaluator(m2 =>
	        {
	            foreach (Match m3 in new Regex("\\s*({[A-F0-9-]+})\\.(.*?)\\.(.*?)\\s+=\\s+(.*?)[\r\n]+").Matches(m2.Groups[1].ToString()))
	            {
	                String guid = m3.Groups[1].Value;
	                String solutionConfig = m3.Groups[2].Value;
	                String action = m3.Groups[3].Value;
	                String projectConfig = m3.Groups[4].Value;

	                SolutionProject p = s.projects.Where(x => x.ProjectGuid == guid).FirstOrDefault();
	                if (p == null)
	                    continue;

	                int iConfigIndex = s.configurations.IndexOf(solutionConfig);
	                if (iConfigIndex == -1)
	                    continue;

//	                while (p.slnConfigurations.Count < s.configurations.Count)
//	                {
//	                    p.slnConfigurations.Add(null);
//	                    p.slnBuildProject.Add(false);
//	                }
//
//	                if (action == "ActiveCfg")
//	                {
//	                    p.slnConfigurations[iConfigIndex] = projectConfig;
//	                }
//	                else
//	                {
//	                    if (action.StartsWith("Build"))
//	                    {
//	                        p.slnBuildProject[iConfigIndex] = true;
//	                    }
//	                    else
//	                    {
//	                        if (action.StartsWith("Deploy"))
//	                        {
//	                            if (p.slnDeployProject == null) p.slnDeployProject = new List<bool?>();
//
//	                            while (p.slnDeployProject.Count < s.configurations.Count)
//	                                p.slnDeployProject.Add(null);
//
//	                            p.slnDeployProject[iConfigIndex] = true;
//	                        }
//	                    }
//	                } //if-esle
	            }
	            return "";
	        }
	        ));

	        //
	        // Initializes parent-child relationship.
	        //
	        new Regex("GlobalSection\\(NestedProjects\\).*?[\r\n]+(.*?)EndGlobalSection[\r\n]+", RegexOptions.Singleline).Replace(slnTxt, new MatchEvaluator(m4 =>
	        {
	            String v = m4.Groups[1].Value;
	            new Regex("\\s*?({[A-F0-9-]+}) = ({[A-F0-9-]+})[\r\n]+", RegexOptions.Multiline).Replace(v, new MatchEvaluator(m5 =>
	            {
	                String[] args = m5.Groups.Cast<Group>().Skip(1).Select(x => x.Value).ToArray();
	                SolutionProject child = s.projects.Where(x => args[0] == x.ProjectGuid).FirstOrDefault();
	                SolutionProject parent = s.projects.Where(x => args[1] == x.ProjectGuid).FirstOrDefault();
//	                parent.nodes.Add(child);
//	                child.parent = parent;
	                return "";
	            }));
	            return "";
	        }
	        ));

	        return s;
	    } //LoadSolution
	} //class Solution
}