mycroforge/MycroForge.CLI/Commands/MycroForge.Plugin.Install.cs

70 lines
2.4 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();
await _context.Bash($"dotnet publish -c Release -r {platform} --output {pluginInstallPath}");
Console.WriteLine($"Successfully installed plugin {assemblyName}");
}
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);
}
}
}
}