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

IMSBuildProject.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: a43393337cf318c340c4256b67c1acf229b93c98 (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
//
// IMSBuildProject.cs
//
// Author:
//       Lluis Sanchez Gual <lluis@xamarin.com>
//
// Copyright (c) 2014 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using MonoDevelop.Core;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

namespace MonoDevelop.Projects.Formats.MSBuildInternal
{
	public abstract class MSBuildProject
	{
		Dictionary<object,Dictionary<Type,IMSBuildDataObject>> customDataObjects = new Dictionary<object,Dictionary<Type,IMSBuildDataObject>> ();
		Dictionary<string, MSBuildItemGroup> bestGroups;

		public abstract FilePath FileName { get; }

		public FilePath BaseDirectory {
			get { return FileName.ParentDirectory; }
		}

		public abstract void Load (FilePath file);

		public abstract void Save (string fileName);

		public abstract string SaveToString ();

		public MonoDevelop.Projects.Formats.MSBuild.MSBuildFileFormat Format {
			get;
			set;
		}

		public abstract string DefaultTargets { get; set; }

		public abstract string ToolsVersion { get; set; }

		public T GetObject<T> () where T : IMSBuildDataObject, new()
		{
			return GetObject<T> (this);
		}

		public void SetObject<T> (T t) where T : IMSBuildDataObject
		{
			SetObject<T> (this, t);
		}

		internal T GetObject<T> (object owner) where T : IMSBuildDataObject, new()
		{
			Dictionary<Type,IMSBuildDataObject> col;
			if (!customDataObjects.TryGetValue (owner, out col))
				return default(T);
			IMSBuildDataObject res;
			col.TryGetValue (typeof(T), out res);
			return (T)res;
		}

		internal void SetObject<T> (object owner, T t) where T : IMSBuildDataObject
		{
			Dictionary<Type,IMSBuildDataObject> col;
			if (!customDataObjects.TryGetValue (owner, out col))
				col = customDataObjects [owner] = new Dictionary<Type, IMSBuildDataObject> ();
			col [typeof(T)] = t;
		}

		internal IEnumerable<IMSBuildDataObject> GetDataObjects (object owner)
		{
			Dictionary<Type,IMSBuildDataObject> col;
			if (!customDataObjects.TryGetValue (owner, out col))
				return new IMSBuildDataObject[0];
			else
				return col.Values;
		}

		public abstract void AddNewImport (string name, MSBuildImport beforeImport = null);

		public abstract void RemoveImport (MSBuildImport import);

		public abstract IEnumerable<MSBuildImport> Imports { get; }

		public MSBuildPropertyGroup GetGlobalPropertyGroup ()
		{
			return PropertyGroups.FirstOrDefault (p => string.IsNullOrEmpty (p.Condition));
		}

		public abstract MSBuildPropertyGroup AddNewPropertyGroup (MSBuildPropertyGroup beforeGroup = null);

		public abstract void RemovePropertyGroup (MSBuildPropertyGroup grp);

		public abstract IEnumerable<MSBuildItem> GetAllItems ();

		public abstract IEnumerable<MSBuildItem> GetAllItems (params string[] names);

		public abstract IEnumerable<MSBuildPropertyGroup> PropertyGroups { get; }

		public abstract IEnumerable<MSBuildItemGroup> ItemGroups { get; }

		public abstract MSBuildItemGroup AddNewItemGroup ();

		public abstract MSBuildItem AddNewItem (string name, string include);

		public MSBuildItemGroup FindBestGroupForItem (string itemName)
		{
			MSBuildItemGroup group;

			if (bestGroups == null)
				bestGroups = new Dictionary<string, MSBuildItemGroup> ();
			else {
				if (bestGroups.TryGetValue (itemName, out group))
					return group;
			}

			foreach (MSBuildItemGroup grp in ItemGroups) {
				foreach (MSBuildItem it in grp.Items) {
					if (it.Name == itemName) {
						bestGroups [itemName] = grp;
						return grp;
					}
				}
			}
			group = AddNewItemGroup ();
			bestGroups [itemName] = group;
			return group;
		}

		public abstract XmlElement GetProjectExtensions (string section);

		public abstract void SetProjectExtensions (string section, string value);

		public abstract void RemoveProjectExtensions (string section);

		public abstract void RemoveItem (MSBuildItem item);
	}

	public abstract class MSBuildItemGroup
	{
		internal MSBuildItemGroup (MSBuildProject parent)
		{
			Project = parent;
		}

		public abstract MSBuildItem AddNewItem (string name, string include);

		public abstract IEnumerable<MSBuildItem> Items { get; }

		public MSBuildProject Project { get; private set; }
	}

	public abstract class MSBuildPropertyGroup: MSBuildPropertySet
	{
		MSBuildProject project;

		internal MSBuildPropertyGroup (MSBuildProject project): base (project)
		{
			this.project = project;
		}

		public abstract string Label { get; set; }
		public abstract string Condition { get; set; }
	}

	internal interface IPropertySetImpl
	{
		bool HasProperty (string name);
		MSBuildProperty GetProperty (string name, string condition = null);
		MSBuildProperty AddProperty (string name, string condition = null);
		bool RemoveProperty (MSBuildProperty prop);
		bool RemoveProperty (string name);
		void RemoveAllProperties ();
		IEnumerable<MSBuildProperty> Properties { get; }
	}

	public abstract class MSBuildPropertySet: IMSBuildPropertySet
	{
		MSBuildProject project;
		Dictionary<Type,IMSBuildDataObject> customDataObjects = new Dictionary<Type,IMSBuildDataObject> ();

		internal MSBuildPropertySet (MSBuildProject project)
		{
			this.project = project;
		}

		internal abstract IPropertySetImpl PropertySet { get; }

		public T GetObject<T> () where T : IMSBuildDataObject, new()
		{
			IMSBuildDataObject res;
			customDataObjects.TryGetValue (typeof(T), out res);
			return (T)res;
		}

		public void SetObject<T> (T t) where T : IMSBuildDataObject
		{
			customDataObjects [typeof(T)] = t;
		}

		internal void WriteDataObjects ()
		{
			foreach (IMSBuildDataObject ob in customDataObjects.Values)
				ob.Write (this, project.Format);
		}

		public bool HasProperty (string name)
		{
			return PropertySet.HasProperty (name);
		}

		public MSBuildProperty GetProperty (string name, string condition = null)
		{
			return PropertySet.GetProperty (name, condition);
		}

		public MSBuildProperty AddProperty (string name, string condition = null)
		{
			return PropertySet.AddProperty (name, condition);
		}

		MSBuildProperty SafeGetProperty (string name, string condition)
		{
			var prop = GetProperty (name, condition);
			if (prop == null) {
				prop = GetProperty (name);
				if (prop != null) {
					prop.Condition = condition;
					return prop;
				}
			}
			return AddProperty (name, condition);
		}

		public void SetValue (string name, string value, string defaultValue = null, bool preserveExistingCase = false, bool isXmlValue = false, bool mergeToMainGroup = false, string condition = null)
		{
			var prop = SafeGetProperty (name, condition);
			if (value == defaultValue) {
				RemoveProperty (prop);
				return;
			}
			prop.SetValue (value, preserveExistingCase, isXmlValue, mergeToMainGroup);
		}

		public void SetValue (string name, XmlElement elem, bool mergeToMainGroup = false, string condition = null)
		{
			SafeGetProperty (name, condition).SetValue (elem, mergeToMainGroup);
		}

		public void SetValue (string name, XElement elem, bool mergeToMainGroup = false, string condition = null)
		{
			SafeGetProperty (name, condition).SetValue (elem, mergeToMainGroup);
		}

		public void SetValue (string name, FilePath value, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath), bool mergeToMainGroup = false, string condition = null)
		{
			SafeGetProperty (name, condition).SetValue (value, relativeToProject, relativeToPath, mergeToMainGroup);
		}

		public void SetValue (string name, bool value, bool? defaultValue = false, bool mergeToMainGroup = false, string condition = null)
		{
			SafeGetProperty (name, condition).SetValue (value, mergeToMainGroup);
		}

		public string GetValue (string name, string defaultValue = null)
		{
			var prop = GetProperty (name);
			if (prop != null)
				return prop.GetValue ();
			else
				return defaultValue;
		}

		public XElement GetXmlValue (string name)
		{
			var prop = GetProperty (name);
			if (prop != null)
				return XElement.Parse (prop.GetValue ());
			else
				return null;
		}

		public FilePath GetPathValue (string name, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
		{
			var prop = GetProperty (name);
			if (prop != null)
				return prop.GetPathValue (relativeToProject, relativeToPath);
			else
				return defaultValue;
		}

		public bool TryGetPathValue (string name, out FilePath value, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
		{
			var prop = GetProperty (name);
			if (prop != null)
				return prop.TryGetPathValue (out value, relativeToProject, relativeToPath);
			else {
				value = defaultValue;
				return value != default(FilePath);
			}
		}

		public bool GetBoolValue (string name, bool defaultValue = false)
		{
			var prop = GetProperty (name);
			if (prop != null)
				return prop.GetBoolValue ();
			else
				return defaultValue;
		}

		public virtual bool RemoveProperty (string name)
		{
			return PropertySet.RemoveProperty (name);
		}

		public bool RemoveProperty (MSBuildProperty prop)
		{
			return PropertySet.RemoveProperty (prop);
		}

		public void RemoveAllProperties ()
		{
			PropertySet.RemoveAllProperties ();
		}

		public IEnumerable<MSBuildProperty> Properties {
			get { return PropertySet.Properties; }
		}
	}

	public interface IMSBuildPropertySet
	{
		T GetObject<T> () where T:IMSBuildDataObject, new();
		void SetObject<T> (T t) where T:IMSBuildDataObject;

		bool HasProperty (string name);
		MSBuildProperty GetProperty (string name, string condition = null);
		IEnumerable<MSBuildProperty> Properties { get; }

		void SetValue (string name, string value, string defaultValue = null, bool preserveExistingCase = false, bool isXmlValue = false, bool mergeToMainGroup = false, string condition = null);
		void SetValue (string name, XmlElement elem, bool mergeToMainGroup = false, string condition = null);
		void SetValue (string name, XElement elem, bool mergeToMainGroup = false, string condition = null);
		void SetValue (string name, FilePath value, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath), bool mergeToMainGroup = false, string condition = null);
		void SetValue (string name, bool value, bool? defaultValue = false, bool mergeToMainGroup = false, string condition = null);

		string GetValue (string name, string defaultValue = null);
		XElement GetXmlValue (string name);
		FilePath GetPathValue (string name, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath));
		bool TryGetPathValue (string name, out FilePath value, FilePath defaultValue = default(FilePath), bool relativeToProject = true, FilePath relativeToPath = default(FilePath));
		bool GetBoolValue (string name, bool defaultValue = false);

		bool RemoveProperty (string name);
		void RemoveAllProperties ();
	}

	public abstract class MSBuildImport
	{
		public abstract string Target { get; set; }

		public abstract string Label { get; set; }

		public abstract string Condition { get; set; }
	}

	public abstract class MSBuildProperty
	{
		MSBuildProject project;

		internal MSBuildProperty (MSBuildProject project)
		{
			this.project = project;
		}

		public abstract string Name {
			get;
		}

		public abstract string Condition { get; set; }

		public void SetValue (string value, bool preserveExistingCase = false, bool isXmlValue = false, bool mergeToMainGroup = false)
		{
			if (value == null)
				value = String.Empty;

			if (preserveExistingCase) {
				var current = GetPropertyValue ();
				if (current != null) {
					if (current.Equals (value, preserveExistingCase ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture))
						return;
				}
			}
			SetPropertyValue (value, isXmlValue);
		}

		public void SetValue (XmlElement elem, bool mergeToMainGroup = false)
		{
			SetPropertyValue (elem.OuterXml, true);
		}

		public void SetValue (XElement elem, bool mergeToMainGroup = false)
		{
			SetPropertyValue (elem.ToString (), true);
		}

		public void SetValue (FilePath value, bool relativeToProject = true, FilePath relativeToPath = default(FilePath), bool mergeToMainGroup = false)
		{
			string baseDir = null;
			if (relativeToProject) {
				if (relativeToPath != default(FilePath))
					throw new ArgumentException ("relativeToPath argument can't be used together with relativeToProject");
				baseDir = project.BaseDirectory;
			} else if (relativeToPath != null) {
				baseDir = relativeToPath;
			}
			SetPropertyValue (MonoDevelop.Projects.Formats.MSBuild.MSBuildProjectService.ToMSBuildPath (baseDir, value), false);
		}

		public void SetValue (bool value, bool mergeToMainGroup = false)
		{
			SetPropertyValue (value ? "True" : "False", false);
		}

		public string GetValue ()
		{
			return GetPropertyValue ();
		}

		public XElement GetXmlValue ()
		{
			var val = GetPropertyValue ();
			if (val == null)
				return null;
			return XElement.Parse (val);
		}

		public FilePath GetPathValue (bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
		{
			var val = GetPropertyValue ();
			string baseDir = null;
			if (relativeToProject) {
				if (relativeToPath != default(FilePath))
					throw new ArgumentException ("relativeToPath argument can't be used together with relativeToProject");
				baseDir = project.BaseDirectory;
			} else if (relativeToPath != null) {
				baseDir = relativeToPath;
			}
			return MonoDevelop.Projects.Formats.MSBuild.MSBuildProjectService.FromMSBuildPath (baseDir, val);
		}

		public bool TryGetPathValue (out FilePath value, bool relativeToProject = true, FilePath relativeToPath = default(FilePath))
		{
			var val = GetPropertyValue ();
			string baseDir = null;
			if (relativeToProject) {
				if (relativeToPath != default(FilePath))
					throw new ArgumentException ("relativeToPath argument can't be used together with relativeToProject");
				baseDir = project.BaseDirectory;
			} else if (relativeToPath != null) {
				baseDir = relativeToPath;
			}
			string path;
			var res = MonoDevelop.Projects.Formats.MSBuild.MSBuildProjectService.FromMSBuildPath (baseDir, val, out path);
			value = path;
			return res;
		}

		public bool GetBoolValue ()
		{
			var val = GetPropertyValue ();
			return val.Equals ("true", StringComparison.InvariantCultureIgnoreCase);
		}

		protected abstract void SetPropertyValue (string value, bool isXml);
		protected abstract string GetPropertyValue ();
	}

	public abstract class MSBuildItem: MSBuildPropertySet
	{
		MSBuildProject project;

		internal MSBuildItem (MSBuildProject project): base (project)
		{
			this.project = project;
		}

		public abstract string Include { get; }

		public abstract string UnevaluatedInclude { get; }

		public abstract string Name { get; }
	}

	public interface IMSBuildDataObject
	{
		void Read (IMSBuildPropertySet pset, MonoDevelop.Projects.Formats.MSBuild.MSBuildFileFormat format);
		void Write (IMSBuildPropertySet pset, MonoDevelop.Projects.Formats.MSBuild.MSBuildFileFormat format);
	}
}