mycroforge/MycroForge.CLI/Commands/MycroForge.Plugin.Install.cs
mdnapo 457429f7ec
All checks were successful
Build and publish MycroForge.CLI / test (push) Has been skipped
Test MycroForge.CLI / test (push) Successful in 5m39s
Improved plugin feature
2024-10-13 13:39:56 +02:00

74 lines
2.6 KiB
C#

using System.CommandLine;
using Humanizer;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using MycroForge.CLI.Commands.Attributes;
using MycroForge.Core;
using MycroForge.Core.Contract;
namespace MycroForge.CLI.Commands;
public partial class MycroForge
{
public partial class Plugin
{
[RequiresPlugin]
public class Install : Command, ISubCommandOf<Plugin>
{
public enum TargetPlatform
{
linux_arm,
linux_arm64,
linux_x64,
osx_arm64,
osx_x64,
}
private static readonly Option<TargetPlatform> PlatformOption = new(
aliases: ["-p", "--platform"],
description: "The platform to target when building the plugin"
) { IsRequired = true };
private readonly ProjectContext _context;
public Install(ProjectContext context) : base("install", "Install a plugin")
{
_context = context;
AddAlias("i");
AddOption(PlatformOption);
this.SetHandler(ExecuteAsync, PlatformOption);
}
private async Task ExecuteAsync(TargetPlatform target)
{
var assemblyName = GetAssemblyName();
var pluginInstallPath = Path.Join(Plugins.RootDirectory, assemblyName);
var platform = target.ToString().Dasherize();
var exitCode = await _context.Bash(
$"dotnet publish -c Release -r {platform} --output {pluginInstallPath}"
);
Console.WriteLine(exitCode == 0
? $"Successfully installed plugin {assemblyName}"
: $"Could not install {assemblyName}, process exited with code {exitCode}.");
}
private string GetAssemblyName()
{
var matcher = new Matcher().AddInclude("*.csproj");
var currentDirectory = Environment.CurrentDirectory;
var result = matcher.Execute(
new DirectoryInfoWrapper(
new DirectoryInfo(currentDirectory)
)
);
if (!result.HasMatches)
throw new Exception($"Could not find .csproj file in directory {currentDirectory}");
return Path.GetFileNameWithoutExtension(result.Files.First().Path);
}
}
}
}