Kephas.Scripting
11.0.0-dev.4
Prefix Reserved
See the version list below for details.
dotnet add package Kephas.Scripting --version 11.0.0-dev.4
NuGet\Install-Package Kephas.Scripting -Version 11.0.0-dev.4
<PackageReference Include="Kephas.Scripting" Version="11.0.0-dev.4" />
<PackageVersion Include="Kephas.Scripting" Version="11.0.0-dev.4" />
<PackageReference Include="Kephas.Scripting" />
paket add Kephas.Scripting --version 11.0.0-dev.4
#r "nuget: Kephas.Scripting, 11.0.0-dev.4"
#:package Kephas.Scripting@11.0.0-dev.4
#addin nuget:?package=Kephas.Scripting&version=11.0.0-dev.4&prerelease
#tool nuget:?package=Kephas.Scripting&version=11.0.0-dev.4&prerelease
Scripting
Introduction
The scripting area in Kephas handles dynamic code execution.
The entry point is the IScriptProcessor singleton service, which returns a result through the ExecuteAsync method, provided with a script, execution arguments, and globals.
## Usage
```C#
// normally you would get the processor injected into the service constructor.
var processor = injector.Resolve<IScriptProcessor>();
// the next line uses C# scripting, so you will need also to reference Kephas.Scripting.CSharp.
var result = processor.ExecuteAsync(new CSharpStringScript("name[..4] + age.ToString()"), new { name = "Johnny", age = 42 }));
Assert.Equals("John42", result);
// the next line uses Python scripting, so you will need also to reference Kephas.Scripting.Python.
// additionally, instead of using typed arguments, it used an Expando - it could use as well a IDictionary<string, object?>.
var result = processor.ExecuteAsync(new PythonStringScript("name[..4] + str(age)"), new Expando { ["name"] = "Johnny", ["age"] = 42 }));
Assert.Equals("John42", result);
```
## The ```IScriptProcessor``` service
This service is the central piece providing the ```ExecuteAsync``` method through which a script is executed.
The script return value is, in turn, returned by the method.
```DefaultScriptProcessor``` is the default implementation of the ```IScriptProcessor```.
Just like the other default implementations provided by the Kephas framework, it declares a low override priority, so that it can be easily overridden.
By default, it delegates the template processing to a specific ```ILanguageService``` handling the provided script.
It identifies the language service based on the script's language, which the service declares using the ```[Language(...)]``` attribute.
### Passing arguments
The arguments passed to the execution can be referenced in the scripts directly by their name.
However, if the ```DeconstructArgs``` scripting context options is set to false, the arguments can be accessed through the ```Args``` global variable.
#### Example
```c#
var processor = injector.Resolve<IScriptProcessor>();
var script = new CSharpStringScript(
"int Power(int a) => a * a;" +
"return Power((int)Args.a);");
var result = await processor.ExecuteAsync(script, new { a = 2 }, ctx => ctx.DeconstructArgs(false));
```
## Language services
These services implement the ```ILanguageService``` contract and handle specific languages.
By default, the framework does not provide any language service in the ```Kephas.Scripting``` package due to the heavy overhead it brings.
However, there are several language services provided in separate packages:
* [Kephas.Scripting.CSharp](https://www.nuget.org/packages/Kephas.Scripting.CSharp)
* [Kephas.Scripting.Python](https://www.nuget.org/packages/Kephas.Scripting.Python)
* [Kephas.Scripting.Lua](https://www.nuget.org/packages/Kephas.Scripting.Lua)
## Controlling the script execution
### The scripting context
Additional to the script and the arguments, the processing methods receive also a context which can be used to further control the operations.
Just like all the other context instances, it is a dynamic expandable object (```IExpando```) and it aggregates the provided template and the model.
Through the ```Result``` and ```Exception``` properties, the behaviors (see below) can control the processing output.
### The scripting behaviors
The ```DefaultScriptProcessor``` uses also ```IScriptingBehavior``` services to further control the execution. Just like the ```ILanguageService``` services, they declare the language they support,
and are invoked before and after the selected language service executes the script. They can cover various purposes: authorization, pre-processing/post-processing, audit, and whatever other functionality may be required.
## The scripts
A script implements ```IScript```, basically providing a ```Language``` (string), a ```Name``` (string), and source code (```GetSourceCodeAsync()```, ```GetSourceCode()```).
By default, the framework supports these script:
* ```StringScript```: constructed using a string.
* ```StreamScript```: constructed using a ```Stream```.
* ```FileScript```: constructed using a file path. If not provided, the ```Language``` is considered to be the file extension.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Kephas.Injection (>= 11.0.0-dev.4)
- Kephas.Logging (>= 11.0.0-dev.4)
-
net6.0
- Kephas.Injection (>= 11.0.0-dev.4)
- Kephas.Logging (>= 11.0.0-dev.4)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Kephas.Scripting:
| Package | Downloads |
|---|---|
|
Kephas.Scripting.CSharp
Provides the infrastructure for executing C# scripts. Typically used areas and classes/interfaces/services: - CSharpLanguageService. Kephas Framework ("stone" in aramaic) aims to deliver a solid infrastructure for applications and application ecosystems. |
|
|
Kephas.Scripting.Python
Provides the infrastructure for executing Python scripts. Typically used areas and classes/interfaces/services: - PythonLanguageService. Kephas Framework ("stone" in aramaic) aims to deliver a solid infrastructure for applications and application ecosystems. |
|
|
Kephas.Scripting.Lua
Provides the infrastructure for executing LUA scripts. Typically used areas and classes/interfaces/services: - LuaLanguageService. Kephas Framework ("stone" in aramaic) aims to deliver a solid infrastructure for applications and application ecosystems. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 11.1.0 | 1,791 | 4/13/2022 |
| 11.1.0-dev.4 | 287 | 4/6/2022 |
| 11.1.0-dev.3 | 273 | 3/30/2022 |
| 11.1.0-dev.2 | 275 | 3/23/2022 |
| 11.1.0-dev.1 | 254 | 3/23/2022 |
| 11.0.0 | 1,479 | 3/11/2022 |
| 11.0.0-dev.7 | 264 | 3/7/2022 |
| 11.0.0-dev.6 | 302 | 2/28/2022 |
| 11.0.0-dev.5 | 277 | 2/26/2022 |
| 11.0.0-dev.4 | 276 | 2/24/2022 |
| 11.0.0-dev.3 | 271 | 2/23/2022 |
| 11.0.0-dev.2 | 277 | 2/18/2022 |
| 11.0.0-dev.1 | 272 | 2/7/2022 |
| 10.3.0 | 1,502 | 1/18/2022 |
| 10.2.0 | 1,496 | 12/3/2021 |
| 10.1.0 | 5,588 | 11/23/2021 |
| 10.1.0-dev.7 | 369 | 11/17/2021 |
| 10.1.0-dev.6 | 315 | 11/16/2021 |
| 10.1.0-dev.5 | 327 | 11/10/2021 |
| 10.1.0-dev.4 | 307 | 11/8/2021 |
Please check https://github.com/kephas-software/kephas/releases for the change log.
Also check the documentation and the samples from https://github.com/kephas-software/kephas/wiki and https://github.com/kephas-software/kephas/tree/master/Samples.