8

I'm writing .csx build scripts. On my local machine I run them with CSharp interactive from visual studio or via C:\Program Files (x86)\MSBuild\14.0\bin\csi.exe. I've tried running .csx scripts on a TeamCity build agent machine (agent machines perform actual build tasks like compilation), which has Microsoft Built Tools 2015 installed. Surprisingly, there was no csi.exe in MSBuild folders.

I'm looking for a way to install csi.exe or a compatible standalone .csx runner on my build agent machines. The options found in Google, like https://github.com/cake-build/cake , all seem to have their distinct flavor of scripts or lots of additional functionality, whereas I'm looking for a csi.exe clone ideally.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50

4 Answers4

7

Solution turned out to be easy: Roslyn NuGet package contains csi.exe. It will be located at packages\microsoft.net.compilers.2.4.0\tools\csi.exe.

One thing to note - installation script for the package changes .csproj file to use the compiler provided by the package. This is likely not what you want, so make sure to roll-back such changes if they are made during installation.

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50
  • Don't know if I'm missing something evident nowadays, but I find this question/answer extremely useful and also underrated. Just downloaded microsoft.net.compilers.toolset.4.6.0.nupkg (microsoft.net.compilers is deprecated now), opened it as a zip file, and simply extracted the subfolder I needed (tasks\net472), which contains CSI and I can easily copy to any Windows machine. Maybe there's a cleaner/quicker way but I couldn't find it, however this is so straightforward it's more than good enough to me. Thank you! – matpop Jun 08 '23 at 20:21
3

You can compile your own runner with any customization you like using Roslyn API.

This code would do the basic job of running a script:

static void Main(string[] args)
{
    Task<object> result = CSharpScript.EvaluateAsync(File.ReadAllText(args[0]));
    result.Wait();
}
idanp
  • 973
  • 12
  • 18
2

There is a very good tutorial in the Microsoft Documentation: Cross-Platform Code Generation with Roslyn and .NET Core

It's somewhat equivalent to using csi.exe but with added benefits.

In their example, you have a variable const string code. This could be a string from your form or the contents of a script file.

I prefer this approach myself because:

  • It allows you to access and control the resources of your runner, instance it properly, measure its performance, etc. Simply running CSI could potentially explode in the memory if left untouched, and is much more opaque than managing it in code.
  • It returns code analysis in cases of errors. You can let the user easily pinpoint the source of an unsuccessful build.
  • It's cross-platform, unlike e.g. csi.exe sitting on a some hard drive
  • You can choose to compile at run-time, or create a DLL that runs much faster if pre-built
0

TeamCity 2021.2 provides a new C# Script runner, I hope what you need

NikolayP
  • 389
  • 2
  • 5