Quite often it is desirable to perform certain actions before and/or after the script execution. Such actions can be virtually anything (copying files, executing certain applications, setting environment variables, interacting with user etc.).
CS-Script allows such actions to be specified directly in the code in a form of pre- and post-execution scripts. It means that, when composing the script (
main script), you can specify any other C# script to be executed prior to or after the
main script execution.
Having the pre- and post-execution actions implemented as scripts (pre- and post-scripts) gives developers a great level of flexibility. These are just some a few examples of what you can do using this approach:
- dynamically compose C# scripts, which are to be imported by the main script
- import COM type library, which is to be used by the main script
- prepare a fresh copy of the .cs file with the implementation of the C# class for accessing the Web Service
- perform some cleanup after the main script execution
- log execution start and finish time of the main script
- set the environment variables to be accessed from the main script
The way how pre- and post-scripts are specified in the code is similar to the one used for imported scripts or referenced assemblies:
//css_prescript file([arg0][, arg1]..[,arg2])[ignore];
//css_postscript file([arg0][, arg1]..[,arg2])[ignore];
file - script file (extension is optional)
arg0..N - script string arguments
ignore - continue execution of the main script even in case of exception in the pre-/post-script
If the string $this (or $this.name) is specified as arg0..N it will be replaced at execution time with the main script full name (or file name only).
The shorter alias directives //css_pre and //css_post can be used in place of the //css_prescript and //css_postscript respectively.
Note:
All pre- and post-scripts are executed within the same address space as the main script.
The concept of pre- and post-script is the key component of the COM and Web Service "single-line access" implementation. This functionality of the CS-Script allows to interface the Web Services and COM objects with just a single line of code. No manual steps for importing type libraries or executing the
wsdl.exe are required. All can be done in the background as a pre-execution action (see
Using COM and
CS-Script and Web Services for details).
Remember that you may need to escape some path characters that conflict with the //css_ delimiters. See
Delimiters Escaping section.
Example
This is the example of the script, which sets the environment variable greeting to "Hello!":
using System;
class Script
{
static public void Main(string[] args)
{
if (args.Length > 0)
Console.WriteLine("Setting environment variable for "+
System.IO.Path.GetFileName(args[0])+"...");
Environment.SetEnvironmentVariable("greeting", "Hello!");
}
}
Now the value of the environment variable greeting is available for any script, which uses setEV.cs file as a pre-script. This is an example of such script (script.cs):
//css_prescript setEV($this);
using System;
class Script
{
static public void Main(string[] args)
{
Console.WriteLine(Environment.GetEnvironmentVariable("greeting"));
}
}
Output
See Also
Using COM |
CS-Script and Web Services