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

Salihozkara/core layer #3

Merged
merged 3 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 3 deletions src/GitHunter.Application.Shared/ApplicationSharedModule.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>GitHunter.Application</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/GitHunter.Application/ApplicationModule.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +8,6 @@ namespace GitHunter.Application;
typeof(ApplicationSharedModule),
typeof(CoreModule)
)]
public class ApplicationModule : AbpModule
public class ApplicationModule : GitHunterModule
{
}
1 change: 1 addition & 0 deletions src/GitHunter.Application/GitHunter.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>GitHunter.Application</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
7 changes: 4 additions & 3 deletions src/GitHunter.Core.Shared/CoreSharedModule.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace GitHunter.Core.DependencyProcesses;

public interface IDependencyProcess
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GitHunter.Core.DependencyProcesses;

public interface IProcessDependency
{
bool Check();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;

namespace GitHunter.Core.DependencyProcesses;

public interface IProcessDependencyChecker
{
bool CheckDependency(Assembly assembly);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace GitHunter.Core.DependencyProcesses;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ProcessDependencyAttribute<T> : ProcessDependencyAttribute where T : IProcessDependency
{
public ProcessDependencyAttribute()
{
DependencyProcess = typeof(T);
}
}
6 changes: 6 additions & 0 deletions src/GitHunter.Core.Shared/DependencyProcesses/a.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GitHunter.Core.DependencyProcesses;

public abstract class ProcessDependencyAttribute : Attribute
{
public Type DependencyProcess { get; protected set; }
}
6 changes: 6 additions & 0 deletions src/GitHunter.Core.Shared/Git/IGitManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GitHunter.Core.Git;

public interface IGitManager
{

}
1 change: 1 addition & 0 deletions src/GitHunter.Core.Shared/GitHunter.Core.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>GitHunter.Core</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
18 changes: 18 additions & 0 deletions src/GitHunter.Core.Shared/Modules/GitHunterModule.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions src/GitHunter.Core.Shared/Processes/IProcessManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace GitHunter.Core.Processes;

public interface IProcessManager
{
Task<ProcessResult> RunAsync(string command, string arguments, string? workingDirectory = null);

Task<ProcessResult> RunAsync(ProcessStartInfo processStartInfo);

Task<bool> KillAllProcessesAsync();
}
8 changes: 8 additions & 0 deletions src/GitHunter.Core.Shared/Processes/ProcessResult.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
22 changes: 22 additions & 0 deletions src/GitHunter.Core.Shared/Processes/ProcessStartInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace GitHunter.Core.Processes;

public class ProcessStartInfo
{
public ProcessStartInfo(string command, string arguments, string? workingDirectory = null, Action? exited = null,
Action<string>? outputDataReceived = null, Action<string>? 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<string>? OutputDataReceived { get; private set; }
public Action<string>? ErrorDataReceived { get; private set; }
public Action? Exited { get; private set; }
}
42 changes: 40 additions & 2 deletions src/GitHunter.Core/CoreModule.cs
Original file line number Diff line number Diff line change
@@ -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<ProcessDependencyChecker>();
// var processDependencyOptions = services.GetRequiredService<IOptions<ProcessDependencyOptions>>().Value;
//
// var modules = services.GetSingletonInstance<IModuleLoader>()
// .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<ProcessDependencyChecker>();
var processDependencyOptions = scope.ServiceProvider.GetRequiredService<IOptions<ProcessDependencyOptions>>().Value;
var modules = ModuleHelper.FindGitHunterModuleTypes(processDependencyOptions.StartupModule);

foreach (var assembly in modules.Select(m=>m.Assembly))
{
processDependencyChecker?.CheckDependency(assembly);
}
}

base.OnApplicationInitialization(context);
}
}
24 changes: 24 additions & 0 deletions src/GitHunter.Core/DependencyProcesses/ProcessDependencyChecker.cs
Original file line number Diff line number Diff line change
@@ -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<ProcessDependencyAttribute>().Select(t=>(IProcessDependency)ActivatorUtilities.CreateInstance(_serviceProvider, t.DependencyProcess)).ToList();

return dependencies.All(d => d.Check());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace GitHunter.Core.DependencyProcesses;

public class ProcessDependencyOptions
{
public Type StartupModule { get; set; }
}
10 changes: 10 additions & 0 deletions src/GitHunter.Core/Git/GitManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using GitHunter.Core.DependencyProcesses;
using Volo.Abp.DependencyInjection;

namespace GitHunter.Core.Git;

[ProcessDependency<GitProcessDependency>]
public class GitManager : IGitManager, IDependencyProcess, ITransientDependency
{

}
13 changes: 13 additions & 0 deletions src/GitHunter.Core/Git/GitProcessDependency.cs
Original file line number Diff line number Diff line change
@@ -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")));
}
}
1 change: 1 addition & 0 deletions src/GitHunter.Core/GitHunter.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>GitHunter.Core</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
58 changes: 58 additions & 0 deletions src/GitHunter.Core/Modules/ModuleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Reflection;
using Volo.Abp.Modularity;

namespace GitHunter.Core.Modules;

internal static class ModuleHelper
{
public static IEnumerable<Type> FindGitHunterModuleTypes(Type startupModuleType)
{
if(!GitHunterModule.IsGitHunterModule(startupModuleType))
{
throw new ArgumentException("Given type is not an GitHunter module: " + startupModuleType.AssemblyQualifiedName);
}
var moduleTypes = new List<Type>();
AddModuleAndDependenciesRecursively(moduleTypes, startupModuleType);
return moduleTypes;
}

private static List<Type> FindDependedModuleTypes(ICustomAttributeProvider moduleType)
{
var dependencies = new List<Type>();

var dependencyDescriptors = moduleType
.GetCustomAttributes(false)
.OfType<IDependedTypesProvider>();

foreach (var descriptor in dependencyDescriptors)
{
foreach (var dependedModuleType in descriptor.GetDependedTypes())
{
dependencies.AddIfNotContains(dependedModuleType);
}
}

return dependencies;
}

private static void AddModuleAndDependenciesRecursively(
ICollection<Type> 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);
}
}
}
Loading