From a7704dac7a81bf661ff36759654b88aa2b20f48a Mon Sep 17 00:00:00 2001 From: Salih Date: Sat, 10 Dec 2022 15:55:39 +0300 Subject: [PATCH 1/3] Added process dependency check --- .../ApplicationSharedModule.cs | 7 +- .../GitHunter.Application.Shared.csproj | 1 + .../ApplicationModule.cs | 6 +- .../GitHunter.Application.csproj | 1 + src/GitHunter.Core.Shared/CoreSharedModule.cs | 7 +- .../DependencyProcesses/IDependencyProcess.cs | 5 + .../DependencyProcesses/IProcessDependency.cs | 6 ++ .../IProcessDependencyChecker.cs | 8 ++ .../ProcessDependencyAttribute.cs | 10 ++ .../DependencyProcesses/a.cs | 6 ++ src/GitHunter.Core.Shared/Git/IGitManager.cs | 6 ++ .../GitHunter.Core.Shared.csproj | 1 + .../Modules/GitHunterModule.cs | 18 ++++ .../Processes/IProcessManager.cs | 10 ++ .../Processes/ProcessResult.cs | 8 ++ .../Processes/ProcessStartInfo.cs | 22 +++++ src/GitHunter.Core/CoreModule.cs | 42 +++++++- .../ProcessDependencyChecker.cs | 24 +++++ .../ProcessDependencyOptions.cs | 6 ++ src/GitHunter.Core/Git/GitManager.cs | 10 ++ .../Git/GitProcessDependency.cs | 13 +++ src/GitHunter.Core/GitHunter.Core.csproj | 1 + src/GitHunter.Core/Modules/ModuleHelper.cs | 58 +++++++++++ .../Processes/ProcessManager.cs | 95 +++++++++++++++++++ .../DesktopSharedModule.cs | 7 +- .../GitHunter.Desktop.Shared.csproj | 1 + .../DesktopWindowsFormModule.cs | 21 +++- .../GitHunter.Desktop.WindowsForm.csproj | 1 + .../MainForm.Designer.cs | 2 +- src/GitHunter.Desktop.WindowsForm/MainForm.cs | 2 +- src/GitHunter.Desktop.WindowsForm/Program.cs | 2 +- 31 files changed, 387 insertions(+), 20 deletions(-) create mode 100644 src/GitHunter.Core.Shared/DependencyProcesses/IDependencyProcess.cs create mode 100644 src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependency.cs create mode 100644 src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependencyChecker.cs create mode 100644 src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs create mode 100644 src/GitHunter.Core.Shared/DependencyProcesses/a.cs create mode 100644 src/GitHunter.Core.Shared/Git/IGitManager.cs create mode 100644 src/GitHunter.Core.Shared/Modules/GitHunterModule.cs create mode 100644 src/GitHunter.Core.Shared/Processes/IProcessManager.cs create mode 100644 src/GitHunter.Core.Shared/Processes/ProcessResult.cs create mode 100644 src/GitHunter.Core.Shared/Processes/ProcessStartInfo.cs create mode 100644 src/GitHunter.Core/DependencyProcesses/ProcessDependencyChecker.cs create mode 100644 src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs create mode 100644 src/GitHunter.Core/Git/GitManager.cs create mode 100644 src/GitHunter.Core/Git/GitProcessDependency.cs create mode 100644 src/GitHunter.Core/Modules/ModuleHelper.cs create mode 100644 src/GitHunter.Core/Processes/ProcessManager.cs diff --git a/src/GitHunter.Application.Shared/ApplicationSharedModule.cs b/src/GitHunter.Application.Shared/ApplicationSharedModule.cs index 73d4156..6c15975 100644 --- a/src/GitHunter.Application.Shared/ApplicationSharedModule.cs +++ b/src/GitHunter.Application.Shared/ApplicationSharedModule.cs @@ -1,9 +1,10 @@ -using GitHunter.Core.Shared; +using GitHunter.Core; +using GitHunter.Core.Modules; using Volo.Abp.Modularity; -namespace GitHunter.Application.Shared; +namespace GitHunter.Application; [DependsOn(typeof(CoreSharedModule))] -public class ApplicationSharedModule : AbpModule +public class ApplicationSharedModule : GitHunterModule { } \ No newline at end of file diff --git a/src/GitHunter.Application.Shared/GitHunter.Application.Shared.csproj b/src/GitHunter.Application.Shared/GitHunter.Application.Shared.csproj index 055ade0..09eb2ca 100644 --- a/src/GitHunter.Application.Shared/GitHunter.Application.Shared.csproj +++ b/src/GitHunter.Application.Shared/GitHunter.Application.Shared.csproj @@ -2,6 +2,7 @@ net7.0 + GitHunter.Application enable enable diff --git a/src/GitHunter.Application/ApplicationModule.cs b/src/GitHunter.Application/ApplicationModule.cs index 14d2ab6..3af5077 100644 --- a/src/GitHunter.Application/ApplicationModule.cs +++ b/src/GitHunter.Application/ApplicationModule.cs @@ -1,5 +1,5 @@ -using GitHunter.Application.Shared; -using GitHunter.Core; +using GitHunter.Core; +using GitHunter.Core.Modules; using Volo.Abp.Modularity; namespace GitHunter.Application; @@ -8,6 +8,6 @@ namespace GitHunter.Application; typeof(ApplicationSharedModule), typeof(CoreModule) )] -public class ApplicationModule : AbpModule +public class ApplicationModule : GitHunterModule { } \ No newline at end of file diff --git a/src/GitHunter.Application/GitHunter.Application.csproj b/src/GitHunter.Application/GitHunter.Application.csproj index 41c161b..a3db0c8 100644 --- a/src/GitHunter.Application/GitHunter.Application.csproj +++ b/src/GitHunter.Application/GitHunter.Application.csproj @@ -2,6 +2,7 @@ net7.0 + GitHunter.Application enable enable diff --git a/src/GitHunter.Core.Shared/CoreSharedModule.cs b/src/GitHunter.Core.Shared/CoreSharedModule.cs index 8f54850..107b561 100644 --- a/src/GitHunter.Core.Shared/CoreSharedModule.cs +++ b/src/GitHunter.Core.Shared/CoreSharedModule.cs @@ -1,13 +1,14 @@ -using Volo.Abp.Autofac; +using GitHunter.Core.Modules; +using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; using Volo.Abp.Modularity; -namespace GitHunter.Core.Shared; +namespace GitHunter.Core; [DependsOn( typeof(AbpAutofacModule), typeof(AbpAutoMapperModule) )] -public class CoreSharedModule : AbpModule +public class CoreSharedModule : GitHunterModule { } \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/IDependencyProcess.cs b/src/GitHunter.Core.Shared/DependencyProcesses/IDependencyProcess.cs new file mode 100644 index 0000000..fa6da85 --- /dev/null +++ b/src/GitHunter.Core.Shared/DependencyProcesses/IDependencyProcess.cs @@ -0,0 +1,5 @@ +namespace GitHunter.Core.DependencyProcesses; + +public interface IDependencyProcess +{ +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependency.cs b/src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependency.cs new file mode 100644 index 0000000..30be6d3 --- /dev/null +++ b/src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependency.cs @@ -0,0 +1,6 @@ +namespace GitHunter.Core.DependencyProcesses; + +public interface IProcessDependency +{ + bool Check(); +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependencyChecker.cs b/src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependencyChecker.cs new file mode 100644 index 0000000..d579f4e --- /dev/null +++ b/src/GitHunter.Core.Shared/DependencyProcesses/IProcessDependencyChecker.cs @@ -0,0 +1,8 @@ +using System.Reflection; + +namespace GitHunter.Core.DependencyProcesses; + +public interface IProcessDependencyChecker +{ + bool CheckDependency(Assembly assembly); +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs b/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs new file mode 100644 index 0000000..6f9a806 --- /dev/null +++ b/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs @@ -0,0 +1,10 @@ +namespace GitHunter.Core.DependencyProcesses; + +[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] +public class ProcessDependencyAttribute : ProcessDependencyAttribute where T : IProcessDependency +{ + public ProcessDependencyAttribute() + { + DependencyProcess = typeof(T); + } +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/a.cs b/src/GitHunter.Core.Shared/DependencyProcesses/a.cs new file mode 100644 index 0000000..d0c8ed4 --- /dev/null +++ b/src/GitHunter.Core.Shared/DependencyProcesses/a.cs @@ -0,0 +1,6 @@ +namespace GitHunter.Core.DependencyProcesses; + +public abstract class ProcessDependencyAttribute : Attribute +{ + public Type DependencyProcess { get; protected set; } +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/Git/IGitManager.cs b/src/GitHunter.Core.Shared/Git/IGitManager.cs new file mode 100644 index 0000000..3a3ab20 --- /dev/null +++ b/src/GitHunter.Core.Shared/Git/IGitManager.cs @@ -0,0 +1,6 @@ +namespace GitHunter.Core.Git; + +public interface IGitManager +{ + +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/GitHunter.Core.Shared.csproj b/src/GitHunter.Core.Shared/GitHunter.Core.Shared.csproj index 04fcf05..6f853c0 100644 --- a/src/GitHunter.Core.Shared/GitHunter.Core.Shared.csproj +++ b/src/GitHunter.Core.Shared/GitHunter.Core.Shared.csproj @@ -2,6 +2,7 @@ net7.0 + GitHunter.Core enable enable diff --git a/src/GitHunter.Core.Shared/Modules/GitHunterModule.cs b/src/GitHunter.Core.Shared/Modules/GitHunterModule.cs new file mode 100644 index 0000000..517f30c --- /dev/null +++ b/src/GitHunter.Core.Shared/Modules/GitHunterModule.cs @@ -0,0 +1,18 @@ +using System.Reflection; +using Volo.Abp.Modularity; + +namespace GitHunter.Core.Modules; + +public class GitHunterModule : AbpModule +{ + public static bool IsGitHunterModule(Type type) + { + var typeInfo = type.GetTypeInfo(); + + return + typeInfo.IsClass && + !typeInfo.IsAbstract && + !typeInfo.IsGenericType && + typeof(GitHunterModule).GetTypeInfo().IsAssignableFrom(type); + } +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/Processes/IProcessManager.cs b/src/GitHunter.Core.Shared/Processes/IProcessManager.cs new file mode 100644 index 0000000..8eb416b --- /dev/null +++ b/src/GitHunter.Core.Shared/Processes/IProcessManager.cs @@ -0,0 +1,10 @@ +namespace GitHunter.Core.Processes; + +public interface IProcessManager +{ + Task RunAsync(string command, string arguments, string? workingDirectory = null); + + Task RunAsync(ProcessStartInfo processStartInfo); + + Task KillAllProcessesAsync(); +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/Processes/ProcessResult.cs b/src/GitHunter.Core.Shared/Processes/ProcessResult.cs new file mode 100644 index 0000000..cdbb542 --- /dev/null +++ b/src/GitHunter.Core.Shared/Processes/ProcessResult.cs @@ -0,0 +1,8 @@ +namespace GitHunter.Core.Processes; + +public class ProcessResult +{ + public int ExitCode { get; set; } + public string? Output { get; set; } + public string? Error { get; set; } +} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/Processes/ProcessStartInfo.cs b/src/GitHunter.Core.Shared/Processes/ProcessStartInfo.cs new file mode 100644 index 0000000..16223e2 --- /dev/null +++ b/src/GitHunter.Core.Shared/Processes/ProcessStartInfo.cs @@ -0,0 +1,22 @@ +namespace GitHunter.Core.Processes; + +public class ProcessStartInfo +{ + public ProcessStartInfo(string command, string arguments, string? workingDirectory = null, Action? exited = null, + Action? outputDataReceived = null, Action? errorDataReceived = null) + { + Command = command; + Arguments = arguments; + Exited = exited; + WorkingDirectory = workingDirectory; + OutputDataReceived = outputDataReceived; + ErrorDataReceived = errorDataReceived; + } + + public string Command { get; private set; } + public string Arguments { get; private set; } + public string? WorkingDirectory { get; private set; } + public Action? OutputDataReceived { get; private set; } + public Action? ErrorDataReceived { get; private set; } + public Action? Exited { get; private set; } +} \ No newline at end of file diff --git a/src/GitHunter.Core/CoreModule.cs b/src/GitHunter.Core/CoreModule.cs index e70978b..20db9cc 100644 --- a/src/GitHunter.Core/CoreModule.cs +++ b/src/GitHunter.Core/CoreModule.cs @@ -1,9 +1,47 @@ -using GitHunter.Core.Shared; +using GitHunter.Core.DependencyProcesses; +using GitHunter.Core.Modules; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Volo.Abp; using Volo.Abp.Modularity; namespace GitHunter.Core; [DependsOn(typeof(CoreSharedModule))] -public class CoreModule : AbpModule +public class CoreModule : GitHunterModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + // var services = context.Services; + // // var processDependencyChecker = services.GetRequiredService(); + // var processDependencyOptions = services.GetRequiredService>().Value; + // + // var modules = services.GetSingletonInstance() + // .LoadModules( + // services, + // processDependencyOptions.StartupModule, + // new AbpApplicationCreationOptions(context.Services).PlugInSources + // ); + + + } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + + using (var scope = context.ServiceProvider.CreateScope()) + { + var processDependencyChecker = scope.ServiceProvider.GetRequiredService(); + var processDependencyOptions = scope.ServiceProvider.GetRequiredService>().Value; + var modules = ModuleHelper.FindGitHunterModuleTypes(processDependencyOptions.StartupModule); + + foreach (var assembly in modules.Select(m=>m.Assembly)) + { + processDependencyChecker?.CheckDependency(assembly); + } + } + + base.OnApplicationInitialization(context); + } } \ No newline at end of file diff --git a/src/GitHunter.Core/DependencyProcesses/ProcessDependencyChecker.cs b/src/GitHunter.Core/DependencyProcesses/ProcessDependencyChecker.cs new file mode 100644 index 0000000..24ca04b --- /dev/null +++ b/src/GitHunter.Core/DependencyProcesses/ProcessDependencyChecker.cs @@ -0,0 +1,24 @@ +using System.Reflection; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; + +namespace GitHunter.Core.DependencyProcesses; + +public class ProcessDependencyChecker : IProcessDependencyChecker, ISingletonDependency +{ + private readonly IServiceProvider _serviceProvider; + + public ProcessDependencyChecker(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public bool CheckDependency(Assembly assembly) + { + var dependenciesTypes = assembly.GetTypes().Where(t => typeof(IDependencyProcess).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract).ToList(); + var dependenciesAttributes = dependenciesTypes.SelectMany(t => t.GetCustomAttributes(typeof(ProcessDependencyAttribute<>))).ToList(); + var dependencies = dependenciesAttributes.Cast().Select(t=>(IProcessDependency)ActivatorUtilities.CreateInstance(_serviceProvider, t.DependencyProcess)).ToList(); + + return dependencies.All(d => d.Check()); + } +} \ No newline at end of file diff --git a/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs b/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs new file mode 100644 index 0000000..c33643d --- /dev/null +++ b/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs @@ -0,0 +1,6 @@ +namespace GitHunter.Core.DependencyProcesses; + +public class ProcessDependencyOptions +{ + public Type StartupModule { get; set; } +} \ No newline at end of file diff --git a/src/GitHunter.Core/Git/GitManager.cs b/src/GitHunter.Core/Git/GitManager.cs new file mode 100644 index 0000000..3197562 --- /dev/null +++ b/src/GitHunter.Core/Git/GitManager.cs @@ -0,0 +1,10 @@ +using GitHunter.Core.DependencyProcesses; +using Volo.Abp.DependencyInjection; + +namespace GitHunter.Core.Git; + +[ProcessDependency] +public class GitManager : IGitManager, IDependencyProcess, ITransientDependency +{ + +} \ No newline at end of file diff --git a/src/GitHunter.Core/Git/GitProcessDependency.cs b/src/GitHunter.Core/Git/GitProcessDependency.cs new file mode 100644 index 0000000..f27730f --- /dev/null +++ b/src/GitHunter.Core/Git/GitProcessDependency.cs @@ -0,0 +1,13 @@ +using GitHunter.Core.DependencyProcesses; + +namespace GitHunter.Core.Git; + +public class GitProcessDependency : IProcessDependency +{ + public bool Check() + { + var environmentVariable = Environment.GetEnvironmentVariable("PATH"); + var paths = environmentVariable?.Split(';'); + return paths != null && paths.Any(path => File.Exists(Path.Combine(path, "git.exe"))); + } +} \ No newline at end of file diff --git a/src/GitHunter.Core/GitHunter.Core.csproj b/src/GitHunter.Core/GitHunter.Core.csproj index 055ade0..d5dc8ff 100644 --- a/src/GitHunter.Core/GitHunter.Core.csproj +++ b/src/GitHunter.Core/GitHunter.Core.csproj @@ -2,6 +2,7 @@ net7.0 + GitHunter.Core enable enable diff --git a/src/GitHunter.Core/Modules/ModuleHelper.cs b/src/GitHunter.Core/Modules/ModuleHelper.cs new file mode 100644 index 0000000..2b2a77b --- /dev/null +++ b/src/GitHunter.Core/Modules/ModuleHelper.cs @@ -0,0 +1,58 @@ +using System.Reflection; +using Volo.Abp.Modularity; + +namespace GitHunter.Core.Modules; + +internal static class ModuleHelper +{ + public static IEnumerable FindGitHunterModuleTypes(Type startupModuleType) + { + if(!GitHunterModule.IsGitHunterModule(startupModuleType)) + { + throw new ArgumentException("Given type is not an GitHunter module: " + startupModuleType.AssemblyQualifiedName); + } + var moduleTypes = new List(); + AddModuleAndDependenciesRecursively(moduleTypes, startupModuleType); + return moduleTypes; + } + + private static List FindDependedModuleTypes(ICustomAttributeProvider moduleType) + { + var dependencies = new List(); + + var dependencyDescriptors = moduleType + .GetCustomAttributes(false) + .OfType(); + + foreach (var descriptor in dependencyDescriptors) + { + foreach (var dependedModuleType in descriptor.GetDependedTypes()) + { + dependencies.AddIfNotContains(dependedModuleType); + } + } + + return dependencies; + } + + private static void AddModuleAndDependenciesRecursively( + ICollection moduleTypes, + Type moduleType) + { + if(!GitHunterModule.IsGitHunterModule(moduleType)) + return; + + if (moduleTypes.Contains(moduleType)) + { + return; + } + + + moduleTypes.Add(moduleType); + + foreach (var dependedModuleType in FindDependedModuleTypes(moduleType)) + { + AddModuleAndDependenciesRecursively(moduleTypes, dependedModuleType); + } + } +} \ No newline at end of file diff --git a/src/GitHunter.Core/Processes/ProcessManager.cs b/src/GitHunter.Core/Processes/ProcessManager.cs new file mode 100644 index 0000000..00598a5 --- /dev/null +++ b/src/GitHunter.Core/Processes/ProcessManager.cs @@ -0,0 +1,95 @@ +using Volo.Abp.DependencyInjection; + +namespace GitHunter.Core.Processes; + +public class ProcessManager : IProcessManager, IScopedDependency +{ + private readonly List _processes = new(); + + private bool _killAllProcessesRequested; + + public Task RunAsync(string command, string arguments, string? workingDirectory = null) => + RunAsync(new ProcessStartInfo(command, arguments, workingDirectory)); + + + public Task RunAsync(ProcessStartInfo processStartInfo) + { + if (_killAllProcessesRequested) + { + throw new InvalidOperationException( + "Cannot run a new process after KillAllProcessesAsync() has been called."); + } + + var process = CreateProcess(processStartInfo); + + var tcs = new TaskCompletionSource(); + + string? output = null; + string? error = null; + + process.ErrorDataReceived += (sender, args) => + { + if (args.Data == null) return; + error += args.Data + Environment.NewLine; + processStartInfo.ErrorDataReceived?.Invoke(args.Data); + }; + + process.OutputDataReceived += (sender, args) => + { + if (args.Data == null) return; + output += args.Data + Environment.NewLine; + processStartInfo.OutputDataReceived?.Invoke(args.Data); + }; + + process.Exited += (sender, args) => + { + processStartInfo.Exited?.Invoke(); + var result = new ProcessResult + { + ExitCode = process.ExitCode, + Output = output, + Error = error + }; + + tcs.SetResult(result); + }; + + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + process.Start(); + + _processes.Add(process); + + return tcs.Task; + } + + private System.Diagnostics.Process CreateProcess(ProcessStartInfo processStartInfo) + { + return new System.Diagnostics.Process + { + StartInfo = + { + Arguments = processStartInfo.Arguments, + FileName = processStartInfo.Command, + WorkingDirectory = processStartInfo.WorkingDirectory ?? Directory.GetCurrentDirectory(), + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + }, + EnableRaisingEvents = true + }; + } + + public Task KillAllProcessesAsync() + { + _killAllProcessesRequested = true; + foreach (var process in _processes.Where(process => !process.HasExited)) + { + process.Kill(); + } + + return Task.FromResult(true); + } +} \ No newline at end of file diff --git a/src/GitHunter.Desktop.Shared/DesktopSharedModule.cs b/src/GitHunter.Desktop.Shared/DesktopSharedModule.cs index 2006b20..4b9dd7a 100644 --- a/src/GitHunter.Desktop.Shared/DesktopSharedModule.cs +++ b/src/GitHunter.Desktop.Shared/DesktopSharedModule.cs @@ -1,9 +1,10 @@ -using GitHunter.Application.Shared; +using GitHunter.Application; +using GitHunter.Core.Modules; using Volo.Abp.Modularity; -namespace GitHunter.Desktop.Shared; +namespace GitHunter.Desktop; [DependsOn(typeof(ApplicationSharedModule))] -public class DesktopSharedModule : AbpModule +public class DesktopSharedModule : GitHunterModule { } \ No newline at end of file diff --git a/src/GitHunter.Desktop.Shared/GitHunter.Desktop.Shared.csproj b/src/GitHunter.Desktop.Shared/GitHunter.Desktop.Shared.csproj index d239365..f0ac62d 100644 --- a/src/GitHunter.Desktop.Shared/GitHunter.Desktop.Shared.csproj +++ b/src/GitHunter.Desktop.Shared/GitHunter.Desktop.Shared.csproj @@ -2,6 +2,7 @@ net7.0 + GitHunter.Desktop enable enable diff --git a/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs b/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs index cddcabd..d5c2d21 100644 --- a/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs +++ b/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs @@ -1,11 +1,26 @@ using GitHunter.Application; -using GitHunter.Desktop.Shared; +using GitHunter.Core.DependencyProcesses; +using GitHunter.Core.Modules; using Volo.Abp.Modularity; -namespace GitHunter.Desktop.WindowsForm; +namespace GitHunter.Desktop; [DependsOn(typeof(DesktopSharedModule), typeof(ApplicationModule))] -public class DesktopWindowsFormModule : AbpModule +public class DesktopWindowsFormModule : GitHunterModule { + + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(o=>o.StartupModule = typeof(DesktopWindowsFormModule)); + + base.ConfigureServices(context); + } + + public override void PreConfigureServices(ServiceConfigurationContext context) + { + Configure(o=>o.StartupModule = typeof(DesktopWindowsFormModule)); + + base.PreConfigureServices(context); + } } \ No newline at end of file diff --git a/src/GitHunter.Desktop.WindowsForm/GitHunter.Desktop.WindowsForm.csproj b/src/GitHunter.Desktop.WindowsForm/GitHunter.Desktop.WindowsForm.csproj index 68a02bd..8be2b17 100644 --- a/src/GitHunter.Desktop.WindowsForm/GitHunter.Desktop.WindowsForm.csproj +++ b/src/GitHunter.Desktop.WindowsForm/GitHunter.Desktop.WindowsForm.csproj @@ -3,6 +3,7 @@ WinExe net7.0-windows + GitHunter.Desktop enable true enable diff --git a/src/GitHunter.Desktop.WindowsForm/MainForm.Designer.cs b/src/GitHunter.Desktop.WindowsForm/MainForm.Designer.cs index 803e7f3..b08c29a 100644 --- a/src/GitHunter.Desktop.WindowsForm/MainForm.Designer.cs +++ b/src/GitHunter.Desktop.WindowsForm/MainForm.Designer.cs @@ -1,4 +1,4 @@ -namespace GitHunter.Desktop.WindowsForm; +namespace GitHunter.Desktop; partial class MainForm { diff --git a/src/GitHunter.Desktop.WindowsForm/MainForm.cs b/src/GitHunter.Desktop.WindowsForm/MainForm.cs index 678e8f3..618aa67 100644 --- a/src/GitHunter.Desktop.WindowsForm/MainForm.cs +++ b/src/GitHunter.Desktop.WindowsForm/MainForm.cs @@ -1,6 +1,6 @@ using Volo.Abp.DependencyInjection; -namespace GitHunter.Desktop.WindowsForm; +namespace GitHunter.Desktop; public partial class MainForm : Form, ISingletonDependency { diff --git a/src/GitHunter.Desktop.WindowsForm/Program.cs b/src/GitHunter.Desktop.WindowsForm/Program.cs index ca82684..4a3cda1 100644 --- a/src/GitHunter.Desktop.WindowsForm/Program.cs +++ b/src/GitHunter.Desktop.WindowsForm/Program.cs @@ -3,7 +3,7 @@ using Serilog.Events; using Volo.Abp; -namespace GitHunter.Desktop.WindowsForm; +namespace GitHunter.Desktop; public class Program { From c4f7f4e5bd32496f7364aa56895be2ffed0fa312 Mon Sep 17 00:00:00 2001 From: Salih Date: Sat, 10 Dec 2022 17:00:13 +0300 Subject: [PATCH 2/3] Refactoring --- src/GitHunter.Application.Shared/Git/IGitManager.cs | 6 ++++++ .../Git/GitManager.cs | 2 +- .../Git/GitProcessDependency.cs | 8 ++++++-- src/GitHunter.Application/GitHunter.Application.csproj | 4 ++++ .../DependencyProcesses/ProcessDependencyAttribute.cs | 6 ++++++ src/GitHunter.Core.Shared/DependencyProcesses/a.cs | 6 ------ src/GitHunter.Core.Shared/Git/IGitManager.cs | 6 ------ src/GitHunter.Core/CoreModule.cs | 2 +- 8 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 src/GitHunter.Application.Shared/Git/IGitManager.cs rename src/{GitHunter.Core => GitHunter.Application}/Git/GitManager.cs (85%) rename src/{GitHunter.Core => GitHunter.Application}/Git/GitProcessDependency.cs (66%) delete mode 100644 src/GitHunter.Core.Shared/DependencyProcesses/a.cs delete mode 100644 src/GitHunter.Core.Shared/Git/IGitManager.cs diff --git a/src/GitHunter.Application.Shared/Git/IGitManager.cs b/src/GitHunter.Application.Shared/Git/IGitManager.cs new file mode 100644 index 0000000..fb0e654 --- /dev/null +++ b/src/GitHunter.Application.Shared/Git/IGitManager.cs @@ -0,0 +1,6 @@ +namespace GitHunter.Application.Git; + +public interface IGitManager +{ + +} \ No newline at end of file diff --git a/src/GitHunter.Core/Git/GitManager.cs b/src/GitHunter.Application/Git/GitManager.cs similarity index 85% rename from src/GitHunter.Core/Git/GitManager.cs rename to src/GitHunter.Application/Git/GitManager.cs index 3197562..ba43ef9 100644 --- a/src/GitHunter.Core/Git/GitManager.cs +++ b/src/GitHunter.Application/Git/GitManager.cs @@ -1,7 +1,7 @@ using GitHunter.Core.DependencyProcesses; using Volo.Abp.DependencyInjection; -namespace GitHunter.Core.Git; +namespace GitHunter.Application.Git; [ProcessDependency] public class GitManager : IGitManager, IDependencyProcess, ITransientDependency diff --git a/src/GitHunter.Core/Git/GitProcessDependency.cs b/src/GitHunter.Application/Git/GitProcessDependency.cs similarity index 66% rename from src/GitHunter.Core/Git/GitProcessDependency.cs rename to src/GitHunter.Application/Git/GitProcessDependency.cs index f27730f..0cc3fbe 100644 --- a/src/GitHunter.Core/Git/GitProcessDependency.cs +++ b/src/GitHunter.Application/Git/GitProcessDependency.cs @@ -1,8 +1,8 @@ using GitHunter.Core.DependencyProcesses; -namespace GitHunter.Core.Git; +namespace GitHunter.Application.Git; -public class GitProcessDependency : IProcessDependency +public class GitProcessDependency : IGitProcessDependency { public bool Check() { @@ -10,4 +10,8 @@ public bool Check() var paths = environmentVariable?.Split(';'); return paths != null && paths.Any(path => File.Exists(Path.Combine(path, "git.exe"))); } +} + +public interface IGitProcessDependency : IProcessDependency +{ } \ No newline at end of file diff --git a/src/GitHunter.Application/GitHunter.Application.csproj b/src/GitHunter.Application/GitHunter.Application.csproj index a3db0c8..294c274 100644 --- a/src/GitHunter.Application/GitHunter.Application.csproj +++ b/src/GitHunter.Application/GitHunter.Application.csproj @@ -12,4 +12,8 @@ + + + + diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs b/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs index 6f9a806..8c1a86c 100644 --- a/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs +++ b/src/GitHunter.Core.Shared/DependencyProcesses/ProcessDependencyAttribute.cs @@ -1,5 +1,11 @@ namespace GitHunter.Core.DependencyProcesses; + +public abstract class ProcessDependencyAttribute : Attribute +{ + public Type DependencyProcess { get; protected set; } +} + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class ProcessDependencyAttribute : ProcessDependencyAttribute where T : IProcessDependency { diff --git a/src/GitHunter.Core.Shared/DependencyProcesses/a.cs b/src/GitHunter.Core.Shared/DependencyProcesses/a.cs deleted file mode 100644 index d0c8ed4..0000000 --- a/src/GitHunter.Core.Shared/DependencyProcesses/a.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GitHunter.Core.DependencyProcesses; - -public abstract class ProcessDependencyAttribute : Attribute -{ - public Type DependencyProcess { get; protected set; } -} \ No newline at end of file diff --git a/src/GitHunter.Core.Shared/Git/IGitManager.cs b/src/GitHunter.Core.Shared/Git/IGitManager.cs deleted file mode 100644 index 3a3ab20..0000000 --- a/src/GitHunter.Core.Shared/Git/IGitManager.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace GitHunter.Core.Git; - -public interface IGitManager -{ - -} \ No newline at end of file diff --git a/src/GitHunter.Core/CoreModule.cs b/src/GitHunter.Core/CoreModule.cs index 20db9cc..3a5658c 100644 --- a/src/GitHunter.Core/CoreModule.cs +++ b/src/GitHunter.Core/CoreModule.cs @@ -32,7 +32,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex using (var scope = context.ServiceProvider.CreateScope()) { - var processDependencyChecker = scope.ServiceProvider.GetRequiredService(); + var processDependencyChecker = scope.ServiceProvider.GetRequiredService(); var processDependencyOptions = scope.ServiceProvider.GetRequiredService>().Value; var modules = ModuleHelper.FindGitHunterModuleTypes(processDependencyOptions.StartupModule); From 71c617998eb5bd1077980224df419da1a5681391 Mon Sep 17 00:00:00 2001 From: Salih Date: Sat, 10 Dec 2022 18:18:16 +0300 Subject: [PATCH 3/3] Refactoring --- .../Git/IGitManager.cs | 3 +- src/GitHunter.Application/Git/GitManager.cs | 72 +++++++++++++++++++ src/GitHunter.Core/CoreModule.cs | 22 ++---- .../ProcessDependencyOptions.cs | 2 + src/GitHunter.Core/Tasks/TaskManager.cs | 19 +++++ .../DesktopWindowsFormModule.cs | 9 ++- src/GitHunter.Desktop.WindowsForm/MainForm.cs | 5 +- 7 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 src/GitHunter.Core/Tasks/TaskManager.cs diff --git a/src/GitHunter.Application.Shared/Git/IGitManager.cs b/src/GitHunter.Application.Shared/Git/IGitManager.cs index fb0e654..814e2cd 100644 --- a/src/GitHunter.Application.Shared/Git/IGitManager.cs +++ b/src/GitHunter.Application.Shared/Git/IGitManager.cs @@ -2,5 +2,6 @@ public interface IGitManager { - + void Initialize(string username, string password); + void Initialize(string token); } \ No newline at end of file diff --git a/src/GitHunter.Application/Git/GitManager.cs b/src/GitHunter.Application/Git/GitManager.cs index ba43ef9..f5e411f 100644 --- a/src/GitHunter.Application/Git/GitManager.cs +++ b/src/GitHunter.Application/Git/GitManager.cs @@ -1,4 +1,6 @@ using GitHunter.Core.DependencyProcesses; +using Microsoft.Extensions.Logging; +using Octokit; using Volo.Abp.DependencyInjection; namespace GitHunter.Application.Git; @@ -6,5 +8,75 @@ namespace GitHunter.Application.Git; [ProcessDependency] public class GitManager : IGitManager, IDependencyProcess, ITransientDependency { + private ILogger _logger; + private static readonly GitHubClient Client = new(new ProductHeaderValue("GitHunter")); + + public GitManager(ILogger logger) + { + _logger = logger; + } + + private static void Initialize(Credentials? credentials = null) + { + if (credentials != null) + { + Client.Credentials = credentials; + } + } + + private async Task GetRepositories(GitInput input) + { + var request = new SearchRepositoriesRequest() + { + Language = input.Language, + Topic = input.Topic, + SortField = RepoSearchSort.Stars, + Page = input.Page, + PerPage = input.PerPage, + }; + var result = await Client.Search.SearchRepo(request); + return new GitOutput(result.Items, result.TotalCount, result.IncompleteResults); + } + + public void Initialize(string username, string password) + { + Initialize(new Credentials(username, password)); + } + + public void Initialize(string token) + { + Initialize(new Credentials(token)); + } +} + +public class GitInput +{ + public Language? Language { get; set; } + public string? Location { get; set; } + public string? User { get; set; } + public string? Repository { get; set; } + public string? Topic { get; set; } + public string? Keyword { get; set; } + public string? License { get; set; } + public string? Sort { get; set; } + public string? Order { get; set; } + public int Page { get; set; } + public int PerPage { get; set; } +} + +public class GitOutput +{ + public GitOutput(IReadOnlyList resultItems, int resultTotalCount, bool resultIncompleteResults) + { + Repositories = resultItems; + TotalCount = resultTotalCount; + IncompleteResults = resultIncompleteResults; + } + + public bool IncompleteResults { get; } + + public int TotalCount { get; } + + public IReadOnlyList Repositories { get; } } \ No newline at end of file diff --git a/src/GitHunter.Core/CoreModule.cs b/src/GitHunter.Core/CoreModule.cs index 3a5658c..1d6b2c6 100644 --- a/src/GitHunter.Core/CoreModule.cs +++ b/src/GitHunter.Core/CoreModule.cs @@ -1,7 +1,6 @@ using GitHunter.Core.DependencyProcesses; using GitHunter.Core.Modules; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Volo.Abp; using Volo.Abp.Modularity; @@ -11,22 +10,6 @@ namespace GitHunter.Core; [DependsOn(typeof(CoreSharedModule))] public class CoreModule : GitHunterModule { - public override void PreConfigureServices(ServiceConfigurationContext context) - { - // var services = context.Services; - // // var processDependencyChecker = services.GetRequiredService(); - // var processDependencyOptions = services.GetRequiredService>().Value; - // - // var modules = services.GetSingletonInstance() - // .LoadModules( - // services, - // processDependencyOptions.StartupModule, - // new AbpApplicationCreationOptions(context.Services).PlugInSources - // ); - - - } - public override void OnApplicationInitialization(ApplicationInitializationContext context) { @@ -38,7 +21,10 @@ public override void OnApplicationInitialization(ApplicationInitializationContex foreach (var assembly in modules.Select(m=>m.Assembly)) { - processDependencyChecker?.CheckDependency(assembly); + if (!processDependencyChecker.CheckDependency(assembly)) + { + processDependencyOptions.ErrorAction?.Invoke(); + } } } diff --git a/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs b/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs index c33643d..105882d 100644 --- a/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs +++ b/src/GitHunter.Core/DependencyProcesses/ProcessDependencyOptions.cs @@ -3,4 +3,6 @@ public class ProcessDependencyOptions { public Type StartupModule { get; set; } + + public Action ErrorAction { get; set; } } \ No newline at end of file diff --git a/src/GitHunter.Core/Tasks/TaskManager.cs b/src/GitHunter.Core/Tasks/TaskManager.cs new file mode 100644 index 0000000..792b7b2 --- /dev/null +++ b/src/GitHunter.Core/Tasks/TaskManager.cs @@ -0,0 +1,19 @@ +namespace GitHunter.Core.Tasks; + +public class TaskManager +{ + private readonly Dictionary> _tasks = new(); + + public Task RunTask(List tasks, Func? exceptionHandler = null) + { + _tasks.Add(Guid.NewGuid(), tasks); + return Task.WhenAll(tasks).ContinueWith(t => + { + if (!t.IsFaulted) return; + if (exceptionHandler == null || !t.Exception!.InnerExceptions.Any(exceptionHandler)) + { + throw t.Exception!; + } + }); + } +} \ No newline at end of file diff --git a/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs b/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs index d5c2d21..844ced6 100644 --- a/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs +++ b/src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs @@ -12,7 +12,14 @@ public class DesktopWindowsFormModule : GitHunterModule public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(o=>o.StartupModule = typeof(DesktopWindowsFormModule)); + Configure(o=> + { + o.StartupModule = typeof(DesktopWindowsFormModule); + o.ErrorAction = () => + { + MessageBox.Show("Error"); + }; + }); base.ConfigureServices(context); } diff --git a/src/GitHunter.Desktop.WindowsForm/MainForm.cs b/src/GitHunter.Desktop.WindowsForm/MainForm.cs index 618aa67..7c40583 100644 --- a/src/GitHunter.Desktop.WindowsForm/MainForm.cs +++ b/src/GitHunter.Desktop.WindowsForm/MainForm.cs @@ -1,11 +1,14 @@ +using GitHunter.Application.Git; using Volo.Abp.DependencyInjection; namespace GitHunter.Desktop; public partial class MainForm : Form, ISingletonDependency { - public MainForm() + private readonly IGitManager _githubManager; + public MainForm(IGitManager githubManager) { + _githubManager = githubManager; InitializeComponent(); } } \ No newline at end of file