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

Allow WSL extension to use default windows terminal/console to launch and install distributions #3742

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions extensions/WSLExtension/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,17 @@ public static class Constants
public const int WslVersion2 = 2;
public const int WslExeExitSuccess = 0;

// Launch terminal with specific profile and log the user into their home directory in the login shell
// Note: this opens a new terminal window in the UI
public static string LaunchDistributionInTerminalWithProfile { get; } = "--profile {0} -- wsl --shell-type login --cd ~ --distribution {1}";

// Launch without using a Terminal profile and log the user into their home directory using the login shell
// Note: this opens a new terminal window in the UI
public static string LaunchDistributionInTerminalWithNoProfile { get; } = "wsl --shell-type login --cd ~ --distribution {0}";

// Launch into the wsl process without terminal and log the user into their home directory using the login shell
// Note: this opens a new terminal window in the UI
public static string LaunchDistributionWithoutTerminal { get; } = "--shell-type login --cd ~ --distribution {0}";
public static string LaunchDistributionArgs { get; } = "--shell-type login --cd ~ --distribution {0}";

// Arguments to unregister a wsl distribution from a machine using wsl.exe
public const string UnregisterDistributionArgs = "--unregister {0}";

// Arguments to terminate all wsl sessions for a specific distribution using wsl.exe
public const string TerminateDistributionArgs = "--terminate {0}";

// Arguments to download, install and register a wsl distribution using Terminal
// Note: this opens a new terminal window in the UI
public const string InstallDistributionWithTerminal = "wsl --install --distribution {0}";

// Arguments to download, install and register a wsl distribution without using terminal
// Note: this opens a cmd window in the UI
public const string InstallDistributionWithoutTerminal = "--install --distribution {0}";
// Arguments to download, install and register a wsl distribution.
public const string InstallDistributionArgs = "--install --distribution {0}";

// Arguments to list of all running distributions on a machine using wsl.exe
public const string ListAllRunningDistributions = "--list --running";
Expand Down
2 changes: 1 addition & 1 deletion extensions/WSLExtension/Contracts/IWslManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IWslManager
/// <summary> Launches a new WSL distribution.
/// This is a wrapper for <see cref="IWslServicesMediator.LaunchDistribution"/>
/// </summary>
void LaunchDistribution(string distributionName, string? windowsTerminalProfile);
void LaunchDistribution(string distributionName);

/// <summary> Installs a new WSL distribution.
/// This is a wrapper for <see cref="IWslServicesMediator.InstallDistribution(string)"/>
Expand Down
2 changes: 1 addition & 1 deletion extensions/WSLExtension/Contracts/IWslServicesMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface IWslServicesMediator
void UnregisterDistribution(string distributionName);

/// <summary> Launches a new WSL process with the provided distribution. </summary>
void LaunchDistribution(string distributionName, string? windowsTerminalProfile);
void LaunchDistribution(string distributionName);

/// <summary> Installs and registers a new distribution on the machine. </summary>
void InstallDistribution(string distributionName);
Expand Down
2 changes: 1 addition & 1 deletion extensions/WSLExtension/Models/WslComputeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public IAsyncOperation<ComputeSystemOperationResult> ConnectAsync(string options
try
{
UpdateState(ComputeSystemState.Starting);
_wslManager.LaunchDistribution(Id, _distribution.AssociatedTerminalProfileGuid);
_wslManager.LaunchDistribution(Id);
UpdateState(ComputeSystemState.Running);
return new ComputeSystemOperationResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public IAsyncOperation<CreateComputeSystemResult> StartAsync()
var startTime = DateTime.UtcNow;
_log.Information($"Starting installation for {_definition.Name}");
Progress?.Invoke(this, new CreateComputeSystemProgressEventArgs(_preparingToInstall, 0));
_wslManager.InstallDistribution(_definition.Name);

// Cancel waiting for install if the distribution hasn't been installed after 10 minutes.
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(10));
WslRegisteredDistribution? registeredDistribution = null;
var distributionInstalledSuccessfully = false;
_wslManager.InstallDistribution(_definition.Name);

Progress?.Invoke(this, new CreateComputeSystemProgressEventArgs(_waitingToComplete, 0));
while (!cancellationTokenSource.IsCancellationRequested)
Expand Down
4 changes: 2 additions & 2 deletions extensions/WSLExtension/Services/WslManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public void UnregisterDistribution(string distributionName)
}

