
roslyn c#脚本

Glenn Block is working on something interesting that combines C#, NuGet, Roslyn (the new "compiler as a service") and his love of text editors and scripts. Now, with help from Justin Rusbatch (@jrusbatch) and Filip Wojcieszyn (@filip_woj) they are having all kinds of fun...using C# as a scripting language.
Glenn Block正在研究一些有趣的东西,结合了C#,NuGet,Roslyn(新的“编译器即服务”)和他对文本编辑器和脚本的热爱。 现在,在Justin Rusbatch( @jrusbatch )和Filip Wojcieszyn( @filip_woj )的帮助下,他们可以享受各种乐趣……使用C#作为脚本语言。
Every few years someone tries to turn C# into a competent scripting world, myself included. Often this has included batch files and MacGyver magic, file associations and hacks. Clearly the .NET community wants something like this, but we are collectively still trying to figure out what it should look like. PowerShell aficionados - and I count myself amongst them - might look at such efforts as a threat or a pale reinvention of PowerShell, but the fact remains that C# at the command line, be it as a script or a REPL, is an attractive concept.
每隔几年,就会有人尝试将C#变成一个有能力的脚本世界,包括我自己在内。 通常其中包括批处理文件和MacGyver魔术,文件关联和黑客。 显然,.NET社区需要这样的东西,但是我们仍在集体努力弄清楚它的外观。 PowerShell爱好者-我认为自己是其中的一员-可能将此类工作视为对PowerShell的威胁或苍白的重新发明,但事实仍然是命令行中的C#(无论是作为脚本还是REPL )都是很有吸引力的概念。
Simply put by example, ScriptCS lets me do this:
简单地举例来说,ScriptCS让我做到了:
C:\temp>copy con hello.csx
Console.WriteLine("Pants");
^Z
1 file(s) copied.
C:\temp>scriptcs hello.csx
Pants
That's Hello World. There's no namespace, no class, just some C# in a .csx file. Roslyn takes care of the compilation and the resulting code and .exe never hits the disk.
那就是Hello World。 没有名称空间,没有类,.csx文件中只有一些C#。 Roslyn负责编译,生成的代码和.exe永远不会到达磁盘。
自托管Web API (Self-hosting Web APIs)
So that's interesting, but what about bootstrapping a web server using NancyFX to host a Web API?
因此,这很有趣,但是使用NancyFX托管Web API引导Web服务器又如何呢?
Go and clone this repo:
去克隆这个仓库:
git clone https://github.com/scriptcs/scriptcs-samples.git
Look in the Nancy folder. There's a packages.config. Just like a node.js application has a packages.json file with the dependencies in has, a .NET app usually has a packages.config with the name. In node, you type npm install to restore those packages from the main repository. Here I'll type scriptcs -install...
在Nancy文件夹中查找。 有一个packages.config。 就像一个node.js应用程序有一个具有includes依赖关系的packages.json文件一样,.NET应用程序通常也具有其名称的packages.config。 在节点中,键入npm install以从主存储库还原那些软件包。 在这里,我将键入scriptcs -install ...
C:\temp\scriptcs-samples\nancy>scriptcs -install
Installing packages...
Installed: Nancy.Hosting.Self 0.16.1.0
Installed: Nancy.Bootstrappers.Autofac 0.16.1.0
Installed: Autofac 2.6.3.862
Installation successful.
Now, running start.csx fires up an instance of Nancy listening on localhost:1234. There's no IIS, no ASP.NET.
现在,运行start.csx会启动Nancy在localhost:1234上侦听的实例。 没有IIS,没有ASP.NET。
C:\temp\scriptcs-samples\nancy>scriptcs start.csx
Found assembly reference: Autofac.Configuration.dll
Found assembly reference: Autofac.dll
Found assembly reference: Nancy.Bootstrappers.Autofac.dll
Found assembly reference: Nancy.dll
Found assembly reference: Nancy.Hosting.Self.dll
Nancy is running at http://localhost:1234/
Press any key to end
There is also the notion of a "ScriptPack" such that you can Require<T> a library and hide a lot of the bootstrapping and complexity. For example, I could start up WebAPI after installing a Web API package that includes some starter code. Note this is all from the command line. I'm using "copy con file" to get started.
还有一个“ ScriptPack”的概念,因此您可以Require <T>一个库并隐藏很多引导程序和复杂性。 例如,我可以在安装包含一些入门代码的Web API程序包后启动WebAPI。 请注意,所有这些都是从命令行完成的。 我正在使用“复制文件”开始。
C:\temp\foo>scriptcs -install ScriptCs.WebApi
Installing packages...
Installed: ScriptCs.WebApi
Installation completed successfully.
...snip...
Added ScriptCs.WebApi, Version 0.1.0, .NET 4.5
Packages.config successfully created!
C:\temp\foo>copy con start.csx
public class TestController : ApiController {
public string Get() {
return "Hello world!";
}
}
var webApi = Require<WebApi>();
var server = webApi.CreateServer("http://localhost:8080");
server.OpenAsync().Wait();
Console.WriteLine("Listening...");
Console.ReadKey();
server.CloseAsync().Wait();
^Z
1 file(s) copied.
C:\temp\foo>scriptcs start.csx
Found assembly reference: Newtonsoft.Json.dll
...snip...
Listening...
Pretty slick. Add in a little Live Reload-style action and we could have a very node-ish experience, all from the command line and from within your text editor of choice, except using C#.
很漂亮加上一些Live Reload风格的操作,除了使用C#之外,我们都可以从命令行和所选的文本编辑器中获得非常节点式的体验。
Note that this is all using the same CLR and .NET that you've already got, running at full speed. Only the compilation is handled differently to give this script-like feel.
请注意,所有这些都使用了已经获得的相同的CLR和.NET,并以全速运行。 只有对编译的处理方式有所不同,才能呈现出类似脚本的感觉。
安装ScriptCS (Installing ScriptCS)
The easiest way to install and use ScriptCS is to use Chocolatey (a system-wide NuGet-based application/component installer. "Chocolatey NuGet," get it?) And yes, it's Chocolatey spelled incorrectly with an "-ey."
安装和使用ScriptCS的最简单方法是使用Chocolatey (基于系统的基于NuGet的应用程序/组件安装程序。“ Chocolatey NuGet”,明白吗?),是的,它的Chocolatey拼写不正确,带有“ -ey”。
You can use Chocolatey to do things like "cinst 7zip" or "cinst git" but we'll be using it just to get ScriptCS set up. It's also easily removed if it freaks you out and it installs no services and won't change anything major up save your PATH.
您可以使用Chocolatey进行“ cinst 7zip”或“ cinst git”之类的操作,但是我们将仅使用它来设置ScriptCS。 如果它使您不满意,也很容易删除,并且不安装任何服务,并且在保存PATH之前不会进行任何重大更改。
First paste this into a cmd.exe prompt:
首先将其粘贴到cmd.exe提示符下:
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
This will PowerShell, run https://chocolatey.org/install.ps1 and add Chocolatey to your path.
这将执行PowerShell,运行https://chocolatey.org/install.ps1并将Chocolatey添加到您的路径中。
Then, run
然后跑
cinst ScriptCS
Which will put ScriptCS in a path like C:\Chocolatey\lib\ScriptCs.0.0.0 while Chocolatey makes it available in your PATH.
这会将ScriptCS放置在C:\ Chocolatey \ lib \ ScriptCs.0.0.0之类的路径中,而Chocolatey则将其在您的PATH中可用。
Sublime Text或Visual Studio (Sublime Text or Visual Studio)
You can get syntax highlighting for your CSX files inside of Sublime Text 2 with the "ScriptCS" package you can install from package control. If you're using Visual Studio you can get the Roslyn CTP to turn on CSX syntax highlighting.
您可以通过Sublime Text 2中的“ ScriptCS”软件包为CSX文件突出显示语法,该软件包可以从软件包控件中安装。 如果您使用的是Visual Studio,则可以获取Roslyn CTP来打开CSX语法突出显示。

