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
50 changes: 50 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,53 @@ void CascadiaSettings::_CreateDefaultProfiles()

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

JBanks marked this conversation as resolved.
Show resolved Hide resolved
HANDLE readPipe;
JBanks marked this conversation as resolved.
Show resolved Hide resolved
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{};
JBanks marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}

// Method Description:
Expand Down
12 changes: 2 additions & 10 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 @@ -118,7 +110,7 @@
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>WindowsApp.lib;shell32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Kernel32.lib;WindowsApp.lib;shell32.lib;%(AdditionalDependencies)</AdditionalDependencies>
JBanks marked this conversation as resolved.
Show resolved Hide resolved
</Link>
</ItemDefinitionGroup>
<Import Project="$(OpenConsoleDir)src\common.build.post.props" />
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>