65 lines
2.0 KiB
C#
65 lines
2.0 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 Create : Command, ISubCommandOf<Script>
|
|
{
|
|
private static readonly Argument<string> NameArgument =
|
|
new(name: "name", description: "The name of the script");
|
|
|
|
public Create() : base(name: "create", description: "Create a script")
|
|
{
|
|
AddArgument(NameArgument);
|
|
this.SetHandler(ExecuteAsync, NameArgument);
|
|
}
|
|
|
|
private async Task ExecuteAsync(string name)
|
|
{
|
|
var path = await CreateFile(name);
|
|
await OpenFile(path);
|
|
}
|
|
|
|
private static async Task<string> CreateFile(string? name = null, string fileExtension = "py")
|
|
{
|
|
var folder = Path.Join(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".m4g"
|
|
);
|
|
|
|
Directory.CreateDirectory(folder);
|
|
|
|
var filePath = Path.Join(folder, $"{name}.{fileExtension}");
|
|
|
|
if (File.Exists(filePath))
|
|
throw new Exception($"File {filePath} already exists.");
|
|
|
|
await File.WriteAllTextAsync(filePath, string.Empty);
|
|
|
|
return filePath;
|
|
}
|
|
|
|
private static async Task OpenFile(string file)
|
|
{
|
|
var process = new Process
|
|
{
|
|
StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "code",
|
|
Arguments = $"--wait {file}",
|
|
WindowStyle = ProcessWindowStyle.Hidden,
|
|
UseShellExecute = true,
|
|
}
|
|
};
|
|
|
|
process.Start();
|
|
|
|
await process.WaitForExitAsync();
|
|
}
|
|
}
|
|
}
|
|
} |