Repository navigation
Expand file tree
/
Copy pathSolutionExtensions.cs
More file actions
120 lines (109 loc) · 4.05 KB
/
Copy pathSolutionExtensions.cs
File metadata and controls
120 lines (109 loc) · 4.05 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using EnvDTE;
using EnvDTE80;
namespace NuGet.VisualStudio
{
public static class SolutionExtensions
{
/// <summary>
/// Get the list of all supported projects in the current solution. This method
/// recursively iterates through all projects.
/// </summary>
public static IEnumerable<Project> GetAllProjects(this Solution solution)
{
if (solution == null || !solution.IsOpen)
{
yield break;
}
var projects = new Stack<Project>();
foreach (Project project in solution.Projects)
{
if (!project.IsExplicitlyUnsupported())
{
projects.Push(project);
}
}
while (projects.Any())
{
Project project = projects.Pop();
if (project.IsSupported())
{
yield return project;
}
else if (project.IsExplicitlyUnsupported())
{
// do not drill down further if this project is explicitly unsupported, e.g. LightSwitch projects
continue;
}
ProjectItems projectItems = null;
try
{
// bug 1138: Oracle Database Project doesn't implement the ProjectItems property
projectItems = project.ProjectItems;
}
catch (NotImplementedException)
{
continue;
}
// ProjectItems property can be null if the project is unloaded
if (projectItems != null)
{
foreach (ProjectItem projectItem in projectItems)
{
try
{
if (projectItem.SubProject != null)
{
projects.Push(projectItem.SubProject);
}
}
catch (NotImplementedException)
{
// Some project system don't implement the SubProject property,
// just ignore those
continue;
}
}
}
}
}
public static string GetName(this Solution solution)
{
return (string)solution.Properties.Item("Name").Value;
}
public static Project GetSolutionFolder(this Solution solution, string solutionFolderName)
{
Solution2 solution2 = (Solution2)solution;
Project project = solution2.Projects
.OfType<Project>()
.FirstOrDefault(p => p.Name.Equals(solutionFolderName, StringComparison.OrdinalIgnoreCase));
if (project == null)
{
try
{
project = solution2.AddSolutionFolder(solutionFolderName);
}
catch (Exception)
{
// VWD doesn't allow adding solution folder.
// In that case, just silently ignore and return
}
}
return project;
}
public static void AddFolderToSolution(this Solution solution, string solutionFolderName, string physicalFolderPath)
{
var project = solution.GetSolutionFolder(solutionFolderName);
if (project != null)
{
foreach (string file in Directory.EnumerateFiles(physicalFolderPath))
{
project.ProjectItems.AddFromFile(file);
}
}
}
}
}