Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- os: ubuntu-latest
rid: linux-x64
name: linux-x64
- os: ubuntu-latest
rid: linux-arm64
name: linux-arm64
- os: windows-latest
rid: win-x64
name: windows-x64
Expand Down
120 changes: 66 additions & 54 deletions src/Module.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.CommandLine;
using Hypr.Commands;
using Hypr.Configuration;
using Hypr.Hooks;
using Hypr.Services;
Expand All @@ -10,64 +12,74 @@ namespace Hypr;

internal static class Module
{
internal static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration) => services
.Configure<TerminalConfig>(configuration.GetSection("terminal"))
.Configure<WorktreeConfig>(configuration.GetSection("worktree"))
.Configure<CleanupConfig>(configuration.GetSection("cleanup"))
.Configure<ScriptsConfig>(configuration.GetSection("scripts"))
.Configure<ConfirmationsConfig>(configuration.GetSection("confirmations"))
.AddSingleton<StateService>()
.AddSingleton<GitService>()
.AddSingleton<GitHubService>()
.AddSingleton<TerminalService>()
.AddSingleton<VersionCheckService>()
.AddSingleton<HookRunner>()
.AddTerminalProviders();
internal static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration) => services
// Configuration sections
.Configure<TerminalConfig>(configuration.GetSection("terminal"))
.Configure<WorktreeConfig>(configuration.GetSection("worktree"))
.Configure<CleanupConfig>(configuration.GetSection("cleanup"))
.Configure<ScriptsConfig>(configuration.GetSection("scripts"))
.Configure<ConfirmationsConfig>(configuration.GetSection("confirmations"))
// Services
.AddSingleton<StateService>()
.AddSingleton<GitService>()
.AddSingleton<GitHubService>()
.AddSingleton<TerminalService>()
.AddSingleton<VersionCheckService>()
.AddSingleton<HookRunner>()
// Commands and terminal providers (explicit registration for commands, Scrutor for terminal providers)
.AddCommands()
.AddTerminalProviders();

private static IServiceCollection AddTerminalProviders(this IServiceCollection services)
{
var currentPlatform = GetCurrentPlatform();
internal static IServiceCollection AddCommands(this IServiceCollection services) => services
.AddSingleton<Command, ListCommand>()
.AddSingleton<Command, SwitchCommand>()
.AddSingleton<Command, ConfigCommand>()
.AddSingleton<Command, CleanupCommand>();

// Use Scrutor to scan and register all terminal providers that support the current platform
services.Scan(scan => scan
.FromAssemblyOf<ITerminalProvider>()
.AddClasses(classes => classes
.AssignableTo<ITerminalProvider>()
.Where(type => SupportsCurrentPlatform(type, currentPlatform)))
.AsImplementedInterfaces()
.WithSingletonLifetime());
private static IServiceCollection AddTerminalProviders(this IServiceCollection services)
{
var currentPlatform = GetCurrentPlatform();

return services;
}
// Use Scrutor to scan and register all terminal providers that support current platform
services.Scan(scan => scan
.FromAssemblyOf<ITerminalProvider>()
.AddClasses(classes => classes
.AssignableTo<ITerminalProvider>()
.Where(type => SupportsCurrentPlatform(type, currentPlatform)))
.AsImplementedInterfaces()
.WithSingletonLifetime());

private static Platform GetCurrentPlatform()
{
if (PlatformUtils.IsWindows) return Platform.Windows;
if (PlatformUtils.IsMacOS) return Platform.MacOS;
if (PlatformUtils.IsLinux) return Platform.Linux;
return Platform.None;
}
return services;
}

private static bool SupportsCurrentPlatform(Type providerType, Platform currentPlatform)
{
return GetSupportedPlatformsForType(providerType).HasFlag(currentPlatform);
}
private static Platform GetCurrentPlatform()
{
if (PlatformUtils.IsWindows) return Platform.Windows;
if (PlatformUtils.IsMacOS) return Platform.MacOS;
if (PlatformUtils.IsLinux) return Platform.Linux;
return Platform.None;
}

private static bool SupportsCurrentPlatform(Type providerType, Platform currentPlatform)
{
return GetSupportedPlatformsForType(providerType).HasFlag(currentPlatform);
}

private static Platform GetSupportedPlatformsForType(Type providerType)
private static Platform GetSupportedPlatformsForType(Type providerType)
{
// Map known provider types to their supported platforms
return providerType.Name switch
{
// Map known provider types to their supported platforms
return providerType.Name switch
{
nameof(WindowsTerminalProvider) => Platform.Windows,
nameof(ITerm2Provider) => Platform.MacOS,
nameof(TerminalAppProvider) => Platform.MacOS,
nameof(GnomeTerminalProvider) => Platform.Linux,
nameof(TmuxProvider) => Platform.Linux | Platform.MacOS,
nameof(VSCodeProvider) => Platform.All,
nameof(CursorProvider) => Platform.All,
nameof(EchoProvider) => Platform.All,
nameof(InplaceProvider) => Platform.All,
_ => Platform.None
};
}
}
nameof(WindowsTerminalProvider) => Platform.Windows,
nameof(ITerm2Provider) => Platform.MacOS,
nameof(TerminalAppProvider) => Platform.MacOS,
nameof(GnomeTerminalProvider) => Platform.Linux,
nameof(TmuxProvider) => Platform.Linux | Platform.MacOS,
nameof(VSCodeProvider) => Platform.All,
nameof(CursorProvider) => Platform.All,
nameof(EchoProvider) => Platform.All,
nameof(InplaceProvider) => Platform.All,
_ => Platform.None
};
}
}
8 changes: 1 addition & 7 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.CommandLine;
using Hypr.Commands;
using Hypr.Configuration;
using Hypr.Logging;
using Microsoft.Extensions.Configuration;
Expand All @@ -26,14 +25,9 @@
// Register configuration
builder.Services.AddSingleton<IConfiguration>(configuration);

// Register services and configuration sections
// Register services, configuration sections, and commands
builder.Services.AddServices(builder.Configuration);

// Automatically discover and register all commands
builder.Services.Scan(s => s.FromAssemblyOf<ListCommand>()
.AddClasses(c => c.AssignableTo<Command>())
.As<Command>());

var host = builder.Build();
var rootCommand = new RootCommand("hypr - Git worktree manager");
rootCommand.Options.Add(new DebugOption());
Expand Down
10 changes: 6 additions & 4 deletions src/hypr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>link</TrimMode>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<!-- Embed native libraries (LibGit2Sharp) into single-file and auto-extract at runtime -->
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Scrutor" Version="6.1.0" />
<PackageReference Include="Scrutor" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
Expand All @@ -50,7 +52,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.0" />
</ItemGroup>

<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="README.md" />
</ItemGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="README.md" />
</ItemGroup>
</Project>
Loading