-
Notifications
You must be signed in to change notification settings - Fork 34
/
ProjectConfigurationManager.cs
346 lines (292 loc) · 12.3 KB
/
ProjectConfigurationManager.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
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.VCProjectEngine;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Threading.Tasks;
using System.Windows.Forms;
using VSLangProj;
namespace conan_vs_extension
{
public class ProjectConfigurationManager
{
public ProjectConfigurationManager()
{
}
public static bool conandataFileExists(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
string projectDirectory = System.IO.Path.GetDirectoryName(project.FullName);
string path = Path.Combine(projectDirectory, "conandata.yml");
return File.Exists(path);
}
public static async Task InjectConanDepsToAllConfigsAsync(Project project)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (project.Object is VCProject vcProject)
{
string propsFilePath = GetPropsFilePath(project);
if (File.Exists(propsFilePath))
{
foreach (VCConfiguration vcConfig in (IEnumerable)vcProject.Configurations)
{
InjectConanDepsToConfig(vcConfig, propsFilePath);
}
}
else
{
System.Diagnostics.Debug.WriteLine($"Properties file '{propsFilePath}' does not exist.");
}
}
}
public static async Task InjectConanDepsAsync(Project project, VCConfiguration vcConfig)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (!conandataFileExists(project))
{
System.Diagnostics.Debug.WriteLine("conandata.yml not found. Skipping Conan Deps.");
return;
}
string propsFilePath = GetPropsFilePath(project);
if (File.Exists(propsFilePath))
{
InjectConanDepsToConfig(vcConfig, propsFilePath);
project.Save();
}
else
{
System.Diagnostics.Debug.WriteLine($"Properties file '{propsFilePath}' does not exist.");
}
}
private static void InjectConanDepsToConfig(VCConfiguration vcConfig, string propsFilePath)
{
bool isAlreadyIncluded = false;
IVCCollection propertySheets = vcConfig.PropertySheets as IVCCollection;
foreach (VCPropertySheet sheet in propertySheets)
{
if (sheet.PropertySheetFile.Equals(propsFilePath, StringComparison.OrdinalIgnoreCase))
{
isAlreadyIncluded = true;
break;
}
}
if (!isAlreadyIncluded)
{
vcConfig.AddPropertySheet(propsFilePath);
}
}
private static string GetPropsFilePath(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
string projectFilePath = project.FullName;
string projectDirectory = Path.GetDirectoryName(projectFilePath);
return Path.Combine(projectDirectory, "conan", "conandeps.props");
}
private static async Task GenerateConanInstallScriptAsync(Project project)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
string conan_script_name = "conan_install.bat";
string projectDirectory = Path.GetDirectoryName(project.FullName);
string conanScriptDirectory = Path.Combine(projectDirectory, ".conan");
string scriptPath = Path.Combine(conanScriptDirectory, conan_script_name);
string conanPath = GlobalSettings.ConanExecutablePath;
string conanCommandContents = $@"@echo off
setlocal enabledelayedexpansion
REM Initialize flags
REM Check if the control files exist
set performInstall=0
if not exist "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" set ""performInstall=1""
if not exist "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" set ""performInstall=1""
REM Check for changes in conandata.yml and conanfile.py
if exist "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" (
echo Checking changes in conandata.yml
fc ""conandata.yml"" "".conan\CONANDATA_%CONAN_BUILD_CONFIG%"" > nul
if errorlevel 1 (
set performInstall=1
echo Changes detected in conandata.yml
)
)
if exist "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" (
echo Checking changes in conanfile.py
fc ""conanfile.py"" "".conan\CONANFILE_%CONAN_BUILD_CONFIG%"" > nul
if errorlevel 1 (
set performInstall=1
echo Changes detected in conanfile.py
)
)
REM Check for the .runconan file that indicates changes in profiles
if exist "".conan\.runconan"" (
echo Changes in profiles detected
set performInstall=1
del "".conan\.runconan""
)
if %performInstall% equ 1 (
REM Echo changes detected
echo Changes detected, executing conan install...
set ""args=""
:args_loop
if ""%~1""=="""" goto after_args_loop
set ""args=!args! %1""
shift
goto args_loop
:after_args_loop
echo Arguments for conan install: !args!
""{conanPath}"" install !args!
if !errorlevel! neq 0 (
echo ERROR: Conan installation failed. Please check the console output to troubleshoot issues.
exit /b 1
)
REM Update control files to reflect current state
copy /Y conandata.yml .conan\CONANDATA_%CONAN_BUILD_CONFIG%
copy /Y conanfile.py .conan\CONANFILE_%CONAN_BUILD_CONFIG%
REM Display the message indicating Conan install finished
echo ****************************************************************
echo * *
echo * Conan installation completed successfully. *
echo * Please relaunch the build to apply the new changes. *
echo * *
echo ****************************************************************
echo ERROR: Conan installation completed successfully. Please relaunch the build to apply the new changes.
exit /b 1
)
REM Echo no changes detected
echo No changes detected, skipping conan install...
";
Directory.CreateDirectory(conanScriptDirectory);
File.WriteAllText(scriptPath, conanCommandContents);
}
private static async Task SaveConanPrebuildEventAsync(Project project, VCConfiguration vcConfig)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
VCProject vcProject = project.Object as VCProject;
IVCCollection tools = vcConfig.Tools as IVCCollection;
VCPreBuildEventTool preBuildTool = tools.Item("VCPreBuildEventTool") as VCPreBuildEventTool;
if (preBuildTool != null)
{
string conan_script_name = "conan_install.bat";
string commandLine = $"set CONAN_BUILD_CONFIG=\"$(Configuration)_$(Platform)\" && \"$(ProjectDir).conan\\{conan_script_name}\" . -pr:h=.conan/$(Configuration)_$(Platform) -pr:b=default --build=missing";
if (!preBuildTool.CommandLine.Contains(conan_script_name))
{
preBuildTool.CommandLine += Environment.NewLine + commandLine;
vcProject.Save();
}
}
}
public static async Task SaveConanPrebuildEventsAllConfigAsync(Project project)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (!conandataFileExists(project))
{
System.Diagnostics.Debug.WriteLine("conandata.yml not found. Skipping Conan PreBuildEvent.");
return;
}
await GenerateConanInstallScriptAsync(project); // all the config share the same script
VCProject vcProject = project.Object as VCProject;
foreach (VCConfiguration vcConfig in (IEnumerable)vcProject.Configurations)
{
await SaveConanPrebuildEventAsync(project, vcConfig);
}
}
public static VCConfiguration GetActiveVCConfiguration(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (project.Object is VCProject vcProject)
{
IVCCollection configurations = vcProject.Configurations as IVCCollection;
VCConfiguration activeConfiguration = null;
foreach (VCConfiguration config in configurations)
{
string configName = config.ConfigurationName;
VCPlatform vcPlatform = config.Platform as VCPlatform;
if (config.ConfigurationName == project.ConfigurationManager.ActiveConfiguration.ConfigurationName &&
vcPlatform.Name == project.ConfigurationManager.ActiveConfiguration.PlatformName)
{
activeConfiguration = config;
break;
}
}
return activeConfiguration;
}
return null;
}
public static Project GetStartupProject(DTE dte)
{
ThreadHelper.ThrowIfNotOnUIThread();
// Access startup projects from SolutionBuild
var startupProjects = dte.Solution.SolutionBuild.StartupProjects as object[];
if (startupProjects != null && startupProjects.Length > 0)
{
// Get the name of the first startup project
string startupProjectName = (string)startupProjects[0];
// Iterate through all projects in the solution to find a matching project
foreach (Project project in dte.Solution.Projects)
{
Project result = FindProjectByNameRecursive(project, startupProjectName);
if (result != null)
{
return result;
}
}
}
return null; // No startup project found
}
// Helper function to find the project by name recursively
private static Project FindProjectByNameRecursive(Project project, string projectName)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (project.UniqueName.Equals(projectName, StringComparison.OrdinalIgnoreCase))
{
return project;
}
// Handle solution folders
if (project.Kind == EnvDTE.Constants.vsProjectKindSolutionItems)
{
foreach (ProjectItem item in project.ProjectItems)
{
if (item.SubProject != null)
{
Project foundProject = FindProjectByNameRecursive(item.SubProject, projectName);
if (foundProject != null)
{
return foundProject;
}
}
}
}
return null;
}
public static Project GetProjectByName(DTE dte, string name)
{
ThreadHelper.ThrowIfNotOnUIThread();
foreach (Project project in dte.Solution.Projects)
{
Project result = FindProjectByNameRecursive(project, name);
if (result != null)
{
return result;
}
}
return null;
}
public static VCConfiguration GetVCConfig(Project project, string ProjectConfig, string Platform)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (project.Object is VCProject vcProject)
{
foreach (VCConfiguration vcConfig in (IEnumerable)vcProject.Configurations)
{
VCPlatform vcPlatform = vcConfig.Platform as VCPlatform;
if (vcConfig.ConfigurationName.Equals(ProjectConfig)
&& vcPlatform.Name.Equals(Platform)) {
return vcConfig;
}
}
}
return null;
}
}
}