Shared script referencing another script
This is an example of a Shared script that is referenced by another script and exposes a simple method.
A shared script can include other shared scripts. However, it is important to avoid creating cyclic references. In the event that cyclic references are found during the compilation, the build output will provide some details about the conflicting scripts. Use this when you want to reuse centralized script logic across multiple dependent script.
-
Ensure that all dependent scripts reference the shared script correctly.
-
Define properties and configurations shared between the main and dependent scripts, such as entity IDs or relation names.
Shared script
For this example, the name of the shared script is My Shared Script.
public string GetExtension(string path)
{
var tokens = path.Split('.');
if (tokens.Length > 1)
{
return "." + tokens[tokens.Length - 1];
}
return null;
}
Shared script explanation
This script consists of a method that retrieves the file extension from a given file path. It splits the path at each dot (.
) and returns the last segment with a preceding dot (.
) if an extension exists. If no extension is found, it returns null
.
public string GetExtension(string path)
{
var tokens = path.Split('.');
if (tokens.Length > 1)
{
return "." + tokens[tokens.Length - 1];
}
return null;
}
Dependent script
The following example is partially derived from the Action script example (Pre-commit phase), but the approach would be the same for any other script type.
#load "My Shared Script.csx"
var entity = Context.Target as IEntity;
// Make sure the following members are loaded
await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));
var fileName = entity.GetPropertyValue<string>("FileName");
var extension = GetExtension(fileName);
// ...
Dependent script explanation
Reference the shared script by its name. Multiple shared scripts can be referenced by adding multiple #load "<Script Name>"
statements.
#load "My Shared Script.csx"
If you are using the IntelliSense support provided by the CLI, you can add the .csx
extension to the script name. That allows files to be resolved when working with scripts in the local file system.
Retrieve the Target
object from the Context
and cast it to IEntity
. Target
is the asset involved in the creation or modification event.
var entity = Context.Target as IEntity;
Make sure that the file name property and asset type relation are loaded. If they are not, they will be lazy-loaded.
await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));
Retrieve the FileName
property from the Target
object using GetPropertyValue
.
var filename = entity.GetPropertyValue<string>("FileName");
Call the GetExtension(string path)
method, which is made available through the shared script.
var extension = GetExtension(fileName);
Scope
Although you can add members, properties, and so on, to the global scope of a script, it is better to group them into classes to allow better separation and easier maintenance. This example could be converted into the following:
public static class FileHelper
{
public static string GetExtension(string path)
{
var tokens = path.Split('.');
if (tokens.Length > 1)
{
return "." + tokens[tokens.Length - 1];
}
return null;
}
}
The GetExtension
method has been made static, otherwise a FileHelper
instance would need to be created before calling the method.
The call to the method would look like this:
var extension = FileHelper.GetExtension(fileName);