Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleny113 committed Dec 17, 2023
1 parent 89ce9d0 commit 4311b61
Show file tree
Hide file tree
Showing 41 changed files with 192 additions and 222 deletions.
32 changes: 17 additions & 15 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ insert_final_newline = true
# Organize usings
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
file_header_template =
file_header_template = # this. and Me. preferences



# this. and Me. preferences
dotnet_style_qualification_for_event = false
dotnet_style_qualification_for_field = false
dotnet_style_qualification_for_method = false
Expand Down Expand Up @@ -204,35 +205,36 @@ dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

# ReSharper properties
resharper_csharp_wrap_after_invocation_lpar = true
resharper_csharp_wrap_before_declaration_rpar = true
resharper_csharp_wrap_before_invocation_rpar = true
resharper_csharp_wrap_after_invocation_lpar = false
resharper_csharp_wrap_before_declaration_rpar = false
resharper_csharp_wrap_before_invocation_rpar = false
resharper_trailing_comma_in_multiline_lists = true
resharper_wrap_before_primary_constructor_declaration_lpar = false
resharper_wrap_before_primary_constructor_declaration_rpar = false
resharper_wrap_after_invocation_lpar = false
resharper_wrap_before_declaration_rpar = false
resharper_wrap_before_invocation_rpar = false

[*.{cs,vb}]
dotnet_style_operator_placement_when_wrapping = beginning_of_line
Expand Down
2 changes: 1 addition & 1 deletion Hexus.Daemon.Runner/Hexus.Daemon.Runner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Hexus.Daemon\Hexus.Daemon.csproj" />
<ProjectReference Include="..\Hexus.Daemon\Hexus.Daemon.csproj"/>
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions Hexus.Daemon/CircularBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ public async IAsyncEnumerable<T> ReadAllAsync([EnumeratorCancellation] Cancellat
{
var bufferRead = _buffer[readIndex];

if (bufferRead is not null)
{
if (bufferRead is not null)
yield return bufferRead;
}

readIndex = (readIndex + 1) % _buffer.Length;
continue;
Expand Down
7 changes: 2 additions & 5 deletions Hexus.Daemon/Configuration/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public static void EnsureDirectoriesExistence()

Directory.CreateDirectory(LogsDirectory);
}

public static string NormalizePath(string path)
{
return Path.GetFullPath(path);
}

public static string NormalizePath(string path) => Path.GetFullPath(path);
}
1 change: 1 addition & 0 deletions Hexus.Daemon/Configuration/HexusApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed record HexusApplication
public Dictionary<string, string> EnvironmentVariables { get; set; } = [];

#region Internal proprieties

[YamlIgnore] internal Process? Process { get; set; }

// Logs
Expand Down
1 change: 0 additions & 1 deletion Hexus.Daemon/Configuration/HexusConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public sealed record HexusConfiguration
public Dictionary<string, HexusApplication> Applications { get; init; } = [];
}


public sealed record HexusConfigurationFile
{
public string? UnixSocket { get; init; }
Expand Down
9 changes: 3 additions & 6 deletions Hexus.Daemon/Configuration/HexusConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ private void LoadConfiguration()

if (!File.Exists(_configurationFile))
{
Configuration = new HexusConfiguration
{
UnixSocket = _socketFile,
};
Configuration = new HexusConfiguration { UnixSocket = _socketFile };

SaveConfiguration();
return;
Expand Down Expand Up @@ -64,7 +61,7 @@ internal void SaveConfiguration()
Applications = Configuration.Applications.Values,
AppSettings = AppSettings,
};

var yamlString = YamlSerializer.Serialize(configFile);

lock (this)
Expand Down Expand Up @@ -93,7 +90,7 @@ internal HexusConfigurationManager(bool isDevelopment)
throw new Exception("Unable to parse the configuration file", exception);
}
}

