Skip to content

Commit 31ee16e

Browse files
committed
feat: a new hope
1 parent 5f06d7e commit 31ee16e

File tree

6 files changed

+53
-87
lines changed

6 files changed

+53
-87
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using DDS.Tools.Enumerators;
2+
using DDS.Tools.Exceptions;
3+
using DDS.Tools.Interfaces.Services;
4+
using DDS.Tools.Models;
5+
using DDS.Tools.Settings.Base;
6+
7+
using Spectre.Console;
8+
using Spectre.Console.Cli;
9+
10+
namespace DDS.Tools.Commands.Base;
11+
12+
/// <summary>
13+
/// The convert command base class.
14+
/// </summary>
15+
/// <inheritdoc/>
16+
internal abstract class ConvertCommandBase<TSettings>(ITodoService todoService) : Command<TSettings> where TSettings : CommandSettings
17+
{
18+
private readonly ITodoService _todoService = todoService;
19+
20+
protected int Action(ConvertSettings settings, ImageType imageType)
21+
{
22+
TodoCollection todos;
23+
24+
if (!Directory.Exists(settings.SourceFolder))
25+
throw new CommandException($"Directory '{settings.SourceFolder}' not found.");
26+
27+
string jsonFilePath = Path.Combine(settings.SourceFolder, "Result.json");
28+
29+
todos = File.Exists(jsonFilePath)
30+
? _todoService.GetTodosFromJson(settings, imageType, jsonFilePath)
31+
: _todoService.GetTodos(settings, imageType);
32+
33+
if (todos.Count.Equals(0))
34+
{
35+
AnsiConsole.MarkupLine($"[yellow]There is nothing todo![/]");
36+
return 1;
37+
}
38+
39+
_todoService.GetTodosDone(todos, settings, imageType);
40+
41+
return 0;
42+
}
43+
}

src/DDS.Tools/Commands/DdsConvertCommand.cs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System.Diagnostics.CodeAnalysis;
22

3+
using DDS.Tools.Commands.Base;
34
using DDS.Tools.Enumerators;
4-
using DDS.Tools.Exceptions;
55
using DDS.Tools.Interfaces.Services;
6-
using DDS.Tools.Models;
76
using DDS.Tools.Settings;
87

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

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

3028
/// <inheritdoc/>
3129
public override int Execute([NotNull] CommandContext context, [NotNull] DdsConvertSettings settings)
32-
{
33-
return AnsiConsole.Status()
34-
.Spinner(Spinner.Known.Line)
35-
.Start("Processing..", action => Action(settings));
36-
}
37-
38-
private int Action(DdsConvertSettings settings)
3930
{
4031
try
4132
{
42-
TodoCollection todos = [];
43-
44-
if (!Directory.Exists(settings.SourceFolder))
45-
throw new CommandException($"Directory '{settings.SourceFolder}' not found.");
46-
47-
string jsonFilePath = Path.Combine(settings.SourceFolder, "Result.json");
48-
49-
todos = File.Exists(jsonFilePath)
50-
? _todoService.GetTodosFromJson(settings, Type, jsonFilePath)
51-
: _todoService.GetTodos(settings, Type);
52-
53-
if (todos.Count.Equals(0))
54-
{
55-
AnsiConsole.MarkupLine($"[yellow]There is nothing todo![/]");
56-
return 1;
57-
}
58-
59-
_todoService.GetTodosDone(todos, settings, Type);
60-
61-
return 0;
33+
return AnsiConsole.Status()
34+
.Spinner(Spinner.Known.Line)
35+
.Start("Processing..", action => Action(settings, Type));
6236
}
6337
catch (Exception ex)
6438
{

src/DDS.Tools/Commands/PngConvertCommand.cs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System.Diagnostics.CodeAnalysis;
22

3+
using DDS.Tools.Commands.Base;
34
using DDS.Tools.Enumerators;
4-
using DDS.Tools.Exceptions;
55
using DDS.Tools.Interfaces.Services;
6-
using DDS.Tools.Models;
76
using DDS.Tools.Settings;
87

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

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

3028
/// <inheritdoc/>
3129
public override int Execute([NotNull] CommandContext context, [NotNull] PngConvertSettings settings)
32-
{
33-
return AnsiConsole.Status()
34-
.Spinner(Spinner.Known.Line)
35-
.Start("Processing..", action => Action(settings));
36-
}
37-
38-
private int Action(PngConvertSettings settings)
3930
{
4031
try
4132
{
42-
TodoCollection todos = [];
43-
44-
if (!Directory.Exists(settings.SourceFolder))
45-
throw new CommandException($"Directory '{settings.SourceFolder}' not found.");
46-
47-
string jsonFilePath = Path.Combine(settings.SourceFolder, "Result.json");
48-
49-
todos = File.Exists(jsonFilePath)
50-
? _todoService.GetTodosFromJson(settings, Type, jsonFilePath)
51-
: _todoService.GetTodos(settings, Type);
52-
53-
if (todos.Count.Equals(0))
54-
{
55-
AnsiConsole.MarkupLine($"[yellow]There is nothing todo![/]");
56-
return 1;
57-
}
58-
59-
_todoService.GetTodosDone(todos, settings, Type);
60-
61-
return 0;
33+
return AnsiConsole.Status()
34+
.Spinner(Spinner.Known.Line)
35+
.Start("Processing..", action => Action(settings, Type));
6236
}
6337
catch (Exception ex)
6438
{

src/DDS.Tools/Extensions/StreamExtensions.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/DDS.Tools/Models/DdsImageModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using BCnEncoder.ImageSharp;
44

5-
using DDS.Tools.Extensions;
65
using DDS.Tools.Interfaces.Models;
76
using DDS.Tools.Interfaces.Services;
87
using DDS.Tools.Models.Base;

src/DDS.Tools/Models/PngImageModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using BCnEncoder.Shared;
55
using BCnEncoder.Shared.ImageFiles;
66

7-
using DDS.Tools.Extensions;
87
using DDS.Tools.Interfaces.Models;
98
using DDS.Tools.Interfaces.Services;
109
using DDS.Tools.Models.Base;

0 commit comments

Comments
 (0)