Supplemented script command
This commit is contained in:
parent
6c50852959
commit
5d3d3173d9
65
MycroForge.CLI/Commands/MycroForge.Script.Create.cs
Normal file
65
MycroForge.CLI/Commands/MycroForge.Script.Create.cs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
55
MycroForge.CLI/Commands/MycroForge.Script.Edit.cs
Normal file
55
MycroForge.CLI/Commands/MycroForge.Script.Edit.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -43,7 +43,7 @@ public partial class MycroForge
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var scriptPath = Path.Combine(
|
var scriptPath = Path.Combine(
|
||||||
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".m4g", name
|
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".m4g", $"{name}.py"
|
||||||
);
|
);
|
||||||
engine.ExecuteFile(scriptPath, scope);
|
engine.ExecuteFile(scriptPath, scope);
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ public static class ServiceCollectionExtensions
|
|||||||
|
|
||||||
// Register "m4g script"
|
// Register "m4g script"
|
||||||
services.AddScoped<ISubCommandOf<Commands.MycroForge>, Commands.MycroForge.Script>();
|
services.AddScoped<ISubCommandOf<Commands.MycroForge>, Commands.MycroForge.Script>();
|
||||||
|
services.AddScoped<ISubCommandOf<Commands.MycroForge.Script>, Commands.MycroForge.Script.Create>();
|
||||||
|
services.AddScoped<ISubCommandOf<Commands.MycroForge.Script>, Commands.MycroForge.Script.Edit>();
|
||||||
services.AddScoped<ISubCommandOf<Commands.MycroForge.Script>, Commands.MycroForge.Script.Run>();
|
services.AddScoped<ISubCommandOf<Commands.MycroForge.Script>, Commands.MycroForge.Script.Run>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
|
Loading…
Reference in New Issue
Block a user