Skip to content

Package installation

MartinDoms edited this page May 13, 2013 · 4 revisions

scriptcs integrates tightly with Nuget packages. This page describes how to use Nuget package binding with scriptcs.

Using scriptcs -install

You can install packages directly from scriptcs CLI. This will pull the relevant packages from Nuget, and copy them to the bin folder, which is what scriptcs uses to load assemblies when executing scripts.

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

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

  • scriptcs -install {package name} will install the desired package from Nuget.

    For example:

     scriptcs -install ServiceStack
    
  • scriptcs -install (without package name) will look for the packages.config file located in the current execution directory, and install all the packages specified there. You only need to specify top level packages.

    For example, you might create the following 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 packages.config, including all dependencies, will be downloaded and copied to the bin folder.

At this point, you might ask which package feeds will scriptcs -install use?

We rely on the regular nuget.config convention. Here's the order of precedence when locating this file:

  1. local nuget.config in the script folder, or in the packages folder relative to the script location, feeds from that file will be used. This is especially useful if, for a given script app, you want to pull something from private feed or if you are testing local packages from your own hard disk

  2. main nuget.config from %APPDATA%\NuGet\. If you use Visual Studio, this is the same file that VS Package Manager uses, so if you have pre-configured any feeds there, scriptcs will actually pick them up

  3. if there is no nuget.config at your machine (i.e. you've never used nuget.exe), we'll default to official Nuget package source

Because of that, scriptcs will also use your local Nuget cache.

Using nuget install

If you prefer using nuget.exe, you can do that too. You should instruct it to install packages into packages folder relative to your script.

For example:

nuget.exe install -o packages ServiceStack

You can repeat the process multiple times for as many packages as you wish. Finally, once all the desired packages are downloaded, you should run

`scriptcs -restore` 

to copy the downloaded assemblies into bin folder (again, th3at's where scriptcs will look for them at runtime).

If you don' want to to install by packages name, but rather by using packages.config, that's obviously fine too - as long as the packages get installed into the packages folder. However, you should be aware that nuget.exe does not download dependencies when installing against packages.config (contrary to the built in scriptcs -install) - therefore, you'd need to specify all packages and their dependencies explicitly.

For this reason, when you choose to use packages.confg, we recommend you use scriptcs -install instead of nuget.exe.

Manually copying packages

Finally, you can also just copy packages from somewhere (i.e. another project) into the packages folder relative to the script location.

In that case you just need to call

scriptcs -restore

to copy the relevant DLLs to the bin folder.

You can also chain the restore command with the script execution, i.e.:

scriptcs {myscript.csx} -restore

This will first copy the DLLs, and then execute the script.

If you have packages.config, the restore command will use that to determine which assemblies it should copy to bin. If packages.config file does not exist, restore will simply copy all the .NET compatible, latest versions of DLLs discovered in the packages folder.

Generating packages.config

Having packages.config makes redistributing your script easy, as you only have to copy CSX files and packages.config, without worrying about moving the DLLs too. You can generate a package.config file based on the contents of your packages folder by calling:

scriptcs -save

This is really handy when you installed packages using nuget.exe, or copied them from another project and have no packages.config for your script application.

Now when you move your script to a different machine/client you can just run install again - against the packages.config file.

Clone this wiki locally