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

44 lines
1.3 KiB
C#

using System.CommandLine;
using MycroForge.CLI.Commands.Attributes;
using MycroForge.Core;
using MycroForge.Core.Contract;
namespace MycroForge.CLI.Commands;
public partial class MycroForge
{
[RequiresVenv]
public class Install : Command, ISubCommandOf<MycroForge>
{
private static readonly Argument<IEnumerable<string>> PackagesArgument =
new(name: "packages", description: "The names of the packages to install");
private readonly ProjectContext _context;
public Install(ProjectContext context) :
base("install", "Install packages and update the requirements.txt")
{
_context = context;
AddAlias("i");
AddArgument(PackagesArgument);
this.SetHandler(ExecuteAsync, PackagesArgument);
}
private async Task ExecuteAsync(IEnumerable<string> packages)
{
var packs = packages.ToArray();
if (packs.Length == 0)
{
Console.WriteLine("'m4g install' requires at least one package.");
return;
}
await _context.Bash(
"source .venv/bin/activate",
$"pip install {string.Join(' ', packs)}",
"pip freeze > requirements.txt"
);
}
}
}