-
Notifications
You must be signed in to change notification settings - Fork 1
/
SolutionGenerator.cs
1041 lines (896 loc) · 42.9 KB
/
SolutionGenerator.cs
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Copyright MIT License
/*
* Copyright © 2008 François St-Arnaud and John Wood
*
* 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.
*
* SolGen, Visual Studio Solution Generator for C# Projects (http://codeproject.com/SolGen)
* Based on original work by John Wood developer of Priorganizer (http://www.priorganizer.com)
* Adapted for Visual Studio 2005 by François St-Arnaud (francois.starnaud@videotron.ca)
* Published to CodePlex with original author's permission.
*
* Original software copyright notice:
* © Copyright 2005 J. Wood Software Services. All rights reserved.
* You are free to modify this source code at will, but please give credit to John Wood
* if decide to incorporate or redistribute the resultant binary.
* http://www.servicestuff.com/jwss/page.aspx?page=utility.htm&utility=solgen
*
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Build.BuildEngine;
namespace SolGen
{
class SolutionGenerator
{
#region Constants
private const string ProjectRefs = ".projRefs";
private const string AssemblyRefs = ".dllRefs";
private const string CsProjFileExtension = ".csproj";
private const string SolutionFileExtension = ".sln";
private const string AssemblyName = "AssemblyName";
private const string ProjectGuid = "ProjectGuid";
private const string AllCsProj = "*" + CsProjFileExtension;
private const string AllFiles = "*.*";
private const string Reference = "Reference";
private const string ProjectReference = "ProjectReference";
private const string Project = "Project";
// From Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Projects\
private const string CsProjGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
private const string FolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";
#endregion
#region Enums
#endregion
#region Members
private readonly IDictionary<string, ProjectInfo> _projectsByName = new Dictionary<string, ProjectInfo>();
private readonly IDictionary<string, ProjectInfo> _projectsByFile = new Dictionary<string, ProjectInfo>();
private readonly IDictionary<string, ProjectInfo> _projectsByGuid = new Dictionary<string, ProjectInfo>();
private readonly IDictionary<string, ProjectInfo> _foldersByPath = new Dictionary<string, ProjectInfo>();
private readonly IList<ProjectInfo> _assemblyRefProjectFilesHandled = new List<ProjectInfo>();
private readonly IList<ProjectInfo> _projectRefProjectFilesHandled = new List<ProjectInfo>();
private readonly IList<string> _projectFiles = new List<string>();
private readonly Engine m_Engine = new Engine();
private Arguments _arguments = new Arguments();
#endregion
#region Private sub-classes
#endregion
#region Private methods
#region Command-line arguments handling
private static void Header()
{
Console.WriteLine("SolGen: C# Solution Generator, Version " + Assembly.GetExecutingAssembly().GetName().Version);
Console.WriteLine("Copyright © 2008 François St-Arnaud and John Wood under MIT License.");
Console.WriteLine("For more information, see http://codeplex.com/SolGen");
}
private static void Usage()
{
Console.WriteLine("\nSolGen scans the specified root folder and subfolders looking for");
Console.WriteLine("C# project files (.csproj). For each project file found, it creates up to 4 files:\n");
Console.WriteLine("If /refs is set to 'project' or 'both':");
Console.WriteLine("1) a .projRefs.csproj file which contains project references");
Console.WriteLine("2) a .projRefs.sln file that contains the current project and all referenced projects\n");
Console.WriteLine("If /refs is set to 'assembly' or 'both':");
Console.WriteLine("3) a .dllRefs.csproj file that contains assembly references");
Console.WriteLine("4) a .dllRefs.sln file that contains only .dllRefs.csproj\n");
Console.WriteLine("Also, SolGen creates top-level solution files for each subfolder of the specified root");
Console.WriteLine("until a .csproj file is found. A top-level solution file is a .projRefs.sln file that");
Console.WriteLine("contains all created .projRefs.csproj files below the root folder.\n");
Console.WriteLine("Usage: solgen [switches]");
Console.WriteLine(" /root:folder, the root folder where to start looking for projects");
Console.WriteLine("</refs:[assembly|project|both]>, optional, default: 'both'");
Console.WriteLine("</ignore:folder,(...)>, optional, the list of sub-folders to ignore (e.g.: .svn)");
Console.WriteLine("</ignorePattern:match,(...)>, optional, the list of text matches in project names to ignore (e.g.: Test)");
Console.WriteLine("</probe:folder,(...)>, optional, additional folders where to look for referenced projects\n");
Console.WriteLine("</overwrite>, optional, when 'refs' is 'assembly' or 'project', overwrite existing files\n");
Console.WriteLine("</solution:[flat|deep]>, optional, organize projects in folder structure, default: 'flat'\n");
}
private static void GetArg(string line, string prefix, ref string output)
{
if (line.ToUpper().StartsWith(prefix.ToUpper()))
{
output = line.Substring(prefix.Length);
}
}
//TODO: JAG move to argument
private static bool ParseArguments(ICollection<string> args, out Arguments arguments)
{
string probe = null;
string refs = null;
string ignore = null;
string ignorePattern = null;
string overwrite = null;
string solution = null;
arguments = new Arguments();
if (args.Count == 0)
{
Usage();
return false;
}
foreach (var str in args)
{
GetArg(str, "/probe:", ref probe);
GetArg(str, "/hintpath:", ref arguments.HintPath);
GetArg(str, "/root:", ref arguments.RootFolder);
GetArg(str, "/ignore:", ref ignore);
GetArg(str, "/ignorePattern:", ref ignorePattern);
GetArg(str, "/refs:", ref refs);
GetArg(str, "/overwrite", ref overwrite);
GetArg(str, "/solution:", ref solution);
}
if (arguments.RootFolder == null)
{
Usage();
return false;
}
if (!Directory.Exists(arguments.RootFolder))
{
Console.WriteLine("\nSpecified root folder does not exists.");
Console.WriteLine(arguments.RootFolder);
return false;
}
arguments.RootFolder = Path.GetFullPath(arguments.RootFolder);
if (ignore != null)
{
var ignores = ignore.Split(new[] { ';', ',' });
arguments.Ignore = new List<string>(Array.ConvertAll(ignores, Path.GetFullPath));
}
arguments.IgnorePattern = new List<string>();
if (ignorePattern != null)
{
arguments.IgnorePattern = ignorePattern.Split(new[] { ';', ',' }).Select(s => s.Trim()).ToList();
}
if (overwrite != null)
{
if (overwrite != String.Empty)
{
Console.WriteLine("\nOverwrite argument is binary.");
return false;
}
arguments.Overwrite = true;
}
if (solution != null)
{
switch (solution.ToLower())
{
case "flat":
arguments.Solution = SolutionTypes.Flat;
break;
case "deep":
arguments.Solution = SolutionTypes.Deep;
break;
default:
Console.WriteLine("\nUnknown /solution switch value " + solution + "!.");
return false;
}
}
if (probe != null)
{
var probes = probe.Split(new[] { ';', ',' });
arguments.ProbeFolders = new List<string>(probes);
foreach (var probeFolder in arguments.ProbeFolders.Where(f => !Directory.Exists(f)))
{
Console.WriteLine("\nSpecified probe folder does not exists.");
Console.WriteLine(probeFolder);
return false;
}
}
else
{
arguments.ProbeFolders = new List<string>();
}
arguments.ProbeFolders.Add(arguments.RootFolder);
if (refs != null)
{
switch (refs.ToLower())
{
case "assembly":
arguments.Refs = RefTypes.Assembly;
break;
case "project":
arguments.Refs = RefTypes.Project;
break;
case "both":
arguments.Refs = RefTypes.Both;
break;
default:
Console.WriteLine("\nUnknown /refs switch value " + refs + "!.");
return false;
}
}
if (arguments.HintPath == null)
{
arguments.HintPath = string.Empty;
}
return true;
}
#endregion
#region Utility methods
private static string GetAssemblyNameFromFullyQualifiedName(string fullyQualifiedName)
{
var assemblyName = fullyQualifiedName ?? string.Empty;
if (assemblyName.IndexOf(",") != -1)
{
assemblyName = assemblyName.Remove(assemblyName.IndexOf(","));
}
return assemblyName;
}
private static string GetRelativePath(string fromPath, string toPath)
{
var fromDirectories = fromPath.Split(Path.DirectorySeparatorChar);
var toDirectories = toPath.Split(Path.DirectorySeparatorChar);
// Get the shortest of the two paths
var length = fromDirectories.Length < toDirectories.Length
? fromDirectories.Length
: toDirectories.Length;
int lastCommonRoot = -1;
int index;
// Find common root
for (index = 0; index < length; index++)
{
if (fromDirectories[index].Equals(toDirectories[index], StringComparison.InvariantCultureIgnoreCase))
{
lastCommonRoot = index;
}
else
{
break;
}
}
// If we didn't find a common prefix then abandon
if (lastCommonRoot == -1)
{
return null;
}
// Add the required number of "..\" to move up to common root level
var relativePath = new StringBuilder();
for (index = lastCommonRoot + 1; index < fromDirectories.Length; index++)
{
relativePath.Append(".." + Path.DirectorySeparatorChar);
}
// Add on the folders to reach the destination
for (index = lastCommonRoot + 1; index < toDirectories.Length - 1; index++)
{
relativePath.Append(toDirectories[index] + Path.DirectorySeparatorChar);
}
relativePath.Append(toDirectories[toDirectories.Length - 1]);
return relativePath.ToString();
}
private static bool IsInIgnoreList(string folder, IEnumerable<string> ignoreList)
{
return folder != null && ignoreList != null &&
ignoreList.Any(ignore => folder.StartsWith(ignore, StringComparison.InvariantCultureIgnoreCase));
}
#endregion
#region Project and DLL probing and reference handling methods
/// <summary>
/// Extracts information from a project file
/// </summary>
private void ProcessProject(string folder, string file)
{
// ignore projects that match to the IgnorePattern
if (_arguments.IgnorePattern.Any(pattern => Regex.IsMatch(file, pattern)))
{
return;
}
// ignore projects that *we* created...
if ((file.EndsWith(ProjectRefs + CsProjFileExtension)) ||
(file.EndsWith(AssemblyRefs + CsProjFileExtension)))
{
return;
}
_projectFiles.Add(file);
var qualifiedFile = Path.Combine(folder, file);
Project project = new Project(m_Engine);
project.Load(qualifiedFile);
ProjectInfo pinfo = new ProjectInfo
{
MsBuildProject = project,
ProjectName = Path.GetFileNameWithoutExtension(file),
Filename = qualifiedFile
};
foreach (BuildPropertyGroup buildPropertyGroup in project.PropertyGroups)
{
foreach (BuildProperty buildProperty in buildPropertyGroup)
{
if (buildProperty.Name == AssemblyName)
{
pinfo.AssemblyName = buildProperty.Value;
}
else if (buildProperty.Name == ProjectGuid)
{
pinfo.ProjectGuid = buildProperty.Value;
}
}
if ((pinfo.AssemblyName != null) && (pinfo.ProjectGuid != null))
{
break;
}
}
Debug.Assert(pinfo.AssemblyName != null);
Debug.Assert(pinfo.ProjectGuid != null);
if (_projectsByName.ContainsKey(pinfo.AssemblyName.ToUpper()))
{
ProjectInfo existing = _projectsByName[pinfo.AssemblyName.ToUpper()];
Console.WriteLine("\nThe project " + pinfo.ProjectName);
Console.WriteLine("With assembly name " + pinfo.AssemblyName);
Console.WriteLine("Is defined in more than one project file; using:");
Console.WriteLine(existing.Filename);
}
else
{
_projectsByName.Add(pinfo.AssemblyName.ToUpper(), pinfo);
}
// only record the project if it's not already there
if (!_projectsByFile.ContainsKey(pinfo.Filename.ToUpper()))
{
_projectsByFile.Add(pinfo.Filename.ToUpper(), pinfo);
}
// keep a record of the project keyed by its GUID
if (!_projectsByGuid.ContainsKey(pinfo.ProjectGuid))
{
_projectsByGuid.Add(pinfo.ProjectGuid, pinfo);
}
else
{
Console.WriteLine("\nProjects with same GUID found ! " + pinfo.ProjectGuid);
Console.WriteLine(pinfo.Filename);
Console.WriteLine(_projectsByGuid[pinfo.ProjectGuid].Filename);
}
}
/// <summary>
/// Locates all projects under the given folder
/// </summary>
private void FindAllProjects(string folder)
{
foreach (string file in Directory.GetFiles(folder, AllCsProj))
{
ProcessProject(folder, file);
}
foreach (string subFolder in Directory.GetDirectories(folder, AllFiles))
{
if (IsInIgnoreList(subFolder, _arguments.Ignore))
{
continue;
}
string newFolder = Path.Combine(folder, subFolder);
FindAllProjects(newFolder);
}
}
/// <summary>
/// Retrieves the information on references in the project
/// </summary>
private void GatherProjectReferences()
{
foreach (ProjectInfo projectInfo in _projectsByName.Values)
{
Debug.Assert(projectInfo.MsBuildProject != null);
foreach (BuildItemGroup buildItemGroup in projectInfo.MsBuildProject.ItemGroups)
{
foreach (BuildItem buildItem in buildItemGroup)
{
// Add assembly references
if (buildItem.Name == Reference)
{
projectInfo.References.Add(GetAssemblyNameFromFullyQualifiedName(buildItem.Include));
}
// Add project references
else if (buildItem.Name == ProjectReference)
{
projectInfo.References.Add(buildItem.GetMetadata(Project));
}
}
}
}
}
private void CollectRelatedProjects(string projectFile, IDictionary<string, ProjectInfo> projects,
IList<string> seenProjects = null)
{
if (seenProjects == null)
{
seenProjects = new List<string>();
}
string upperCaseProjectFile = projectFile.ToUpper();
Debug.Assert(_projectsByFile.ContainsKey(upperCaseProjectFile));
ProjectInfo pinfo = _projectsByFile[upperCaseProjectFile];
if (!projects.ContainsKey(pinfo.AssemblyName))
{
projects.Add(pinfo.AssemblyName, pinfo);
}
if (!seenProjects.Contains(pinfo.AssemblyName))
{
seenProjects.Add(pinfo.AssemblyName);
}
foreach (string assembly in pinfo.References)
{
ProjectInfo reference;
_projectsByName.TryGetValue(assembly.ToUpper(), out reference);
if (reference != null)
{
if (seenProjects.Contains(reference.AssemblyName))
{
seenProjects.Add(reference.AssemblyName);
throw new CircularAssemblyReferenceException(seenProjects);
}
CollectRelatedProjects(reference.Filename, projects, seenProjects);
}
}
seenProjects.Remove(pinfo.AssemblyName);
}
private void ResolveProjectGuids()
{
foreach (ProjectInfo pinfo in _projectsByName.Values)
{
List<string> newReferences = new List<string>();
foreach (string reference in pinfo.References)
{
string newReference = reference;
if (reference.StartsWith("{"))
{
// must be a GUID...
if (_projectsByGuid.ContainsKey(reference))
{
newReference = _projectsByGuid[reference].AssemblyName;
}
else
{
Console.WriteLine("\nReferenced GUID not found !");
Console.WriteLine(reference);
Console.WriteLine(pinfo.Filename);
}
}
newReferences.Add(newReference);
}
pinfo.References = newReferences;
}
}
private void SetFolderParents()
{
int rootIndex = _arguments.RootFolder.Length + 1;
foreach (ProjectInfo projectInfo in _projectsByFile.Values)
{
// TODO Substring unsafe; assumes Filename.StartsWith(RootFolder)
string projectPath = Path.GetDirectoryName(projectInfo.Filename).Substring(rootIndex);
string[] projParts = projectPath.Split(Path.DirectorySeparatorChar);
StringBuilder sb = new StringBuilder(_arguments.RootFolder);
ProjectInfo parent = null;
for (int projIndex = 0; projIndex < projParts.Length; projIndex++)
{
string folderName = projParts[projIndex];
ProjectInfo folderInfo;
sb.Append(Path.DirectorySeparatorChar);
sb.Append(folderName);
_foldersByPath.TryGetValue(sb.ToString(), out folderInfo);
if (folderInfo == null)
{
folderInfo = new ProjectInfo
{
ProjectGuid = "{" + Guid.NewGuid().ToString().ToUpper() + "}",
ProjectName = folderName,
Filename = folderName
};
if (parent != null)
{
folderInfo.FolderGuid = parent.ProjectGuid;
}
_foldersByPath.Add(sb.ToString(), folderInfo);
}
parent = folderInfo;
}
}
}
#endregion
#region Project file creation methods
private void CreateProjectReferenceProjectFile(IDictionary<string, ProjectInfo> projects)
{
foreach (var projectInfo in projects.Values.Where(projectInfo => !_projectRefProjectFilesHandled.Contains(projectInfo)))
{
Debug.Assert(projectInfo.MsBuildProject != null);
var assemblyReferencesToMove = new List<BuildItem>();
BuildItemGroup assemblyReferencesBuildItemGroup = null;
BuildItemGroup projectReferencesBuildItemGroup = null;
if ( projectInfo.MsBuildProject.ItemGroups == null ) continue;
foreach (BuildItemGroup buildItemGroup in projectInfo.MsBuildProject.ItemGroups)
{
foreach (BuildItem buildItem in buildItemGroup)
{
// Find all assembly references to convert to project references
if (buildItem.Name == Reference)
{
// Keep a bookmark on the BuildItemGroup that holds assembly references
assemblyReferencesBuildItemGroup = buildItemGroup;
// Take a look at all assembly references to identify those to convert to project references
ProjectInfo referencedProjectInfo;
var projectName = GetAssemblyNameFromFullyQualifiedName(buildItem.Include);
projects.TryGetValue(projectName, out referencedProjectInfo);
if (referencedProjectInfo != null)
{
assemblyReferencesToMove.Add(buildItem);
}
}
if (buildItem.Name == ProjectReference)
{
// Keep a bookmark on the BuildItemGroup that holds project references
projectReferencesBuildItemGroup = buildItemGroup;
}
}
}
// If no assembly references found or none to move, nothing to change; save file as-is
if (assemblyReferencesBuildItemGroup == null || assemblyReferencesToMove.Count == 0)
{
//Console.WriteLine("Nothing to do.");
}
else
{
// If no project reference group exists, create one.
if (projectReferencesBuildItemGroup == null)
{
projectReferencesBuildItemGroup = projectInfo.MsBuildProject.AddNewItemGroup();
}
// Remove assembly reference and replace by corresponding project reference.
foreach (BuildItem buildItem in assemblyReferencesToMove)
{
assemblyReferencesBuildItemGroup.RemoveItem(buildItem);
string replacementAssemblyName = GetAssemblyNameFromFullyQualifiedName(buildItem.Include);
var replacementProjectInfo = _projectsByName[replacementAssemblyName.ToUpperInvariant()];
string remplacementInclude = GetRelativePath(Path.GetDirectoryName(projectInfo.Filename), replacementProjectInfo.Filename);
BuildItem newBuildItem = projectReferencesBuildItemGroup.AddNewItem(ProjectReference, remplacementInclude);
newBuildItem.SetMetadata("Project", replacementProjectInfo.ProjectGuid);
newBuildItem.SetMetadata("Name", replacementProjectInfo.ProjectName);
}
}
StringBuilder sb = new StringBuilder(Path.ChangeExtension(projectInfo.Filename, null));
if (_arguments.Overwrite == false)
{
sb.Append(ProjectRefs);
}
sb.Append(CsProjFileExtension);
string projectFileName = sb.ToString();
projectInfo.MsBuildProject.Save(projectFileName);
_projectRefProjectFilesHandled.Add(projectInfo);
}
}
private void CreateAssemblyReferenceProjectFile(IDictionary<string, ProjectInfo> projects)
{
foreach (var projectInfo in projects.Values.Where(projectInfo => !_assemblyRefProjectFilesHandled.Contains(projectInfo)))
{
Debug.Assert(projectInfo.MsBuildProject != null);
var projectReferencesToMove = new List<BuildItem>();
BuildItemGroup assemblyReferencesBuildItemGroup = null;
BuildItemGroup projectReferencesBuildItemGroup = null;
if (projectInfo.MsBuildProject.ItemGroups == null) continue;
foreach (BuildItemGroup buildItemGroup in projectInfo.MsBuildProject.ItemGroups)
{
foreach (BuildItem buildItem in buildItemGroup)
{
if (buildItem.Name == Reference)
{
// Keep a bookmark on the BuildItemGroup that holds assembly references
assemblyReferencesBuildItemGroup = buildItemGroup;
}
if (buildItem.Name == ProjectReference)
{
// Keep a bookmark on the BuildItemGroup that holds project references
projectReferencesBuildItemGroup = buildItemGroup;
// Take a look at all project references to identify those to convert to assembly references
ProjectInfo referencedProjectInfo;
var projectGuid = buildItem.GetMetadata(Project);
_projectsByGuid.TryGetValue(projectGuid, out referencedProjectInfo);
if (referencedProjectInfo != null)
{
projectReferencesToMove.Add(buildItem);
}
}
}
}
// If no project references found or none to move, nothing to change; save file as-is
if ( projectReferencesBuildItemGroup != null && projectReferencesToMove.Count > 0 )
{
if (assemblyReferencesBuildItemGroup == null)
{
assemblyReferencesBuildItemGroup = projectInfo.MsBuildProject.AddNewItemGroup();
}
foreach (var buildItem in projectReferencesToMove)
{
projectReferencesBuildItemGroup.RemoveItem(buildItem);
// Get the assembly name corresponding to the project GUID
foreach (var pinfo in projects.Values)
{
if (pinfo.ProjectGuid == buildItem.GetMetadata(Project))
{
var newBuildItem =
assemblyReferencesBuildItemGroup.AddNewItem(Reference, pinfo.AssemblyName);
newBuildItem.SetMetadata("Private", bool.FalseString);
newBuildItem.SetMetadata("SpecificVersion", bool.FalseString);
if (_arguments.HintPath != string.Empty)
{
var relative =
GetRelativePath(Path.GetDirectoryName(projectInfo.Filename), _arguments.HintPath);
newBuildItem.SetMetadata("HintPath", Path.Combine(relative, pinfo.AssemblyName) + ".dll");
}
break;
}
}
}
}
StringBuilder sb = new StringBuilder(Path.ChangeExtension(projectInfo.Filename, null));
if (_arguments.Overwrite == false)
{
sb.Append(AssemblyRefs);
}
sb.Append(CsProjFileExtension);
string projectFileName = sb.ToString();
projectInfo.MsBuildProject.Save(projectFileName);
_assemblyRefProjectFilesHandled.Add(projectInfo);
}
}
/// <summary>
/// Generates a new project file containing only references to project GUIDs
/// </summary>
private void CreateProjectFiles(IDictionary<string, ProjectInfo> projects)
{
if (_arguments.Refs == RefTypes.Project || _arguments.Refs == RefTypes.Both)
{
CreateProjectReferenceProjectFile(projects);
}
if (_arguments.Refs == RefTypes.Assembly || _arguments.Refs == RefTypes.Both)
{
CreateAssemblyReferenceProjectFile(projects);
}
}
#endregion
#region Solution file creation methods
private void WriteSolutionFile(string solutionFile, IDictionary<string, ProjectInfo> projects, RefTypes refType)
{
Debug.Assert(solutionFile != null);
using (var writer = new StreamWriter(solutionFile))
{
writer.WriteLine("Microsoft Visual Studio Solution File, Format Version 11.00");
writer.WriteLine("# Visual Studio 2010");
// Folders
if (_arguments.Solution == SolutionTypes.Deep)
{
foreach ( var kvp in _foldersByPath.Where( kvp => kvp.Key.StartsWith(Path.GetDirectoryName(solutionFile) + Path.DirectorySeparatorChar)))
{
WriteProjectEntry(writer, kvp.Value, true, Path.GetDirectoryName(solutionFile));
}
}
// Projects
foreach (var projectInfo in projects.Values)
{
var sb = new StringBuilder(Path.Combine(Path.GetDirectoryName(projectInfo.Filename), Path.GetFileNameWithoutExtension(projectInfo.Filename)));
if (_arguments.Overwrite == false)
{
switch (refType)
{
case RefTypes.Project:
sb.Append(ProjectRefs);
break;
case RefTypes.Assembly:
sb.Append(AssemblyRefs);
break;
}
}
sb.Append(CsProjFileExtension);
// Create a copy of the project information to avoid modifying the project information in the reference
// m_ProjectsByName, m_ProjectsByFile, m_ProjectsByGuid data structures.
var projectInfoCopy = projectInfo.ShallowCopy();
projectInfoCopy.Filename = sb.ToString();
WriteProjectEntry(writer, projectInfoCopy, false, Path.GetDirectoryName(solutionFile));
}
// Project and folder relations
if (_arguments.Solution == SolutionTypes.Deep)
{
writer.WriteLine("Global");
writer.WriteLine("\tGlobalSection(NestedProjects) = preSolution");
const string format = "\t\t{0} = {1}";
// Folder relations
// TODO: improve; output only required folder relations
foreach (var folderInfo in _foldersByPath.Values.Where(i => i.ProjectGuid != null && i.FolderGuid != null))
{
writer.WriteLine(format, folderInfo.ProjectGuid, folderInfo.FolderGuid);
}
// Project relations
foreach (var projectInfo in projects.Values)
{
ProjectInfo folderInfo;
_foldersByPath.TryGetValue(Path.GetDirectoryName(projectInfo.Filename), out folderInfo);
if (folderInfo != null)
{
writer.WriteLine(format, projectInfo.ProjectGuid, folderInfo.ProjectGuid);
}
}
writer.WriteLine("\tEndGlobalSection");
writer.WriteLine("EndGlobal");
}
writer.Close();
}
}
private static void WriteProjectEntry(TextWriter writer, ProjectInfo projectInfo, bool folder, string rootFolder)
{
string projectPath;
string guid;
if (folder == false)
{
// TODO
projectPath = projectInfo.Filename.StartsWith(rootFolder)
? projectInfo.Filename.Substring(rootFolder.Length + 1)
: projectInfo.Filename;
guid = CsProjGuid;
}
else
{
projectPath = projectInfo.ProjectName;
guid = FolderGuid;
}
var format = "Project('{0}') = '{1}', '{2}', '{3}'".Replace('\'', '"');
writer.WriteLine(format, guid, projectInfo.ProjectName, projectPath, projectInfo.ProjectGuid);
writer.WriteLine("EndProject");
}
private void CreateSolutionFiles(string projectFile, IDictionary<string, ProjectInfo> projects)
{
Debug.Assert(projectFile != null);
// Create single project solution file for project with assembly references
var singleProject = new Dictionary<string, ProjectInfo>
{
{projectFile, _projectsByFile[projectFile.ToUpper()]}
};
var sb = new StringBuilder(Path.Combine(Path.GetDirectoryName(projectFile), Path.GetFileNameWithoutExtension(projectFile)));
if (_arguments.Overwrite == false)
{
sb.Append(AssemblyRefs);
}
sb.Append(SolutionFileExtension);
var singleProjectSolutionFile = sb.ToString();
WriteSolutionFile(singleProjectSolutionFile, singleProject, RefTypes.Assembly);
// Create "complete" solution with all projects required to fulfill project references
if (_arguments.Refs == RefTypes.Project || _arguments.Refs == RefTypes.Both)
{
var assemblyRefsSolutionFile = Path.Combine(Path.GetDirectoryName(projectFile), Path.GetFileNameWithoutExtension(projectFile)) + ProjectRefs + SolutionFileExtension;
WriteSolutionFile(assemblyRefsSolutionFile, projects, RefTypes.Project);
}
}
private void CreateTopLevelSolutionFiles(string folder, IDictionary<string, Dictionary<string, ProjectInfo>> allRelatedProjects)
{
Debug.Assert(!string.IsNullOrEmpty(folder) && Directory.Exists(folder));
// No need to create a top-level solution file if root folder contains a .csproj file
// No need to create a top-level solution file if root folder contains no subfolders
if (Directory.GetFiles(folder, AllCsProj).Length > 0 || Directory.GetDirectories(folder).Length == 0)
{
return;
}
// Recursively process all sub-folders of root folder
foreach (string subFolder in Directory.GetDirectories(folder, AllFiles))
{
// Ignore sub-folders in ignore list
if (IsInIgnoreList(subFolder, _arguments.Ignore))
{
continue;
}
// Ignore sub-folders that contain a project file
if (Directory.GetFiles(subFolder, AllCsProj).Length > 0)
{
continue;
}
CreateTopLevelSolutionFiles(subFolder, allRelatedProjects);
}
// Create a solution containing a list of all project files under this subfolder
IDictionary<string, ProjectInfo> projects = new Dictionary<string, ProjectInfo>();
foreach (KeyValuePair<string, ProjectInfo> kvp in _projectsByFile)
{
if (kvp.Key.StartsWith(folder, StringComparison.InvariantCultureIgnoreCase) && !projects.ContainsKey(kvp.Key))
{
projects.Add(kvp.Key, kvp.Value);
Dictionary<string, ProjectInfo> relatedProjects;
allRelatedProjects.TryGetValue(kvp.Value.Filename, out relatedProjects);
if (relatedProjects != null)
{
foreach (var kvp2 in relatedProjects)
{
var key = kvp2.Value.Filename.ToUpperInvariant();
if (!projects.ContainsKey(key))
{
projects.Add(key, kvp2.Value);
}
}
}
}
}
if (projects.Count > 0)
{
StringBuilder sb = new StringBuilder(folder);
string[] parts = folder.Split(Path.DirectorySeparatorChar);
if (parts.Length > 1)
{
sb.Append(Path.DirectorySeparatorChar);
sb.Append(parts[parts.Length - 2]);
}
sb.Append('.');
sb.Append(parts[parts.Length - 1]);
if (_arguments.Overwrite == false)
{
sb.Append(ProjectRefs);
}
sb.Append(SolutionFileExtension);
string solutionFileName = sb.ToString();
WriteSolutionFile(solutionFileName, projects, RefTypes.Project);
}
}
#endregion
#endregion
#region Start
public void Start(string[] args)
{
Header();
if (!ParseArguments(args, out _arguments))
{
return;
}
Console.WriteLine("\nProbing for projects...");
// Find all projects under that specified folder
foreach (string folder in _arguments.ProbeFolders)
{
FindAllProjects(folder);
}
// Retrieve project references for all loaded projects
GatherProjectReferences();
// Resolve any project GUIDs in the project references
ResolveProjectGuids();
// Assign GUIDs to all subfolders
SetFolderParents();