Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Муканов Арман #203

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ConsoleApp/CommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CommandLine;
using ConsoleApp.Handlers;
using ConsoleApp.Options;

namespace ConsoleApp;

public class CommandLineParser: ICommandLineParser
{
private readonly IOptionsHandler[] handlers;
private readonly IOptions[] options;

public CommandLineParser(IOptionsHandler[] handlers, IOptions[] options)
{
this.handlers = handlers;
this.options = options;
}

public void ParseFromConsole()
{
var types = options
.Select(opt => opt.GetType())
.ToArray();

Console.WriteLine("Доступные команды \"--help\"");
while (true)
{
var input = Console.ReadLine();
var args = input.Split();
Parser.Default.ParseArguments(args, types)
.WithParsed<IOptions>(Parse);
}
}

private void Parse<T>(T options) where T : IOptions
{
var handler = handlers.FirstOrDefault(h => h.CanParse(options));
if (handler is null)
throw new Exception("Обработчик параметров не найден.");

var message = handler.WithParsed(options);
Console.WriteLine(message);
}
}
18 changes: 18 additions & 0 deletions ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хорошо что выделил в отдельную сборку CUI


<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TagsCloudContainer\TagsCloudContainer.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="8.0.0" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions ConsoleApp/Handlers/ExitOptionsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ConsoleApp.Options;

namespace ConsoleApp.Handlers;

public class ExitOptionsHandler : IOptionsHandler
{
public bool CanParse(IOptions options)
{
return options is ExitOptions;
}

public string WithParsed(IOptions options)
{
Environment.Exit(0);
return "Завершение выполнения программы.";
}
}
59 changes: 59 additions & 0 deletions ConsoleApp/Handlers/GenerateCloudOptionsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using ConsoleApp.Options;
using MyStemWrapper;
using TagsCloudContainer;
using TagsCloudContainer.Settings;

namespace ConsoleApp.Handlers;

public class GenerateCloudOptionsHandler : IOptionsHandler
{
private readonly MyStem myStem;
private readonly IAppSettings appSettings;
private readonly IAnalyseSettings analyseSettings;
private readonly ITagsCloudContainer cloudContainer;

public GenerateCloudOptionsHandler(IAppSettings appSettings, MyStem myStem, IAnalyseSettings analyseSettings,
ITagsCloudContainer cloudContainer)
{
this.appSettings = appSettings;
this.myStem = myStem;
this.analyseSettings = analyseSettings;
this.cloudContainer = cloudContainer;
}

public bool CanParse(IOptions options)
{
return options is GenerateCloudOptions;
}

public string WithParsed(IOptions options)
{
Map(options);
return Execute();
}

private void Map(IOptions options)
{
if (options is GenerateCloudOptions opts)
Map(opts);
else
throw new ArgumentException(nameof(options));
}

private void Map(GenerateCloudOptions options)
{
appSettings.InputFile = options.InputFile;
appSettings.OutputFile = options.OutputFile;

if (!string.IsNullOrWhiteSpace(options.AnalyseParameters))
myStem.Parameters = "-" + options.AnalyseParameters;
if (options.ValidSpeechParts.Any())
analyseSettings.ValidSpeechParts = options.ValidSpeechParts.ToArray();
}

private string Execute()
{
cloudContainer.GenerateImageToFile(appSettings.InputFile, appSettings.OutputFile);
return $"Успешно сохранено в файл - \"{appSettings.OutputFile}\".";
}
}
10 changes: 10 additions & 0 deletions ConsoleApp/Handlers/IOptionsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using ConsoleApp.Options;

namespace ConsoleApp.Handlers;

public interface IOptionsHandler
{
public bool CanParse(IOptions options);

public string WithParsed(IOptions options);
}
48 changes: 48 additions & 0 deletions ConsoleApp/Handlers/SetImageOptionsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using ConsoleApp.Options;
using SixLabors.ImageSharp;
using TagsCloudContainer.Settings;

namespace ConsoleApp.Handlers;

public class SetImageOptionsHandler : IOptionsHandler
{
private readonly IImageSettings imageSettings;

public SetImageOptionsHandler(IImageSettings imageSettings)
{
this.imageSettings = imageSettings;
}

private void Map(SetImageOptions options)
{
if (options.PrimaryColor != default)
imageSettings.PrimaryColor = options.PrimaryColor;
if (options.BackgroundColor != default)
imageSettings.BackgroundColor = options.BackgroundColor;
if (options.Width != default)
imageSettings.ImageSize = new Size(options.Width, imageSettings.ImageSize.Height);
if (options.Height != default)
imageSettings.ImageSize = new Size(imageSettings.ImageSize.Width, options.Height);
if (options.Font is not null)
imageSettings.TextOptions.Font = options.Font;
}

private void Map(IOptions options)
{
if (options is SetImageOptions opts)
Map(opts);
else
throw new ArgumentException(nameof(options));
}

public bool CanParse(IOptions options)
{
return options is SetImageOptions;
}

public string WithParsed(IOptions options)
{
Map(options);
return "Настройки изображения установлены.";
}
}
6 changes: 6 additions & 0 deletions ConsoleApp/ICommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ConsoleApp;

