Add a ScriptCsCatalog which can be used to add some ScriptCs scripts to a MEF container.
Simply create a new ScriptCsCatalog
and use it in your MEF composition:
// You can add script by script
//var catalog = new ScriptCsCatalog(new[] { "ScriptA.csx", "ScriptB.csx" });
// Or an entire folder
var catalog = new ScriptCsCatalog("Scripts");
var container = new CompositionContainer(catalog);
var batch = new CompositionBatch();
batch.AddPart(this);
container.Compose(batch);
Add References
It can be useful to add a references to all scripts loaded by the ScriptCsCatalog
.
For example, you provide the interface IGreeter
for imports in MEF.
So a basic script should be:
#r "Greeter.Contracts.dll"
using Greeter.Contracts;
public class MyGreeter : IGreeter
{
...
}
But you can pass the IGreeter
type as reference to the ScriptCsCatalog
:
// You can add script by script
var catalog = new ScriptCsCatalog(new[] { "ScriptA.csx", "ScriptB.csx" }, new ScriptCsCatalogOptions { References = new[] { typeof(IGreeter) } });
// Or an entire folder
var catalog = new ScriptCsCatalog("Scripts", new ScriptCsCatalogOptions { References = new[] typeof(IGreeter) } });
And the script will become:
public class MyGreeter : IGreeter
{
...
}
Script args
It is also possible to passes arguments to the script with the ScriptArgs property of the options:
var catalog = new ScriptCsCatalog("Scripts", new ScriptCsCatalogOptions { ScriptArgs = new[] { "-loglevel", "INFO" } });
It can be helpful with the use of Script Packs.
Script Packs
Script Packs can be used, the location varies depending on the approach used:
- script by script: the packages location is the current directory of your application
- folder: the packages location is the folder specified
Load script one by one
By default all scripts are loaded in one file to be interpreted by ScriptCS, but it can be source of issues like when alias are used.
The KeepScriptsSeparated
option allow to load script one by one and remove these issues:
var catalog = new ScriptCsCatalog("Scripts", new ScriptCsCatalogOptions { KeepScriptsSeparated = true });
Thanks to Glenn Block Script Packs can be used!
Thanks to beolutz for the custom file system support and KeepScriptsSeparated option.