Skip to content

Commit

Permalink
Run ReSharper code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Octobob committed Nov 20, 2024
1 parent fe9f3bd commit 4f0397b
Show file tree
Hide file tree
Showing 30 changed files with 1,418 additions and 1,426 deletions.
2 changes: 0 additions & 2 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class Build : NukeBuild
.Executes(() =>
{
foreach (var dir in SourceDirectory.GlobDirectories("**/bin", "**/obj", "**/TestResults"))
{
dir.DeleteDirectory();
}

ArtifactsDirectory.CreateOrCleanDirectory();
});
Expand Down
2 changes: 1 addition & 1 deletion source/Shellfish/CallbackOutputTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static partial class ShellCommandExtensionMethods
{
public static ShellCommand WithStdOutTarget(this ShellCommand shellCommand, Action<string> callback)
=> shellCommand.WithStdOutTarget(new CallbackOutputTarget(callback));

public static ShellCommand WithStdErrTarget(this ShellCommand shellCommand, Action<string> callback)
=> shellCommand.WithStdErrTarget(new CallbackOutputTarget(callback));
}
13 changes: 6 additions & 7 deletions source/Shellfish/IXPlatAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
using System.Net;
using System.Text;

namespace Octopus.Shellfish
namespace Octopus.Shellfish;

interface IXPlatAdapter
{
interface IXPlatAdapter
{
void ConfigureStartInfoForUser(ProcessStartInfo startInfo, NetworkCredential runAs, IReadOnlyDictionary<string, string>? customEnvironmentVariables);
void TryKillProcessAndChildrenRecursively(Process process);
Encoding GetOemEncoding();
}
void ConfigureStartInfoForUser(ProcessStartInfo startInfo, NetworkCredential runAs, IReadOnlyDictionary<string, string>? customEnvironmentVariables);
void TryKillProcessAndChildrenRecursively(Process process);
Encoding GetOemEncoding();
}
53 changes: 26 additions & 27 deletions source/Shellfish/Nix/NixAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,35 @@
using System.Net;
using System.Text;

namespace Octopus.Shellfish.Nix
{
class NixAdapter : IXPlatAdapter
{
public void ConfigureStartInfoForUser(ProcessStartInfo startInfo, NetworkCredential runAs, IReadOnlyDictionary<string, string>? customEnvironmentVariables)
=> throw new PlatformNotSupportedException("NetCore on Linux or Mac does not support running a process as a different user.");
namespace Octopus.Shellfish.Nix;

public void TryKillProcessAndChildrenRecursively(Process process)
{
var messages = new List<string>();
var result = ShellExecutor.ExecuteCommand(
"/bin/bash",
$"-c \"kill -TERM {process.Id}\"",
Environment.CurrentDirectory,
m => { },
m => { },
m => messages.Add(m)
);
class NixAdapter : IXPlatAdapter
{
public void ConfigureStartInfoForUser(ProcessStartInfo startInfo, NetworkCredential runAs, IReadOnlyDictionary<string, string>? customEnvironmentVariables)
=> throw new PlatformNotSupportedException("NetCore on Linux or Mac does not support running a process as a different user.");

if (result != 0)
throw new ShellExecutionException(result, messages);
public void TryKillProcessAndChildrenRecursively(Process process)
{
var messages = new List<string>();
var result = ShellExecutor.ExecuteCommand(
"/bin/bash",
$"-c \"kill -TERM {process.Id}\"",
Environment.CurrentDirectory,
m => { },
m => { },
m => messages.Add(m)
);

//process.Kill() doesnt seem to work in netcore 2.2 there have been some improvments in netcore 3.0 as well as also allowing to kill child processes
//https://github.com/dotnet/corefx/pull/34147
//In netcore 2.2 if the process is terminated we still get stuck on process.WaitForExit(); we need to manually check to see if the process has exited and then close it.
if (process.HasExited)
process.Close();
}
if (result != 0)
throw new ShellExecutionException(result, messages);

public Encoding GetOemEncoding()
=> Encoding.UTF8;
//process.Kill() doesnt seem to work in netcore 2.2 there have been some improvments in netcore 3.0 as well as also allowing to kill child processes
//https://github.com/dotnet/corefx/pull/34147
//In netcore 2.2 if the process is terminated we still get stuck on process.WaitForExit(); we need to manually check to see if the process has exited and then close it.
if (process.HasExited)
process.Close();
}

public Encoding GetOemEncoding()
=> Encoding.UTF8;
}
29 changes: 12 additions & 17 deletions source/Shellfish/PasteArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@
// - Changed to use regular StringBuilder as ValueStringBuilder is internal to the CLR
// - Added static JoinArguments function to encapsulate the logic of joining arguments

using System;
using System.Collections.Generic;
using System.Text;

namespace Octopus.Shellfish;

static class PasteArguments
{
const char Quote = '\"';
const char Backslash = '\\';

internal static string JoinArguments(IEnumerable<string> arguments)
{
var stringBuilder = new StringBuilder();
foreach(var argument in arguments)
{
foreach (var argument in arguments)
AppendArgument(stringBuilder, argument);
}

return stringBuilder.ToString();
}

internal static void AppendArgument(StringBuilder stringBuilder, string argument)
{
if (stringBuilder.Length != 0)
{
stringBuilder.Append(' ');
}

// Parsing rules for non-argv[0] arguments:
// - Backslash is a normal character except followed by a quote.
Expand All @@ -51,13 +51,13 @@ internal static void AppendArgument(StringBuilder stringBuilder, string argument
else
{
stringBuilder.Append(Quote);
int idx = 0;
var idx = 0;
while (idx < argument.Length)
{
char c = argument[idx++];
var c = argument[idx++];
if (c == Backslash)
{
int numBackSlash = 1;
var numBackSlash = 1;
while (idx < argument.Length && argument[idx] == Backslash)
{
idx++;
Expand Down Expand Up @@ -101,20 +101,15 @@ internal static void AppendArgument(StringBuilder stringBuilder, string argument
}
}

private static bool ContainsNoWhitespaceOrQuotes(string s)
static bool ContainsNoWhitespaceOrQuotes(string s)
{
for (int i = 0; i < s.Length; i++)
for (var i = 0; i < s.Length; i++)
{
char c = s[i];
var c = s[i];
if (char.IsWhiteSpace(c) || c == Quote)
{
return false;
}
}

return true;
}

private const char Quote = '\"';
private const char Backslash = '\\';
}
Loading

0 comments on commit 4f0397b

Please sign in to comment.