2025-03-08 23:52:01 +03:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2025-08-01 19:08:02 +03:00
|
|
|
using System.Reflection;
|
2025-03-08 23:52:01 +03:00
|
|
|
using OneScript.Sources;
|
|
|
|
|
using OneScript.StandardLibrary;
|
2025-08-01 19:08:02 +03:00
|
|
|
using OneSwiss.V8.Platform;
|
2025-06-11 12:42:53 +03:00
|
|
|
using ScriptEngine;
|
2025-03-08 23:52:01 +03:00
|
|
|
using ScriptEngine.HostedScript;
|
|
|
|
|
using ScriptEngine.Hosting;
|
2025-08-01 19:08:02 +03:00
|
|
|
using ExecutionContext = ScriptEngine.Machine.ExecutionContext;
|
2025-03-08 23:52:01 +03:00
|
|
|
|
2025-07-02 10:32:56 +03:00
|
|
|
namespace OneSwiss.OneScript;
|
2025-03-08 23:52:01 +03:00
|
|
|
|
|
|
|
|
public class OneScriptExecutor : IHostApplication
|
|
|
|
|
{
|
2025-06-11 12:42:53 +03:00
|
|
|
public EventHandler<(string Message, MessageStatusEnum Status)>? OnEcho = null;
|
|
|
|
|
public EventHandler<Exception>? OnError = null;
|
2025-07-02 10:32:56 +03:00
|
|
|
private string[] _args;
|
2025-03-08 23:52:01 +03:00
|
|
|
|
2025-08-01 19:08:02 +03:00
|
|
|
public void ExecutePackageScript(string path, OpmMetadata metadata, string[] args, Action<ExecutionContext> engineBuilder)
|
2025-03-08 23:52:01 +03:00
|
|
|
{
|
2025-07-02 10:32:56 +03:00
|
|
|
_args = args;
|
2025-06-11 12:42:53 +03:00
|
|
|
var executablePath = Path.Combine(path, metadata.Executable);
|
|
|
|
|
var librariesPath = Path.Combine(path, "oscript_modules");
|
|
|
|
|
|
2025-08-01 19:08:02 +03:00
|
|
|
using var engine = CreateEngine(librariesPath, engineBuilder);
|
2025-03-08 23:52:01 +03:00
|
|
|
engine.Initialize();
|
|
|
|
|
|
|
|
|
|
var source = SourceCodeBuilder
|
|
|
|
|
.Create()
|
2025-06-11 12:42:53 +03:00
|
|
|
.FromFile(executablePath)
|
2025-03-08 23:52:01 +03:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var process = engine.CreateProcess(this, source);
|
|
|
|
|
var exitCode = process.Start();
|
|
|
|
|
|
|
|
|
|
if (exitCode != 0)
|
|
|
|
|
throw new Exception("Ошибка выполнения скрипта");
|
|
|
|
|
}
|
2025-06-11 12:42:53 +03:00
|
|
|
|
2025-08-01 19:08:02 +03:00
|
|
|
private static HostedScriptEngine CreateEngine(string librariesPath, Action<ExecutionContext> engineBuilder)
|
2025-06-11 12:42:53 +03:00
|
|
|
{
|
|
|
|
|
var builder = DefaultEngineBuilder
|
2025-03-08 23:52:01 +03:00
|
|
|
.Create()
|
|
|
|
|
.SetDefaultOptions()
|
|
|
|
|
.UseImports()
|
|
|
|
|
.SetupEnvironment(e =>
|
|
|
|
|
{
|
|
|
|
|
e.AddStandardLibrary();
|
2025-08-01 19:08:02 +03:00
|
|
|
e.AddAssembly(typeof(OneScriptExecutor).Assembly);
|
|
|
|
|
engineBuilder.Invoke(e);
|
2025-06-11 12:42:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.RegisterSingleton<IDependencyResolver>(new FileSystemDependencyResolver
|
|
|
|
|
{
|
|
|
|
|
LibraryRoot = librariesPath
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new HostedScriptEngine(builder.Build());
|
|
|
|
|
}
|
2025-03-08 23:52:01 +03:00
|
|
|
|
|
|
|
|
public void Echo(string str, MessageStatusEnum status = MessageStatusEnum.Ordinary)
|
|
|
|
|
{
|
|
|
|
|
OnEcho?.Invoke(this, (str, status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowExceptionInfo(Exception exc)
|
|
|
|
|
{
|
|
|
|
|
OnError?.Invoke(this, exc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool InputString([UnscopedRef] out string result, string prompt, int maxLen, bool multiline)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetCommandLineArguments()
|
|
|
|
|
{
|
2025-07-02 10:32:56 +03:00
|
|
|
return _args;
|
2025-03-08 23:52:01 +03:00
|
|
|
}
|
|
|
|
|
}
|