~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Projects/MonoDevelop.Projects.Formats.MSBuild/MSBuildFileFormat.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// MSBuildFileFormat.cs
 
2
//
 
3
// Author:
 
4
//   Lluis Sanchez Gual <lluis@novell.com>
 
5
//
 
6
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
 
7
//
 
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:
 
14
//
 
15
// The above copyright notice and this permission notice shall be included in
 
16
// all copies or substantial portions of the Software.
 
17
//
 
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
 
24
// THE SOFTWARE.
 
25
//
 
26
//
 
27
 
 
28
using System;
 
29
using System.Xml;
 
30
using System.Collections;
 
31
using System.Collections.Generic;
 
32
using System.IO;
 
33
using MonoDevelop.Core;
 
34
using MonoDevelop.Projects.Extensions;
 
35
 
 
36
namespace MonoDevelop.Projects.Formats.MSBuild
 
37
{
 
38
        class MSBuildFileFormat: IFileFormat
 
39
        {
 
40
                SlnFileFormat slnFileFormat = new SlnFileFormat ();
 
41
                string productVersion;
 
42
                string toolsVersion;
 
43
                string slnVersion;
 
44
                string productDescription;
 
45
                string[] frameworkVersions;
 
46
                
 
47
                public MSBuildFileFormat (string productVersion, string toolsVersion, string slnVersion, string productDescription, string[] frameworkVersions)
 
48
                {
 
49
                        this.productVersion = productVersion;
 
50
                        this.toolsVersion = toolsVersion;
 
51
                        this.slnVersion = slnVersion;
 
52
                        this.productDescription = productDescription;
 
53
                        this.frameworkVersions = frameworkVersions;
 
54
                }
 
55
                
 
56
                public string Name {
 
57
                        get {
 
58
                                return "MSBuild";
 
59
                        }
 
60
                }
 
61
 
 
62
                public string GetValidFormatName (object obj, string fileName)
 
63
                {
 
64
                        if (slnFileFormat.CanWriteFile (obj, this))
 
65
                                return slnFileFormat.GetValidFormatName (obj, fileName, this);
 
66
                        else {
 
67
                                ItemTypeNode node = MSBuildProjectService.FindHandlerForItem ((SolutionEntityItem)obj);
 
68
                                if (node != null)
 
69
                                        return Path.ChangeExtension (fileName, "." + node.Extension);
 
70
                                else
 
71
                                        return Path.ChangeExtension (fileName, ".mdproj");
 
72
                        }
 
73
                }
 
74
 
 
75
                public bool CanReadFile (string file, Type expectedType)
 
76
                {
 
77
                        if (expectedType.IsAssignableFrom (typeof(Solution)) && slnFileFormat.CanReadFile (file, this))
 
78
                                return true;
 
79
                        else if (expectedType.IsAssignableFrom (typeof(SolutionEntityItem))) {
 
80
                                ItemTypeNode node = MSBuildProjectService.FindHandlerForFile (file);
 
81
                                if (node == null)
 
82
                                        return false;
 
83
                                return toolsVersion == ReadToolsVersion (file);
 
84
                        }
 
85
                        return false;
 
86
                }
 
87
 
 
88
                public bool CanWriteFile (object obj)
 
89
                {
 
90
                        if (slnFileFormat.CanWriteFile (obj, this))
 
91
                                return true;
 
92
                        else if (obj is SolutionEntityItem) {
 
93
                                ItemTypeNode node = MSBuildProjectService.FindHandlerForItem ((SolutionEntityItem)obj);
 
94
                                return node != null;
 
95
                        } else
 
96
                                return false;
 
97
                }
 
98
 
 
99
                public void WriteFile (string file, object obj, MonoDevelop.Core.IProgressMonitor monitor)
 
100
                {
 
101
                        if (slnFileFormat.CanWriteFile (obj, this)) {
 
102
                                slnFileFormat.WriteFile (file, obj, this, monitor);
 
103
                        } else {
 
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);
 
110
                        }
 
111
                }
 
112
 
 
113
                public object ReadFile (string file, Type expectedType, MonoDevelop.Core.IProgressMonitor monitor)
 
114
                {
 
115
                        if (slnFileFormat.CanReadFile (file, this))
 
116
                                return slnFileFormat.ReadFile (file, this, monitor);
 
117
                        else
 
118
                                return MSBuildProjectService.LoadItem (monitor, file, null, null);
 
119
                }
 
120
 
 
121
                public List<string> GetItemFiles (object obj)
 
122
                {
 
123
                        return new List<string> ();
 
124
                }
 
125
 
 
126
                public void InitializeSolutionItem (SolutionItem item)
 
127
                {
 
128
                }
 
129
 
 
130
                public void ConvertToFormat (object obj)
 
131
                {
 
132
                        if (obj == null)
 
133
                                return;
 
134
                        
 
135
                        MSBuildHandler handler;
 
136
                        SolutionItem item = obj as SolutionItem;
 
137
                        if (item != null) {
 
138
                                handler = item.GetItemHandler() as MSBuildHandler;
 
139
                                if (handler != null) {
 
140
                                        handler.SetTargetFormat (this);
 
141
                                        return;
 
142
                                }
 
143
                        }
 
144
                        
 
145
                        MSBuildProjectService.InitializeItemHandler (item);
 
146
                        handler = (MSBuildHandler) item.ItemHandler;
 
147
                        handler.SetTargetFormat (this);
 
148
                }
 
149
                
 
150
                public bool SupportsMixedFormats {
 
151
                        get { return false; }
 
152
                }
 
153
 
 
154
                public string ToolsVersion {
 
155
                        get {
 
156
                                return toolsVersion;
 
157
                        }
 
158
                }
 
159
 
 
160
                public string SlnVersion {
 
161
                        get {
 
162
                                return slnVersion;
 
163
                        }
 
164
                }
 
165
 
 
166
                public string ProductVersion {
 
167
                        get {
 
168
                                return productVersion;
 
169
                        }
 
170
                }
 
171
 
 
172
                public string ProductDescription {
 
173
                        get {
 
174
                                return productDescription;
 
175
                        }
 
176
                }
 
177
 
 
178
                public string[] FrameworkVersions {
 
179
                        get {
 
180
                                return frameworkVersions;
 
181
                        }
 
182
                }
 
183
 
 
184
                string ReadToolsVersion (string file)
 
185
                {
 
186
                        try {
 
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
 
192
                                                else
 
193
                                                        return tv;
 
194
                                        }
 
195
                                }
 
196
                        } catch {
 
197
                                // Ignore
 
198
                        }
 
199
                        return string.Empty;
 
200
                }
 
201
 
 
202
                public virtual IEnumerable<string> GetCompatibilityWarnings (object obj)
 
203
                {
 
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);
 
207
                }
 
208
        }
 
209
        
 
210
        class MSBuildFileFormatVS05: MSBuildFileFormat
 
211
        {
 
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" };
 
217
                
 
218
                public MSBuildFileFormatVS05 (): base (Version, toolsVersion, slnVersion, productComment, frameworkVersions)
 
219
                {
 
220
                }
 
221
        }
 
222
        
 
223
        class MSBuildFileFormatVS08: MSBuildFileFormat
 
224
        {
 
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" };
 
230
                
 
231
                public MSBuildFileFormatVS08 (): base (Version, toolsVersion, slnVersion, productComment, frameworkVersions)
 
232
                {
 
233
                }
 
234
        }
 
235
        
 
236
        class MSBuildFileFormatVS10: MSBuildFileFormat
 
237
        {
 
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" };
 
243
                
 
244
                public MSBuildFileFormatVS10 (): base (Version, toolsVersion, slnVersion, productComment, frameworkVersions)
 
245
                {
 
246
                }
 
247
        }
 
248
}