From b1d4f3693b53caa9f504e6a6e1e1b265000b4057 Mon Sep 17 00:00:00 2001 From: golem84 Date: Wed, 10 Dec 2025 22:32:48 +0500 Subject: [PATCH] move interfaces to separate folder --- CodeContextGenerator/App.xaml.cs | 1 + .../Interfaces/IContextFileGenerator.cs | 6 + .../Interfaces/IFileProcessorService.cs | 6 + .../Interfaces/IFileScannerService.cs | 9 + .../Interfaces/IProjectLoaderService.cs | 9 + .../Interfaces/ISettingsService.cs | 7 + CodeContextGenerator/Interfaces/IUIService.cs | 11 ++ .../Services/ContextFileGenerator.cs | 8 +- .../Services/FileProcessorService.cs | 8 +- .../Services/FileScannerService.cs | 9 +- .../Services/ProjectLoaderService.cs | 9 +- .../Services/ProjectScannerService.cs | 149 ++++++++------- .../Services/SettingsService.cs | 9 +- CodeContextGenerator/Services/UIService.cs | 11 +- .../ViewModels/MainViewModel.cs | 4 +- CodeContextGenerator/Views/MainWindow.xaml | 171 +++++++++++------- 16 files changed, 244 insertions(+), 183 deletions(-) create mode 100644 CodeContextGenerator/Interfaces/IContextFileGenerator.cs create mode 100644 CodeContextGenerator/Interfaces/IFileProcessorService.cs create mode 100644 CodeContextGenerator/Interfaces/IFileScannerService.cs create mode 100644 CodeContextGenerator/Interfaces/IProjectLoaderService.cs create mode 100644 CodeContextGenerator/Interfaces/ISettingsService.cs create mode 100644 CodeContextGenerator/Interfaces/IUIService.cs diff --git a/CodeContextGenerator/App.xaml.cs b/CodeContextGenerator/App.xaml.cs index 3d753f7..606ff08 100644 --- a/CodeContextGenerator/App.xaml.cs +++ b/CodeContextGenerator/App.xaml.cs @@ -1,4 +1,5 @@ using System.Windows; +using CodeContextGenerator.Interfaces; using CodeContextGenerator.Services; using CodeContextGenerator.ViewModels; using Microsoft.Extensions.DependencyInjection; diff --git a/CodeContextGenerator/Interfaces/IContextFileGenerator.cs b/CodeContextGenerator/Interfaces/IContextFileGenerator.cs new file mode 100644 index 0000000..eb178da --- /dev/null +++ b/CodeContextGenerator/Interfaces/IContextFileGenerator.cs @@ -0,0 +1,6 @@ +namespace CodeContextGenerator.Interfaces; + +public interface IContextFileGenerator +{ + Task GenerateContextFileAsync(List selectedFiles, string outputPath, string projectRootPath, IProgress progress, CancellationToken cancellationToken); +} diff --git a/CodeContextGenerator/Interfaces/IFileProcessorService.cs b/CodeContextGenerator/Interfaces/IFileProcessorService.cs new file mode 100644 index 0000000..624a773 --- /dev/null +++ b/CodeContextGenerator/Interfaces/IFileProcessorService.cs @@ -0,0 +1,6 @@ +namespace CodeContextGenerator.Interfaces; + +public interface IFileProcessorService +{ + string ProcessFileContent(string content, string fileName); +} diff --git a/CodeContextGenerator/Interfaces/IFileScannerService.cs b/CodeContextGenerator/Interfaces/IFileScannerService.cs new file mode 100644 index 0000000..6225c27 --- /dev/null +++ b/CodeContextGenerator/Interfaces/IFileScannerService.cs @@ -0,0 +1,9 @@ +using CodeContextGenerator.Models; + +namespace CodeContextGenerator.Interfaces; + +public interface IFileScannerService +{ + Task BuildDirectoryTreeAsync(string path, FileItem parentItem, IProgress progress = null, CancellationToken cancellationToken = default); + List GetSelectedFiles(FileItem rootItem); +} diff --git a/CodeContextGenerator/Interfaces/IProjectLoaderService.cs b/CodeContextGenerator/Interfaces/IProjectLoaderService.cs new file mode 100644 index 0000000..f44039c --- /dev/null +++ b/CodeContextGenerator/Interfaces/IProjectLoaderService.cs @@ -0,0 +1,9 @@ +using CodeContextGenerator.Models; + +namespace CodeContextGenerator.Interfaces; + +public interface IProjectLoaderService +{ + Task LoadProjectFromPathAsync(string projectPath, IProgress progress, CancellationToken cancellationToken); + string GetDefaultOutputFileName(string projectPath); +} diff --git a/CodeContextGenerator/Interfaces/ISettingsService.cs b/CodeContextGenerator/Interfaces/ISettingsService.cs new file mode 100644 index 0000000..6175d7b --- /dev/null +++ b/CodeContextGenerator/Interfaces/ISettingsService.cs @@ -0,0 +1,7 @@ +namespace CodeContextGenerator.Interfaces; + +public interface ISettingsService +{ + string GetLastProjectPath(); + void SaveLastProjectPath(string path); +} diff --git a/CodeContextGenerator/Interfaces/IUIService.cs b/CodeContextGenerator/Interfaces/IUIService.cs new file mode 100644 index 0000000..31a263c --- /dev/null +++ b/CodeContextGenerator/Interfaces/IUIService.cs @@ -0,0 +1,11 @@ +using System.Windows; + +namespace CodeContextGenerator.Interfaces; + +public interface IUIService +{ + string ShowFolderBrowserDialog(string initialDirectory = null); + bool ShowOpenProjectFileDialog(out string selectedPath); + bool ShowSaveFileDialog(string defaultFileName, string initialDirectory, out string savePath); + void ShowMessage(string message, string title, MessageBoxImage icon = MessageBoxImage.Information); +} diff --git a/CodeContextGenerator/Services/ContextFileGenerator.cs b/CodeContextGenerator/Services/ContextFileGenerator.cs index ead5f64..3b9a280 100644 --- a/CodeContextGenerator/Services/ContextFileGenerator.cs +++ b/CodeContextGenerator/Services/ContextFileGenerator.cs @@ -1,13 +1,9 @@ -using System.IO; +using CodeContextGenerator.Interfaces; +using System.IO; using System.Text; namespace CodeContextGenerator.Services; -public interface IContextFileGenerator -{ - Task GenerateContextFileAsync(List selectedFiles, string outputPath, string projectRootPath, IProgress progress, CancellationToken cancellationToken); -} - public class ContextFileGenerator : IContextFileGenerator { private readonly IFileProcessorService _fileProcessorService; diff --git a/CodeContextGenerator/Services/FileProcessorService.cs b/CodeContextGenerator/Services/FileProcessorService.cs index e3d31e2..22bce9d 100644 --- a/CodeContextGenerator/Services/FileProcessorService.cs +++ b/CodeContextGenerator/Services/FileProcessorService.cs @@ -1,13 +1,9 @@ -using System.Text; +using CodeContextGenerator.Interfaces; +using System.Text; using System.Text.RegularExpressions; namespace CodeContextGenerator.Services; -public interface IFileProcessorService -{ - string ProcessFileContent(string content, string fileName); -} - public class FileProcessorService : IFileProcessorService { public string ProcessFileContent(string content, string fileName) diff --git a/CodeContextGenerator/Services/FileScannerService.cs b/CodeContextGenerator/Services/FileScannerService.cs index fb14574..425fe35 100644 --- a/CodeContextGenerator/Services/FileScannerService.cs +++ b/CodeContextGenerator/Services/FileScannerService.cs @@ -1,14 +1,9 @@ -using CodeContextGenerator.Models; +using CodeContextGenerator.Interfaces; +using CodeContextGenerator.Models; using System.IO; namespace CodeContextGenerator.Services; -public interface IFileScannerService -{ - Task BuildDirectoryTreeAsync(string path, FileItem parentItem, IProgress progress = null, CancellationToken cancellationToken = default); - List GetSelectedFiles(FileItem rootItem); -} - public class FileScannerService : IFileScannerService { private static readonly string[] ExcludedDirectories = { diff --git a/CodeContextGenerator/Services/ProjectLoaderService.cs b/CodeContextGenerator/Services/ProjectLoaderService.cs index a00d737..86e340e 100644 --- a/CodeContextGenerator/Services/ProjectLoaderService.cs +++ b/CodeContextGenerator/Services/ProjectLoaderService.cs @@ -1,15 +1,10 @@ -using CodeContextGenerator.Models; +using CodeContextGenerator.Interfaces; +using CodeContextGenerator.Models; using System.IO; using System.Windows; namespace CodeContextGenerator.Services; -public interface IProjectLoaderService -{ - Task LoadProjectFromPathAsync(string projectPath, IProgress progress, CancellationToken cancellationToken); - string GetDefaultOutputFileName(string projectPath); -} - public class ProjectLoaderService : IProjectLoaderService { private readonly IFileScannerService _fileScannerService; diff --git a/CodeContextGenerator/Services/ProjectScannerService.cs b/CodeContextGenerator/Services/ProjectScannerService.cs index 69545d5..f71077c 100644 --- a/CodeContextGenerator/Services/ProjectScannerService.cs +++ b/CodeContextGenerator/Services/ProjectScannerService.cs @@ -1,96 +1,91 @@ -using System.IO; -using CodeContextGenerator.Models; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Threading; +using CodeContextGenerator.Models; +using System.IO; -namespace CodeContextGenerator.Services +namespace CodeContextGenerator.Services; + +public static class ProjectScannerService { - public static class ProjectScannerService + private static readonly string[] ExcludedDirectories = { + "bin", "obj", ".git", "packages", ".vs", "Properties", + "node_modules", ".vscode", ".idea", ".vs", "Debug", "Release" + }; + + private static readonly string[] IncludedExtensions = { ".cs", ".xaml" }; + + public static async Task BuildDirectoryTreeAsync(string path, FileItem parentItem, IProgress progress = null, CancellationToken cancellationToken = default) { - private static readonly string[] ExcludedDirectories = { - "bin", "obj", ".git", "packages", ".vs", "Properties", - "node_modules", ".vscode", ".idea", ".vs", "Debug", "Release" - }; + cancellationToken.ThrowIfCancellationRequested(); - private static readonly string[] IncludedExtensions = { ".cs", ".xaml" }; - - public static async Task BuildDirectoryTreeAsync(string path, FileItem parentItem, IProgress progress = null, CancellationToken cancellationToken = default) + try { - cancellationToken.ThrowIfCancellationRequested(); + if (!Directory.Exists(path)) + return; - try + var directories = Directory.GetDirectories(path) + .Where(d => !ExcludedDirectories.Any(ex => d.EndsWith(ex, System.StringComparison.OrdinalIgnoreCase) || + Path.GetFileName(d).Equals(ex, System.StringComparison.OrdinalIgnoreCase))) + .ToList(); + + var files = Directory.GetFiles(path) + .Where(f => IncludedExtensions.Any(ext => f.EndsWith(ext, System.StringComparison.OrdinalIgnoreCase))) + .ToList(); + + int totalItems = directories.Count + files.Count; + int processedItems = 0; + + // Обрабатываем поддиректории + foreach (var dir in directories) { - if (!Directory.Exists(path)) - return; + cancellationToken.ThrowIfCancellationRequested(); - var directories = Directory.GetDirectories(path) - .Where(d => !ExcludedDirectories.Any(ex => d.EndsWith(ex, System.StringComparison.OrdinalIgnoreCase) || - Path.GetFileName(d).Equals(ex, System.StringComparison.OrdinalIgnoreCase))) - .ToList(); - - var files = Directory.GetFiles(path) - .Where(f => IncludedExtensions.Any(ext => f.EndsWith(ext, System.StringComparison.OrdinalIgnoreCase))) - .ToList(); - - int totalItems = directories.Count + files.Count; - int processedItems = 0; - - // Обрабатываем поддиректории - foreach (var dir in directories) + var dirName = Path.GetFileName(dir); + var dirItem = new FileItem { - cancellationToken.ThrowIfCancellationRequested(); + Name = dirName, + FullName = dir, + IsDirectory = true, + Parent = parentItem, + IsSelected = false + }; - var dirName = Path.GetFileName(dir); - var dirItem = new FileItem - { - Name = dirName, - FullName = dir, - IsDirectory = true, - Parent = parentItem, - IsSelected = false - }; + await BuildDirectoryTreeAsync(dir, dirItem, progress, cancellationToken); - await BuildDirectoryTreeAsync(dir, dirItem, progress, cancellationToken); - - // Добавляем директорию только если в ней есть файлы или поддиректории с файлами - if (dirItem.Children.Any()) - { - parentItem.Children.Add(dirItem); - } - - processedItems++; - progress?.Report((int)((processedItems * 100.0) / totalItems)); + // Добавляем директорию только если в ней есть файлы или поддиректории с файлами + if (dirItem.Children.Any()) + { + parentItem.Children.Add(dirItem); } - // Обрабатываем файлы - foreach (var file in files) + processedItems++; + progress?.Report((int)((processedItems * 100.0) / totalItems)); + } + + // Обрабатываем файлы + foreach (var file in files) + { + cancellationToken.ThrowIfCancellationRequested(); + + var fileItem = new FileItem { - cancellationToken.ThrowIfCancellationRequested(); + Name = Path.GetFileName(file), + FullName = file, + IsDirectory = false, + Parent = parentItem, + IsSelected = false + }; - var fileItem = new FileItem - { - Name = Path.GetFileName(file), - FullName = file, - IsDirectory = false, - Parent = parentItem, - IsSelected = false - }; - - parentItem.Children.Add(fileItem); - processedItems++; - progress?.Report((int)((processedItems * 100.0) / totalItems)); - } - } - catch (IOException) - { - // Игнорируем ошибки доступа к директориям - } - catch (UnauthorizedAccessException) - { - // Игнорируем ошибки доступа + parentItem.Children.Add(fileItem); + processedItems++; + progress?.Report((int)((processedItems * 100.0) / totalItems)); } } + catch (IOException) + { + // Игнорируем ошибки доступа к директориям + } + catch (UnauthorizedAccessException) + { + // Игнорируем ошибки доступа + } } } \ No newline at end of file diff --git a/CodeContextGenerator/Services/SettingsService.cs b/CodeContextGenerator/Services/SettingsService.cs index a6dc06b..9891dd0 100644 --- a/CodeContextGenerator/Services/SettingsService.cs +++ b/CodeContextGenerator/Services/SettingsService.cs @@ -1,14 +1,9 @@ -using System.IO; +using CodeContextGenerator.Interfaces; +using System.IO; using System.Text.Json; namespace CodeContextGenerator.Services; -public interface ISettingsService -{ - string GetLastProjectPath(); - void SaveLastProjectPath(string path); -} - public class SettingsService : ISettingsService { private const string SettingsFileName = "app_settings.json"; diff --git a/CodeContextGenerator/Services/UIService.cs b/CodeContextGenerator/Services/UIService.cs index 24370c6..e2d68ec 100644 --- a/CodeContextGenerator/Services/UIService.cs +++ b/CodeContextGenerator/Services/UIService.cs @@ -1,17 +1,10 @@ -using Microsoft.Win32; +using CodeContextGenerator.Interfaces; +using Microsoft.Win32; using System.IO; using System.Windows; namespace CodeContextGenerator.Services; -public interface IUIService -{ - string ShowFolderBrowserDialog(string initialDirectory = null); - bool ShowOpenProjectFileDialog(out string selectedPath); - bool ShowSaveFileDialog(string defaultFileName, string initialDirectory, out string savePath); - void ShowMessage(string message, string title, MessageBoxImage icon = MessageBoxImage.Information); -} - public class UIService : IUIService { public string ShowFolderBrowserDialog(string initialDirectory = null) diff --git a/CodeContextGenerator/ViewModels/MainViewModel.cs b/CodeContextGenerator/ViewModels/MainViewModel.cs index 4007d39..e41cfe9 100644 --- a/CodeContextGenerator/ViewModels/MainViewModel.cs +++ b/CodeContextGenerator/ViewModels/MainViewModel.cs @@ -1,5 +1,5 @@ -using CodeContextGenerator.Models; -using CodeContextGenerator.Services; +using CodeContextGenerator.Interfaces; +using CodeContextGenerator.Models; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System.IO; diff --git a/CodeContextGenerator/Views/MainWindow.xaml b/CodeContextGenerator/Views/MainWindow.xaml index 9738fb8..500e08f 100644 --- a/CodeContextGenerator/Views/MainWindow.xaml +++ b/CodeContextGenerator/Views/MainWindow.xaml @@ -1,68 +1,93 @@ - + - - - - - + + + + + - + - -