-
Notifications
You must be signed in to change notification settings - Fork 8
Package installation
scriptcs integrates tightly with Nuget packages. This page describes how to use Nuget package binding with scriptcs.
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 thepackages.configfile 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 -installAs a result, all packages specified in the
packages.config, including all dependencies, will be downloaded and copied to thebinfolder.
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:
-
local
nuget.configin the script folder, or in thepackagesfolder 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 -
main
nuget.configfrom%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,scriptcswill actually pick them up -
if there is no
nuget.configat your machine (i.e. you've never usednuget.exe), we'll default to official Nuget package source
Because of that, scriptcs will also use your local Nuget cache.
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.
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.
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.