|
1 | 1 | using Hypr.Configuration; |
2 | 2 | using Hypr.Hooks; |
3 | 3 | using Hypr.Services; |
| 4 | +using Hypr.Services.Terminals; |
| 5 | +using Hypr.Utils; |
4 | 6 | using Microsoft.Extensions.Configuration; |
5 | 7 | using Microsoft.Extensions.DependencyInjection; |
6 | 8 |
|
7 | 9 | namespace Hypr; |
8 | 10 |
|
9 | 11 | internal static class Module |
10 | 12 | { |
11 | | - internal static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration) => services |
12 | | - .Configure<TerminalConfig>(configuration.GetSection("terminal")) |
13 | | - .Configure<WorktreeConfig>(configuration.GetSection("worktree")) |
14 | | - .Configure<CleanupConfig>(configuration.GetSection("cleanup")) |
15 | | - .Configure<ScriptsConfig>(configuration.GetSection("scripts")) |
16 | | - .Configure<ConfirmationsConfig>(configuration.GetSection("confirmations")) |
17 | | - .AddSingleton<StateService>() |
18 | | - .AddSingleton<GitService>() |
19 | | - .AddSingleton<GitHubService>() |
20 | | - .AddSingleton<TerminalService>() |
21 | | - .AddSingleton<VersionCheckService>() |
22 | | - .AddSingleton<HookRunner>(); |
| 13 | + internal static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration) => services |
| 14 | + .Configure<TerminalConfig>(configuration.GetSection("terminal")) |
| 15 | + .Configure<WorktreeConfig>(configuration.GetSection("worktree")) |
| 16 | + .Configure<CleanupConfig>(configuration.GetSection("cleanup")) |
| 17 | + .Configure<ScriptsConfig>(configuration.GetSection("scripts")) |
| 18 | + .Configure<ConfirmationsConfig>(configuration.GetSection("confirmations")) |
| 19 | + .AddSingleton<StateService>() |
| 20 | + .AddSingleton<GitService>() |
| 21 | + .AddSingleton<GitHubService>() |
| 22 | + .AddSingleton<TerminalService>() |
| 23 | + .AddSingleton<VersionCheckService>() |
| 24 | + .AddSingleton<HookRunner>() |
| 25 | + .AddTerminalProviders(); |
| 26 | + |
| 27 | + private static IServiceCollection AddTerminalProviders(this IServiceCollection services) |
| 28 | + { |
| 29 | + var currentPlatform = GetCurrentPlatform(); |
| 30 | + |
| 31 | + // Use Scrutor to scan and register all terminal providers that support the current platform |
| 32 | + services.Scan(scan => scan |
| 33 | + .FromAssemblyOf<ITerminalProvider>() |
| 34 | + .AddClasses(classes => classes |
| 35 | + .AssignableTo<ITerminalProvider>() |
| 36 | + .Where(type => SupportsCurrentPlatform(type, currentPlatform))) |
| 37 | + .AsImplementedInterfaces() |
| 38 | + .WithSingletonLifetime()); |
| 39 | + |
| 40 | + return services; |
| 41 | + } |
| 42 | + |
| 43 | + private static Platform GetCurrentPlatform() |
| 44 | + { |
| 45 | + if (PlatformUtils.IsWindows) return Platform.Windows; |
| 46 | + if (PlatformUtils.IsMacOS) return Platform.MacOS; |
| 47 | + if (PlatformUtils.IsLinux) return Platform.Linux; |
| 48 | + return Platform.None; |
| 49 | + } |
| 50 | + |
| 51 | + private static bool SupportsCurrentPlatform(Type providerType, Platform currentPlatform) |
| 52 | + { |
| 53 | + return GetSupportedPlatformsForType(providerType).HasFlag(currentPlatform); |
| 54 | + } |
| 55 | + |
| 56 | + private static Platform GetSupportedPlatformsForType(Type providerType) |
| 57 | + { |
| 58 | + // Map known provider types to their supported platforms |
| 59 | + return providerType.Name switch |
| 60 | + { |
| 61 | + nameof(WindowsTerminalProvider) => Platform.Windows, |
| 62 | + nameof(ITerm2Provider) => Platform.MacOS, |
| 63 | + nameof(TerminalAppProvider) => Platform.MacOS, |
| 64 | + nameof(GnomeTerminalProvider) => Platform.Linux, |
| 65 | + nameof(TmuxProvider) => Platform.Linux | Platform.MacOS, |
| 66 | + nameof(VSCodeProvider) => Platform.All, |
| 67 | + nameof(CursorProvider) => Platform.All, |
| 68 | + nameof(EchoProvider) => Platform.All, |
| 69 | + nameof(InplaceProvider) => Platform.All, |
| 70 | + _ => Platform.None |
| 71 | + }; |
| 72 | + } |
23 | 73 | } |
0 commit comments