NoJobEngine 2.0.7
Loading...
Searching...
No Matches
VisualStudioIntegration.cpp
Go to the documentation of this file.
2
3#ifdef _WIN32
4#define NOMINMAX
5#include <Windows.h>
6#include <shellapi.h>
7#endif
8
9#include <cstdio>
10#include <cstdlib>
11
12namespace NoJob
13{
14 std::filesystem::path VisualStudioIntegration::FindVisualStudioExecutable()
15 {
16#ifdef _WIN32
17 wchar_t* programFilesX86 = nullptr;
18 std::size_t length = 0;
19 if (_wdupenv_s(&programFilesX86, &length, L"ProgramFiles(x86)") != 0 ||
20 !programFilesX86)
21 return {};
22
23 const std::filesystem::path vswhere =
24 std::filesystem::path(programFilesX86) /
25 "Microsoft Visual Studio" / "Installer" / "vswhere.exe";
26 free(programFilesX86);
27
28 if (!std::filesystem::exists(vswhere))
29 return {};
30
31 const std::wstring command =
32 L"\"" + vswhere.wstring() +
33 L"\" -latest -products * -requires Microsoft.Component.MSBuild "
34 L"-property productPath";
35
36 FILE* pipe = _wpopen(command.c_str(), L"rt");
37 if (!pipe)
38 return {};
39
40 wchar_t buffer[2048]{};
41 std::wstring result;
42 if (fgetws(buffer, static_cast<int>(std::size(buffer)), pipe))
43 result = buffer;
44 _pclose(pipe);
45
46 while (!result.empty() &&
47 (result.back() == L'\r' || result.back() == L'\n' ||
48 result.back() == L' ' || result.back() == L'\t'))
49 result.pop_back();
50
51 const std::filesystem::path devenv(result);
52 return std::filesystem::exists(devenv) ? devenv : std::filesystem::path{};
53#else
54 return {};
55#endif
56 }
57
59 const std::filesystem::path& scriptPath,
60 const std::filesystem::path& projectRoot,
61 const LogFunction& log)
62 {
63 const auto absoluteScript =
64 std::filesystem::absolute(scriptPath).lexically_normal();
65
66 if (!std::filesystem::exists(absoluteScript))
67 {
68 log("[Scripts] File not found: " + absoluteScript.string());
69 return false;
70 }
71
72#ifdef _WIN32
73 const auto visualStudio = FindVisualStudioExecutable();
74 if (!visualStudio.empty())
75 {
76 const auto parameters =
77 L"/Edit \"" + absoluteScript.wstring() + L"\"";
78 const auto result = reinterpret_cast<std::intptr_t>(
79 ShellExecuteW(
80 nullptr, L"open", visualStudio.wstring().c_str(),
81 parameters.c_str(), projectRoot.wstring().c_str(),
82 SW_SHOWNORMAL));
83
84 if (result > 32)
85 {
86 log("[Scripts] Opened in existing Visual Studio instance: " +
87 absoluteScript.filename().string());
88 return true;
89 }
90 }
91
92 const auto result = reinterpret_cast<std::intptr_t>(
93 ShellExecuteW(
94 nullptr, L"open", absoluteScript.wstring().c_str(),
95 nullptr, projectRoot.wstring().c_str(), SW_SHOWNORMAL));
96
97 if (result > 32)
98 {
99 log("[Scripts] Opened with Windows file association: " +
100 absoluteScript.filename().string());
101 return true;
102 }
103#endif
104
105 log("[Scripts] Could not open " + absoluteScript.string());
106 return false;
107 }
108}
VisualStudioIntegration declarations for the Project Scripting subsystem.
std::function< void(std::string)> LogFunction
static bool OpenScript(const std::filesystem::path &scriptPath, const std::filesystem::path &projectRoot, const LogFunction &log)