1
// MSBuildFileFormat.cs
4
// Lluis Sanchez Gual <lluis@novell.com>
6
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
// of this software and associated documentation files (the "Software"), to deal
10
// in the Software without restriction, including without limitation the rights
11
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
// copies of the Software, and to permit persons to whom the Software is
13
// furnished to do so, subject to the following conditions:
15
// The above copyright notice and this permission notice shall be included in
16
// all copies or substantial portions of the Software.
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
using System.Collections;
31
using System.Collections.Generic;
33
using MonoDevelop.Core;
34
using MonoDevelop.Projects.Extensions;
36
namespace MonoDevelop.Projects.Formats.MSBuild
38
class MSBuildFileFormat: IFileFormat
40
SlnFileFormat slnFileFormat = new SlnFileFormat ();
41
string productVersion;
44
string productDescription;
45
string[] frameworkVersions;
47
public MSBuildFileFormat (string productVersion, string toolsVersion, string slnVersion, string productDescription, string[] frameworkVersions)
49
this.productVersion = productVersion;
50
this.toolsVersion = toolsVersion;
51
this.slnVersion = slnVersion;
52
this.productDescription = productDescription;
53
this.frameworkVersions = frameworkVersions;
62
public string GetValidFormatName (object obj, string fileName)
64
if (slnFileFormat.CanWriteFile (obj, this))
65
return slnFileFormat.GetValidFormatName (obj, fileName, this);
67
ItemTypeNode node = MSBuildProjectService.FindHandlerForItem ((SolutionEntityItem)obj);
69
return Path.ChangeExtension (fileName, "." + node.Extension);
71
return Path.ChangeExtension (fileName, ".mdproj");
75
public bool CanReadFile (string file, Type expectedType)
77
if (expectedType.IsAssignableFrom (typeof(Solution)) && slnFileFormat.CanReadFile (file, this))
79
else if (expectedType.IsAssignableFrom (typeof(SolutionEntityItem))) {
80
ItemTypeNode node = MSBuildProjectService.FindHandlerForFile (file);
83
return toolsVersion == ReadToolsVersion (file);
88
public bool CanWriteFile (object obj)
90
if (slnFileFormat.CanWriteFile (obj, this))
92
else if (obj is SolutionEntityItem) {
93
ItemTypeNode node = MSBuildProjectService.FindHandlerForItem ((SolutionEntityItem)obj);
99
public void WriteFile (string file, object obj, MonoDevelop.Core.IProgressMonitor monitor)
101
if (slnFileFormat.CanWriteFile (obj, this)) {
102
slnFileFormat.WriteFile (file, obj, this, monitor);
104
SolutionEntityItem item = (SolutionEntityItem) obj;
105
if (!(item.ItemHandler is MSBuildProjectHandler))
106
MSBuildProjectService.InitializeItemHandler (item);
107
MSBuildProjectHandler handler = (MSBuildProjectHandler) item.ItemHandler;
108
handler.SetTargetFormat (this);
109
handler.Save (monitor);
113
public object ReadFile (string file, Type expectedType, MonoDevelop.Core.IProgressMonitor monitor)
115
if (slnFileFormat.CanReadFile (file, this))
116
return slnFileFormat.ReadFile (file, this, monitor);
118
return MSBuildProjectService.LoadItem (monitor, file, null, null);
121
public List<string> GetItemFiles (object obj)
123
return new List<string> ();
126
public void InitializeSolutionItem (SolutionItem item)
130
public void ConvertToFormat (object obj)
135
MSBuildHandler handler;
136
SolutionItem item = obj as SolutionItem;
138
handler = item.GetItemHandler() as MSBuildHandler;
139
if (handler != null) {
140
handler.SetTargetFormat (this);
145
MSBuildProjectService.InitializeItemHandler (item);
146
handler = (MSBuildHandler) item.ItemHandler;
147
handler.SetTargetFormat (this);
150
public bool SupportsMixedFormats {
151
get { return false; }
154
public string ToolsVersion {
160
public string SlnVersion {
166
public string ProductVersion {
168
return productVersion;
172
public string ProductDescription {
174
return productDescription;
178
public string[] FrameworkVersions {
180
return frameworkVersions;
184
string ReadToolsVersion (string file)
187
using (XmlTextReader tr = new XmlTextReader (new StreamReader (file))) {
188
if (tr.MoveToContent () == XmlNodeType.Element) {
189
string tv = tr.GetAttribute ("ToolsVersion");
190
if (string.IsNullOrEmpty (tv))
191
return "2.0"; // Some old VS versions don't specify the tools version, so assume 2.0
202
public virtual IEnumerable<string> GetCompatibilityWarnings (object obj)
204
DotNetProject prj = obj as DotNetProject;
205
if (!((IList)frameworkVersions).Contains (prj.TargetFramework.Id))
206
yield return 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);
210
class MSBuildFileFormatVS05: MSBuildFileFormat
212
public const string Version = "8.0.50727";
213
const string toolsVersion = "2.0";
214
const string slnVersion = "9.00";
215
const string productComment = "Visual Studio 2005";
216
static string[] frameworkVersions = { "2.0" };
218
public MSBuildFileFormatVS05 (): base (Version, toolsVersion, slnVersion, productComment, frameworkVersions)
223
class MSBuildFileFormatVS08: MSBuildFileFormat
225
public const string Version = "9.0.21022";
226
const string toolsVersion = "3.5";
227
const string slnVersion = "10.00";
228
const string productComment = "Visual Studio 2008";
229
static string[] frameworkVersions = { "2.0", "3.0", "3.5" };
231
public MSBuildFileFormatVS08 (): base (Version, toolsVersion, slnVersion, productComment, frameworkVersions)
236
class MSBuildFileFormatVS10: MSBuildFileFormat
238
public const string Version = "10.0.0";
239
const string toolsVersion = "4.0";
240
const string slnVersion = "11.00";
241
const string productComment = "Visual Studio 2010";
242
static string[] frameworkVersions = { "2.0", "3.0", "3.5", "4.0" };
244
public MSBuildFileFormatVS10 (): base (Version, toolsVersion, slnVersion, productComment, frameworkVersions)