Main Content

matlab.buildtool.Task class

Package: matlab.buildtool

Single unit of work in a build

Since R2022b

Description

The matlab.buildtool.Task class represents a single unit of work in a build, such as identifying code issues, running tests, and packaging a toolbox.

A task has three fundamental characteristics:

  • Name — The name of a task uniquely identifies the task in a build plan.

  • Dependencies — The dependencies of a task are other tasks in the plan that must run before the task runs.

  • Actions — The actions of a task define functions that execute when the task runs.

To run a task, the build runner first runs all its dependencies and then performs actions of the task in the order they appear in the Actions property.

Creation

Description

example

task = matlab.buildtool.Task returns a Task object whose properties have their default values.

example

task = matlab.buildtool.Task(Name=Value) sets properties using one or more name-value arguments. However, you cannot use this syntax to set the Name property. For example, task = matlab.buildtool.Task(Actions=@(~)assertSuccess(runtests)) creates a task that runs the tests in your current folder and fails if any of the tests fail.

Properties

expand all

Name of the task, returned as a string scalar. The name must be a valid MATLAB® identifier. A valid MATLAB identifier is a string scalar or character vector of alphanumerics (A–Z, a–z, 0–9) and underscores, where the first character is a letter and the length of the text is not greater than namelengthmax.

By default, the property contains a missing value. The build tool sets the property when you assign the task to a build plan. For example, plan("taskName") = task sets the Name property of task to "taskName".

Attributes:

GetAccess
public
SetAccess
Restricts access

Description of the task, specified as a string scalar or character vector, and returned as a string scalar. When the build tool lists the tasks, it also includes task descriptions.

Attributes:

GetAccess
public
SetAccess
public

Names of the tasks on which the task depends, specified as a string vector, character vector, or cell vector of character vectors, and returned as a string row vector. To run the task, the build runner first runs all the depended-on tasks. The order of task names in Dependencies does not matter.

Attributes:

GetAccess
public
SetAccess
public

Actions of the task, specified as a function handle, cell vector of function handles, or vector of matlab.buildtool.TaskAction objects, and returned as a row vector of TaskAction objects. The build runner performs actions in the order they appear in this property. Most tasks require no more than one action.

Attributes:

GetAccess
public
SetAccess
public

Task inputs, specified as one of these values:

If you specify a string vector, character vector, or cell vector of character vectors, MATLAB automatically converts the value to a FileCollection row vector.

Setting this property lets you run the task as part of an incremental build. For more information, see Improve Performance with Incremental Builds.

Attributes:

GetAccess
public
SetAccess
public

Task outputs, specified as one of these values:

If you specify a string vector, character vector, or cell vector of character vectors, MATLAB automatically converts the value to a FileCollection row vector.

Setting this property lets you run the task as part of an incremental build. For more information, see Improve Performance with Incremental Builds.

Attributes:

GetAccess
public
SetAccess
public

Examples

collapse all

Create tasks and add them to a build plan. Then, run the build.

Import the classes used in this example.

import matlab.buildtool.Task
import matlab.buildtool.tasks.CodeIssuesTask
import matlab.buildtool.tasks.TestTask

Create a plan with no tasks.

plan = buildplan;

Create a task named "check" that identifies code issues in the current folder and its subfolders and fails the build if any errors are found. To create the task, use the CodeIssuesTask built-in class.

plan("check") = CodeIssuesTask;

Create a task named "test" that runs the tests in the current folder and its subfolders and fails the build if any of the tests fail. To create the task, use the TestTask built-in class.

plan("test") = TestTask;

Add another task to the plan that creates an archive of the current folder. Make the task dependent on the "check" and "test" tasks.

plan("archive") = Task( ...
    Description="Create ZIP file", ...
    Dependencies=["check" "test"], ...
    Actions=@archive);

Now, make the "archive" task the default task in the plan.

plan.DefaultTasks = "archive";

Run the default task. The build tool runs the "archive" task as well as both the tasks on which it depends. In this example, the tasks run successfully.

result = run(plan);
** Starting check
Analysis Summary:
    Total Files: 3
         Errors: 0 (Threshold: 0)
       Warnings: 0 (Threshold: Inf)
** Finished check

** Starting test
...

Test Summary:
    Total Tests: 3
         Passed: 3
         Failed: 0
     Incomplete: 0
       Duration: 0.24699 seconds testing time.
                 
** Finished test

** Starting archive
** Finished archive

Local Function

This code shows the local function used in this example.

function archive(~)
filename = "source_" + ...
    string(datetime("now",Format="yyyyMMdd'T'HHmmss"));
zip(filename,"*")
end

Create and run a task that has inputs and outputs. Tasks with specified inputs or outputs support incremental builds.

Import the Task class.

import matlab.buildtool.Task

Create the files and folders used in this example. See the code of the local function createFile, which is used to create the files, at the end of this example.

mkdir source
createFile(fullfile("source","file1.m"))
createFile(fullfile("source","file2.m"))
mkdir source private
createFile(fullfile("source","private","file3.m"))
createFile(fullfile("source","private","file4.m"))

Create a plan with no tasks.

plan = buildplan;

Add a task named "pcode" to the plan that creates P-code files. To specify the inputs and outputs of the task, set the Inputs and Outputs properties. See the code of the local function pcodeAction, which is used in this task definition, at the end of this example.

plan("pcode") = Task( ...
    Description="Create P-code files", ...
    Actions=@pcodeAction, ...
    Inputs="source/**/*.m", ...
    Outputs="source/**/*.p");

Run the "pcode" task. The task obfuscates its inputs and creates the P-code files in the same folders as the inputs.

run(plan,"pcode");
** Starting pcode
** Finished pcode

Run the task again. The build tool skips the task because none of the inputs or outputs of the task have changed since the last run.

run(plan,"pcode");
** Skipped pcode: up-to-date

Add a file to the source folder, and then rerun the task. The build tool runs the task because the inputs of the task have changed.

createFile(fullfile("source","newFile.m"))
run(plan,"pcode");
** Starting pcode
** Finished pcode

Local Functions

This section contains the local functions used in this example.

function createFile(filename)
fclose(fopen(filename,"w"));
end

function pcodeAction(context)
filePaths = context.Task.Inputs.paths;
pcode(filePaths{:},"-inplace")
end

Version History

Introduced in R2022b

expand all