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
65 changes: 65 additions & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "pch.h"
#include <argb.h>
#include <conattrs.hpp>
#include <io.h>
#include <fcntl.h>
#include "CascadiaSettings.h"
#include "../../types/inc/utils.hpp"
#include "../../inc/DefaultSettings.h"
Expand Down Expand Up @@ -227,6 +229,11 @@ void CascadiaSettings::_CreateDefaultProfiles()

_profiles.emplace_back(powershellProfile);
_profiles.emplace_back(cmdProfile);
try
{
_CreateWslProfiles(_profiles);
}
CATCH_LOG()
}

// Method Description:
Expand Down Expand Up @@ -467,6 +474,64 @@ 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)
{
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{};
std::wstring command = L"wsl.exe --list";
JBanks marked this conversation as resolved.
Show resolved Hide resolved

THROW_IF_WIN32_BOOL_FALSE(CreateProcessW(nullptr, const_cast<LPWSTR>(command.c_str()), nullptr, nullptr,
TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi));
switch (WaitForSingleObject(pi.hProcess, INFINITE)) {
JBanks marked this conversation as resolved.
Show resolved Hide resolved
case WAIT_OBJECT_0:
break;
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
THROW_HR(ERROR_CHILD_NOT_COMPLETE);
case WAIT_FAILED:
THROW_LAST_ERROR();
default:
THROW_HR(ERROR_UNHANDLED_EXCEPTION);
}
DWORD exitCode;
if ((GetExitCodeProcess(pi.hProcess, &exitCode) == false) || (exitCode != 0)) {
THROW_HR(E_INVALIDARG);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A non-zero exit code will indicate that there are no distribuitons. This is not an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you rather have this throw something so that it gets logged CATCHLOG()? I'm thinking ERROR_NO_DATA would be descriptive enough as to what's happening.

For the time being, I've changed it to return without error.

}
DWORD bytesAvailable;
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.
JBanks marked this conversation as resolved.
Show resolved Hide resolved
std::wfstream pipe{ hPipe };
std::wstring wline;
std::getline(pipe, wline); //remove the header from the output.
while (pipe.tellp() < bytesAvailable) {
std::getline(pipe, wline);
std::wstringstream wlinestream(wline);
if (wlinestream) {
std::wstring distName;
std::getline(wlinestream, distName, L' ');
auto WSLDistro{ _CreateDefaultProfile(distName) };
WSLDistro.SetCommandline(L"wsl.exe -d " + distName);
WSLDistro.SetColorScheme({ L"Campbell" });
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
profileStorage.emplace_back(WSLDistro);
}
}
}

// 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);
};
1 change: 1 addition & 0 deletions src/inc/LibraryIncludes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <iterator>
#include <math.h>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <filesystem>
#include <functional>
Expand Down