Skip to content

Commit

Permalink
feat: a new hope
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoBaSs84 committed Mar 10, 2024
1 parent 5f06d7e commit 31ee16e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 87 deletions.
43 changes: 43 additions & 0 deletions src/DDS.Tools/Commands/Base/ConvertCommandBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using DDS.Tools.Enumerators;
using DDS.Tools.Exceptions;
using DDS.Tools.Interfaces.Services;
using DDS.Tools.Models;
using DDS.Tools.Settings.Base;

using Spectre.Console;
using Spectre.Console.Cli;

namespace DDS.Tools.Commands.Base;

/// <summary>
/// The convert command base class.
/// </summary>
/// <inheritdoc/>
internal abstract class ConvertCommandBase<TSettings>(ITodoService todoService) : Command<TSettings> where TSettings : CommandSettings
{
private readonly ITodoService _todoService = todoService;

protected int Action(ConvertSettings settings, ImageType imageType)
{
TodoCollection todos;

if (!Directory.Exists(settings.SourceFolder))
throw new CommandException($"Directory '{settings.SourceFolder}' not found.");

string jsonFilePath = Path.Combine(settings.SourceFolder, "Result.json");

todos = File.Exists(jsonFilePath)
? _todoService.GetTodosFromJson(settings, imageType, jsonFilePath)
: _todoService.GetTodos(settings, imageType);

if (todos.Count.Equals(0))
{
AnsiConsole.MarkupLine($"[yellow]There is nothing todo![/]");
return 1;
}

_todoService.GetTodosDone(todos, settings, imageType);

return 0;
}
}
36 changes: 5 additions & 31 deletions src/DDS.Tools/Commands/DdsConvertCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Diagnostics.CodeAnalysis;

using DDS.Tools.Commands.Base;
using DDS.Tools.Enumerators;
using DDS.Tools.Exceptions;
using DDS.Tools.Interfaces.Services;
using DDS.Tools.Models;
using DDS.Tools.Settings;

using Microsoft.Extensions.Logging;
Expand All @@ -18,47 +17,22 @@ namespace DDS.Tools.Commands;
/// </summary>
/// <param name="loggerService">The logger service instance to use.</param>
/// <param name="todoService">The todo service instance to use.</param>
internal sealed class DdsConvertCommand(ILoggerService<DdsConvertCommand> loggerService, ITodoService todoService) : Command<DdsConvertSettings>
internal sealed class DdsConvertCommand(ILoggerService<DdsConvertCommand> loggerService, ITodoService todoService) : ConvertCommandBase<DdsConvertSettings>(todoService)
{
private const ImageType Type = ImageType.DDS;
private readonly ILoggerService<DdsConvertCommand> _loggerService = loggerService;
private readonly ITodoService _todoService = todoService;

private static readonly Action<ILogger, Exception?> LogException =
LoggerMessage.Define(LogLevel.Error, 0, "Exception occured.");

/// <inheritdoc/>
public override int Execute([NotNull] CommandContext context, [NotNull] DdsConvertSettings settings)
{
return AnsiConsole.Status()
.Spinner(Spinner.Known.Line)
.Start("Processing..", action => Action(settings));
}

private int Action(DdsConvertSettings settings)
{
try
{
TodoCollection todos = [];

if (!Directory.Exists(settings.SourceFolder))
throw new CommandException($"Directory '{settings.SourceFolder}' not found.");

string jsonFilePath = Path.Combine(settings.SourceFolder, "Result.json");

todos = File.Exists(jsonFilePath)
? _todoService.GetTodosFromJson(settings, Type, jsonFilePath)
: _todoService.GetTodos(settings, Type);

if (todos.Count.Equals(0))
{
AnsiConsole.MarkupLine($"[yellow]There is nothing todo![/]");
return 1;
}

_todoService.GetTodosDone(todos, settings, Type);

return 0;
return AnsiConsole.Status()
.Spinner(Spinner.Known.Line)
.Start("Processing..", action => Action(settings, Type));
}
catch (Exception ex)
{
Expand Down
36 changes: 5 additions & 31 deletions src/DDS.Tools/Commands/PngConvertCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Diagnostics.CodeAnalysis;

using DDS.Tools.Commands.Base;
using DDS.Tools.Enumerators;
using DDS.Tools.Exceptions;
using DDS.Tools.Interfaces.Services;
using DDS.Tools.Models;
using DDS.Tools.Settings;

using Microsoft.Extensions.Logging;
Expand All @@ -18,47 +17,22 @@ namespace DDS.Tools.Commands;
/// </summary>
/// <param name="loggerService">The logger service instance to use.</param>
/// <param name="todoService">The todo service instance to use.</param>
internal sealed class PngConvertCommand(ILoggerService<DdsConvertCommand> loggerService, ITodoService todoService) : Command<PngConvertSettings>
internal sealed class PngConvertCommand(ILoggerService<DdsConvertCommand> loggerService, ITodoService todoService) : ConvertCommandBase<PngConvertSettings>(todoService)
{
private const ImageType Type = ImageType.PNG;
private readonly ILoggerService<DdsConvertCommand> _loggerService = loggerService;
private readonly ITodoService _todoService = todoService;

private static readonly Action<ILogger, Exception?> LogException =
LoggerMessage.Define(LogLevel.Error, 0, "Exception occured.");

/// <inheritdoc/>
public override int Execute([NotNull] CommandContext context, [NotNull] PngConvertSettings settings)
{
return AnsiConsole.Status()
.Spinner(Spinner.Known.Line)
.Start("Processing..", action => Action(settings));
}

private int Action(PngConvertSettings settings)
{
try
{
TodoCollection todos = [];

if (!Directory.Exists(settings.SourceFolder))
throw new CommandException($"Directory '{settings.SourceFolder}' not found.");

string jsonFilePath = Path.Combine(settings.SourceFolder, "Result.json");

todos = File.Exists(jsonFilePath)
? _todoService.GetTodosFromJson(settings, Type, jsonFilePath)
: _todoService.GetTodos(settings, Type);

if (todos.Count.Equals(0))
{
AnsiConsole.MarkupLine($"[yellow]There is nothing todo![/]");
return 1;
}

_todoService.GetTodosDone(todos, settings, Type);

return 0;
return AnsiConsole.Status()
.Spinner(Spinner.Known.Line)
.Start("Processing..", action => Action(settings, Type));
}
catch (Exception ex)
{
Expand Down
23 changes: 0 additions & 23 deletions src/DDS.Tools/Extensions/StreamExtensions.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/DDS.Tools/Models/DdsImageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using BCnEncoder.ImageSharp;

using DDS.Tools.Extensions;
using DDS.Tools.Interfaces.Models;
using DDS.Tools.Interfaces.Services;
using DDS.Tools.Models.Base;
Expand Down
1 change: 0 additions & 1 deletion src/DDS.Tools/Models/PngImageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using BCnEncoder.Shared;
using BCnEncoder.Shared.ImageFiles;

using DDS.Tools.Extensions;
using DDS.Tools.Interfaces.Models;
using DDS.Tools.Interfaces.Services;
using DDS.Tools.Models.Base;
Expand Down

0 comments on commit 31ee16e

Please sign in to comment.