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

Dependency Process Refactoring #13

Merged
merged 1 commit into from
Dec 18, 2022
Merged
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
7 changes: 6 additions & 1 deletion src/GitHunter.Application/Git/GitProcessDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ 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")));
return paths != null && paths.Any(path => File.Exists(Path.Combine(path, ProcessName)));
}

public string ProcessName => "git.exe";
public string ErrorMessage => "Git is not installed. Please download and install Git from " + DownloadUrl;
public string ErrorTitle => "Git not found";
public string DownloadUrl => "https://git-scm.com/downloads";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public bool Check()
{
return File.Exists(Resource.SourceMonitor.SourceMonitorExe.Path);
}

public string ProcessName => "SourceMonitor";
public string ErrorMessage => $"SourceMonitor.exe not found. Please download it from {DownloadUrl} and place it in the same folder as GitHunter Resources.";
public string ErrorTitle => "SourceMonitor.exe not found";
public string DownloadUrl => "https://www.campwoodsw.com/sourcemonitor.html";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
public interface IProcessDependency
{
bool Check();

string ProcessName { get; }

string ErrorMessage { get; }

string ErrorTitle { get; }

string? DownloadUrl { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace GitHunter.Core.DependencyProcesses;

public interface IProcessDependencyChecker
{
bool CheckDependency(Assembly assembly);
bool CheckDependency(Assembly assembly, out IProcessDependency? dependency);
}
4 changes: 2 additions & 2 deletions src/GitHunter.Core/CoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
var modules = ModuleHelper.FindGitHunterModuleTypes(processDependencyOptions.StartupModule);

foreach (var assembly in modules.Select(m => m.Assembly))
if (!processDependencyChecker.CheckDependency(assembly))
processDependencyOptions.ErrorAction?.Invoke();
if (!processDependencyChecker.CheckDependency(assembly, out var dependency))
processDependencyOptions.ErrorAction?.Invoke(dependency);
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/GitHunter.Core/DependencyProcesses/ProcessDependencyChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public ProcessDependencyChecker(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
}

public bool CheckDependency(Assembly assembly)
public bool CheckDependency(Assembly assembly, out IProcessDependency? processDependency)
{
processDependency = null;
var dependenciesTypes = assembly.GetTypes().Where(t =>
t.GetCustomAttributes(typeof(ProcessDependencyAttribute<>)) is { } attributes && attributes.Any());
var dependenciesAttributes = dependenciesTypes
Expand All @@ -26,6 +27,15 @@ public bool CheckDependency(Assembly assembly)
return null;
});

return dependencies.All(d => d != null && d.Check());
foreach (var dependency in dependencies)
{
if (dependency == null)
continue;
if (dependency.Check()) continue;
processDependency = dependency;
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public class ProcessDependencyOptions
{
public Type? StartupModule { get; set; }

public Action? ErrorAction { get; set; }
public Action<IProcessDependency?>? ErrorAction { get; set; }
}
13 changes: 11 additions & 2 deletions src/GitHunter.Desktop.WindowsForm/DesktopWindowsFormModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitHunter.Application;
using System.Diagnostics;
using GitHunter.Application;
using GitHunter.Core.DependencyProcesses;
using GitHunter.Core.Modules;
using Volo.Abp.Modularity;
Expand All @@ -14,7 +15,15 @@ public override void ConfigureServices(ServiceConfigurationContext context)
Configure<ProcessDependencyOptions>(o =>
{
o.StartupModule = typeof(DesktopWindowsFormModule);
o.ErrorAction = () => { MessageBox.Show("Error"); };
o.ErrorAction = (pd) =>
{
var text = pd?.ErrorMessage + Environment.NewLine + "Do you want to download it now?";
if(MessageBox.Show(text, pd?.ErrorTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
{
if (pd?.DownloadUrl != null) Process.Start("explorer", pd.DownloadUrl);
}
Environment.Exit(0);
};
});

base.ConfigureServices(context);
Expand Down