-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectExtensions.cs
More file actions
122 lines (104 loc) · 4.52 KB
/
Copy pathProjectExtensions.cs
File metadata and controls
122 lines (104 loc) · 4.52 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
121
122
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;
namespace EfModelGenerator
{
public static class ProjectExtensions
{
public static bool IsWebProject(this Project project)
{
return project.GetProjectTypes().Any<string>((Func<string, bool>)(g =>
{
if (!g.EqualsIgnoreCase("{349C5851-65DF-11DA-9384-00065B846F21}"))
return g.EqualsIgnoreCase("{E24C65DC-7377-472B-9ABA-BC803B73C61A}");
return true;
}));
}
public static string ProjectPath(this Project project)
{
return project.Properties.Item("FullPath").Value as string;
}
/// <summary>
/// Get the executing projects configuration
/// </summary>
/// <returns></returns>
public static System.Configuration.Configuration GetProjectConfiguration(this Project project)
{
XDocument document = XDocument.Load(Path.Combine(project.ProjectPath() as string, project.IsWebProject() ? "Web.config" : "App.config"));
string tempFileName = Path.GetTempFileName();
document.Save(tempFileName);
return System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
{
ExeConfigFilename = tempFileName
}, ConfigurationUserLevel.None);
}
private static IEnumerable<string> GetProjectTypes(this Project project)
{
IVsSolution service;
using (ServiceProvider serviceProvider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)project.DTE))
service = (IVsSolution)serviceProvider.GetService(typeof(IVsSolution));
IVsHierarchy ppHierarchy;
int projectOfUniqueName = service.GetProjectOfUniqueName(project.UniqueName, out ppHierarchy);
if (projectOfUniqueName != 0)
Marshal.ThrowExceptionForHR(projectOfUniqueName);
string pbstrProjTypeGuids;
int projectTypeGuids = ((IVsAggregatableProject)ppHierarchy).GetAggregateProjectTypeGuids(out pbstrProjTypeGuids);
if (projectTypeGuids != 0)
Marshal.ThrowExceptionForHR(projectTypeGuids);
return (IEnumerable<string>)pbstrProjTypeGuids.Split(';');
}
public static bool EqualsIgnoreCase(this string s1, string s2)
{
return string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase);
}
public static CodeElement GetNameSpace(this ProjectItem projectItem)
{
var model = projectItem.FileCodeModel;
foreach (CodeElement element in model.CodeElements)
{
if (element.Kind == vsCMElement.vsCMElementNamespace)
{
return element;
}
}
return null;
}
public static bool IsDerivedFromDbContext(this ProjectItem item)
{
var model = item.FileCodeModel;
if (model == null) return false;
var isDerivedFromDbContext = false;
foreach (CodeElement element in model.CodeElements)
{
if (element is EnvDTE.CodeNamespace)
{
var namespaceElement = element as EnvDTE.CodeNamespace;
foreach (var property in namespaceElement.Members)
{
var codeType = property as CodeType;
if (codeType == null) continue;
foreach (var member in codeType.Bases)
{
var codeClass = member as CodeClass;
if (codeClass == null) continue;
var name = codeClass.Name;
if (!string.IsNullOrEmpty(name) && name.EqualsIgnoreCase("DbContext"))
{
isDerivedFromDbContext = true;
break;
}
}
}
}
}
return isDerivedFromDbContext;
}
}
}