Skip to content

Commit

Permalink
Update readme (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitii authored Jun 12, 2022
1 parent 17d1fb3 commit 631a1cb
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 89 deletions.
35 changes: 34 additions & 1 deletion Community.Wsl.Sdk/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Community.Wsl.Sdk;

/// <summary>
/// Implementation for execution of commands in a wsl distribution.
/// </summary>
public class Command : ICommand
{
private readonly string[] _arguments;
Expand Down Expand Up @@ -55,7 +58,7 @@ public Command(
if (
options.StdInDataProcessingMode
is DataProcessingMode.Binary
or DataProcessingMode.String
or DataProcessingMode.String
)
{
throw new ArgumentException(
Expand All @@ -69,6 +72,9 @@ or DataProcessingMode.String

internal IStreamReader StdoutReader => _stdoutReader;

/// <summary>
/// <inheritdoc />
/// </summary>
public void Dispose()
{
if (!_isDisposed)
Expand All @@ -79,14 +85,29 @@ public void Dispose()
}
}

/// <summary>
/// <inheritdoc />
/// </summary>
public bool IsStarted => _isStarted;

/// <summary>
/// <inheritdoc />
/// </summary>
public bool HasWaited => _hasWaited;

/// <summary>
/// <inheritdoc />
/// </summary>
public bool IsDisposed => _isDisposed;

/// <summary>
/// <inheritdoc />
/// </summary>
public bool HasExited => _process?.HasExited ?? false;

/// <summary>
/// <inheritdoc />
/// </summary>
public CommandStreams Start()
{
if (IsStarted)
Expand Down Expand Up @@ -133,6 +154,9 @@ public CommandStreams Start()
};
}

/// <summary>
/// <inheritdoc />
/// </summary>
public CommandResult WaitAndGetResults()
{
if (!IsStarted)
Expand Down Expand Up @@ -165,6 +189,9 @@ public CommandResult WaitAndGetResults()
return result;
}

/// <summary>
/// <inheritdoc />
/// </summary>
public async Task<CommandResult> WaitAndGetResultsAsync()
{
if (!IsStarted)
Expand Down Expand Up @@ -197,12 +224,18 @@ public async Task<CommandResult> WaitAndGetResultsAsync()
return result;
}

/// <summary>
/// <inheritdoc />
/// </summary>
public CommandResult StartAndGetResults()
{
Start();
return WaitAndGetResults();
}

/// <summary>
/// <inheritdoc />
/// </summary>
public Task<CommandResult> StartAndGetResultsAsync()
{
Start();
Expand Down
64 changes: 64 additions & 0 deletions Community.Wsl.Sdk/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,79 @@

namespace Community.Wsl.Sdk;

/// <summary>
/// Interface for a command that is used to execute programs in a WSL distribution.
/// </summary>
public interface ICommand : IDisposable
{
/// <summary>
/// Returns <c>true</c> when the command has been started but hasn't finished, yet. Otherwise <c>false</c>.
/// </summary>
bool IsStarted { get; }

/// <summary>
///Returns <c>true</c> when the result of the command is being awaited. Otherwise <c>false</c> is returned.
/// </summary>
bool HasWaited { get; }

/// <summary>
/// Returns <c>true</c> when the native resources have been disposed.
/// </summary>
bool IsDisposed { get; }

/// <summary>
/// Returns <c>true</c> when the command has been started and exited.
/// </summary>
bool HasExited { get; }

/// <summary>
/// Starts the command and can only be executed once.
/// This method will block the executing thread and not wait for the command to finish.
/// </summary>
/// <returns>
/// Streams of the command.
/// </returns>
CommandStreams Start();

/// <summary>
/// Waits until the started command has exited and returns the result.
/// This method will block the executing thread.
/// The command needs to be started first (see <see cref="Start"/>).
/// </summary>
/// <returns>
/// The result of the command execution.
/// </returns>
CommandResult WaitAndGetResults();

/// <summary>
/// Waits until the started command has exited and returns the result.
/// This method will <b>not</b> block the executing thread.
/// The command needs to be started first (see <see cref="Start"/>).
/// </summary>
/// <returns>
/// The result of the command execution.
/// </returns>
Task<CommandResult> WaitAndGetResultsAsync();

/// <summary>
/// Starts and waits until the started command has exited and returns the result.
/// This method will block the executing thread.
/// The command must <b>not</b> be started first (see <see cref="Start"/>).
/// This method executes <see cref="Start"/> and <see cref="WaitAndGetResults"/> internally.
/// </summary>
/// <returns>
/// The result of the command execution.
/// </returns>
CommandResult StartAndGetResults();

/// <summary>
/// Starts and waits until the started command has exited and returns the result.
/// This method will <b>not</b> block the executing thread.
/// The command must <b>not</b> be started first (see <see cref="Start"/>).
/// This method executes <see cref="Start"/> and <see cref="WaitAndGetResultsAsync"/> internally.
/// </summary>
/// <returns>
/// The result of the command execution.
/// </returns>
Task<CommandResult> StartAndGetResultsAsync();
}
72 changes: 12 additions & 60 deletions Community.Wsl.Sdk/IWslApi.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace Community.Wsl.Sdk;

Expand All @@ -11,78 +8,33 @@ namespace Community.Wsl.Sdk;
public interface IWslApi
{
/// <summary>
/// Checks if the environment you are running in now supports WSL.
/// Checks if is WSL supported. This check is independent of <see cref="IsInstalled"/>.
/// </summary>
public bool IsWslSupported();

/// <summary>
/// Checks if WSL is installed. WSL being installed implies that <see cref="IsWslSupported()"/> to return <c>true</c>, too.
/// </summary>
public bool IsWslSupported()
{
return IsWslSupported(out _);
}
public bool IsInstalled { get; }

/// <summary>
/// Checks if the environment you are running in now supports WSL.
/// The error message is returned as out parameter. If wsl is supported, <paramref name="missingCapabilities"/> is <c>null</c>.
/// </summary>
public bool IsWslSupported(out string? missingCapabilities)
{
missingCapabilities = null;

var commonErrorMessage =
"Windows Subsystems for Linux requires 64-bit system and latest version of Windows 10 or higher than Windows Server 1709.";

if (!Environment.Is64BitOperatingSystem || !Environment.Is64BitProcess)
{
missingCapabilities = commonErrorMessage;
return false;
}

if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
missingCapabilities = commonErrorMessage;
return false;
}

if (
Environment.OSVersion.Version.Major < 10
|| Environment.OSVersion.Version.Minor < 0
|| Environment.OSVersion.Version.Build < 16299
)
{
missingCapabilities = commonErrorMessage;
return false;
}

var systemDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);

if (!File.Exists(Path.Combine(systemDirectory, "wslapi.dll")))
{
missingCapabilities = "This system does not have WSL enabled.";
return false;
}

if (!File.Exists(Path.Combine(systemDirectory, "wsl.exe")))
{
missingCapabilities = "This system does not have wsl.exe CLI.";
return false;
}

return true;
}
public bool IsWslSupported(out string? missingCapabilities);

/// <summary>
/// Returns information about the default WSL distribution.
/// </summary>
/// <returns>
/// Returns default WSL distribution information.
/// Returns null if no WSL distro is installed or no distro is set as the default.
/// Returns null if no WSL distribution is installed or no distribution is set as the default.
/// </returns>
public DistroInfo? GetDefaultDistro()
{
return GetDistroList().FirstOrDefault((d) => d.IsDefault);
}
public DistroInfo? GetDefaultDistribution();

/// <summary>
/// Returns all installed WSL distributions.
/// </summary>
/// <returns>Returns a list of information about the installed WSL distributions.</returns>
public IReadOnlyList<DistroInfo> GetDistroList();
public IReadOnlyList<DistroInfo> GetDistributionList();
}
55 changes: 40 additions & 15 deletions Community.Wsl.Sdk/WslApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Community.Wsl.Sdk;

/// <summary>
/// <inheritdoc cref="IWslApi"/>
/// <inheritdoc />
/// </summary>
public class WslApi : IWslApi
{
Expand All @@ -26,7 +26,15 @@ public WslApi(IRegistry? registry = null, IIo? io = null, IEnvironment? environm
}

/// <summary>
/// <inheritdoc cref="IWslApi.IsWslSupported(out string?)"/>
/// <inheritdoc />
/// </summary>
public bool IsWslSupported()
{
return IsWslSupported(out _);
}

/// <summary>
/// <inheritdoc />
/// </summary>
public bool IsWslSupported(out string? missingCapabilities)
{
Expand Down Expand Up @@ -57,27 +65,36 @@ public bool IsWslSupported(out string? missingCapabilities)
return false;
}

var systemDirectory = _environment.GetFolderPath(Environment.SpecialFolder.System);
return true;
}

if (!_io.FileExists(_io.Combine(systemDirectory, "wslapi.dll")))
/// <summary>
/// <inheritdoc />
/// </summary>
public bool IsInstalled
{
get
{
missingCapabilities = "This system does not have WSL enabled.";
return false;
}
var systemDirectory = _environment.GetFolderPath(Environment.SpecialFolder.System);

if (!_io.FileExists(_io.Combine(systemDirectory, "wsl.exe")))
{
missingCapabilities = "This system does not have wsl.exe CLI.";
return false;
}
if (!_io.FileExists(_io.Combine(systemDirectory, "wslapi.dll")))
{
return false;
}

return true;
if (!_io.FileExists(_io.Combine(systemDirectory, "wsl.exe")))
{
return false;
}

return true;
}
}

/// <summary>
/// <inheritdoc cref="IWslApi.GetDistroList"/>
/// <inheritdoc />
/// </summary>
public IReadOnlyList<DistroInfo> GetDistroList()
public IReadOnlyList<DistroInfo> GetDistributionList()
{
if (!((IWslApi)this).IsWslSupported(out var missingCapabilities))
{
Expand Down Expand Up @@ -133,4 +150,12 @@ public IReadOnlyList<DistroInfo> GetDistroList()
DefaultEnvironmentVariables = Array.Empty<string>()
};
}

/// <summary>
/// <inheritdoc />
/// </summary>
public DistroInfo? GetDefaultDistribution()
{
return GetDistributionList().FirstOrDefault((d) => d.IsDefault);
}
}
Loading

0 comments on commit 631a1cb

Please sign in to comment.