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
|
//
// VS2003ProjectFileFormat.cs
//
// Author:
// Ankit Jain <jankit@novell.com>
//
// Copyright (C) 2007 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 MonoDevelop.Core;
using MonoDevelop.Core.ProgressMonitoring;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using MonoDevelop.Projects.Extensions;
using MonoDevelop.Ide;
namespace MonoDevelop.Prj2Make
{
public class VS2003ProjectFileFormat : IFileFormat
{
GuiHelper helper;
public VS2003ProjectFileFormat ()
{
}
public string Name {
get { return "Visual Studio 2003"; }
}
GuiHelper GuiHelper {
get {
if (helper == null)
helper = new GuiHelper ();
return helper;
}
}
public FilePath GetValidFormatName (object obj, FilePath fileName)
{
return fileName;
}
public bool CanReadFile (FilePath file, Type expectedObjectType)
{
if (expectedObjectType.IsAssignableFrom (typeof(Solution)) && String.Compare (file.Extension, ".sln", true) == 0) {
string ver = GetSlnFileVersion (file);
if (ver == "7.00" || ver == "8.00")
return true;
}
if (!expectedObjectType.IsAssignableFrom (typeof(DotNetProject)))
return false;
if (String.Compare (file.Extension, ".csproj", true) != 0 &&
String.Compare (file.Extension, ".vbproj", true) != 0)
return false;
try {
using (XmlReader xr = XmlReader.Create (file)) {
xr.MoveToContent ();
if (xr.NodeType == XmlNodeType.Element && String.Compare (xr.LocalName, "VisualStudioProject") == 0)
return true;
}
} catch {
return false;
}
return false;
}
public bool CanWriteFile (object obj)
{
return false;
}
public void WriteFile (FilePath file, object node, IProgressMonitor monitor)
{
}
public object ReadFile (FilePath file, Type expectedType, IProgressMonitor monitor)
{
if (expectedType.IsAssignableFrom (typeof(DotNetProject)))
return ReadProjectFile (file, monitor);
else
return ReadSolutionFile (file, monitor);
}
public object ReadProjectFile (FilePath fileName, IProgressMonitor monitor)
{
if (IdeApp.IsInitialized) {
TargetConvert choice = GuiHelper.QueryProjectConversion (fileName);
if (choice == TargetConvert.None)
throw new InvalidOperationException ("VS2003 projects are not supported natively.");
SolutionEntityItem project = ImportCsproj (fileName);
project.FileName = fileName;
if (choice == TargetConvert.MonoDevelop) {
project.FileFormat = IdeApp.Services.ProjectService.FileFormats.GetFileFormat ("MD1");
} else if (choice == TargetConvert.VisualStudio) {
project.FileFormat = IdeApp.Services.ProjectService.FileFormats.GetFileFormat ("MSBuild05");
}
project.Save (monitor);
return project;
}
else {
SolutionEntityItem project = ImportCsproj (fileName);
project.FileName = fileName;
return project;
}
}
public object ReadSolutionFile (FilePath fileName, IProgressMonitor monitor)
{
if (IdeApp.IsInitialized) {
TargetConvert choice = GuiHelper.QuerySolutionConversion (fileName);
if (choice == TargetConvert.None)
throw new InvalidOperationException ("VS2003 solutions are not supported natively.");
Solution solution = ImportSln (fileName);
if (choice == TargetConvert.MonoDevelop) {
solution.ConvertToFormat (IdeApp.Services.ProjectService.FileFormats.GetFileFormat ("MD1"), true);
} else if (choice == TargetConvert.VisualStudio) {
solution.ConvertToFormat (IdeApp.Services.ProjectService.FileFormats.GetFileFormat ("MSBuild05"), true);
}
solution.Save (monitor);
return solution;
}
else
return ImportSln (fileName);
}
internal SolutionEntityItem ImportCsproj (FilePath fileName)
{
SolutionEntityItem project = null;
SlnMaker slnmaker = new SlnMaker ();
try {
IProgressMonitor m = IdeApp.IsInitialized ? GuiHelper.CreateProgressMonitor () : new ConsoleProgressMonitor ();
using (m) {
project = slnmaker.CreatePrjxFromCsproj (fileName, m);
}
} catch (Exception e) {
LoggingService.LogError ("exception while converting: " + e.ToString ());
throw;
}
return project;
}
public void ConvertToFormat (object obj)
{
}
public List<FilePath> GetItemFiles (object obj)
{
return new List<FilePath> ();
}
// Converts a vs2003 solution to a Combine object
internal Solution ImportSln (FilePath fileName)
{
SlnMaker slnmaker = new SlnMaker ();
Solution solution = null;
IProgressMonitor m = IdeApp.IsInitialized ? GuiHelper.CreateProgressMonitor () : new ConsoleProgressMonitor ();
try {
solution = slnmaker.MsSlnToCmbxHelper (fileName, m);
} catch (Exception e) {
LoggingService.LogError ("exception while converting : " + e.ToString ());
throw;
} finally {
if (m != null)
m.Dispose ();
}
return solution;
}
// Utility function to determine the sln file version
string GetSlnFileVersion(string strInSlnFile)
{
using (var stream = File.OpenText (strInSlnFile)) {
// ReadLine returns null at EOF which makes the regex throw an ArgNull exception
var match = SlnMaker.SlnVersionRegex.Match (stream.ReadLine () ?? "");
if (!match.Success)
match = SlnMaker.SlnVersionRegex.Match (stream.ReadLine () ?? "");
return match.Success ? match.Groups[1].Value : null;
}
}
public bool SupportsMixedFormats {
get { return false; }
}
public IEnumerable<string> GetCompatibilityWarnings (object obj)
{
yield break;
}
public bool SupportsFramework (MonoDevelop.Core.Assemblies.TargetFramework framework)
{
return framework.Id == MonoDevelop.Core.Assemblies.TargetFrameworkMoniker.NET_1_1;
}
}
enum TargetConvert
{
None,
VisualStudio,
MonoDevelop
}
class GuiHelper: GuiSyncObject
{
TargetConvert QueryConversion (string text)
{
AlertButton vs2005 = new AlertButton (GettextCatalog.GetString ("Convert to MSBuild"));
AlertButton choice = MessageService.AskQuestion (text,
GettextCatalog.GetString ("Converting to MSBuild format will overwrite existing files."),
AlertButton.Cancel, vs2005);
if (choice == vs2005)
return TargetConvert.VisualStudio;
else
return TargetConvert.None;
}
public TargetConvert QueryProjectConversion (string file)
{
string text = GettextCatalog.GetString ("The project file {0} is a Visual Studio 2003 project. It must be converted to a MSBuild project.", file);
return QueryConversion (text);
}
public TargetConvert QuerySolutionConversion (string file)
{
string text = GettextCatalog.GetString ("The solution file {0} is a Visual Studio 2003 solution. It must be converted to a MSBuild project.", file);
return QueryConversion (text);
}
public IProgressMonitor CreateProgressMonitor ()
{
return new MonoDevelop.Ide.ProgressMonitoring.MessageDialogProgressMonitor (true, false, true, false);
}
}
}
|