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

MSBuildProjectService.cs « MonoDevelop.Projects.MSBuild « MonoDevelop.Core « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2874912c73a2c2ae88e8b5ae856126e7c65ec1e (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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
// MSBuildProjectService.cs
//
// Author:
//   Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.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 System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Mono.Addins;
using MonoDevelop.Core;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Core.ProgressMonitoring;
using MonoDevelop.Core.Serialization;
using MonoDevelop.Projects.Extensions;

namespace MonoDevelop.Projects.MSBuild
{
	public static class MSBuildProjectService
	{
		internal const string ItemTypesExtensionPath = "/MonoDevelop/ProjectModel/MSBuildItemTypes";
		internal const string ImportRedirectsExtensionPath = "/MonoDevelop/ProjectModel/MSBuildImportRedirects";
		internal const string GlobalPropertyProvidersExtensionPath = "/MonoDevelop/ProjectModel/MSBuildGlobalPropertyProviders";
		internal const string UnknownMSBuildProjectTypesExtensionPath = "/MonoDevelop/ProjectModel/UnknownMSBuildProjectTypes";
		internal const string MSBuildProjectItemTypesPath = "/MonoDevelop/ProjectModel/MSBuildProjectItemTypes";
		internal const string MSBuildImportSearchPathsPath = "/MonoDevelop/ProjectModel/MSBuildImportSearchPaths";

		public const string GenericItemGuid = "{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}";
		public const string FolderTypeGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";

		[Obsolete ("This no longer has any purpose")]
		public const string DefaultFormat = "MSBuild12";

		internal const string ToolsVersion = "15.0";

		static DataContext dataContext;

		static IMSBuildGlobalPropertyProvider [] globalPropertyProviders;
		static Dictionary<string, Type> genericProjectTypes = new Dictionary<string, Type> ();
		static Dictionary<string, string> importRedirects = new Dictionary<string, string> ();
		static UnknownProjectTypeNode [] unknownProjectTypeNodes;
		static IDictionary<string, TypeExtensionNode> projecItemTypeNodes;
		static ImportSearchPathExtensionNode [] searchPathNodes;

		static Dictionary<TargetRuntime, List<ImportSearchPathExtensionNode>> defaultImportSearchPaths = new Dictionary<TargetRuntime, List<ImportSearchPathExtensionNode>> ();
		static List<ImportSearchPathExtensionNode> importSearchPaths = new List<ImportSearchPathExtensionNode> ();

		internal static bool ShutDown { get; private set; }

		static ExtensionNode [] itemTypeNodes;

		/// <summary>
		/// Occurs when there is a change in any global property provider
		/// </summary>
		internal static event EventHandler GlobalPropertyProvidersChanged;

		/// <summary>
		/// Occurs when import search paths change.
		/// </summary>
		public static event EventHandler ImportSearchPathsChanged;

		public static DataContext DataContext {
			get {
				if (dataContext == null) {
					dataContext = new MSBuildDataContext ();
					Services.ProjectService.InitializeDataContext (dataContext);
				}
				return dataContext;
			}
		}

		internal static IEnumerable<IMSBuildGlobalPropertyProvider> GlobalPropertyProviders {
			get {
				if (customGlobalPropertyProviders != null)
					return globalPropertyProviders.Concat (customGlobalPropertyProviders);
				else
					return globalPropertyProviders;
			}
		}

		static MSBuildProjectService ()
		{
			Services.ProjectService.DataContextChanged += delegate {
				dataContext = null;
			};

			PropertyService.PropertyChanged += HandlePropertyChanged;
			DefaultMSBuildVerbosity = Runtime.Preferences.MSBuildVerbosity;

			Runtime.ShuttingDown += (sender, e) => ShutDown = true;

			AddinManager.ExtensionChanged += OnExtensionChanged;
			LoadExtensionData ();

			specialCharactersEscaped = new Dictionary<char, string> (specialCharacters.Length);
			specialCharactersUnescaped = new Dictionary<string, char> (specialCharacters.Length);
			for (int i = 0; i < specialCharacters.Length; ++i) {
				var escaped = ((int)specialCharacters [i]).ToString ("X");
				specialCharactersEscaped [specialCharacters [i]] = '%' + escaped;
				specialCharactersUnescaped [escaped] = specialCharacters [i];
			}

			SetupDotNetCore ();
		}

		static void SetupDotNetCore ()
		{
			if (Platform.IsWindows)
				return;

			// Add .NET Core root directory to PATH. Without this, the MSBuild SDK resolver doesn't work.

			string dotnetDir;
			if (Platform.IsMac)
				dotnetDir = "/usr/local/share/dotnet";
			else
				dotnetDir = "/usr/share/dotnet";
		
			var systemPath = Environment.GetEnvironmentVariable ("PATH");
			if (!systemPath.Split (new char [] { ':' }).Contains (dotnetDir))
				Environment.SetEnvironmentVariable ("PATH", systemPath + ":" + dotnetDir);
		}
	

		static void OnExtensionChanged (object sender, ExtensionEventArgs args)
		{
			if (args.Path == ItemTypesExtensionPath || 
				args.Path == ImportRedirectsExtensionPath || 
				args.Path == UnknownMSBuildProjectTypesExtensionPath || 
				args.Path == GlobalPropertyProvidersExtensionPath ||
				args.Path == MSBuildProjectItemTypesPath ||
				args.Path == MSBuildImportSearchPathsPath)
				LoadExtensionData ();

			if (args.Path == MSBuildImportSearchPathsPath)
				OnImportSearchPathsChanged ();
		}

		static void LoadExtensionData ()
		{
			// Get global property providers

			if (globalPropertyProviders != null) {
				foreach (var gpp in globalPropertyProviders)
					gpp.GlobalPropertiesChanged -= HandleGlobalPropertyProviderChanged;
			}

			globalPropertyProviders = AddinManager.GetExtensionObjects<IMSBuildGlobalPropertyProvider> (GlobalPropertyProvidersExtensionPath);

			foreach (var gpp in globalPropertyProviders)
				gpp.GlobalPropertiesChanged += HandleGlobalPropertyProviderChanged;

			// Get item type nodes

			itemTypeNodes = AddinManager.GetExtensionNodes<ExtensionNode> (ItemTypesExtensionPath).Concat (Enumerable.Repeat (GenericItemNode.Instance,1)).ToArray ();

			// Get import redirects

			var newImportRedirects = new Dictionary<string, string> ();
			foreach (ImportRedirectTypeNode node in AddinManager.GetExtensionNodes (ImportRedirectsExtensionPath))
				newImportRedirects [node.Project] = node.Addin.GetFilePath (node.Target);
			importRedirects = newImportRedirects;

			// Unknown project type information

			unknownProjectTypeNodes = AddinManager.GetExtensionNodes<UnknownProjectTypeNode> (UnknownMSBuildProjectTypesExtensionPath).ToArray ();

			projecItemTypeNodes = AddinManager.GetExtensionNodes<TypeExtensionNode> (MSBuildProjectItemTypesPath).ToDictionary (e => e.TypeName);

			searchPathNodes = AddinManager.GetExtensionNodes<ImportSearchPathExtensionNode> (MSBuildImportSearchPathsPath).ToArray();
		}

		static Dictionary<string,Type> customProjectItemTypes = new Dictionary<string,Type> ();

		internal static void RegisterCustomProjectItemType (string name, Type type)
		{
			customProjectItemTypes [name] = type;
		}

		internal static void UnregisterCustomProjectItemType (string name)
		{
			customProjectItemTypes.Remove (name);
		}

		static List<IMSBuildGlobalPropertyProvider> customGlobalPropertyProviders;

		internal static void RegisterGlobalPropertyProvider (IMSBuildGlobalPropertyProvider provider)
		{
			if (customGlobalPropertyProviders == null)
				customGlobalPropertyProviders = new List<IMSBuildGlobalPropertyProvider> ();
			customGlobalPropertyProviders.Add (provider);
		}

		internal static void UnregisterGlobalPropertyProvider (IMSBuildGlobalPropertyProvider provider)
		{
			if (customGlobalPropertyProviders != null)
				customGlobalPropertyProviders.Remove (provider);
		}

		/// <summary>
		/// Registers a custom project import search path. This path will be used as a fallback when evaluating
		/// an import and targets file is not found using the value assigned by MSBuild to the property.
		/// </summary>
		/// <param name="propertyName">Name of the property for which to add a fallback path</param>
		/// <param name="path">The fallback path</param>
		public static void RegisterProjectImportSearchPath (string propertyName, FilePath path)
		{
			if (!importSearchPaths.Any (sp => sp.Property == propertyName && sp.Path == path)) {
				importSearchPaths.Add (new ImportSearchPathExtensionNode { Property = propertyName, Path = path });
				OnImportSearchPathsChanged ();
			}
		}

		/// <summary>
		/// Unregisters a previously registered import search path
		/// </summary>
		/// <param name="propertyName">Name of the property for which a fallback path was added.</param>
		/// <param name="path">The fallback path to remove</param>
		public static void UnregisterProjectImportSearchPath (string propertyName, FilePath path)
		{
			importSearchPaths.RemoveAll (i => i.Property == propertyName && i.Path == path);
			OnImportSearchPathsChanged ();
		}

		/// <summary>
		/// Gets a list of all search paths assigned to properties
		/// </summary>
		/// <returns>The search paths</returns>
		/// <param name="runtime">Runtime for which to get the search paths.</param>
		/// <param name="includeImplicitImports">If set to <c>true</c>, it returns all search paths, including those registered by
		/// MSBuild and those registered using RegisterProjectImportSearchPath. If <c>false</c>, it only returns the paths
		/// registered by RegisterProjectImportSearchPath.</param>
		internal static IEnumerable<ImportSearchPathExtensionNode> GetProjectImportSearchPaths (TargetRuntime runtime, bool includeImplicitImports)
		{
			var result = searchPathNodes.Concat (importSearchPaths);
			if (includeImplicitImports)
				result = LoadDefaultProjectImportSearchPaths (runtime).Concat (result);
			return result;
		}

		internal static string GetDefaultSdksPath (TargetRuntime runtime)
		{
			return Path.Combine (GetMSBuildBinPath (runtime), "Sdks");
		}

		internal static IEnumerable<SdkInfo> FindRegisteredSdks ()
		{
			foreach (var node in GetProjectImportSearchPaths (null, false).Where (n => n.Property == "MSBuildSDKsPath")) {
				if (Directory.Exists (node.Path)) {
					foreach (var dir in Directory.GetDirectories (node.Path)) {
						if (File.Exists (Path.Combine (dir, "Sdk", "Sdk.props")))
							yield return new SdkInfo (Path.GetFileName (dir), null, Path.Combine (dir, "Sdk"));
					}
				}
			}
		}

		static List<ImportSearchPathExtensionNode> LoadDefaultProjectImportSearchPaths (TargetRuntime runtime)
		{
			// Load the default search paths defined in MSBuild.dll.config

			lock (defaultImportSearchPaths) {
				List<ImportSearchPathExtensionNode> list;
				if (defaultImportSearchPaths.TryGetValue (runtime, out list))
					return list;

				list = new List<ImportSearchPathExtensionNode> ();
				defaultImportSearchPaths [runtime] = list;

				string binDir = GetMSBuildBinPath (runtime);

				var configFileName = Platform.IsWindows ? "MSBuild.exe.config" : "MSBuild.dll.config";
				var configFile = Path.Combine (binDir, configFileName);
				if (File.Exists (configFile)) {
					var doc = XDocument.Load (configFile);
					var projectImportSearchPaths = doc.Root.Elements ("msbuildToolsets").FirstOrDefault ()?.Elements ("toolset")?.FirstOrDefault ()?.Element ("projectImportSearchPaths");
					if (projectImportSearchPaths != null) {
						var os = Platform.IsMac ? "osx" : Platform.IsWindows ? "windows" : "unix";
						foreach (var searchPaths in projectImportSearchPaths.Elements ("searchPaths")) {
							var pathOs = (string)searchPaths.Attribute ("os")?.Value;
							if (!string.IsNullOrEmpty (pathOs) && pathOs != os)
								continue;
							foreach (var property in searchPaths.Elements ("property"))
								list.Add (new ImportSearchPathExtensionNode { Property = property.Attribute ("name").Value, Path = property.Attribute ("value").Value });
						}
					}
				}
				return list;
			}
		}

		static void OnImportSearchPathsChanged ()
		{
			ImportSearchPathsChanged?.Invoke (null, EventArgs.Empty);
		}

		static void HandleGlobalPropertyProviderChanged (object sender, EventArgs e)
		{
			GlobalPropertyProvidersChanged?.Invoke (null, EventArgs.Empty);
		}

		static void HandlePropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			if (e.Key == "MonoDevelop.Ide.MSBuildVerbosity") {
				DefaultMSBuildVerbosity = (MSBuildVerbosity) e.NewValue;
			}
		}

		internal static MSBuildVerbosity DefaultMSBuildVerbosity { get; private set; }

		public static bool IsTargetsAvailable (string targetsPath)
		{
			if (string.IsNullOrEmpty (targetsPath))
				return false;

			string msbuild = Runtime.SystemAssemblyService.CurrentRuntime.GetMSBuildExtensionsPath ();
			Dictionary<string, string> variables = new Dictionary<string, string> (StringComparer.OrdinalIgnoreCase) {
				{ "MSBuildExtensionsPath64", msbuild },
				{ "MSBuildExtensionsPath32", msbuild },
				{ "MSBuildExtensionsPath",   msbuild }
			};

			string path = StringParserService.Parse (targetsPath, variables);
			if (Path.DirectorySeparatorChar != '\\')
				path = path.Replace ('\\', Path.DirectorySeparatorChar);

			return File.Exists (path);
		}

		/// <summary>
		/// Given a project referenced in an Import, returns a project that should be loaded instead, or null if there is no redirect
		/// </summary>
		internal static string GetImportRedirect (string project)
		{
			string target;
			if (importRedirects.TryGetValue (project, out target))
				return target;
			return null;
		}

		/// <summary>
		/// Loads a solution item
		/// </summary>
		/// <returns>The item.</returns>
		/// <param name="monitor">Progress monitor</param>
		/// <param name="fileName">File path to the item file</param>
		/// <param name="expectedFormat">File format that the project should have</param>
		/// <param name="typeGuid">Optional item type GUID. If not provided, the type is guessed from the file extension.</param>
		/// <param name="itemGuid">Optional item Id</param>
		/// <param name="ctx">Optional solution context</param>
		public async static Task<SolutionItem> LoadItem (ProgressMonitor monitor, string fileName, MSBuildFileFormat expectedFormat, string typeGuid, string itemGuid, SolutionLoadContext ctx)
		{
			SolutionItem item = null;

			// Find an extension node that can handle this item type
			var node = GetItemTypeNodes ().FirstOrDefault (n => n.CanHandleFile (fileName, typeGuid));

			if (node != null) {
				item = await node.CreateSolutionItem (monitor, ctx, fileName).ConfigureAwait (false);
				if (item == null)
					return null;
			}

			if (item == null) {
				// If it is a known unsupported project, load it as UnknownProject
				item = CreateUnknownSolutionItem (monitor, fileName, typeGuid, typeGuid, ctx);
				if (item == null)
					return null;
			}

			item.EnsureInitialized ();

			item.BeginLoad ();
			ctx.LoadCompleted += delegate {
				item.EndLoad ();
				item.NotifyItemReady ();
			};

			await item.LoadAsync (monitor, fileName, expectedFormat, itemGuid).ConfigureAwait (false);
			return item;
		}

		internal static SolutionItem CreateUnknownSolutionItem (ProgressMonitor monitor, string fileName, string typeGuid, string unknownTypeGuid, SolutionLoadContext ctx)
		{
			bool loadAsProject = false;
			string unsupportedMessage;

			var relPath = ctx != null && ctx.Solution != null ? new FilePath (fileName).ToRelative (ctx.Solution.BaseDirectory).ToString() : new FilePath (fileName).FileName;
			var guids = !string.IsNullOrEmpty (unknownTypeGuid) ? unknownTypeGuid.Split (new char[] {';'}, StringSplitOptions.RemoveEmptyEntries) : new string[0];

			if (!string.IsNullOrEmpty (unknownTypeGuid)) {
				var projectInfo = MSBuildProjectService.GetUnknownProjectTypeInfo (guids, fileName);
				if (projectInfo != null) {
					loadAsProject = projectInfo.LoadFiles;
					unsupportedMessage = projectInfo.GetInstructions ();
					LoggingService.LogWarning (string.Format ("Could not load {0} project '{1}'. {2}", projectInfo.Name, relPath, projectInfo.GetInstructions ()));
					if (!loadAsProject)
						monitor.ReportWarning (GettextCatalog.GetString ("Could not load {0} project '{1}'. {2}", projectInfo.Name, relPath, projectInfo.GetInstructions ()));
				} else {
					unsupportedMessage = GettextCatalog.GetString ("Unknown project type: {0}", unknownTypeGuid);
					LoggingService.LogWarning (string.Format ("Could not load project '{0}' with unknown item type '{1}'", relPath, unknownTypeGuid));
					monitor.ReportWarning (GettextCatalog.GetString ("Could not load project '{0}' with unknown item type '{1}'", relPath, unknownTypeGuid));
					return null;
				}
			} else {
				unsupportedMessage = GettextCatalog.GetString ("Unknown project type");
				LoggingService.LogWarning (string.Format ("Could not load project '{0}' with unknown item type", relPath));
				monitor.ReportWarning (GettextCatalog.GetString ("Could not load project '{0}' with unknown item type", relPath));
			}
			if (loadAsProject) {
				var project = (Project) CreateUninitializedInstance (typeof(UnknownProject));
				project.UnsupportedProjectMessage = unsupportedMessage;
				project.SetCreationContext (Project.CreationContext.Create (typeGuid, new string[0]));
				return project;
			} else
				return null;
		}


		/// <summary>
		/// Creates an uninitialized solution item instance
		/// </summary>
		/// <param name="type">Solution item type</param>
		/// <remarks>
		/// Some subclasses (such as ProjectTypeNode) need to assign some data to
		/// the object before it is initialized. However, by default initialization
		/// is automatically made by the constructor, so to support this scenario
		/// the initialization has to be delayed. This is done by setting the
		/// MonoDevelop.DelayItemInitialization logical context property.
		/// When this property is set, the object is not initialized, and it has
		/// to be manually initialized by calling EnsureInitialized.
		/// </remarks>
		internal static SolutionItem CreateUninitializedInstance (Type type)
		{
			try {
				System.Runtime.Remoting.Messaging.CallContext.LogicalSetData ("MonoDevelop.DelayItemInitialization", true);
				return (SolutionItem)Activator.CreateInstance (type, true);
			} finally {
				System.Runtime.Remoting.Messaging.CallContext.LogicalSetData ("MonoDevelop.DelayItemInitialization", false);
			}
		}

		internal static bool CanCreateSolutionItem (string type, ProjectCreateInformation info, System.Xml.XmlElement projectOptions)
		{
			type = ConvertTypeAliasToGuid (type);

			foreach (var node in GetItemTypeNodes ()) {
				if (node.CanCreateSolutionItem (type, info, projectOptions))
					return true;
			}
			return false;
		}

		internal static Project CreateProject (string typeGuid, params string[] flavorGuids)
		{
			flavorGuids = ConvertTypeAliasesToGuids (flavorGuids);

			foreach (var node in GetItemTypeNodes ().OfType<ProjectTypeNode> ()) {
				if (node.Guid.Equals (typeGuid, StringComparison.OrdinalIgnoreCase) || node.TypeAlias == typeGuid) {
					var p = node.CreateProject (flavorGuids);
					p.EnsureInitialized ();
					p.NotifyItemReady ();
					return p;
				}
			}
			throw new InvalidOperationException ("Unknown project type: " + typeGuid);
		}

		internal static bool CanCreateProject (string typeGuid, params string[] flavorGuids)
		{
			return IsKnownTypeGuid (ConvertTypeAliasToGuid (typeGuid)) && ConvertTypeAliasesToGuids (flavorGuids).All (IsKnownFlavorGuid);
		}

		internal static SolutionItem CreateSolutionItem (string type, ProjectCreateInformation info, System.Xml.XmlElement projectOptions)
		{
			type = ConvertTypeAliasToGuid (type);

			foreach (var node in GetItemTypeNodes ()) {
				if (node.CanCreateSolutionItem (type, info, projectOptions)) {
					var item = node.CreateSolutionItem (type, info, projectOptions);
					item.NotifyItemReady ();
					return item;
				}
			}
			throw new InvalidOperationException ("Unknown project type: " + type);
		}

		internal static Project CreateProject (string typeGuid, ProjectCreateInformation info, System.Xml.XmlElement projectOptions, params string[] flavorGuids)
		{
			flavorGuids = ConvertTypeAliasesToGuids (flavorGuids);

			foreach (var node in GetItemTypeNodes ().OfType<ProjectTypeNode> ()) {
				if (node.Guid.Equals (typeGuid, StringComparison.OrdinalIgnoreCase) || typeGuid == node.TypeAlias) {
					var p = node.CreateProject (flavorGuids);
					p.EnsureInitialized ();
					p.InitializeFromTemplate (info, projectOptions);
					p.NotifyItemReady ();
					return p;
				}
			}
			throw new InvalidOperationException ("Unknown project type: " + typeGuid);
		}

		public  static string ConvertTypeAliasToGuid (string type)
		{
			var node = GetItemTypeNodes ().FirstOrDefault (n => n.TypeAlias == type);
			if (node != null)
				return node.Guid;
			var enode = WorkspaceObject.GetModelExtensions (null).OfType<SolutionItemExtensionNode> ().FirstOrDefault (n => n.TypeAlias == type);
			if (enode != null)
				return enode.Guid;
			return type;
		}

		public static string[] ConvertTypeAliasesToGuids (string[] types)
		{
			string[] copy = null;
			for (int n=0; n<types.Length; n++) {
				var nt = ConvertTypeAliasToGuid (types[n]);
				if (nt != types[n]) {
					if (copy == null)
						copy = types.ToArray ();
					copy [n] = nt;
				}
			}
			return copy ?? types;
		}

		internal static bool GetMSBuildSupportForFlavors (IEnumerable<string> flavorGuids)
		{
			#pragma warning disable 612
			foreach (var fid in flavorGuids) {
				var node = WorkspaceObject.GetModelExtensions (null).OfType<SolutionItemExtensionNode> ().FirstOrDefault (n => n.Guid != null && n.Guid.Equals (fid, StringComparison.InvariantCultureIgnoreCase));
				if (node != null) {
					if (node.MSBuildSupport == MSBuildSupport.NotSupported)
						return false;
				} else if (!IsKnownTypeGuid (fid))
					throw new UnknownSolutionItemTypeException (fid);
			}
			return true;
			#pragma warning restore 612
		}

		internal static List<SolutionItemExtensionNode> GetMigrableFlavors (string[] flavorGuids)
		{
			var list = new List<SolutionItemExtensionNode> ();
			foreach (var fid in flavorGuids) {
				foreach (var node in WorkspaceObject.GetModelExtensions (null).OfType<SolutionItemExtensionNode> ()) {
					if (node.SupportsMigration && node.Guid != null && node.Guid.Equals (fid, StringComparison.InvariantCultureIgnoreCase))
						list.Add (node);
				}
			}
			return list;
		}

		internal static async Task MigrateFlavors (ProgressMonitor monitor, string fileName, string typeGuid, MSBuildProject p, List<SolutionItemExtensionNode> nodes)
		{
			var language = GetLanguageFromGuid (typeGuid);

			bool migrated = false;

			foreach (var node in nodes) {
				if (await MigrateProject (monitor, node, p, fileName, language))
					migrated = true;
			}
			if (migrated)
				await p.SaveAsync (fileName);
		}

		static async Task<bool> MigrateProject (ProgressMonitor monitor, SolutionItemExtensionNode st, MSBuildProject p, string fileName, string language)
		{
			var projectLoadMonitor = GetProjectLoadProgressMonitor (monitor);
			if (projectLoadMonitor == null) {
				// projectLoadMonitor will be null when running through md-tool, but
				// this is not fatal if migration is not required, so just ignore it. --abock
				if (!st.IsMigrationRequired)
					return false;

				LoggingService.LogError (Environment.StackTrace);
				monitor.ReportError (GettextCatalog.GetString ("Could not open unmigrated project and no migrator was supplied"), null);
				throw new UserException (GettextCatalog.GetString ("Project migration failed"));
			}
			
			var migrationType = st.MigrationHandler.CanPromptForMigration
				? await st.MigrationHandler.PromptForMigration (projectLoadMonitor, p, fileName, language)
				: projectLoadMonitor.ShouldMigrateProject ();
			if (migrationType == MigrationType.Ignore) {
				if (st.IsMigrationRequired) {
					monitor.ReportError (GettextCatalog.GetString ("{1} cannot open the project '{0}' unless it is migrated.", Path.GetFileName (fileName), BrandingService.ApplicationName), null);
					throw new UserException (GettextCatalog.GetString ("The user choose not to migrate the project"));
				} else
					return false;
			}
			
			var baseDir = (FilePath) Path.GetDirectoryName (fileName);
			if (migrationType == MigrationType.BackupAndMigrate) {
				var backupDirFirst = baseDir.Combine ("backup");
				string backupDir = backupDirFirst;
				int i = 0;
				while (Directory.Exists (backupDir)) {
					backupDir = backupDirFirst + "-" + i.ToString ();
					if (i++ > 20) {
						throw new Exception ("Too many backup directories");
					}
				}
				Directory.CreateDirectory (backupDir);
				foreach (var file in st.MigrationHandler.FilesToBackup (fileName))
					File.Copy (file, Path.Combine (backupDir, Path.GetFileName (file)));
			}

			if (!await st.MigrationHandler.Migrate (projectLoadMonitor, p, fileName, language))
				throw new UserException (GettextCatalog.GetString ("Project migration failed"));

			return true;
		}

		static ProjectLoadProgressMonitor GetProjectLoadProgressMonitor (ProgressMonitor monitor)
		{
			var projectLoadMonitor = monitor as ProjectLoadProgressMonitor;
			if (projectLoadMonitor != null)
				return projectLoadMonitor;

			var aggregatedMonitor = monitor as AggregatedProgressMonitor;
			if (aggregatedMonitor != null)
				return aggregatedMonitor.LeaderMonitor as ProjectLoadProgressMonitor;

			return null;
		}

		internal static string GetLanguageGuid (string language)
		{
			foreach (var node in GetItemTypeNodes ().OfType<DotNetProjectTypeNode> ()) {
				if (node.Language == language && node.IsDefaultGuid)
					return node.Guid;
			}
			throw new InvalidOperationException ("Language not supported: " + language);
		}

		internal static string GetLanguageFromGuid (string guid)
		{
			foreach (var node in GetItemTypeNodes ().OfType<DotNetProjectTypeNode> ()) {
				if (node.Guid.Equals (guid, StringComparison.OrdinalIgnoreCase))
					return node.Language;
			}
			throw new InvalidOperationException ("Language not supported: " + guid);
		}

		internal static bool IsKnownFlavorGuid (string guid)
		{
			return WorkspaceObject.GetModelExtensions (null).OfType<SolutionItemExtensionNode> ().Any (n => n.Guid != null && n.Guid.Equals (guid, StringComparison.InvariantCultureIgnoreCase));
		}

		internal static bool IsKnownTypeGuid (string guid)
		{
			foreach (var node in GetItemTypeNodes ()) {
				if (node.Guid.Equals (guid, StringComparison.OrdinalIgnoreCase))
					return true;
			}
			return false;
		}

		internal static string GetTypeGuidFromAlias (string alias)
		{
			foreach (var node in GetItemTypeNodes ()) {
				if (node.TypeAlias.Equals (alias, StringComparison.OrdinalIgnoreCase))
					return node.Guid;
			}
			return null;
		}

		[Obsolete]
		public static void CheckHandlerUsesMSBuildEngine (SolutionFolderItem item, out bool useByDefault, out bool require)
		{
			useByDefault = require = item is Project proj && proj.MSBuildProject.UseMSBuildEngine;
		}

		static IEnumerable<SolutionItemTypeNode> GetItemTypeNodes ()
		{
			foreach (ExtensionNode node in itemTypeNodes) {
				if (node is SolutionItemTypeNode)
					yield return (SolutionItemTypeNode) node;
			}
			foreach (var node in customNodes)
				yield return node;
		}

		static List<SolutionItemTypeNode> customNodes = new List<SolutionItemTypeNode> ();

		internal static void RegisterCustomItemType (SolutionItemTypeNode node)
		{
			customNodes.Add (node);
		}
		
		internal static void UnregisterCustomItemType (SolutionItemTypeNode node)
		{
			customNodes.Remove (node);
		}

		internal static bool CanReadFile (FilePath file)
		{
			foreach (SolutionItemTypeNode node in GetItemTypeNodes ()) {
				if (node.CanHandleFile (file, null)) {
					return true;
				}
			}
			return GetUnknownProjectTypeInfo (new string[0], file) != null;
		}
		
		internal static string GetExtensionForItem (SolutionItem item)
		{
			foreach (SolutionItemTypeNode node in GetItemTypeNodes ()) {
				if (node.Guid.Equals (item.TypeGuid, StringComparison.OrdinalIgnoreCase))
					return node.Extension;
			}
			// The generic handler should always be found
			throw new InvalidOperationException ();
		}

		internal static string GetTypeGuidForItem (SolutionItem item)
		{
			var className = item.GetType ().FullName;
			foreach (SolutionItemTypeNode node in GetItemTypeNodes ()) {
				if (node.ItemTypeName == className)
					return node.Guid;
			}
			return GenericItemGuid;
		}

		internal static bool UseMSBuildEngineForProject (Project project)
		{
			#pragma warning disable 612
			if (project is UnknownProject || project is GenericProject)
				return false;
			
			foreach (var node in GetItemTypeNodes ().OfType<ProjectTypeNode> ()) {
				if (node.Guid.Equals (project.TypeGuid, StringComparison.OrdinalIgnoreCase)) {
					if (node.MSBuildSupport == MSBuildSupport.NotSupported)
						return false;
					return GetMSBuildSupportForFlavors (project.FlavorGuids);
				}
			}
			return false;
			#pragma warning restore 612
		}

		public static void RegisterGenericProjectType (string projectId, Type type)
		{
			lock (genericProjectTypes) {
				if (!typeof(Project).IsAssignableFrom (type))
					throw new ArgumentException ("Type is not a subclass of MonoDevelop.Projects.Project");
				genericProjectTypes [projectId] = type;
			}
		}

		internal static Task<SolutionItem> CreateGenericProject (string file)
		{
			if (file == null)
				return Task.FromResult<SolutionItem> (new GenericProject ());

			// Unknown project types are already displayed in the solution view, we don't need to tell the user with a modal dialog as well
			return Task<SolutionItem>.Factory.StartNew (delegate {
				var t = ReadGenericProjectType (file);
				if (t == null)
					throw new UnknownSolutionItemTypeException (GettextCatalog.GetString ("Unknown project type"));

				var dt = Services.ProjectService.DataContext.GetConfigurationDataType (t);
				if (dt != null) {
					if (!typeof (Project).IsAssignableFrom (dt.ValueType))
						throw new UnknownSolutionItemTypeException (GettextCatalog.GetString ("Unknown project type: {0}", t));

					return (SolutionItem)Activator.CreateInstance (dt.ValueType);
				}

				Type type;
				lock (genericProjectTypes) {
					if (!genericProjectTypes.TryGetValue (t, out type))
						throw new UnknownSolutionItemTypeException (GettextCatalog.GetString ("Unknown project type: {0}", t));
				}
				return (SolutionItem)Activator.CreateInstance (type);
			});
		}

		static string ReadGenericProjectType (string file)
		{
			using (XmlTextReader tr = new XmlTextReader (file)) {
				tr.MoveToContent ();
				if (tr.LocalName != "Project")
					return null;
				if (tr.IsEmptyElement)
					return null;
				tr.ReadStartElement ();
				tr.MoveToContent ();
				while (tr.LocalName != "PropertyGroup" && !tr.EOF) {
					tr.Skip ();
					tr.MoveToContent ();
				}
				if (tr.LocalName != "PropertyGroup")
					return null;
				if (tr.IsEmptyElement)
					return null;
				tr.ReadStartElement ();
				tr.MoveToContent ();
				while (tr.NodeType != XmlNodeType.EndElement) {
					if (tr.NodeType == XmlNodeType.Element && !tr.IsEmptyElement && tr.LocalName == "ItemType")
						return tr.ReadElementString ();
					tr.Skip ();
				}
				return null;
			}
		}
		
		static char[] specialCharacters = new char [] {'%', '$', '@', '(', ')', '\'', ';', '?' };
		static Dictionary<char, string> specialCharactersEscaped;
		static Dictionary<string, char> specialCharactersUnescaped;
		
		public static string EscapeString (string str)
		{
			int i = str.IndexOfAny (specialCharacters);
			if (i != -1) {
				var sb = new System.Text.StringBuilder ();
				int start = 0;
				while (i != -1) {
					sb.Append (str, start, i - start);
					sb.Append (specialCharactersEscaped [str [i]]);
					if (i >= str.Length)
						break;
					start = i + 1;
					i = str.IndexOfAny (specialCharacters, start);
				}
				if (start < str.Length)
					sb.Append (str, start, str.Length - start);
				return sb.ToString ();
			}
			return str;
		}

		public static string UnescapePath (string path)
		{
			if (string.IsNullOrEmpty (path))
				return path;
			
			if (!Platform.IsWindows)
				path = path.Replace ("\\", "/");
			
			return UnscapeString (path);
		}
		
		public static string UnscapeString (string str)
		{
			int i = str.IndexOf ('%');
			if (i != -1) {
				var sb = new System.Text.StringBuilder ();
				int start = 0;
				while (i != -1) {
					int c;
					char ch;
					var sub = str.Substring (i + 1, 2);
					if (specialCharactersUnescaped.TryGetValue (sub, out ch)) {
						sb.Append (str, start, i - start);
						sb.Append (ch);
					} else if (int.TryParse (sub, NumberStyles.HexNumber, null, out c)) {
						sb.Append (str, start, i - start);
						sb.Append ((char)c);
					}
					start = i + 3;
					i = str.IndexOf ('%', start);
				}
				sb.Append (str, start, str.Length - start);
				return sb.ToString ();
			}
			return str;
		}
		
		public static string ToMSBuildPath (string baseDirectory, string absPath, bool normalize = true)
		{
			if (string.IsNullOrEmpty (absPath))
				return absPath;
			if (baseDirectory != null) {
				absPath = FileService.AbsoluteToRelativePath (baseDirectory, absPath);
				if (normalize)
					absPath = FileService.NormalizeRelativePath (absPath);
			}
			return EscapeString (absPath).Replace ('/', '\\');
		}
		
		internal static string ToMSBuildPathRelative (string baseDirectory, string absPath)
		{
			FilePath file = ToMSBuildPath (baseDirectory, absPath);
			return file.ToRelative (baseDirectory);
		}

		
		internal static string FromMSBuildPathRelative (string basePath, string relPath)
		{
			FilePath file = FromMSBuildPath (basePath, relPath);
			return file.ToRelative (basePath);
		}

		public static string FromMSBuildPath (string basePath, string relPath)
		{
			string res;
			FromMSBuildPath (basePath, relPath, out res);
			return res;
		}
		
		internal static bool IsAbsoluteMSBuildPath (string path)
		{
			if (path.Length > 1 && char.IsLetter (path [0]) && path[1] == ':')
				return true;
			if (path.Length > 0 && path [0] == '\\')
				return true;
			return false;
		}
		
		internal static bool FromMSBuildPath (string basePath, string relPath, out string resultPath)
		{
			resultPath = relPath;
			
			if (string.IsNullOrEmpty (relPath))
				return false;
			
			string path = UnescapePath (relPath);

			if (char.IsLetter (path [0]) && path.Length > 1 && path[1] == ':') {
				if (Platform.IsWindows) {
					resultPath = path; // Return the escaped value
					return true;
				} else
					return false;
			}
			
			bool isRooted = Path.IsPathRooted (path);
			
			if (!isRooted && basePath != null) {
				path = Path.Combine (basePath, path);
				isRooted = Path.IsPathRooted (path);
			}
			
			// Return relative paths as-is, we can't do anything else with them
			if (!isRooted) {
				resultPath = FileService.NormalizeRelativePath (path);
				return true;
			}
			
			// If we're on Windows, don't need to fix file casing.
			//if (Platform.IsWindows) {
				resultPath = FileService.GetFullPath (path);
				return true;
			//}

			// Code below and IF above are commented because after we replaced XBuild with MSBuild .targets files
			// load time of Main.sln(as example) went from 30sec to 2.5min because MSBuild .targets have many more Before/After targets tries
			// resulting in a lot of Directory.EnumerateFileSystemEntries fetching from hard drive resulting in slow load time.
			// Since MSBuild.exe doesn't handle file name case mismatches on case-sensetive file system, it doesn't make sense to do this in IDE.

			//// If the path exists with the exact casing specified, then we're done
			//if (System.IO.File.Exists (path) || System.IO.Directory.Exists (path)){
			//	resultPath = Path.GetFullPath (path);
			//	return true;
			//}

			//// Not on Windows, file doesn't exist. That could mean the project was brought from Windows
			//// and the filename case in the project doesn't match the file on disk, because Windows is
			//// case-insensitive. Since we have an absolute path, search the directory for the file with
			//// the correct case.
			//string[] names = path.Substring (1).Split ('/');
			//string part = "/";
			
			//for (int n=0; n<names.Length; n++) {
			//	IEnumerable<string> entries;

			//	if (names [n] == ".."){
			//		if (part == "/")
			//			return false; // Can go further back. It's not an existing file
			//		part = Path.GetFullPath (part + "/..");
			//		continue;
			//	}
				
			//	entries = Directory.EnumerateFileSystemEntries (part);
				
			//	string fpath = null;
			//	foreach (string e in entries) {
			//		if (string.Compare (Path.GetFileName (e), names [n], StringComparison.OrdinalIgnoreCase) == 0) {
			//			fpath = e;
			//			break;
			//		}
			//	}
			//	if (fpath == null) {
			//		// Part of the path does not exist. Can't do any more checking.
			//		part = Path.GetFullPath (part);
			//		if (n < names.Length)
			//			part += "/" + string.Join ("/", names, n, names.Length - n);
			//		resultPath = part;
			//		return true;
			//	}

			//	part = fpath;
			//}
			//resultPath = Path.GetFullPath (part);
			//return true;
		}

		//Given a filename like foo.it.resx, splits it into - foo, it, resx
		//Returns true only if a valid culture is found
		//Note: hand-written as this can get called lotsa times
		public static bool TrySplitResourceName (string fname, out string only_filename, out string culture, out string extn)
		{
			only_filename = culture = extn = null;

			int last_dot = -1;
			int culture_dot = -1;
			int i = fname.Length - 1;
			while (i >= 0) {
				if (fname [i] == '.') {
					last_dot = i;
					break;
				}
				i --;
			}
			if (i < 0)
				return false;

			i--;
			while (i >= 0) {
				if (fname [i] == '.') {
					culture_dot = i;
					break;
				}
				i --;
			}
			if (culture_dot < 0)
				return false;

			culture = fname.Substring (culture_dot + 1, last_dot - culture_dot - 1);
			if (!CultureNamesTable.ContainsKey (culture))
				return false;

			only_filename = fname.Substring (0, culture_dot);
			extn = fname.Substring (last_dot + 1);
			return true;
		}

		internal static string GetMSBuildBinPath (TargetRuntime runtime)
		{
			return runtime.GetMSBuildBinPath (ToolsVersion)
				?? throw new Exception ("Did not find MSBuild for runtime " + runtime.Id);
		}

		static Dictionary<string, string> cultureNamesTable;
		static Dictionary<string, string> CultureNamesTable {
			get {
				if (cultureNamesTable == null) {
					cultureNamesTable = new Dictionary<string, string> ();
					foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures))
						cultureNamesTable [ci.Name] = ci.Name;
				}

				return cultureNamesTable;
			}
		}

		internal static UnknownProjectTypeNode GetUnknownProjectTypeInfo (string[] guids, string fileName = null)
		{
			var ext = fileName != null ? Path.GetExtension (fileName).TrimStart ('.') : null;
			var nodes = unknownProjectTypeNodes.Where (p => guids.Any (p.MatchesGuid) || (ext != null && p.Extension == ext)).ToList ();
			return nodes.FirstOrDefault (n => !n.IsSolvable) ?? nodes.FirstOrDefault (n => n.IsSolvable);
		}

		internal static Type GetProjectItemType (string itemName)
		{
			var node = projecItemTypeNodes.Values.FirstOrDefault (e => e.Id == itemName);
			if (node != null) {
				var t = node.Addin.GetType (node.TypeName, true);
				if (!typeof(ProjectItem).IsAssignableFrom (t))
					throw new InvalidOperationException ("Project item type '" + node.TypeName + "' is not a subclass of ProjectItem");
				return t;
			}
			Type tt;
			if (customProjectItemTypes.TryGetValue (itemName, out tt))
				return tt;
			else
				return null;
		}

		internal static string GetNameForProjectItem (Type type)
		{
			TypeExtensionNode node;
			if (projecItemTypeNodes.TryGetValue (type.FullName, out node))
				return node.Id;

			var r = customProjectItemTypes.FirstOrDefault (k => k.Value == type);
			if (r.Key != null)
				return r.Key;
			return null;
		}
	}
	
	class MSBuildDataContext: DataContext
	{
		protected override DataType CreateConfigurationDataType (Type type)
		{
			if (type == typeof(bool))
				return new MSBuildBoolDataType ();
			else if (type == typeof(bool?))
				return new MSBuildNullableBoolDataType ();
			else
				return base.CreateConfigurationDataType (type);
		}
	}
	
	class MSBuildBoolDataType: PrimitiveDataType
	{
		public MSBuildBoolDataType (): base (typeof(bool))
		{
		}
		
		internal protected override DataNode OnSerialize (SerializationContext serCtx, object mapData, object value)
		{
			return new MSBuildBoolDataValue (Name, (bool) value);
		}
		
		internal protected override object OnDeserialize (SerializationContext serCtx, object mapData, DataNode data)
		{
			return String.Equals (((DataValue)data).Value, "true", StringComparison.OrdinalIgnoreCase);
		}
	}

	class MSBuildBoolDataValue : DataValue
	{
		public MSBuildBoolDataValue (string name, bool value)
			: base (name, value ? "True" : "False")
		{
			RawValue = value;
		}

		public bool RawValue { get; private set; }
	}

	class MSBuildNullableBoolDataType: PrimitiveDataType
	{
		public MSBuildNullableBoolDataType (): base (typeof(bool))
		{
		}

		internal protected override DataNode OnSerialize (SerializationContext serCtx, object mapData, object value)
		{
			return new MSBuildNullableBoolDataValue (Name, (bool?) value);
		}

		internal protected override object OnDeserialize (SerializationContext serCtx, object mapData, DataNode data)
		{
			var d = (DataValue)data;
			if (string.IsNullOrEmpty (d.Value))
				return (bool?) null;
			return (bool?) String.Equals (d.Value, "true", StringComparison.OrdinalIgnoreCase);
		}
	}

	class MSBuildNullableBoolDataValue : DataValue
	{
		public MSBuildNullableBoolDataValue (string name, bool? value)
			: base (name, value.HasValue? (value.Value? "True" : "False") : null)
		{
			RawValue = value;
		}

		public bool? RawValue { get; private set; }
	}

	class GenericItemNode : ProjectTypeNode
	{
		public static readonly GenericItemNode Instance = new GenericItemNode ();

		public GenericItemNode ()
		{
			Guid = MSBuildProjectService.GenericItemGuid;
			Extension = "mdproj";
			#pragma warning disable 612
			MSBuildSupport = MSBuildSupport.NotSupported;
			#pragma warning restore 612
			TypeAlias = "GenericProject";
		}

		public override Type ItemType {
			get {
				return typeof (GenericItemFactory);
			}
		}

		class GenericItemFactory: SolutionItemFactory
		{
			public override Task<SolutionItem> CreateItem (string fileName, string typeGuid)
			{
				return MSBuildProjectService.CreateGenericProject (fileName);
			}
		}
	}

	class BuilderCache
	{
		Dictionary<string,List<RemoteBuildEngine>> builders = new Dictionary<string, List<RemoteBuildEngine>> ();

		public void Add (string key, RemoteBuildEngine builder)
		{
			List<RemoteBuildEngine> list;
			if (!builders.TryGetValue (key, out list))
				builders [key] = list = new List<RemoteBuildEngine> ();
			list.Add (builder);
        }

		public IEnumerable<RemoteBuildEngine> GetBuilders (string key)
		{
			List<RemoteBuildEngine> list;
			if (builders.TryGetValue (key, out list))
				return list;
			else
				return Enumerable.Empty<RemoteBuildEngine> ();
		}

		public IEnumerable<RemoteBuildEngine> GetAllBuilders ()
		{
			return builders.Values.SelectMany (r => r);
		}

		public void Remove (RemoteBuildEngine builder)
		{
			foreach (var p in builders) {
				if (p.Value.Remove (builder)) {
					if (p.Value.Count == 0)
						builders.Remove (p.Key);
					return;
                }
            }
        }
	}
}