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
|
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using MonoDevelop.Prj2Make.Schema.Csproj;
using MonoDevelop.Projects;
using MonoDevelop.Projects.Text;
using MonoDevelop.Core;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.CSharp.Project;
namespace MonoDevelop.Prj2Make
{
public class SlnMaker
{
public static string slash;
Hashtable projNameInfo = new Hashtable();
Hashtable projGuidInfo = new Hashtable();
private string prjxFileName;
private string m_strSlnVer;
private string m_strCsprojVer;
private bool m_bIsUnix;
private bool m_bIsMcs;
private bool m_bIsUsingLib;
TargetFramework fx = Runtime.SystemAssemblyService.GetTargetFramework (TargetFrameworkMoniker.NET_1_1);
// Flag use to determine if the LIB variable will be used in
// the Makefile that prj2make generates
public bool IsUsingLib
{
get{ return m_bIsUsingLib; }
set{ m_bIsUsingLib = value; }
}
// Determines if the makefile is intended for nmake or gmake for urposes of path separator character
public bool IsUnix
{
get{ return m_bIsUnix; }
set{ m_bIsUnix = value; }
}
// Determines if using MCS or CSC
public bool IsMcs
{
get{ return m_bIsMcs; }
set{ m_bIsMcs = value; }
}
public string SlnVersion
{
get { return m_strSlnVer; }
set { m_strSlnVer = value; }
}
public string CsprojVersion
{
get { return m_strCsprojVer; }
set { m_strCsprojVer = value; }
}
// Shuld contain the file name
// of the most resent prjx generation
public string PrjxFileName {
get { return prjxFileName; }
}
// Default constructor
public SlnMaker()
{
m_bIsUnix = false;
m_bIsMcs = false;
m_bIsUsingLib = false;
}
// Utility function to determine the sln file version
protected string GetSlnFileVersion(string strInSlnFile)
{
string strVersion = null;
string strInput = null;
Match match;
FileStream fis = new FileStream(strInSlnFile, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader reader = new StreamReader(fis);
strInput = reader.ReadLine();
match = SlnVersionRegex.Match(strInput);
if (match.Success)
{
strVersion = match.Groups[1].Value;
}
// Close the stream
reader.Close();
// Close the File Stream
fis.Close();
return strVersion;
}
// Utility function to determine the csproj file version
protected string GetCsprojFileVersion(string strInCsprojFile)
{
string strRetVal = null;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strInCsprojFile);
strRetVal = xmlDoc.SelectSingleNode("/VisualStudioProject/CSHARP/@ProductVersion").Value;
return strRetVal;
}
protected void ParseMsCsProj(string fname)
{
string projectName = System.IO.Path.GetFileNameWithoutExtension (fname);
string csprojPath = System.IO.Path.GetFileName (fname);
string projectGuid = "";
CsprojInfo pi = new CsprojInfo (m_bIsUnix, m_bIsMcs, projectName, projectGuid, csprojPath);
projNameInfo[projectName] = pi;
projGuidInfo[projectGuid] = pi;
}
protected void ParseSolution(string fname, IProgressMonitor monitor)
{
FileStream fis = new FileStream(fname,FileMode.Open, FileAccess.Read, FileShare.Read);
using (StreamReader reader = new StreamReader(fis)) {
while (true)
{
string s = reader.ReadLine();
Match match;
match = ProjectRegex.Match(s);
if (match.Success)
{
string projectName = match.Groups[2].Value;
string csprojPath = match.Groups[3].Value;
string projectGuid = match.Groups[4].Value;
try {
if (csprojPath.EndsWith (".csproj") && !csprojPath.StartsWith("http://"))
{
csprojPath = MapPath (Path.GetDirectoryName (fname), csprojPath);
CsprojInfo pi = new CsprojInfo (m_bIsUnix, m_bIsMcs, projectName, projectGuid, csprojPath);
projNameInfo[projectName] = pi;
projGuidInfo[projectGuid] = pi;
}
} catch (Exception ex) {
Console.WriteLine (GettextCatalog.GetString ("Could not import project:") + csprojPath);
Console.WriteLine (ex.ToString ());
monitor.ReportError (GettextCatalog.GetString ("Could not import project:") + csprojPath, ex);
throw;
}
}
if (s.StartsWith("Global"))
{
break;
}
}
}
}
public SolutionEntityItem CreatePrjxFromCsproj (string csprojFileName, IProgressMonitor monitor)
{
try {
FileFormat format = Services.ProjectService.FileFormats.GetFileFormatForFile (csprojFileName, typeof(SolutionEntityItem));
if (format != null && format.Id != "VS2003ProjectFileFormat")
return (SolutionEntityItem) Services.ProjectService.ReadSolutionItem (monitor, csprojFileName);
MonoDevelop.Prj2Make.Schema.Csproj.VisualStudioProject csprojObj = null;
monitor.BeginTask (GettextCatalog.GetString ("Importing project: ") + csprojFileName, 4);
DotNetAssemblyProject prjxObj = new DotNetAssemblyProject ("C#", null, null);
prjxFileName = String.Format ("{0}.mdp",
Path.Combine (Path.GetDirectoryName (csprojFileName),
Path.GetFileNameWithoutExtension (csprojFileName))
);
// Load the csproj
using (TextFileReader fsIn = new TextFileReader (csprojFileName)) {
XmlSerializer xmlDeSer = new XmlSerializer (typeof(VisualStudioProject));
csprojObj = (VisualStudioProject) xmlDeSer.Deserialize (fsIn);
}
monitor.Step (1);
// Begin prjxObj population
prjxObj.FileName = prjxFileName;
prjxObj.Name = Path.GetFileNameWithoutExtension(csprojFileName);
prjxObj.Description = "";
prjxObj.NewFileSearch = NewFileSearch.None;
prjxObj.DefaultNamespace = csprojObj.CSHARP.Build.Settings.RootNamespace;
GetContents (prjxObj, csprojObj.CSHARP.Files.Include, prjxObj.Files, monitor);
monitor.Step (1);
GetReferences (prjxObj, csprojObj.CSHARP.Build.References, prjxObj.References, monitor);
monitor.Step (1);
prjxObj.Configurations.Clear ();
foreach (Config cblock in csprojObj.CSHARP.Build.Settings.Config)
{
prjxObj.Configurations.Add (CreateConfigurationBlock (
prjxObj,
cblock,
csprojObj.CSHARP.Build.Settings.AssemblyName,
csprojObj.CSHARP.Build.Settings.OutputType,
monitor
));
}
monitor.Step (1);
return prjxObj;
} catch (Exception ex) {
monitor.ReportError (GettextCatalog.GetString ("Could not import project:") + csprojFileName, ex);
throw;
} finally {
monitor.EndTask ();
}
}
public Solution MsSlnToCmbxHelper (string slnFileName, IProgressMonitor monitor)
{
Solution solution = new Solution();
monitor.BeginTask (GettextCatalog.GetString ("Importing solution"), 2);
try
{
// We invoke the ParseSolution
// by passing the file obtained
ParseSolution (slnFileName, monitor);
// Create all of the prjx files form the csproj files
monitor.BeginTask (null, projNameInfo.Values.Count * 2);
foreach (CsprojInfo pi in projNameInfo.Values) {
string mappedPath = MapPath (Path.GetDirectoryName (slnFileName), pi.csprojpath);
if (mappedPath == null) {
monitor.Step (2);
monitor.ReportWarning (GettextCatalog.GetString ("Project file not found: ") + pi.csprojpath);
continue;
}
SolutionEntityItem prj;
if (pi.NeedsConversion)
prj = CreatePrjxFromCsproj (mappedPath, monitor);
else
prj = (DotNetProject) Services.ProjectService.ReadSolutionItem (monitor, mappedPath);
if (prj == null)
return null;
monitor.Step (1);
solution.RootFolder.Items.Add (prj);
foreach (ItemConfiguration conf in prj.Configurations) {
if (!solution.GetConfigurations ().Contains (conf.Id))
solution.AddConfiguration (conf.Id, false);
}
monitor.Step (1);
}
monitor.EndTask ();
monitor.Step (1);
solution.SetLocation (Path.GetDirectoryName (slnFileName), Path.GetFileNameWithoutExtension(slnFileName));
monitor.Step (1);
return solution;
}
catch (Exception e)
{
monitor.ReportError (GettextCatalog.GetString ("The solution could not be imported."), e);
throw;
}
finally
{
monitor.EndTask ();
}
}
protected void GetReferences (Project project, MonoDevelop.Prj2Make.Schema.Csproj.Reference[] References, ProjectReferenceCollection references, IProgressMonitor monitor)
{
if (References == null || References.Length == 0)
return;
monitor.BeginTask (null, 5 + References.Length);
try {
// Get the GAC path
string strBasePathMono1_0 = GetPackageDirectory ("mono", "mono/1.0");
monitor.Step (1);
string strBasePathGtkSharp = GetPackageDirectory ("gtk-sharp", "mono/gtk-sharp");
monitor.Step (1);
string strBasePathGtkSharp2_0 = GetPackageDirectory ("gtk-sharp-2.0", "mono/gtk-sharp-2.0");
monitor.Step (1);
string strBasePathGeckoSharp = GetPackageDirectory ("gecko-sharp", "mono/gecko-sharp");
monitor.Step (1);
string strBasePathGeckoSharp2_0 = GetPackageDirectory ("gecko-sharp-2.0", "mono/gecko-sharp-2.0");
string[] monoLibs = new string [] {
strBasePathMono1_0,
strBasePathGtkSharp2_0,
strBasePathGtkSharp,
strBasePathGeckoSharp2_0,
strBasePathGeckoSharp
};
// Iterate through the reference collection of the csproj file
foreach (MonoDevelop.Prj2Make.Schema.Csproj.Reference rf in References)
{
monitor.Step (1);
ProjectReference rfOut = null;
if (rf.Package != null && rf.Package.Length != 0)
{
rfOut = new ProjectReference (MonoDevelop.Projects.ReferenceType.Project, Path.GetFileName (rf.Name));
rfOut.LocalCopy = true;
references.Add (rfOut);
}
else if (rf.AssemblyName != null)
{
string rname = rf.AssemblyName;
if (rname == "System.XML")
rname = "System.Xml";
string oref = Runtime.SystemAssemblyService.DefaultAssemblyContext.GetAssemblyFullName (rname, fx);
if (oref == null) {
if (rf.HintPath != null) {
string asm = MapPath (project.ItemDirectory, rf.HintPath);
if (!System.IO.File.Exists (asm))
monitor.ReportWarning (GettextCatalog.GetString ("Referenced assembly not found: ") + asm);
ProjectReference aref = new ProjectReference (MonoDevelop.Projects.ReferenceType.Assembly, asm);
references.Add (aref);
continue;
}
monitor.ReportWarning (GettextCatalog.GetString ("Assembly reference could not be imported: ") + rf.AssemblyName);
continue;
}
rfOut = new ProjectReference (MonoDevelop.Projects.ReferenceType.Package, oref);
rfOut.LocalCopy = true;
references.Add (rfOut);
}
else if (rf.HintPath != null)
{
// HACK - under Unix filenames are case sensitive
// Under Windows there's no agreement on Xml vs XML ;-)
if (Path.GetFileName (rf.HintPath) == "System.XML.dll")
{
ProjectReference pref = GetMonoReferece (strBasePathMono1_0, "System.Xml.dll");
if (pref != null) {
references.Add (pref);
continue;
}
} else {
foreach (string libDir in monoLibs) {
if (libDir == null) continue;
if (rf.HintPath == null)
continue;
rfOut = GetMonoReferece (libDir, rf.HintPath);
if (rfOut != null)
break;
}
if (rfOut == null) {
rfOut = new ProjectReference (MonoDevelop.Projects.ReferenceType.Package, Path.GetFileName (rf.HintPath));
rfOut.LocalCopy = true;
}
references.Add (rfOut);
}
}
else {
monitor.ReportWarning (GettextCatalog.GetString ("Assembly reference could not be imported: ") + rf.Name);
}
}
} finally {
monitor.EndTask ();
}
}
string GetPackageDirectory (string package, string subdir)
{
string dir = MonoDevelop.Prj2Make.PkgConfigInvoker.GetPkgVariableValue (package, "libdir");
return dir != null ? Path.Combine (dir.TrimEnd(), subdir) : null;
}
ProjectReference GetMonoReferece (string libPath, string reference)
{
string strRefFileName = Path.Combine (libPath, Path.GetFileName (reference));
// Test to see if file exist in GAC location
if (System.IO.File.Exists (strRefFileName)) {
ProjectReference rfOut = new ProjectReference (MonoDevelop.Projects.ReferenceType.Package, Runtime.SystemAssemblyService.DefaultAssemblyContext.GetAssemblyFullName (strRefFileName, fx));
rfOut.LocalCopy = true;
return rfOut;
}
return null;
}
protected void GetContents (MonoDevelop.Projects.Project project, MonoDevelop.Prj2Make.Schema.Csproj.File[] Include, ProjectFileCollection files, IProgressMonitor monitor)
{
if (Include == null || Include.Length == 0)
return;
// Iterate through the file collection of the csproj file
foreach(MonoDevelop.Prj2Make.Schema.Csproj.File fl in Include)
{
ProjectFile flOut = new ProjectFile ();
string name;
if ((fl.Link == null) || (fl.Link.Length == 0)) {
name = MapPath (project.BaseDirectory, fl.RelPath);
} else {
name = MapPath (null, fl.Link);
}
if (name == null) {
monitor.ReportWarning (GettextCatalog.GetString ("Can't import file: ") + fl.RelPath);
continue;
}
flOut.Name = name;
// Adding here as GetDefaultResourceIdInternal needs flOut.Project
files.Add (flOut);
switch (fl.SubType)
{
case "Code":
flOut.Subtype = Subtype.Code;
break;
}
switch (fl.BuildAction)
{
case MonoDevelop.Prj2Make.Schema.Csproj.FileBuildAction.Compile:
flOut.BuildAction = BuildAction.Compile;
break;
case MonoDevelop.Prj2Make.Schema.Csproj.FileBuildAction.Content:
flOut.BuildAction = BuildAction.Content;
break;
case MonoDevelop.Prj2Make.Schema.Csproj.FileBuildAction.EmbeddedResource:
flOut.BuildAction = BuildAction.EmbeddedResource;
break;
case MonoDevelop.Prj2Make.Schema.Csproj.FileBuildAction.None:
flOut.BuildAction = BuildAction.None;
break;
}
// DependentUpon is relative to flOut
flOut.DependsOn = MapPath (Path.GetDirectoryName (flOut.Name), fl.DependentUpon);
flOut.Data = "";
}
}
protected SolutionItemConfiguration CreateConfigurationBlock (MonoDevelop.Projects.DotNetProject project, Config ConfigBlock, string AssemblyName, string OuputType, IProgressMonitor monitor)
{
DotNetProjectConfiguration confObj = project.CreateConfiguration (ConfigBlock.Name) as DotNetProjectConfiguration;
confObj.RunWithWarnings = false;
confObj.DebugMode = ConfigBlock.DebugSymbols;
project.CompileTarget = (CompileTarget) Enum.Parse (typeof(CompileTarget), OuputType, true);
string dir = MapPath (project.BaseDirectory, ConfigBlock.OutputPath);
if (dir == null) {
dir = Path.Combine ("bin", ConfigBlock.Name);
monitor.ReportWarning (string.Format (GettextCatalog.GetString ("Output directory '{0}' can't be mapped to a local directory. The directory '{1}' will be used instead"), ConfigBlock.OutputPath, dir));
}
confObj.OutputDirectory = dir;
confObj.OutputAssembly = AssemblyName;
CSharpCompilerParameters compilerParams = new CSharpCompilerParameters ();
compilerParams.WarningLevel = ConfigBlock.WarningLevel;
compilerParams.NoWarnings = "";
compilerParams.Optimize = ConfigBlock.Optimize;
compilerParams.DefineSymbols = ConfigBlock.DefineConstants;
compilerParams.UnsafeCode = ConfigBlock.AllowUnsafeBlocks;
compilerParams.GenerateOverflowChecks = ConfigBlock.CheckForOverflowUnderflow;
return confObj;
}
internal static string MapPath (string basePath, string relPath)
{
if (relPath == null || relPath.Length == 0)
return null;
string path = relPath;
if (Path.DirectorySeparatorChar != '\\')
path = path.Replace ("\\", "/");
if (char.IsLetter (path [0]) && path.Length > 1 && path[1] == ':')
return null;
if (basePath != null)
path = Path.Combine (basePath, path);
if (System.IO.File.Exists (path)){
return Path.GetFullPath (path);
}
if (Path.IsPathRooted (path)) {
// Windows paths are case-insensitive. When mapping an absolute path
// we can try to find the correct case for the path.
string[] names = path.Substring (1).Split ('/');
string part = "/";
for (int n=0; n<names.Length; n++) {
string[] entries;
if (names [n] == ".."){
part = Path.GetFullPath (part + "/..");
continue;
}
entries = Directory.GetFileSystemEntries (part);
string fpath = null;
foreach (string e in entries) {
if (string.Compare (Path.GetFileName (e), names[n], true) == 0) {
fpath = e;
break;
}
}
if (fpath == null) {
// Part of the path does not exist. Can't do any more checking.
part = Path.GetFullPath (part);
for (; n < names.Length; n++)
part += "/" + names[n];
return part;
}
part = fpath;
}
return Path.GetFullPath (part);
} else {
return Path.GetFullPath (path);
}
}
// static regexes
static Regex projectRegex = null;
internal static Regex ProjectRegex {
get {
if (projectRegex == null)
projectRegex = new Regex(@"Project\(""(\{[^}]*\})""\) = ""(.*)"", ""(.*)"", ""(\{[^{]*\})""");
return projectRegex;
}
}
static Regex slnVersionRegex = null;
internal static Regex SlnVersionRegex {
get {
if (slnVersionRegex == null)
slnVersionRegex = new Regex (@"Microsoft Visual Studio Solution File, Format Version (\d?\d.\d\d)");
return slnVersionRegex;
}
}
}
}
|