internal static IEnumerable<KeyValuePair<string, string?>> FlatDictionary(Dictionary<object, object?>? dictionary, string prefix = "")
{
if (dictionary is null)
Expand Down
9 changes: 4 additions & 5 deletions Hexus.Daemon/Contracts/ErrorResponses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

public static class ErrorResponses
{
public static readonly ErrorResponse ApplicationIsNotRunningMessage = new(Error: "The application is not running.");
public static readonly ErrorResponse ApplicationIsRunningMessage = new(Error: "The application is already running.");
public static readonly ErrorResponse ApplicationWithTheSameNameAlreadyExiting = new(Error: "There is already an application with the same name.");
public static readonly ErrorResponse CantEditRunningApplication = new(Error: "To edit an application, the application must not be running.");

public static readonly ErrorResponse ApplicationIsNotRunningMessage = new("The application is not running.");
public static readonly ErrorResponse ApplicationIsRunningMessage = new("The application is already running.");
public static readonly ErrorResponse ApplicationWithTheSameNameAlreadyExiting = new("There is already an application with the same name.");
public static readonly ErrorResponse CantEditRunningApplication = new("To edit an application, the application must not be running.");
}
2 changes: 1 addition & 1 deletion Hexus.Daemon/Contracts/NewApplicationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public sealed record NewApplicationRequest(
string Arguments = "",
string WorkingDirectory = "",
string Note = "",
Dictionary<string, string> EnvironmentVariables = null!
Dictionary<string, string>? EnvironmentVariables = null
) : IContract
{
[Required, AbsolutePath] public string WorkingDirectory { get; set; } = WorkingDirectory;
Expand Down
2 changes: 1 addition & 1 deletion Hexus.Daemon/Contracts/SendInputRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
namespace Hexus.Daemon.Contracts;

public sealed record SendInputRequest(
[property: Required] string Text,
[property: Required] string Text,
bool AddNewLine = true
) : IContract;
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ internal sealed class DeleteApplicationEndpoint : IEndpoint
{
[HttpMap(HttpMapMethod.Delete, "/{name}/delete")]
public static Results<NoContent, NotFound> Handle(
[FromServices]
HexusConfigurationManager configManager,
[FromServices]
ProcessManagerService processManager,
[FromServices] HexusConfigurationManager configManager,
[FromServices] ProcessManagerService processManager,
[FromRoute] string name,
[FromQuery] bool forceStop = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ internal sealed class EditApplicationEndpoint : IEndpoint
if (editRequest.IsReloadingEnvironmentVariables == false)
{
foreach (var (key, value) in editRequest.NewEnvironmentVariables)
{
newEnvironmentVariables[key] = value;
}
}

foreach (var env in editRequest.RemoveEnvironmentVariables)
{
newEnvironmentVariables.Remove(env);
}

// Edit the configuration
application.Name = editRequest.Name;
Expand Down
8 changes: 2 additions & 6 deletions Hexus.Daemon/Endpoints/Applications/GetLogsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ internal class GetLogsEndpoint : IEndpoint
{
var requestTime = DateTimeOffset.UtcNow;

foreach (var log in GetLogs(application, lines))
{
foreach (var log in GetLogs(application, lines))
yield return log;
}

if (noStreaming)
yield break;
Expand All @@ -50,10 +48,8 @@ internal class GetLogsEndpoint : IEndpoint
.ReadAllAsync(ct)
.Where(log => CheckIfSendLog(log, requestTime));

await foreach (var log in logAsyncEnumerator)
{
await foreach (var log in logAsyncEnumerator)
yield return log;
}
}

private static IEnumerable<string> GetLogs(HexusApplication application, int lines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class NewApplicationEndpoint : IEndpoint
{
if (request.WorkingDirectory is "")
request.WorkingDirectory = EnvironmentHelper.Home;

if (!request.ValidateContract(out var errors))
return TypedResults.ValidationProblem(errors);

Expand All @@ -38,4 +38,3 @@ internal sealed class NewApplicationEndpoint : IEndpoint
return TypedResults.Ok(application.MapToResponse());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ internal sealed class RestartApplicationEndpoint : IEndpoint
{
if (!configuration.Applications.TryGetValue(name, out var application))
return TypedResults.NotFound();

processManager.StopApplication(application.Name, forceStop);

if (!processManager.StartApplication(application))
return TypedResults.StatusCode((int)HttpStatusCode.InternalServerError);

return TypedResults.NoContent();
}
}
12 changes: 7 additions & 5 deletions Hexus.Daemon/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
namespace Hexus.Daemon.Extensions;

internal static class DictionaryExtensions {
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> factory) where TKey : notnull
internal static class DictionaryExtensions
{
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> factory)
where TKey : notnull
{
if (dictionary.TryGetValue(key, out var value))
return value;

value = factory(key);
dictionary[key] = value;

return value;
}
}
}
24 changes: 9 additions & 15 deletions Hexus.Daemon/Extensions/MapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ namespace Hexus.Daemon.Extensions;

internal static class MapperExtensions
{
public static HexusApplication MapToApplication(this NewApplicationRequest request)
{
return new HexusApplication
public static HexusApplication MapToApplication(this NewApplicationRequest request) =>
new()
{
Name = request.Name,
Executable = EnvironmentHelper.NormalizePath(request.Executable),
Expand All @@ -17,14 +16,12 @@ public static HexusApplication MapToApplication(this NewApplicationRequest reque
Note = request.Note,
EnvironmentVariables = request.EnvironmentVariables,
};
}

public static HexusApplicationResponse MapToResponse(this HexusApplication application)
{
return new HexusApplicationResponse(
Name: application.Name,
Executable: EnvironmentHelper.NormalizePath(application.Executable),
Arguments: application.Arguments,
public static HexusApplicationResponse MapToResponse(this HexusApplication application) =>
new(
application.Name,
EnvironmentHelper.NormalizePath(application.Executable),
application.Arguments,
Note: application.Note,
WorkingDirectory: EnvironmentHelper.NormalizePath(application.WorkingDirectory),
EnvironmentVariables: application.EnvironmentVariables,
Expand All @@ -34,11 +31,8 @@ public static HexusApplicationResponse MapToResponse(this HexusApplication appli
CpuUsage: application.LastCpuUsage,
MemoryUsage: PerformanceTrackingService.GetMemoryUsage(application)
);
}

public static IEnumerable<HexusApplicationResponse> MapToResponse(this Dictionary<string, HexusApplication> applications)
{
return applications
public static IEnumerable<HexusApplicationResponse> MapToResponse(this Dictionary<string, HexusApplication> applications) =>
applications
.Select(pair => pair.Value.MapToResponse());
}
}
8 changes: 1 addition & 7 deletions Hexus.Daemon/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static double GetProcessCpuUsage(this Process process, HexusApplication.C
var processorTimeDifference = currentTotalProcessorTime - cpuStats.LastTotalProcessorTime;

var cpuUsage = processorTimeDifference / Environment.ProcessorCount / timeDifference;

cpuStats.LastTotalProcessorTime = currentTotalProcessorTime;
cpuStats.LastGetProcessCpuUsageInvocation = currentTime;

Expand All @@ -31,17 +31,11 @@ public static IEnumerable<Process> GetChildProcesses(this Process process)
Func<int, Process[]> function;

if (OperatingSystem.IsWindows())
{
function = ProcessChildren.GetChildProcessesWindows;
}
else if (OperatingSystem.IsLinux())
{
function = ProcessChildren.GetChildProcessesLinux;
}
else
{
throw new NotSupportedException("Getting the child processes is only supported on Windows and Linux");
}

return GetChildProcesses(function, process.Id);
}
Expand Down
20 changes: 10 additions & 10 deletions Hexus.Daemon/Hexus.Daemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
<InvariantGlobalization>true</InvariantGlobalization>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<PublishSingleFile>true</PublishSingleFile>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EndpointMapper" Version="2.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.Management" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
<InternalsVisibleTo Include="Hexus" />
<InternalsVisibleTo Include="Hexus.Daemon.Runner" />
<PackageReference Include="EndpointMapper" Version="2.0.0"/>
<PackageReference Include="System.Linq.Async" Version="6.0.1"/>
<PackageReference Include="System.Management" Version="8.0.0"/>
<PackageReference Include="YamlDotNet" Version="13.7.1"/>

<InternalsVisibleTo Include="Hexus"/>
<InternalsVisibleTo Include="Hexus.Daemon.Runner"/>
</ItemGroup>

<ItemGroup Condition="('$(SelfContained)' == 'true' And '$(OS)' == 'Windows_NT') Or '$(SelfContained)' == 'false'">
<None Update="windows-kill.dll">
<None Update="windows-kill.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>
<None Remove="Properties\launchSettings.json" />
<None Remove="Properties\launchSettings.json"/>
</ItemGroup>
</Project>
Loading

0 comments on commit 4311b61

Please sign in to comment.