-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
ProjectHelpers.cs
234 lines (195 loc) · 7.71 KB
/
ProjectHelpers.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
using EnvDTE;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using System;
using System.Collections.Generic;
using System.IO;
namespace MadsKristensen.EditorExtensions
{
internal static class ProjectHelpers
{
public static string GetRootFolder()
{
try
{
EnvDTE80.DTE2 dte = EditorExtensionsPackage.DTE;
Project activeProject = null;
if (dte.Solution.Projects.Count == 1 && !string.IsNullOrEmpty(dte.Solution.Projects.Item(1).FullName))
{
return dte.Solution.Projects.Item(1).Properties.Item("FullPath").Value.ToString();
}
Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
{
activeProject = activeSolutionProjects.GetValue(0) as Project;
}
return activeProject.Properties.Item("FullPath").Value.ToString();
}
catch (Exception ex)
{
Logger.Log(ex);
return string.Empty;
}
}
internal static bool AddFileToActiveProject(string fileName, string itemType = null)
{
Project project = GetActiveProject();
if (project != null)
{
string projectFilePath = project.Properties.Item("FullPath").Value.ToString();
string projectDirPath = Path.GetDirectoryName(projectFilePath);
if (fileName.StartsWith(projectDirPath, StringComparison.OrdinalIgnoreCase))
{
ProjectItem item = project.ProjectItems.AddFromFile(fileName);
if (itemType != null && item != null && !project.FullName.Contains("://"))
{
try
{
item.Properties.Item("ItemType").Value = itemType;
}
catch { }
}
}
}
return false;
}
public static Project GetActiveProject()
{
Project activeProject = null;
try
{
Array activeSolutionProjects = EditorExtensionsPackage.DTE.ActiveSolutionProjects as Array;
if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
{
activeProject = activeSolutionProjects.GetValue(0) as Project;
}
}
catch
{
Logger.Log("Error getting the active project");
}
return activeProject;
}
public static string ToAbsoluteFilePath(string relativeUrl, string rootFolder = null)
{
string imageUrl = relativeUrl.Trim(new[] { '\'', '"' });
string filePath = string.Empty;
if (imageUrl.StartsWith("/", StringComparison.Ordinal))
{
string root = rootFolder ?? ProjectHelpers.GetRootFolder();
if (root.Contains("://"))
{
filePath = root + imageUrl;
}
else if (!string.IsNullOrEmpty(root))
{
if (!Directory.Exists(root))
{
filePath = new FileInfo(root).Directory + imageUrl;
}
else
{
return root + imageUrl.Replace("/", "\\");
}
}
}
else
{
FileInfo fi = new FileInfo(EditorExtensionsPackage.DTE.ActiveDocument.FullName);
DirectoryInfo dir = fi.Directory;
while (imageUrl.Contains("../"))
{
imageUrl = imageUrl.Remove(imageUrl.IndexOf("../", StringComparison.Ordinal), 3);
dir = dir.Parent;
}
filePath = Path.Combine(dir.FullName, imageUrl.Replace("/", "\\"));
}
return filePath;
}
public static ITextBuffer GetCurentTextBuffer()
{
return GetCurentTextView().TextBuffer;
}
public static IWpfTextView GetCurentTextView()
{
var componentModel = GetComponentModel();
if (componentModel != null)
{
var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var textManager = (IVsTextManager)ServiceProvider.GlobalProvider.GetService(typeof(SVsTextManager));
IVsTextView activeView = null;
textManager.GetActiveView(1, null, out activeView);
return editorAdapter.GetWpfTextView(activeView);
}
return null;
}
public static IComponentModel GetComponentModel()
{
return (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel));
}
public static IEnumerable<string> GetSelectedItemPaths()
{
var items = (Array)EditorExtensionsPackage.DTE.ToolWindows.SolutionExplorer.SelectedItems;
foreach (UIHierarchyItem selItem in items)
{
var item = selItem.Object as ProjectItem;
if (item != null)
{
yield return item.Properties.Item("FullPath").Value.ToString();
}
}
}
public static bool CheckOutFileFromSourceControl(string fileName)
{
try
{
var dte = EditorExtensionsPackage.DTE;
if (File.Exists(fileName) && dte.Solution.FindProjectItem(fileName) != null)
{
if (dte.SourceControl.IsItemUnderSCC(fileName) && !dte.SourceControl.IsItemCheckedOut(fileName))
{
dte.SourceControl.CheckOutItem(fileName);
}
return true;
}
}
catch (Exception ex)
{
Logger.Log(ex);
}
return false;
}
public static string GetSolutionFolderPath()
{
EnvDTE.Solution solution = EditorExtensionsPackage.DTE.Solution;
if (solution == null || string.IsNullOrEmpty(solution.FullName))
return null;
return Path.GetDirectoryName(solution.FullName);
}
public static string GetProjectFolder(string fileNameOrFolder)
{
if (string.IsNullOrEmpty(fileNameOrFolder))
return GetRootFolder();
ProjectItem item = EditorExtensionsPackage.DTE.Solution.FindProjectItem(fileNameOrFolder);
if (item == null || item.ContainingProject == null || string.IsNullOrEmpty(item.ContainingProject.FullName)) // Solution items
return null;
return item.ContainingProject.Properties.Item("FullPath").Value.ToString();
}
public static IEnumerable<ProjectItem> GetSelectedItems()
{
var items = (Array)EditorExtensionsPackage.DTE.ToolWindows.SolutionExplorer.SelectedItems;
foreach (UIHierarchyItem selItem in items)
{
var item = selItem.Object as ProjectItem;
if (item != null)
{
yield return item;
}
}
}
}
}