-
Notifications
You must be signed in to change notification settings - Fork 10
Standalone Script Execution
Further reading:
Script stand alone execution is the most mature and stable part of CS-Script feature set. Thus the major online documentation has been written quite some time ago and while it may currently have some referencing discrepancies (e.g. VS2012 instead of VS2015) it is still an accurate primary source of CS-Script knowledge. This Wiki article on the other hand is the most concise and concentrated description of the relevant content and in many cases it will be fully sufficient for majority of the readers. In all other cases please refer to the primary Documentation online.
CS-Script allows a direct single step execution of files containing ECMA-compliant C# code. Canonical Hello World script:
using System;
using System.Windows.Forms;
class Script
{
static public void Main(string[] args)
{
Console.WriteLine("Hello World!");
MessageBox.Show("Hello World!");
}
}
The script engine executable comes in three forms:
- cscs.exe - console application executable
- csws.exe - Windows application executable
- css.exe - Universal executable. It launches cscs.exe and makes console window visible on the first console output from the script being executed. The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter. Thus the minimal deployment file set for CS-Script is either cscs.exe or csws.exe file. Any of these files is sufficient for the wast majority of the scripting scenarios.
The simplest way to start using CS-Script is to install it from Chocolatey, which is a repository for the Windows software packages. Chocolatey it is a Windows version of Linux apt-get. CS-Script can be installed with the following command:
C:\> choco install cs-script
During the installation Chocolatey downloads the latest version of CS-Script (from the official releases repository), extracts all files in the default Chocolatey libraries location and executes CS-Script's css_config.exe, which configures the OS for running C# scripts. Strictly speaking running css_config.exe is optional. The only thing that it does is adding the CS-Script location to the system PATH, and adds Windows Explorer shell extensions (context menu).
Any OS that have .NET/Mono installed can run C# scripts. The minimal distribution file set is the script engine executable and the script file itself. Of course if the script depends on the third-party library or other scripts then they also need to be deployed on the target system. The easiest way of preparing the script for running on another system is to use Notepad++ with C# Intellisense plugin enabled. Just click the "distro" button and the plugin will analyse the script and package it with all its dependencies along with the script engine itself. Optionally it may convert the script into a self sufficient single executable.
From the start CS-Script was heavily influenced by Python and the developer experiences it delivers. Thus the it tries to match the most useful Python features (apart from the Python syntax). Here are some highlight of the CS-Script features. Some more info can be found here.
- Scripts are written in 'plain vanilla' CLS-compliant C#. Though classless scripts are also supported.
- Remarkable execution speed matching performance of compiled managed applications.
- Including (referencing) dependency scripts from the main script.
- Referencing external assemblies from the script.
- Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings').
- Automatic resolving (downloading and referencing) NuGet packages.
- Building a self sufficient executable from the script.
- Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
- Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
- Full integration with Windows, Visual Studio and Notepad++ (CS-Script plugin for Notepad++ brings true Intellisense to the 'peoples editor')
CS-Script relies on so called 'script directives' for implementing some features. These directives are the C# comments that do not abstract standard C# compilers and yet provide user defined instructions for the script engine. Simple WebAPI server classless script:
//css_args /ac; - interpret as auto-class (classless) script
//css_include webapi; - include webapi.cs script
//css_include my_dto.cs; - include DTO definitions script
//css_nuget NLog; - reference NLog assembly
using System;
using WebApi;
void main()
{
WebApi.SimpleHost
.StartAsConosle("http://localhost:8080",
server =>
{
Console.WriteLine("Press Enter to quit.");
Console.WriteLine("---------------------");
server.Configuration.OutputRouts(Console.WriteLine);
Console.WriteLine("---------------------");
Console.ReadLine();
});
}
Set of supported directives is very small set of commend. The full description with code samples can be found here.
The following is a set of the most useful directives:
//css_inc - import/include another C# script (similar to C++ #include).
//css_ref - reference .NET assembly.
//css_args - provide startup arguments.
//css_dir - set additional probing directories for dependency scripts and assemblies.
//css_nuget - reference (and download if not present) NuGet package.
Note, due to the fact that Microsoft stopped distributing the latest C# compiler with it's .NET deployment, support for C# 6.0 syntax requires one extra step to be done. You will need to indicate the location of the Roslyn compiler (deployed with CS-Script) either globally or per-script. Read more about it here.
The scripts can be edited with any suitable text editor. Though if want to take advantage of Intellisense you may prefer Visual Studio with CS-Script extension. The extension can be enabled via VS Extension Manager. Though in many cases you may be better off with just Notepad++. You will need to enable C# intellisense plugin from the plugin manager. THis plugin was developed specifically to allow VS-like experience when dealing with C# scripts and any C# code. The plugin comes with it's own CS-Script package and allows true C# Intellisence assisted editing as well as the execution and debugging.