scriptcs / scriptcs

Write C# apps with a text editor, nuget and the power of Roslyn!
http://scriptcs.net
Other
2.46k stars 371 forks source link
csx roslyn scriptcs

scriptcs

Chocolatey Version Chocolatey Downloads NuGet version (ScriptCs.Hosting)

*nix Build Status Windows Build Status Coverity Scan Build Status

Issue Stats Issue Stats

What is it?

scriptcs makes it easy to write and execute C# with a simple text editor.

While Visual Studio, and other IDEs, are powerful tools, they can sometimes hinder productivity more than they promote it. You don’t always need, or want, the overhead of a creating a new solution or project. Sometimes you want to just type away in your favorite text editor.

scriptcs frees you from Visual Studio, without sacrificing the advantages of a strongly-typed language.

Getting scriptcs

Releases and nightly builds should be installed using Chocolatey. To install Chocolatey, execute the following command in your command prompt:

@powershell -NoProfile -ExecutionPolicy Unrestricted -Command "iex ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

If the above fails with the error indicating that proxy authentication is required (i.e. HTTP 407) then try again with the following on the command prompt that uses your default credentials:

@powershell -NoProfile -ExecutionPolicy Unrestricted -Command "[Net.WebRequest]::DefaultWebProxy.Credentials = [Net.CredentialCache]::DefaultCredentials; iex ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

Note: If you are using a version of Chocolatey > 0.9.9.0 you can pass the -y into the install and upgrade commands to prevent the confirmation that will appear.

Installing scriptcs

Once Chocolatey has been installed, you can install the latest stable version of scriptcs from your command prompt:

choco install scriptcs

Chocolatey will install scriptcs to %LOCALAPPDATA%\scriptcs\ and update your PATH accordingly.

Note: You may need to restart your command prompt after the installation completes.

Staying up-to-date

With Chocolatey, keeping scriptcs updated is just as easy:

choco upgrade scriptcs

Note: If you are using a version of Chocolatey < 0.9.0.0 you will need to use choco update scriptcs, but also think about updating Chocolatey itself.

Nightly builds

Nightly builds are hosted on MyGet, and can also be installed through with Chocolatey:

choco install scriptcs -pre -source https://www.myget.org/F/scriptcsnightly/ 

Building from source

Windows

  1. Ensure you have .NET Framework 4.6.1 installed.

  2. Execute the build script.

    build.cmd

Linux

  1. Ensure you have Mono 5.12 or later installed.

  2. Execute the build script

    ./build.sh

Getting Started

Using the REPL

The scriptcs REPL can be started by running scriptcs without any parameters. The REPL allows you to execute C# statements directly from your command prompt.

C:\> scriptcs
scriptcs (ctrl-c or blank to exit)

> var message = "Hello, world!";
> Console.WriteLine(message);
Hello, world!
> 

C:\>

REPL supports all C# language constructs (i.e. class definition, method definition), as well as multi-line input. For example:

C:\> scriptcs
scriptcs (ctrl-c or blank to exit)

> public class Test {
    public string Name { get; set; }
  }
> var x = new Test { Name = "Hello" };
> x
{Name: "Hello"}

C:\>

Writing a script

using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;

Console.WriteLine("Starting RavenDB server...");

using (var documentStore = new EmbeddableDocumentStore { UseEmbeddedHttpServer = true })
{
    documentStore.Initialize();
    Console.WriteLine($"RavenDB started, listening on http://localhost:{documentStore.Configuration.Port}");
    Console.ReadKey();
}
scriptcs -install RavenDB.Embedded
> scriptcs app.csx
INFO : Starting to create execution components
INFO : Starting execution
Starting RavenDB server...
.. snip ..
RavenDB started, listening on http://localhost:8080.

Bootstrap scripts with Script Packs

Script Packs can be used to further reduce the amount of code you need to write when working with common frameworks.

scriptcs -install ScriptCs.WebApi
public class TestController : ApiController {
    public string Get() {
        return "Hello world!";
    }
}

var webApi = Require<WebApi>();
var server = webApi.CreateServer("http://localhost:8888");
server.OpenAsync().Wait();

Console.WriteLine("Listening...");
Console.ReadKey();
server.CloseAsync().Wait();
scriptcs server.csx 
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello world!</string>

Referencing scripts

#load "controller.csx"
scriptcs server.csx 
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello world!</string>

Referencing assemblies

You can reference additional assemblies from the GAC or from the bin folder in your script's directory using the #r directive:

#r "nunit.core.dll"
#r "nunit.core.interfaces.dll"

var path = "UnitTests.dll";
var runner = TestSetup.GetRunner(new[] {path});
var result = runner.Run(new ConsoleListener(msg => Console.WriteLine(msg)), TestFilter.Empty, true,     LoggingThreshold.All);

Console.ReadKey();

Debugging

Instructions for debugging scripts using Visual Studio can be found on the wiki.

Package installation

You can install any NuGet packages directly from the scriptcs CLI. This will pull the relevant packages from NuGet, and install them in the scriptcs_packages folder.

Once the packages are installed, you can simply start using them in your script code directly (just import the namespaces - no additional bootstrapping or DLL referencing is needed).

The install command will also create a scriptcs_packages.config file if you don't have one - so that you can easily redistribute your script (without having to copy the package binaries).

For example, you might create the following scriptcs_packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
    <package id="Nancy.Hosting.Self" version="0.16.1" targetFramework="net40" />
    <package id="Nancy.Bootstrappers.Autofac" version="0.16.1" targetFramework="net40" />
    <package id="Autofac" version="2.6.3.862" targetFramework="net40" />
</packages>

And then just call:

scriptcs -install

As a result, all packages specified in the scriptcs_packages.config, including all dependencies, will be downloaded and installed in the scriptcs_packages folder.

Contributing

Samples and Documentation

Additional samples can be contributed to our samples repository. Documentation can be found on our wiki.

Community

Want to chat? In addition to Twitter, you can find us on Google Groups and JabbR!

Coordinators

Core Committers

Credits

License

Apache 2 License