问题描述
如果我想在我的应用程序中支持脚本编写,scriptcs 是否比仅使用普通的 脚本引擎?
If I want to support scripting in my application, does scriptcs offer any particular advantages over just using the plain vanilla Roslyn script engine?
推荐答案
不幸的是,关于托管 scriptcs 的文档还不多,但我会尽量给你一个简短的总结.
Unfortunately there's not much documentation on hosting scriptcs yet, but I'll try to give you a short summary.
在您的应用程序中托管 scriptcs 提供了一些普通 Roslyn 没有的功能:
Hosting scriptcs in your application provides several features that vanilla Roslyn doesn't:
虽然 scriptcs 默认带有 Roslyn 和 Mono 引擎,但您可以轻松地将其替换为另一个引擎,即 F#、LOLcode 甚至 Brainfuck.
While scriptcs comes with the Roslyn and Mono engines by default, you can easily replace it with another engine, i.e. F#, LOLcode or even Brainfuck.
scriptcs 将处理您的脚本并提取诸如引用(#r
)之类的内容并加载其他脚本(#load
).最近还引入了自定义 ILineProcessor
s 允许您连接到管道以进行自定义处理.示例处理器可能如下所示:
scriptcs will process your scripts and extract things like references (#r
) and load other scripts (#load
). There was also recently introduced custom ILineProcessor
s which lets you hook into the pipeline for custom processing. An example processor could look like this:
public class GistLineProcessor : DirectiveLineProcessor
{
protected string DirectiveName
{
return "gist";
}
protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
{
var gistId = GetDirectiveArgument(line);
var gistContents = DownloadGistContents(gistId);
parser.ParseScript(gistContents, context);
return true;
}
private static string DownloadGistContents(string gistId)
{
// Download gist contents...
}
}
此处理器将下载一个要点并将其作为脚本的一部分执行,即 #gist 12345678
.
This processor will download a gist and execute it as part of your script, i.e. #gist 12345678
.
scriptcs 与 NuGet 集成.这意味着,如果您希望脚本能够使用 NuGet 包,只需安装它们,它们就会自动从 packages 文件夹中加载.
scriptcs has integration with NuGet. This means that if you want scripts to be able to use NuGet packages, just install them and they will automatically be loaded from within the packages folder.
脚本包是 scriptcs 删除样板代码的方法.他们可以通过 Require()
导入命名空间、引用程序集并向脚本公开功能.请参阅 Martin Doms 关于构建 scriptcs 脚本包一>.有关可用脚本包的完整列表,请参阅 脚本包主列表.
Script packs is scriptcs' way to remove boilerplate code. They can import namespaces, reference assemblies and expose functionality to scripts through Require<T>()
. See Martin Doms' excellent blog post about building a scriptcs script pack. For a comprehensive list of available script packs, see Script packs master list.
您可能知道,scriptcs 有 REPL一>.这可以在您自己的应用程序中重复使用,以提供交互式脚本会话.
As you probably know, scriptcs has a REPL. This can be reused in your own application to provide an interactive scripting session.
使用 vanilla Roslyn 脚本引擎,您不能很容易地调试脚本.scriptcs 使您能够通过在预处理期间插入的 #line
指令使用源映射来调试脚本.
Using the vanilla Roslyn script engine, you can't debug scripts very easily. scriptcs gives you the ability to debug scripts with source mapping through #line
directives inserted during pre-processing.
我可能忘记了一些东西,但这些是选择 scriptcs 而不是 vanilla Roslyn 的要点.当谈到实际托管时,您有两个选择:
I may have forgotten something, but these are the main points for choosing scriptcs over vanilla Roslyn. When it comes to the actual hosting, you have two options:
这是一个超轻量级的库,包含了scriptcs管道的核心组件.但是,它不包含 IScriptEngine
(实际执行代码的引擎)和 IInstallationProvider
(安装包的组件,即 NuGet)的实现,这些位于 ScriptCs.Hosting 和 ScriptCs.Engine.Roslyn.如果您使用这个库,您将必须自己完成所有组件的连接,您还需要为引擎和包安装程序提供一个实现.
This is a super lightweight library that contains the core components of the scriptcs pipeline. However, it doesn't contain implementations for IScriptEngine
(the engine that actually executes code) and IInstallationProvider
(the component that installs packages, i.e. NuGet), these live in ScriptCs.Hosting and ScriptCs.Engine.Roslyn. If you use this library, you will have to do all the wire-up of the components yourself and you also need to provide an implementation for the engine and the package installer.
ScriptCs.Hosting 是一个方便的层,用于在应用程序中托管 scriptc.它在 scriptcs.exe 内部使用,并完成所有组件的连接(通过 Autofac)为你.它包含包安装程序的 NuGet 实现,默认情况下依赖于 ScriptCs.Engine.Roslyn.这是托管 scriptcs 的首选方式,因为它提供了一个 ScriptServicesBuilder
来轻松替换 scriptcs 的内部服务.有关用法示例,请参阅 scriptcs 的 Program.cs.
ScriptCs.Hosting is a convenience layer for hosting scriptcs in an application. It's used internally in scriptcs.exe and does all the wire-up of the components (via Autofac) for you. It contains the NuGet implementation for the package installer and has a dependency on ScriptCs.Engine.Roslyn by default. This is the preferred way to host scriptcs as it provides a ScriptServicesBuilder
to easily replace scriptcs' internal services. See scriptcs' Program.cs for example usage.
这听起来可能令人困惑,因此如果您有任何疑问,请在 JabbR、Github 或 Google 群组.
This could sound confusing so if you have questions, please ask on JabbR, Github or on the Google Group.
这篇关于scriptcs 托管 - 优于 Roslyn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!