public interface ICommandLineParser
{
public void ParseFromConsole();
}
8 changes: 8 additions & 0 deletions ConsoleApp/Options/ExitOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using CommandLine;

namespace ConsoleApp.Options;

[Verb("exit", HelpText = "Закончить выполнение программы")]
public class ExitOptions: IOptions
{
}
19 changes: 19 additions & 0 deletions ConsoleApp/Options/GenerateCloudOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using CommandLine;

namespace ConsoleApp.Options;

[Verb("generate", HelpText = "Предобработка слов")]
public class GenerateCloudOptions: IOptions
{
[Option('i', "input", Required = true, HelpText = "Путь к файлу текста для анализа.")]
public string InputFile { get; set; }

[Option('o', "output", Required = true, HelpText = "Путь к сохранению изображения.")]
public string OutputFile { get; set; }

[Option('p', "params", HelpText = "Параметры вывода MyStem")]
public string AnalyseParameters { get; set; }

[Value(1, Max = 14, HelpText = "Части речи, которые буду задействованы при анализе.")]
public IEnumerable<string> ValidSpeechParts { get; set; } = new string[0];
}
5 changes: 5 additions & 0 deletions ConsoleApp/Options/IOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace ConsoleApp.Options;

public interface IOptions
{
}
24 changes: 24 additions & 0 deletions ConsoleApp/Options/SetImageOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommandLine;
using SixLabors.Fonts;
using SixLabors.ImageSharp;

namespace ConsoleApp.Options;

[Verb("image", HelpText = "Настройка изображения")]
public class SetImageOptions: IOptions
{
[Option('c', "color", HelpText = "Основной цвет")]
public Color PrimaryColor { get; set; }

[Option('b', "background", HelpText = "Цвет заднего фона")]
public Color BackgroundColor { get; set; }

[Option('w', "width", HelpText = "Ширина")]
public int Width { get; set; }

[Option('h', "height", HelpText = "Высота")]
public int Height { get; set; }

[Option('f', "font",HelpText = "Шрифт")]
public Font Font { get; set; }
}
46 changes: 46 additions & 0 deletions ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Reflection;
using Autofac;
using MyStemWrapper;
using TagsCloudContainer;
using TagsCloudContainer.Settings;

namespace ConsoleApp;

public class Program
{
public static void Main()
{
var builder = new ContainerBuilder();
ConfigureService(builder);
var container = builder.Build();

using var scope = container.BeginLifetimeScope();
var commandLineReader = scope.Resolve<ICommandLineParser>();
commandLineReader.ParseFromConsole();
}

public static void ConfigureService(ContainerBuilder builder)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. обычно все таки регистрация зависиомстей происходит не ручками, представь что у тебя сотни классов, надо будет каждый зарегать и еще убедиться что никто не пропущен, для этого у autofac существует отдельный метод RegisterAllAssemblyTypes, но для этого надо еще правильно уметь правильно указывать сборки для регистрации, а вот там где нужно что то хитрое, какая то особая регистрация - вот в тех только местах и регистрировать явно
  2. чтобы не хранить в одной копилке все регистрации, в больших проектах это довольно тяжело поддерживать, у autofac есть очень хороший инструмент - модули, это позволит в CUI оставить только регистрацию связанную непосредственно с CUI или характенрную только для этой сборки, а в либу вынести то что связано только с ней

{
RegisterAssemblyTypes(builder, typeof(Tag).GetTypeInfo().Assembly);
RegisterAssemblyTypes(builder, typeof(CommandLineParser).GetTypeInfo().Assembly);

var location = Assembly.GetExecutingAssembly().Location;
var path = Path.GetDirectoryName(location);
var myStem = new MyStem
{
PathToMyStem = $"{path}\\mystem.exe",
Parameters = "-nli",
};
builder.RegisterInstance(myStem).AsSelf().SingleInstance();

builder.RegisterType<AppSettings>().As<IAppSettings>().SingleInstance();
builder.RegisterType<AnalyseSettings>().As<IAnalyseSettings>().SingleInstance();
builder.RegisterType<ImageSettings>().As<IImageSettings>().SingleInstance();
}

private static void RegisterAssemblyTypes(ContainerBuilder builder, Assembly assembly)
{
builder.RegisterAssemblyTypes(assembly)
.AsImplementedInterfaces();
}
}
Loading