Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code to add WSLProfiles. #1050

Merged
merged 11 commits into from
Jun 7, 2019
102 changes: 56 additions & 46 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,52 +231,7 @@ void CascadiaSettings::_CreateDefaultProfiles()
_profiles.emplace_back(powershellProfile);
_profiles.emplace_back(cmdProfile);

JBanks marked this conversation as resolved.
Show resolved Hide resolved
HANDLE readPipe;
HANDLE writePipe;
SECURITY_ATTRIBUTES sa{ sizeof(sa), nullptr, true };
if (CreatePipe(&readPipe, &writePipe, &sa, 0)) {
STARTUPINFO si{};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = writePipe;
si.hStdError = writePipe;

PROCESS_INFORMATION pi{};
std::wstring command = L"wsl.exe --list";
bool processSuccess = CreateProcessW(nullptr, const_cast<LPWSTR>(command.c_str()), nullptr, nullptr,
TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi);
if (processSuccess) {
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode;
HRESULT hr = 0;
if ((GetExitCodeProcess(pi.hProcess, &exitCode) == false) || (exitCode != 0)) {
hr = E_INVALIDARG;
}
if (SUCCEEDED(hr)) {
DWORD bytesAvailable = 0;
PeekNamedPipe(readPipe, nullptr, NULL, nullptr, &bytesAvailable, nullptr);
std::wfstream pipe{ _wfdopen(_open_osfhandle((intptr_t)readPipe, _O_WTEXT | _O_RDONLY), L"r") };
std::wstring wline = L"";
std::getline(pipe, wline); //remove the header from the output.
while (pipe.tellp() < bytesAvailable) {
std::getline(pipe, wline);
std::wstringstream wlinestream(wline);
std::wstring name = L"";
if (wlinestream) {
std::getline(wlinestream, name, L' ');
auto WSLDistro{ _CreateDefaultProfile(name) };
WSLDistro.SetCommandline(L"wsl.exe -d " + name);
WSLDistro.SetColorScheme({ L"Campbell" });
_profiles.emplace_back(WSLDistro);
}
}
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
CloseHandle(readPipe);
CloseHandle(writePipe);
}
_CreateWslProfiles(_profiles);
}

// Method Description:
Expand Down Expand Up @@ -517,6 +472,61 @@ bool CascadiaSettings::_isPowerShellCoreInstalledInPath(const std::wstring_view
return false;
}

// Function Description:
// - Adds all of the WSL profiles to the provided container.
// Arguments:
// - A ref to the profiles container where the WSL profiles are to be added
// Return Value:
// - <none>
void CascadiaSettings::_CreateWslProfiles(std::vector<TerminalApp::Profile>& profileStorage)
{
try
{
DWORD bytesAvailable = 0;
JBanks marked this conversation as resolved.
Show resolved Hide resolved
DWORD exitCode = 0;
std::wstring command = L"wsl.exe --list";
std::wstring wline = L"";
JBanks marked this conversation as resolved.
Show resolved Hide resolved
std::wstring distName = L"";
JBanks marked this conversation as resolved.
Show resolved Hide resolved
wil::unique_handle readPipe;
wil::unique_handle writePipe;
SECURITY_ATTRIBUTES sa{ sizeof(sa), nullptr, true };
THROW_IF_WIN32_BOOL_FALSE(CreatePipe(&readPipe, &writePipe, &sa, 0));
STARTUPINFO si{};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = writePipe.get();
si.hStdError = writePipe.get();
wil::unique_process_information pi{};

THROW_IF_WIN32_BOOL_FALSE(CreateProcessW(nullptr, const_cast<LPWSTR>(command.c_str()), nullptr, nullptr,
TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi));
if (WaitForSingleObject(pi.hProcess, INFINITE) != NULL)
{
throw;
JBanks marked this conversation as resolved.
Show resolved Hide resolved
}
if ((GetExitCodeProcess(pi.hProcess, &exitCode) == false) || (exitCode != 0)) {
THROW_HR(E_INVALIDARG);
}
THROW_IF_WIN32_BOOL_FALSE(PeekNamedPipe(readPipe.get(), nullptr, NULL, nullptr, &bytesAvailable, nullptr));
FILE* hPipe = _wfdopen(_open_osfhandle((intptr_t)readPipe.get(), _O_WTEXT | _O_RDONLY), L"r");
//don't call fclose on hPipe because the readPipe handle is managed by wil and this will cause an error.
std::wfstream pipe{ hPipe };
std::getline(pipe, wline); //remove the header from the output.
while (pipe.tellp() < bytesAvailable) {
std::getline(pipe, wline);
std::wstringstream wlinestream(wline);
JBanks marked this conversation as resolved.
Show resolved Hide resolved
if (wlinestream) {
std::getline(wlinestream, distName, L' ');
auto WSLDistro{ _CreateDefaultProfile(distName) };
WSLDistro.SetCommandline(L"wsl.exe -d " + distName);
WSLDistro.SetColorScheme({ L"Campbell" });
profileStorage.emplace_back(WSLDistro);
}
}
}
CATCH_LOG()
JBanks marked this conversation as resolved.
Show resolved Hide resolved
}

// Function Description:
// - Get a environment variable string.
// Arguments:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TerminalApp::CascadiaSettings final
static std::optional<winrt::hstring> _LoadAsUnpackagedApp();
static bool _isPowerShellCoreInstalledInPath(const std::wstring_view programFileEnv, std::filesystem::path& cmdline);
static bool _isPowerShellCoreInstalled(std::filesystem::path& cmdline);
static void _CreateWslProfiles(std::vector<TerminalApp::Profile>& profileStorage);
static std::wstring ExpandEnvironmentVariableString(std::wstring_view source);
static Profile _CreateDefaultProfile(const std::wstring_view name);
};
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalApp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>Kernel32.lib;WindowsApp.lib;shell32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>WindowsApp.lib;shell32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(OpenConsoleDir)src\common.build.post.props" />
Expand Down