/// <inheritdoc cref="IWslManager.LaunchDistribution"/>
public void LaunchDistribution(string distributionName, string? windowsTerminalProfile = null)
public void LaunchDistribution(string distributionName)
{
_wslServicesMediator.LaunchDistribution(distributionName, windowsTerminalProfile);
_wslServicesMediator.LaunchDistribution(distributionName);
}

/// <inheritdoc cref="IWslManager.InstallDistribution"/>
Expand Down
54 changes: 8 additions & 46 deletions extensions/WSLExtension/Services/WslServicesMediator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;
using Microsoft.Win32;
using WSLExtension.ClassExtensions;
using WSLExtension.Contracts;
Expand Down Expand Up @@ -148,36 +147,12 @@ public void UnregisterDistribution(string distributionName)
}

/// <inheritdoc cref="IWslServicesMediator.LaunchDistribution"/>
public void LaunchDistribution(string distributionName, string? windowsTerminalProfile)
public void LaunchDistribution(string distributionName)
{
var executable = GetFileNameForProcessLaunch();

// Only launch with terminal if its installed
if (executable.Equals(WindowsTerminalShimExe, StringComparison.OrdinalIgnoreCase))
{
LaunchDistributionUsingTerminal(distributionName, windowsTerminalProfile);
return;
}

// Default to starting the wsl process directly and passing in its command line args
_processCreator.CreateProcessWithWindow(executable, LaunchDistributionWithoutTerminal.FormatArgs(distributionName));
}

private void LaunchDistributionUsingTerminal(string distributionName, string? windowsTerminalProfile)
{
var terminalArgs = LaunchDistributionInTerminalWithNoProfile.FormatArgs(distributionName);

if (!string.IsNullOrEmpty(windowsTerminalProfile))
{
// Launch into terminal with the specified profile and run wsl.exe in the console window
terminalArgs = LaunchDistributionInTerminalWithProfile.FormatArgs(windowsTerminalProfile, distributionName);
_processCreator.CreateProcessWithWindow(WindowsTerminalShimExe, terminalArgs);
}
else
{
// Launch into terminal and run wsl.exe in the console window without a profile
_processCreator.CreateProcessWithWindow(WindowsTerminalShimExe, terminalArgs);
}
// Start the wsl process directly and passing in its command line args
_processCreator.CreateProcessWithWindow(
WslExe,
LaunchDistributionArgs.FormatArgs(distributionName));
}

/// <inheritdoc cref="IWslServicesMediator.TerminateDistribution"/>
Expand All @@ -194,21 +169,8 @@ public void TerminateDistribution(string distributionName)
/// <inheritdoc cref="IWslServicesMediator.InstallDistribution"/>
public void InstallDistribution(string distributionName)
{
var executable = GetFileNameForProcessLaunch();

// Launch into terminal if its installed and run wsl.exe in the console window
if (executable.Equals(WindowsTerminalShimExe, StringComparison.OrdinalIgnoreCase))
{
_processCreator.CreateProcessWithWindow(executable, InstallDistributionWithTerminal.FormatArgs(distributionName));
return;
}

// Default to starting the wsl process directly and passing in its command line args
_processCreator.CreateProcessWithWindow(executable, InstallDistributionWithoutTerminal.FormatArgs(distributionName));
}

private string GetFileNameForProcessLaunch()
{
return _packageHelper.IsPackageInstalled(WindowsTerminalPackageFamilyName) ? WindowsTerminalShimExe : WslExe;
_processCreator.CreateProcessWithWindow(
WslExe,
InstallDistributionArgs.FormatArgs(distributionName));
}
}
Loading