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
60 changes: 60 additions & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "pch.h"
#include <argb.h>
#include <conattrs.hpp>
#include <fstream>
JBanks marked this conversation as resolved.
Show resolved Hide resolved
#include <io.h>
#include <fcntl.h>
#include "CascadiaSettings.h"
#include "../../types/inc/utils.hpp"
#include "../../inc/DefaultSettings.h"
Expand Down Expand Up @@ -227,6 +230,8 @@ void CascadiaSettings::_CreateDefaultProfiles()

_profiles.emplace_back(powershellProfile);
_profiles.emplace_back(cmdProfile);

JBanks marked this conversation as resolved.
Show resolved Hide resolved
_CreateWslProfiles(_profiles);
}

// Method Description:
Expand Down Expand Up @@ -467,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);
};
10 changes: 1 addition & 9 deletions src/cascadia/TerminalApp/TerminalApp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<ProjectName>TerminalApp</ProjectName>
<RootNamespace>TerminalApp</RootNamespace>
</PropertyGroup>

<!-- ========================= XAML files ======================== -->
<ItemGroup>
<!-- HERE BE DRAGONS:
Expand All @@ -33,7 +32,6 @@
<SubType>Designer</SubType>
</Page>
</ItemGroup> -->

<!-- ========================= Headers ======================== -->
<ItemGroup>
<ClInclude Include="Tab.h" />
Expand All @@ -50,7 +48,6 @@
<DependentUpon>App.xaml</DependentUpon>
</ClInclude>
</ItemGroup>

<!-- ========================= Cpp Files ======================== -->
<ItemGroup>
<ClCompile Include="Tab.cpp" />
Expand All @@ -71,21 +68,18 @@
</ClCompile>
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
</ItemGroup>

<!-- ========================= idl Files ======================== -->
<ItemGroup>
<Midl Include="App.idl">
<DependentUpon>App.xaml</DependentUpon>
</Midl>
<Midl Include="AppKeyBindings.idl" />
</ItemGroup>

<!-- ========================= Misc Files ======================== -->
<ItemGroup>
<None Include="packages.config" />
<None Include="TerminalApp.def" />
</ItemGroup>

<!-- ========================= Project References ======================== -->
<ItemGroup>
<!--
Expand All @@ -99,14 +93,12 @@
<ProjectReference Include="$(OpenConsoleDir)src\cascadia\TerminalControl\TerminalControl.vcxproj">
<Project>{CA5CAD1A-44BD-4AC7-AC72-6CA5B3AB89ED}</Project>
</ProjectReference>

<!-- This is needed to be able to reference the XamlApplication type. -->
<ProjectReference Include="..\Microsoft.UI.Xaml.Markup\Microsoft.UI.Xaml.Markup.vcxproj">
<Project>{015a0047-772d-4f1a-88c9-45c18f0adfb6}</Project>
<Private>true</Private>
<CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies>
</ProjectReference>

</ItemGroup>
<PropertyGroup>
<!--
Expand All @@ -130,4 +122,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\..\packages\Microsoft.UI.Xaml.2.1.190405001-prerelease\build\native\Microsoft.UI.Xaml.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.UI.Xaml.2.1.190405001-prerelease\build\native\Microsoft.UI.Xaml.targets'))" />
</Target>
</Project>
</Project>