Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SlnFile.cs « MonoDevelop.Projects.Formats.MSBuild « MonoDevelop.Core « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba9c875b75e0a19e2d0d74096822b2bbb3443c1d (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Collections;
using MonoDevelop.Core;

namespace MonoDevelop.Projects.Formats.MSBuild
{
	public class SlnFile
	{
		SlnProjectCollection projects = new SlnProjectCollection ();
		SlnSectionCollection sections = new SlnSectionCollection ();
		SlnPropertySet metadata = new SlnPropertySet (true);

		public string FormatVersion { get; set; }
		public string ProductDescription { get; set; }

		public string VisualStudioVersion {
			get { return metadata.GetValue ("VisualStudioVersion"); }
			set { metadata.SetValue ("VisualStudioVersion", value); }
		}

		public string MinimumVisualStudioVersion {
			get { return metadata.GetValue ("MinimumVisualStudioVersion"); }
			set { metadata.SetValue ("MinimumVisualStudioVersion", value); }
		}

		public SlnFile ()
		{
		}

		/// <summary>
		/// The directory to be used as base for converting absolute paths to relative
		/// </summary>
		public FilePath BaseDirectory {
			get;
			set;
		}

		public SlnPropertySet SolutionConfigurationsSection {
			get { return sections.GetOrCreateSection ("SolutionConfigurationPlatforms", "preSolution").Properties; }
		}

		public SlnPropertySetCollection ProjectConfigurationsSection {
			get { return sections.GetOrCreateSection ("ProjectConfigurationPlatforms", "postSolution").NestedPropertySets; }
		}

		public SlnSectionCollection Sections {
			get { return sections; }
		}

		public SlnProjectCollection Projects {
			get { return projects; }
		}

		public void Read (string file)
		{
			using (var sr = new StreamReader (file))
				Read (sr);
		}

		public void Read (TextReader reader)
		{
			string line;
			int curLineNum = 0;
			bool globalFound = false;
			bool productRead = false;

			while ((line = reader.ReadLine ()) != null) {
				curLineNum++;
				line = line.Trim ();
				if (line.StartsWith ("Microsoft Visual Studio Solution File")) {
					int i = line.LastIndexOf (' ');
					if (i == -1)
						throw new InvalidSolutionFormatException (curLineNum);
					FormatVersion = line.Substring (i + 1);
				}
				if (line.StartsWith ("# ")) {
					if (!productRead) {
						productRead = true;
						ProductDescription = line.Substring (2);
					}
				} else if (line.StartsWith ("Project")) {
					SlnProject p = new SlnProject ();
					p.Read (reader, line, ref curLineNum);
					projects.Add (p);
				} else if (line == "Global") {
					if (globalFound)
						throw new InvalidSolutionFormatException (curLineNum, "Global section specified more than once");
					globalFound = true;
					while ((line = reader.ReadLine ()) != null) {
						curLineNum++;
						line = line.Trim ();
						if (line == "EndGlobal") {
							break;
						} else if (line.StartsWith ("GlobalSection")) {
							var sec = new SlnSection ();
							sec.Read (reader, line, ref curLineNum);
							sections.Add (sec);
						} else
							throw new InvalidSolutionFormatException (curLineNum);
					}
					if (line == null)
						throw new InvalidSolutionFormatException (curLineNum, "Global section not closed");
				} else if (line.IndexOf ('=') != -1) {
					metadata.ReadLine (line, curLineNum);
				}
			}
			if (FormatVersion == null)
				throw new InvalidSolutionFormatException (curLineNum, "File header is missing");
		}

		public void Write (string file)
		{
			using (var sw = new StreamWriter (file))
				Write (sw);
		}

		public void Write (TextWriter writer)
		{
			writer.NewLine = "\r\n";
			writer.WriteLine ("\r\nMicrosoft Visual Studio Solution File, Format Version " + FormatVersion);
			writer.WriteLine ("# " + ProductDescription);

			metadata.Write (writer);

			foreach (var p in projects)
				p.Write (writer);

			writer.WriteLine ("Global");
			foreach (SlnSection s in sections)
				s.Write (writer, "GlobalSection");
			writer.WriteLine ("EndGlobal");
		}
	}

	public class SlnProject
	{
		SlnSectionCollection sections = new SlnSectionCollection ();

		public string Id { get; set; }
		public string TypeGuid { get; set; }
		public string Name { get; set; }
		public string FilePath { get; set; }
		public int Line { get; private set; }
		internal bool Processed { get; set; }

		public SlnSectionCollection Sections {
			get { return sections; }
		}

		internal void Read (TextReader reader, string line, ref int curLineNum)
		{
			Line = curLineNum;

			int n = 0;
			FindNext (curLineNum, line, ref n, '(');
			n++;
			FindNext (curLineNum, line, ref n, '"');
			int n2 = n + 1;
			FindNext (curLineNum, line, ref n2, '"');
			TypeGuid = line.Substring (n + 1, n2 - n - 1);

			n = n2 + 1;
			FindNext (curLineNum, line, ref n, ')');
			FindNext (curLineNum, line, ref n, '=');

			FindNext (curLineNum, line, ref n, '"');
			n2 = n + 1;
			FindNext (curLineNum, line, ref n2, '"');
			Name = line.Substring (n + 1, n2 - n - 1);

			n = n2 + 1;
			FindNext (curLineNum, line, ref n, ',');
			FindNext (curLineNum, line, ref n, '"');
			n2 = n + 1;
			FindNext (curLineNum, line, ref n2, '"');
			FilePath = line.Substring (n + 1, n2 - n - 1);

			n = n2 + 1;
			FindNext (curLineNum, line, ref n, ',');
			FindNext (curLineNum, line, ref n, '"');
			n2 = n + 1;
			FindNext (curLineNum, line, ref n2, '"');
			Id = line.Substring (n + 1, n2 - n - 1);

			while ((line = reader.ReadLine ()) != null) {
				curLineNum++;
				line = line.Trim ();
				if (line == "EndProject") {
					return;
				}
				if (line.StartsWith ("ProjectSection")) {
					if (sections == null)
						sections = new SlnSectionCollection ();
					var sec = new SlnSection ();
					sections.Add (sec);
					sec.Read (reader, line, ref curLineNum);
				}
			}

			throw new InvalidSolutionFormatException (curLineNum, "Project section not closed");
		}

		void FindNext (int ln, string line, ref int i, char c)
		{
			i = line.IndexOf (c, i);
			if (i == -1)
				throw new InvalidSolutionFormatException (ln);
		}

		public void Write (TextWriter writer)
		{
			writer.Write ("Project(\"");
			writer.Write (TypeGuid);
			writer.Write ("\") = \"");
			writer.Write (Name);
			writer.Write ("\", \"");
			writer.Write (FilePath);
			writer.Write ("\", \"");
			writer.Write (Id);
			writer.WriteLine ("\"");
			if (sections != null) {
				foreach (SlnSection s in sections)
					s.Write (writer, "ProjectSection");
			}
			writer.WriteLine ("EndProject");
		}
	}

	public class SlnSection
	{
		SlnPropertySetCollection nestedPropertySets;
		SlnPropertySet properties;
		List<string> sectionLines;
		int baseIndex;

		public string Id { get; set; }
		public int Line { get; private set; }
		internal bool Processed { get; set; }

		public SlnPropertySet Properties {
			get {
				if (properties == null) {
					properties = new SlnPropertySet ();
					if (sectionLines != null) {
						foreach (var line in sectionLines)
							properties.ReadLine (line, Line);
						sectionLines = null;
					}
				}
				return properties;
			}
		}

		public SlnPropertySetCollection NestedPropertySets {
			get {
				if (nestedPropertySets == null) {
					nestedPropertySets = new SlnPropertySetCollection ();
					if (sectionLines != null)
						LoadPropertySets ();
				}
				return nestedPropertySets;
			}
		}

		public string SectionType { get; set; }

		internal void Read (TextReader reader, string line, ref int curLineNum)
		{
			Line = curLineNum;
			int k = line.IndexOf ('(');
			if (k == -1)
				throw new InvalidSolutionFormatException (curLineNum, "Section id missing");
			var tag = line.Substring (0, k).Trim ();
			var k2 = line.IndexOf (')', k);
			if (k2 == -1)
				throw new InvalidSolutionFormatException (curLineNum);
			Id = line.Substring (k + 1, k2 - k - 1);

			k = line.IndexOf ('=', k2);
			SectionType = line.Substring (k + 1).Trim ();

			var endTag = "End" + tag;

			sectionLines = new List<string> ();
			baseIndex = ++curLineNum;
			while ((line = reader.ReadLine()) != null) {
				curLineNum++;
				line = line.Trim ();
				if (line == endTag)
					break;
				sectionLines.Add (line);
			}
			if (line == null)
				throw new InvalidSolutionFormatException (curLineNum, "Closing section tag not found");
		}

		void LoadPropertySets ()
		{
			if (sectionLines != null) {
				SlnPropertySet curSet = null;
				for (int n = 0; n < sectionLines.Count; n++) {
					var line = sectionLines [n];
					var i = line.IndexOf ('.');
					if (i == -1)
						throw new InvalidSolutionFormatException (baseIndex + i);
					var id = line.Substring (0, i);
					if (curSet == null || id != curSet.Id) {
						curSet = new SlnPropertySet (id);
						nestedPropertySets.Add (curSet);
					}
					curSet.ReadLine (line.Substring (i + 1), baseIndex + i);
				}
				sectionLines = null;
			}
		}

		internal void Write (TextWriter writer, string sectionTag)
		{
			writer.Write ("\t");
			writer.Write (sectionTag);
			writer.Write ('(');
			writer.Write (Id);
			writer.Write (") = ");
			writer.WriteLine (SectionType);
			if (sectionLines != null) {
				foreach (var l in sectionLines)
					writer.WriteLine ("\t\t" + l);
			} else if (properties != null)
				properties.Write (writer);
			else if (nestedPropertySets != null) {
				foreach (var ps in nestedPropertySets)
					ps.Write (writer);
			}
			writer.WriteLine ("\tEnd" + sectionTag);
		}
	}

	public class SlnPropertySet: IDictionary<string,string>
	{
		OrderedDictionary values = new OrderedDictionary ();
		bool isMetadata;

		internal bool Processed { get; set; }
		public int Line { get; private set; }

		public SlnPropertySet ()
		{
		}

		public SlnPropertySet (string id)
		{
			Id = id;
		}

		internal SlnPropertySet (bool isMetadata)
		{
			this.isMetadata = isMetadata;
		}

		internal void ReadLine (string line, int currentLine)
		{
			if (Line == 0)
				Line = currentLine;
			line = line.Trim ();
			int k = line.IndexOf ('=');
			if (k != -1) {
				var name = line.Substring (0, k).Trim ();
				var val = line.Substring (k + 1).Trim ();
				values.Add (name, val);
			} else {
				values.Add (line, null);
			}
		}

		internal void Write (TextWriter writer)
		{
			foreach (DictionaryEntry e in values) {
				if (!isMetadata)
					writer.Write ("\t\t");
				if (Id != null)
					writer.Write (Id + ".");
				writer.WriteLine (e.Key + " = " + e.Value);
			}
		}

		public string Id { get; private set; }

		public string GetValue (string key)
		{
			return (string) values [key];
		}

		public void SetValue (string key, string value)
		{
			values [key] = value;
		}

		public void Add (string key, string value)
		{
			SetValue (key, value);
		}

		public bool ContainsKey (string key)
		{
			return values.Contains (key);
		}

		public bool Remove (string key)
		{
			var wasThere = values.Contains (key);
			values.Remove (key);
			return wasThere;
		}

		public bool TryGetValue (string key, out string value)
		{
			value = (string) values [key];
			return value != null;
		}

		public string this [string index] {
			get {
				return (string) values [index];
			}
			set {
				values [index] = value;
			}
		}

		public ICollection<string> Values {
			get {
				return values.Values.Cast<string>().ToList ();
			}
		}

		public ICollection<string> Keys {
			get { return values.Keys.Cast<string> ().ToList (); }
		}

		public void Add (KeyValuePair<string, string> item)
		{
			SetValue (item.Key, item.Value);
		}

		public void Clear ()
		{
			values.Clear ();
		}

		internal void ClearExcept (HashSet<string> keys)
		{
			foreach (var k in values.Keys.Cast<string>().Except (keys).ToArray ())
				values.Remove (k);
		}

		public bool Contains (KeyValuePair<string, string> item)
		{
			var val = GetValue (item.Key);
			return val == item.Value;
		}

		public void CopyTo (KeyValuePair<string, string>[] array, int arrayIndex)
		{
			foreach (DictionaryEntry de in values)
				array [arrayIndex++] = new KeyValuePair<string, string> ((string)de.Key, (string)de.Value);
		}

		public bool Remove (KeyValuePair<string, string> item)
		{
			if (Contains (item)) {
				Remove (item.Key);
				return true;
			} else
				return false;
		}

		public int Count {
			get {
				return values.Count;
			}
		}

		public bool IsReadOnly {
			get {
				return false;
			}
		}

		public IEnumerator<KeyValuePair<string, string>> GetEnumerator ()
		{
			foreach (DictionaryEntry de in values)
				yield return new KeyValuePair<string,string> ((string)de.Key, (string)de.Value);
		}

		IEnumerator IEnumerable.GetEnumerator ()
		{
			foreach (DictionaryEntry de in values)
				yield return new KeyValuePair<string,string> ((string)de.Key, (string)de.Value);
		}
	}

	public class SlnProjectCollection: Collection<SlnProject>
	{
		public SlnProject GetProject (string id)
		{
			return this.FirstOrDefault (s => s.Id == id);
		}

		public SlnProject GetOrCreateProject (string id)
		{
			var p = this.FirstOrDefault (s => s.Id.Equals (id, StringComparison.OrdinalIgnoreCase));
			if (p == null) {
				p = new SlnProject { Id = id };
				Add (p);
			}
			return p;
		}
	}

	public class SlnSectionCollection: Collection<SlnSection>
	{
		public SlnSection GetSection (string id)
		{
			return this.FirstOrDefault (s => s.Id == id);
		}

		public SlnSection GetOrCreateSection (string id, string sectionType)
		{
			var sec = this.FirstOrDefault (s => s.Id == id);
			if (sec == null) {
				sec = new SlnSection { Id = id };
				sec.SectionType = sectionType;
				Add (sec);
			}
			return sec;
		}

		public void RemoveSection (string id)
		{
			var s = GetSection (id);
			if (s != null)
				Remove (s);
		}
	}

	public class SlnPropertySetCollection: Collection<SlnPropertySet>
	{
		public SlnPropertySet GetPropertySet (string id, bool ignoreCase = false)
		{
			var sc = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
			return this.FirstOrDefault (s => s.Id.Equals (id, sc));
		}

		public SlnPropertySet GetOrCreatePropertySet (string id, bool ignoreCase = false)
		{
			var ps = GetPropertySet (id, ignoreCase);
			if (ps == null) {
				ps = new SlnPropertySet (id);
				Add (ps);
			}
			return ps;
		}
	}

	class InvalidSolutionFormatException: Exception
	{
		public InvalidSolutionFormatException (int line): base ("Invalid format in line " + line)
		{
		}

		public InvalidSolutionFormatException (int line, string msg): base ("Invalid format in line " + line + ": " + msg)
		{
		}
	}
}