cachyos-kernel-manager/Program.cs
2025-06-26 18:52:37 +02:00

246 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
class Program
{
private class KernelMenuItem
{
public string Name { get; set; }
public bool IsSelected { get; set; }
public bool IsInitiallyInstalled { get; set; }
}
static void Main(string[] args)
{
Console.CursorVisible = false;
Console.Clear();
Console.WriteLine("CachyOS Kernel Manager");
Console.WriteLine("----------------------");
List<KernelMenuItem> menuItems = InitializeKernelMenuItems();
int selectedIndex = 0;
while (true)
{
DrawMenu(menuItems, selectedIndex);
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.UpArrow:
selectedIndex = (selectedIndex == 0) ? menuItems.Count - 1 : selectedIndex - 1;
break;
case ConsoleKey.DownArrow:
selectedIndex = (selectedIndex == menuItems.Count - 1) ? 0 : selectedIndex + 1;
break;
case ConsoleKey.Spacebar:
menuItems[selectedIndex].IsSelected = !menuItems[selectedIndex].IsSelected;
break;
case ConsoleKey.Enter:
Console.Clear();
Console.WriteLine("Processing kernel changes...");
foreach (var item in menuItems.Where(item => !item.IsInitiallyInstalled && item.IsSelected))
{
Console.WriteLine($"\nPreparing to install {item.Name}...");
if (InstallKernel(item.Name))
{
Console.WriteLine($"Kernel {item.Name} installed successfully.");
}
else
{
Console.WriteLine($"Failed to install {item.Name}. Please check the output above for errors.");
}
}
foreach (var item in menuItems.Where(item => item.IsInitiallyInstalled && !item.IsSelected))
{
Console.WriteLine($"\nPreparing to uninstall {item.Name}...");
if (UninstallKernel(item.Name))
{
Console.WriteLine($"Kernel {item.Name} uninstalled successfully.");
}
else
{
Console.WriteLine($"Failed to uninstall {item.Name}. Please check the output above for errors.");
}
}
Console.WriteLine("\nAll selected operations complete. Please reboot to apply changes. Press any key to exit.");
Console.ReadKey(true);
return;
case ConsoleKey.Q:
return;
}
}
}
static void DrawMenu(List<KernelMenuItem> menuItems, int selectedIndex)
{
Console.SetCursorPosition(0, 3);
for (int i = 0; i < menuItems.Count; i++)
{
Console.SetCursorPosition(0, 3 + i);
if (i == selectedIndex)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
}
Console.Write($"{(menuItems[i].IsSelected ? "[X]" : "[ ]")}" + " " + $"{menuItems[i].Name}");
Console.ResetColor();
Console.WriteLine(new string(' ', Console.WindowWidth - Console.CursorLeft));
}
Console.SetCursorPosition(0, 3 + menuItems.Count);
Console.WriteLine("\nUse \u2191\u2193 to navigate, SPACE to select/deselect, ENTER to apply, Q to quit.");
}
static List<KernelMenuItem> InitializeKernelMenuItems()
{
var installedKernels = GetInstalledKernels();
var availableCachyosKernels = new List<string>
{
"linux-cachyos-bmq",
"linux-cachyos-bore",
"linux-cachyos-deckify",
"linux-cachyos-eevdf",
"linux-cachyos-hardened",
"linux-cachyos-lts",
"linux-cachyos-rc",
"linux-cachyos-rt-bore",
"linux-cachyos-server",
"linux-cachyos"
};
var commonArchKernels = new List<string>
{
"linux",
"linux-lts",
"linux-zen",
"linux-hardened",
"linux-rt"
};
var allKernelNames = availableCachyosKernels
.Concat(commonArchKernels)
.Distinct()
.OrderBy(k => k)
.ToList();
var menuItems = new List<KernelMenuItem>();
foreach (var kernelName in allKernelNames)
{
bool isInstalled = installedKernels.Contains(kernelName);
menuItems.Add(new KernelMenuItem
{
Name = kernelName,
IsSelected = isInstalled,
IsInitiallyInstalled = isInstalled
});
}
return menuItems;
}
static HashSet<string> GetInstalledKernels()
{
var installed = new HashSet<string>();
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "pacman",
Arguments = "-Q",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
try
{
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
foreach (var line in output.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
var parts = line.Split(' ');
if (parts.Length > 0)
{
string packageName = parts[0];
if (packageName.StartsWith("linux") || packageName.StartsWith("cachyos-linux"))
{
installed.Add(packageName);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error getting installed kernels: {ex.Message}");
}
return installed;
}
static bool InstallKernel(string kernelName)
{
string headersPackageName = kernelName + "-headers";
Console.WriteLine($"Running: sudo pacman -S --noconfirm {kernelName} {headersPackageName}");
Console.WriteLine("You may be prompted for your password.");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "sudo",
Arguments = $"pacman -S --noconfirm {kernelName} {headersPackageName}",
UseShellExecute = true,
}
};
try
{
process.Start();
process.WaitForExit();
return process.ExitCode == 0;
}
catch (Exception ex)
{
Console.WriteLine($"Error installing kernel: {ex.Message}");
return false;
}
}
static bool UninstallKernel(string kernelName)
{
string headersPackageName = kernelName + "-headers";
Console.WriteLine($"Running: sudo pacman -Rns --noconfirm {kernelName} {headersPackageName}");
Console.WriteLine("You may be prompted for your password.");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "sudo",
Arguments = $"pacman -Rns --noconfirm {kernelName} {headersPackageName}",
UseShellExecute = true,
}
};
try
{
process.Start();
process.WaitForExit();
return process.ExitCode == 0;
}
catch (Exception ex)
{
Console.WriteLine($"Error uninstalling kernel: {ex.Message}");
return false;
}
}
}