Skip to content

Commit

Permalink
Add support to config max instances in tray (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Aug 8, 2021
1 parent 5b36138 commit 96a1180
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 35 deletions.
4 changes: 3 additions & 1 deletion docs/diff-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ This value can be changed:

Setting the `DiffEngine_MaxInstances` environment variable to the number of instances to launch.

This value can also be set using [the DiffEngineTray options dialog](/docs/tray.md#max-instances-to-launch).


### Using code

Expand All @@ -71,7 +73,7 @@ By default, when a diff is opened, the temp file is on the left and the target f

This value can be changed by setting the `DiffEngine_TargetOnLeft` environment variable to `true`.

This value can also be set using [the DiffEngineTray options dialog](https://github.com/VerifyTests/DiffEngine/blob/main/docs/tray.md#open-on-left).
This value can also be set using [the DiffEngineTray options dialog](/docs/tray.md#open-on-left).


## Successful verification behavior
Expand Down
4 changes: 3 additions & 1 deletion docs/mdsource/diff-tool.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ This value can be changed:

Setting the `DiffEngine_MaxInstances` environment variable to the number of instances to launch.

This value can also be set using [the DiffEngineTray options dialog](/docs/tray.md#max-instances-to-launch).


### Using code

Expand All @@ -58,7 +60,7 @@ By default, when a diff is opened, the temp file is on the left and the target f

This value can be changed by setting the `DiffEngine_TargetOnLeft` environment variable to `true`.

This value can also be set using [the DiffEngineTray options dialog](https://github.com/VerifyTests/DiffEngine/blob/main/docs/tray.md#open-on-left).
This value can also be set using [the DiffEngineTray options dialog](/docs/tray.md#open-on-left).


## Successful verification behavior
Expand Down
5 changes: 5 additions & 0 deletions docs/mdsource/tray.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Runs DiffEngineTray at system startup.
By default, when a diff is opened, the temp file will be on the left and the target file will be on the right. To invert this, select "Open on left".


#### Max instances to launch

Control the [max instances to launch setting](docs/diff-tool.md#maxinstancestolaunch).


#### Accept all HotKey

Registers a system wide HotKey to accept pending:
Expand Down
5 changes: 5 additions & 0 deletions docs/tray.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ Runs DiffEngineTray at system startup.
By default, when a diff is opened, the temp file will be on the left and the target file will be on the right. To invert this, select "Open on left".


#### Max instances to launch

Control the [max instances to launch setting](docs/diff-tool.md#maxinstancestolaunch).


#### Accept all HotKey

Registers a system wide HotKey to accept pending:
Expand Down
3 changes: 1 addition & 2 deletions src/DiffEngine/DiffRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public static partial class DiffRunner

public static void MaxInstancesToLaunch(int value)
{
Guard.AgainstNegativeAndZero(value, nameof(value));
MaxInstance.Set(value);
MaxInstance.SetForAppDomain(value);
}

public static LaunchResult Launch(DiffTool tool, string tempFile, string targetFile)
Expand Down
2 changes: 1 addition & 1 deletion src/DiffEngine/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void AgainstEmpty(string value, string argumentName)
}
}

public static void AgainstEmpty(object[] value, string argumentName)
public static void AgainstEmpty(object?[] value, string argumentName)
{
if (value == null)
{
Expand Down
31 changes: 26 additions & 5 deletions src/DiffEngine/MaxInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

static class MaxInstance
{
static int maxInstancesToLaunch = GetMaxInstances();
public static int MaxInstancesToLaunch { get; private set; } = GetMaxInstances();
static int launchedInstances;
const int defaultMax = 5;

static int GetMaxInstances()
{
var variable = Environment.GetEnvironmentVariable("DiffEngine_MaxInstances");
if (string.IsNullOrEmpty(variable))
{
return 5;
return defaultMax;
}

if (ushort.TryParse(variable, out var result))
Expand All @@ -22,15 +23,35 @@ static int GetMaxInstances()
throw new($"Could not parse the DiffEngine_MaxInstances environment variable: {variable}");
}

public static void Set(int value)
public static void SetForAppDomain(int value)
{
Guard.AgainstNegativeAndZero(value, nameof(value));
maxInstancesToLaunch = value;
MaxInstancesToLaunch = value;
}

public static void SetForUser(int value)
{
if (MaxInstancesToLaunch == value)
{
return;
}
MaxInstancesToLaunch = value;
string? envVariable;
if (value == defaultMax)
{
envVariable = null;
}
else
{
envVariable = value.ToString();
}

Environment.SetEnvironmentVariable("DiffEngine_MaxInstances", envVariable, EnvironmentVariableTarget.User);
}

public static bool Reached()
{
var instanceCount = Interlocked.Increment(ref launchedInstances);
return instanceCount > maxInstancesToLaunch;
return instanceCount > MaxInstancesToLaunch;
}
}
2 changes: 1 addition & 1 deletion src/DiffEngine/ResolvedTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public string BuildCommand(string tempFile, string targetFile)

string GetArguments(string tempFile, string targetFile)
{
if (TargetPositionHelper.TargetOnLeft)
if (TargetPosition.TargetOnLeft)
{
return TargetLeftArguments(tempFile, targetFile);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;

static class TargetPositionHelper
static class TargetPosition
{
public static bool TargetOnLeft { get; private set; }

static TargetPositionHelper()
static TargetPosition()
{
TargetOnLeft = ReadTargetOnLeft().GetValueOrDefault(false);
}
Expand Down Expand Up @@ -33,6 +33,11 @@ static TargetPositionHelper()

public static void SetTargetOnLeft(bool value)
{
if (TargetOnLeft == value)
{
return;
}

TargetOnLeft = value;
string? envVariable;
if (value)
Expand Down
Binary file modified src/DiffEngineTray.Tests/OptionsFormTests.Default.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/DiffEngineTray.Tests/OptionsFormTests.WithKeys.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
{
AcceptAllHotKey: {
Key: T
}
},
MaxInstancesToLaunch: 5
}
4 changes: 3 additions & 1 deletion src/DiffEngineTray/DiffEngineTray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
<None Remove="**\*.png" />
<EmbeddedResource Include="Images\*.ico" />
<EmbeddedResource Include="Images\*.png" />
<Compile Include="..\DiffEngine\TargetPositionHelper.cs" Link="TargetPositionHelper.cs" />
<Compile Include="..\DiffEngine\MaxInstance.cs" Link="MaxInstance.cs" />
<Compile Include="..\DiffEngine\TargetPosition.cs" Link="TargetPosition.cs" />
<Compile Include="..\DiffEngine\Guard.cs" Link="Guard.cs" />
<PackageReference Include="Resourcer.Fody" Version="1.8.0" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.5.2" PrivateAssets="all" />
<PackageReference Include="Serilog" Version="2.10.0" />
Expand Down
86 changes: 71 additions & 15 deletions src/DiffEngineTray/Settings/OptionsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 96a1180

Please sign in to comment.