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

MSBuildFileFormat.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: d3084e8e19b57f985cf9a7b853cc9132ebb09163 (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
// MSBuildFileFormat.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.Xml;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using MonoDevelop.Core;
using MonoDevelop.Core.Assemblies;
using System.Threading.Tasks;
using System.Linq;

namespace MonoDevelop.Projects.MSBuild
{
	public abstract class MSBuildFileFormat
	{
		readonly SlnFileFormat slnFileFormat;

		internal MSBuildFileFormat ()
		{
			slnFileFormat = new SlnFileFormat (this);
		}

		public static readonly MSBuildFileFormat VS2005 = new MSBuildFileFormatVS05 ();
		public static readonly MSBuildFileFormat VS2008 = new MSBuildFileFormatVS08 ();
		public static readonly MSBuildFileFormat VS2010 = new MSBuildFileFormatVS10 ();
		public static readonly MSBuildFileFormat VS2012 = new MSBuildFileFormatVS12 ();

		[Obsolete("This is the same as VS2012")]
		public static readonly MSBuildFileFormat VS2017 = VS2012;

		public static IEnumerable<MSBuildFileFormat> GetSupportedFormats ()
		{
			yield return VS2012;
			yield return VS2010;
			yield return VS2008;
			yield return VS2005;
		}

		public static IEnumerable<MSBuildFileFormat> GetSupportedFormats (IMSBuildFileObject targetItem)
		{
			return GetSupportedFormats ().Where (f => f.CanWriteFile (targetItem));
		}

		public static MSBuildFileFormat DefaultFormat {
			get { return VS2012; }
		}
		
		public string Name {
			get { return "MSBuild"; }
		}

		public abstract Version Version { get; }

		internal SlnFileFormat SlnFileFormat {
			get { return slnFileFormat; }
		}
		
		public bool SupportsMonikers { get { return SupportedFrameworks == null; } }

		public static bool ToolsSupportMonikers (string toolsVersion)
		{
			return new Version (toolsVersion) >= new Version ("4.0");
		}
		
		public bool SupportsFramework (TargetFramework fx)
		{
			return SupportsMonikers || ((IList<TargetFrameworkMoniker>)SupportedFrameworks).Contains (fx.Id);
		}

		internal virtual bool SupportsSlnVersion (string version)
		{
			return version == SlnVersion;
		}

		protected virtual bool SupportsToolsVersion (string version)
		{
			return version == DefaultToolsVersion;
		}

		public FilePath GetValidFormatName (object obj, FilePath fileName)
		{
			if (slnFileFormat.CanWriteFile (obj, this))
				return slnFileFormat.GetValidFormatName (obj, fileName, this);
			else {
				string ext = MSBuildProjectService.GetExtensionForItem ((SolutionItem)obj);
				if (!string.IsNullOrEmpty (ext))
					return fileName.ChangeExtension ("." + ext);
				else
					return fileName;
			}
		}

		internal bool CanReadFile (FilePath file, Type expectedType)
		{
			if (expectedType.IsAssignableFrom (typeof(Solution)) && slnFileFormat.CanReadFile (file, this))
				return true;
			else if (expectedType.IsAssignableFrom (typeof(SolutionItem))) {
				if (!MSBuildProjectService.CanReadFile (file))
					return false;
				//TODO: check ProductVersion first
				return SupportsToolsVersion (ReadToolsVersion (file));
			}
			return false;
		}

		public bool CanWriteFile (object obj)
		{
			if (slnFileFormat.CanWriteFile (obj, this)) {
				Solution sol = (Solution) obj;
				foreach (SolutionItem si in sol.GetAllItems<SolutionItem> ())
					if (!CanWriteFile (si))
						return false;
				return true;
			}
			else if (obj is SolutionItem) {
				DotNetProject p = obj as DotNetProject;
				// Check the framework only if the project is not loading, since otherwise the
				// project may not yet have the framework info set.
				if (p != null && !p.Loading && !SupportsFramework (p.TargetFramework))
					return false;
				
				// This file format can write all types of projects. If there isn't a handler for a project,
				// it will use a generic handler.
				return true;
			} else
				return false;
		}

		public virtual IEnumerable<string> GetCompatibilityWarnings (object obj)
		{
			if (obj is Solution) {
				List<string> msg = new List<string> ();
				foreach (SolutionItem si in ((Solution)obj).GetAllItems<SolutionItem> ()) {
					IEnumerable<string> ws = GetCompatibilityWarnings (si);
					if (ws != null)
						msg.AddRange (ws);
				}
				return msg;
			}
			var prj = obj as DotNetProject;
			if (prj != null && !SupportsMonikers && !((IList)SupportedFrameworks).Contains (prj.TargetFramework.Id))
				return new [] { GettextCatalog.GetString (
					"The project '{0}' is being saved using the file format '{1}', but this version of Visual Studio " +
					"does not support the framework that the project is targetting ({2})",
					prj.Name, ProductDescription, prj.TargetFramework.Name)
				};
			return null;
		}

		internal async Task WriteFile (FilePath file, object obj, ProgressMonitor monitor)
		{
			if (slnFileFormat.CanWriteFile (obj, this)) {
				await slnFileFormat.WriteFile (file, obj, true, monitor);
			} else {
				throw new NotSupportedException ();
			}
		}

		internal async Task<object> ReadFile (FilePath file, Type expectedType, MonoDevelop.Core.ProgressMonitor monitor)
		{
			if (slnFileFormat.CanReadFile (file, this))
				return await slnFileFormat.ReadFile (file, monitor);
			else
				throw new NotSupportedException (); 
		}

		public abstract string DefaultToolsVersion { get; }

		public abstract string SlnVersion { get; }

		public virtual string DefaultProductVersion { get { return null; } }

		public virtual string DefaultSchemaVersion { get { return null; } }

		public abstract string ProductDescription { get; }

		public virtual TargetFrameworkMoniker[] SupportedFrameworks {
			get { return null; }
		}

		static string ReadToolsVersion (FilePath file)
		{
			try {
				using (XmlTextReader tr = new XmlTextReader (new StreamReader (file))) {
					if (tr.MoveToContent () == XmlNodeType.Element) {
						if (tr.LocalName != "Project" || tr.NamespaceURI != "http://schemas.microsoft.com/developer/msbuild/2003")
							return string.Empty;
						string tv = tr.GetAttribute ("ToolsVersion");
						if (string.IsNullOrEmpty (tv))
							return "2.0"; // Some old VS versions don't specify the tools version, so assume 2.0
						else
							return tv;
					}
				}
			} catch {
				// Ignore
			}
			return string.Empty;
		}
		
		public abstract string Id { get; }
	}
	
	class MSBuildFileFormatVS05: MSBuildFileFormat
	{
		static readonly TargetFrameworkMoniker[] supportedFrameworks = {
			TargetFrameworkMoniker.NET_2_0,
		};

		public override string Id {
			get { return "MSBuild05"; }
		}

		public override Version Version {
			get { return new Version ("2005"); }
		}

		public override string DefaultProductVersion {
			get { return "8.0.50727"; }
		}

		public override string DefaultToolsVersion {
			get { return "2.0"; }
		}

		public override string DefaultSchemaVersion {
			get { return "2.0"; }
		}

		public override string SlnVersion {
			get { return "9.00"; }
		}

		public override string ProductDescription {
			get { return "Visual Studio 2005"; }
		}

		public override TargetFrameworkMoniker[] SupportedFrameworks {
			get { return supportedFrameworks; }
		}
	}
	
	class MSBuildFileFormatVS08: MSBuildFileFormat
	{
		static readonly TargetFrameworkMoniker[] supportedFrameworks = {
			TargetFrameworkMoniker.NET_2_0,
			TargetFrameworkMoniker.NET_3_0,
			TargetFrameworkMoniker.NET_3_5,
			TargetFrameworkMoniker.SL_2_0,
			TargetFrameworkMoniker.SL_3_0,
			TargetFrameworkMoniker.MONOTOUCH_1_0,
		};

		public override string Id {
			get { return "MSBuild08"; }
		}

		public override Version Version {
			get { return new Version ("2008"); }
		}

		public override string DefaultProductVersion {
			get { return "9.0.21022"; }
		}

		public override string DefaultToolsVersion {
			get { return "3.5"; }
		}

		public override string DefaultSchemaVersion {
			get { return "2.0"; }
		}

		public override string SlnVersion {
			get { return "10.00"; }
		}

		public override string ProductDescription {
			get { return "Visual Studio 2008"; }
		}

		public override TargetFrameworkMoniker[] SupportedFrameworks {
			get { return supportedFrameworks; }
		}
	}
	
	class MSBuildFileFormatVS10: MSBuildFileFormat
	{
		public override string Id {
			get { return "MSBuild10"; }
		}

		public override Version Version {
			get { return new Version ("2010"); }
		}

		//WTF VS
		public override string DefaultProductVersion {
			get { return "8.0.30703"; }
		}

		public override string DefaultSchemaVersion {
			get { return "2.0"; }
		}

		public override string DefaultToolsVersion {
			get { return "4.0"; }
		}

		public override string SlnVersion {
			get { return "11.00"; }
		}

		public override string ProductDescription {
			get { return "Visual Studio 2010"; }
		}
	}

	// this is actually VS2010 SP1 and later
	class MSBuildFileFormatVS12: MSBuildFileFormat
	{
		public override string Id {
			get { return "MSBuild12"; }
		}

		public override Version Version {
			get { return new Version ("2012"); }
		}

		public override string DefaultToolsVersion {
			get { return "4.0"; }
		}

		public override string SlnVersion {
			get { return "12.00"; }
		}

		public override string ProductDescription {
			get { return "Visual Studio 15"; }
		}

		protected override bool SupportsToolsVersion (string version)
		{
			return version == "4.0" || version == "12.0" || version == "14.0" || version == "15.0";
		}
	}
}