mycroforge/MycroForge.CLI/Commands/MycroForge.Script.Edit.cs

55 lines
1.7 KiB
C#

using System.CommandLine;
using System.Diagnostics;
using MycroForge.CLI.Commands.Interfaces;
namespace MycroForge.CLI.Commands;
public partial class MycroForge
{
public partial class Script
{
public class Edit : Command, ISubCommandOf<Script>
{
private static readonly Argument<string> NameArgument =
new(name: "name", description: "The name of the script");
public Edit() : base(name: "edit", description: "Edit a script")
{
AddArgument(NameArgument);
this.SetHandler(ExecuteAsync, NameArgument);
}
private async Task ExecuteAsync(string name)
{
await EditFile(name);
}
private static async Task EditFile(string name)
{
var folder = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".m4g"
);
var file = Path.Join(folder, $"{name}.py");
if (!File.Exists(file))
throw new Exception($"File {file} does not exists.");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "code",
Arguments = $"--wait {file}",
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
}
};
process.Start();
await process.WaitForExitAsync();
}
}
}
}