Shared script example

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

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.

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.

RequestResponse

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

Script explanation

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

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

Dependent 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.

RequestResponse

#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

  1. Reference the shared script by its name. Multiple shared scripts can be referenced by adding multiple #load "<Script Name>" statements.

    RequestResponse
    
    #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.

    RequestResponse
    
    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.

    RequestResponse
    
    await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));
    
    
  4. Retrieve the FileName property from the Target object using GetPropertyValue.

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

    RequestResponse
    
    var extension = GetExtension(fileName);
    
    

Best practices

Scope

While you can add members (members, properties, and so on) to the global scope of a script, it is better to group them in classes to allow for better separation and easier maintenance. The example above could be converted into the following:

RequestResponse

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:

RequestResponse

var extension = FileHelper.GetExtension(fileName);

Do you have some feedback for us?

If you have suggestions for improving this article,