Main Content

matlab.buildtool.TaskAction class

Package: matlab.buildtool

Action performed when task runs

Description

The matlab.buildtool.TaskAction class defines a function that executes when a task runs.

When you specify the actions of a task, you can use function handles instead of TaskAction objects. MATLAB® automatically converts function handles to TaskAction objects.

Creation

Description

action = matlab.buildtool.TaskAction(fh) creates a TaskAction object that specifies the function to execute as the function handle fh.

example

action = matlab.buildtool.TaskAction(fh,Name=name) also sets the Name property. Use this syntax to provide descriptive names for task actions.

Input Arguments

expand all

Action to perform, specified as a function handle. The function handle must accept a TaskContext object as its first input.

Example: @(~)assert(isempty(codeIssues().Issues))

Example: @(~)assertSuccess(runtests(IncludeSubfolders=true))

Properties

expand all

Name of the action, returned as a string scalar. You can specify the value of this property during creation of the action as a string scalar or character vector. If you do not specify a value, the build tool sets the property to the string representation of fh.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Specify the actions to perform when a task runs.

In a file named runMyTests.m in your current folder, create a function that runs the tests in the current folder and its subfolders, generates an HTML test report from the test results, and asserts that the tests run without failure.

function runMyTests(~)
results = runtests(IncludeSubfolders=true,OutputDetail="terse");
generateHTMLReport(results,"artifacts")
assertSuccess(results);
end

Import the classes used in this example.

import matlab.buildtool.Task
import matlab.buildtool.TaskAction

Create a plan with no tasks.

plan = buildplan;

Add a task to the plan that executes the runMyTests function.

plan("test") = Task(Actions=@runMyTests);

Add another action to the task to open the generated test report.

plan("test").Actions(end+1) = TaskAction( ...
    @(~)open(fullfile("artifacts","index.html")),Name="Presenting");

Display the names of the task actions. Because you did not specify a name for the first action, the build tool displays the string representation of the function handle.

disp({plan("test").Actions.Name}')
    {["runMyTests"]}
    {["Presenting"]}

Now, run the task. The build runner performs the actions of the task in the order specified. In this example, all the tests pass and the report opens.

result = run(plan,"test");
** Starting test
...
Generating test report. Please wait.
    Preparing content for the test report.
    Adding content to the test report.
    Writing test report to file.
Test report has been saved to:
 C:\work\artifacts\index.html
** Finished test

Version History

Introduced in R2022b