logo
logo

Shared script example

This is an example of a Shared script that is referenced by another script and exposes a simple method.

Use case

  • Share and reuse script code

Shared script

For the purpose of 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;
}

Script explanation

  1. This is a public method used to get the extension of the provided file name.

    public string GetExtension(string path)
    {
        var tokens = path.Split('.');
        if (tokens.Length > 1)
        {
            return "." + tokens[tokens.Length - 1];
        }
        return null;
    }

Dependant script

The example below is partially derived from the Action script example (Pre-commit phase), but the approach would be the same for any other script type.

Note

A shared script can also include other shared scripts. However, it is important to be mindful about cyclic references. In the event that cyclic references are found during the compilation, the build output will provide some details about the conflicting scripts.

#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);

// ...

Dependant script explanation

  1. 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"
    Note

    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.

  2. 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;
  3. 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"));
  4. Retrieve the FileName property from the Target object using GetPropertyValue.

    var filename = entity.GetPropertyValue<string>("FileName");
  5. Call the GetExtension(string path) method which is made available through the shared script.

    var extension = GetExtension(fileName);

Best practices

Scope

While it is okay to add members (members, properties, etc.) to the global scope of a script, it is advised to group them in classes to allow for better separation and easier maintenance. The example above 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;
    }
}
Note

The GetExtension(string path) method has been made static, otherwise a FileHelper instance needs to be created before calling the method.

The call to the method would look like this:

var extension = FileHelper.GetExtension(fileName);

Can we improve this article ? Provide feedback