You can even debug your running ScriptCS projects by opening the ScriptCS.exe as a project. (Did you know you can open an EXE as a project?) Add the .csx script to the command line via Project Properties, drag in the scripts you're working on and debug away.
您甚至可以通过打开ScriptCS.exe作为项目来调试正在运行的ScriptCS项目。 (您知道您可以将EXE作为项目打开吗?)通过“项目属性”将.csx脚本添加到命令行中,将正在使用的脚本拖入并进行调试。
Debugging requires the Roslyn SDK, although personally, I've been doing just fine with scripts at the command line which requires nothing more than the basic install and a text editor.
调试需要Roslyn SDK,尽管就我个人而言,我一直在命令行上使用脚本做得很好,这只需要基本安装和文本编辑器。
It's not clear where ScriptCS is going, but it'll be interesting to see! Go get involved at scriptcs.net. This kind of stuff gets me excited about the prospect of a compiler as a service, and also cements my appreciation of C# as my enabling language of choice. Between C# and JavaScript, you can really get a lot done, pretty much anywhere.
目前尚不清楚ScriptCS的去向,但会很有趣! 进入scriptcs.net 。 这种东西使我对编译器即服务的前景感到兴奋,也使我对C#作为我选择的支持语言的赞赏。 在C#和JavaScript之间,您几乎可以在任何地方完成很多工作。
I'll have a video walkthrough on how this works as I explain it to Rob Conery up on TekPub soon! (Here's a referral coupon for 20% off of Tekpub!)
我将在TekPub上向Rob Conery讲解时,将有一个视频演练,介绍其工作原理! (以下是Tekpub优惠20%的推荐券!)
What do you think?
你怎么看?
翻译自: https://www.hanselman.com/blog/projectless-scripted-c-with-scriptcs-and-roslyn
roslyn c#脚本