Clear Filters
Clear Filters

Propagate variables to next build task when using buildtools?

5 views (last 30 days)
I am tryin to leverage buildtools to create a build and subsequently create an installer using:
build_opts = compiler.build.StandaloneApplicationOptions(...);
result = compiler.build.standaloneApplication(build_opts);
I would like to be able to pass the results to the next step to package it up w/ or w/o MCR using
installer_opts = compiler.package.InstallerOptions(...
result, ... );
However when using buildtools it seems impossible to propagate objects or variables from one build task to the next.
My buildplan.m file (relevant parts) only currently look like this to work around it, but it requires duplication of code and does not allow the default way of specifying dependencies for buildtasks as shown in the documentation.
function plan = buildfile
plan = buildplan(localfunctions);
plan.DefaultTasks = "build";
plan("build").Dependencies = [...
"lint", ...
"test", ...
];
plan("package").Dependencies = [...
"lint", ...
"test", ...
];
end
function lintTask(~)
% Identify code issues
issues = codeIssues
assert(isempty(issues.Issues), formattedDisplayText(issues.Issues))
end
function testTask(~)
% Run unit tests
results = runtests(...
IncludeSubfolders = true, ...
OutputDetail = "Detailed");
assertSuccess(results);
end
function buildTask(~)
% Build package
build(which('proton_monthly_qa_analysis.m'));
end
function packageTask(~)
% Create installer(s)
result = build(which('proton_monthly_qa_analysis.m'));
package(result);
end
%...
function result = build(appFile, buildDir)
% Build package
arguments (Input)
%% Required arguments
appFile {mustBeTextScalar, mustBeFile}
buildDir {mustBeTextScalar} = '.\build';
end
arguments (Output)
result (1, 1) compiler.build.Results
end
%...
if isempty(version)
version = 'unknown';
end
% Set compile options
build_opts = compiler.build.StandaloneApplicationOptions(...
appFile, ...
'OutputDir', buildDir, ...
'ExecutableVersion', strtrim(version), ...
'ExecutableName', '***REDACTED***', ...
'Verbose', 'On');
% Build
result = compiler.build.standaloneApplication(build_opts);
end
function result = package(result, runtimeDelivery, install_folder, author_company)
arguments (Input)
%% Required arguments
result (1, 1) compiler.build.Results
%% Optional options
runtimeDelivery (:, 2) string {mustBeText} = [...
"web", "without_mcr"; ...
"installer", "with_mcr"; ...
];
install_folder {mustBeTextScalar} = 'C:\Progam Files';
author_company {mustBeTextScalar} = 'Maastro Clinic';
end
arguments (Output)
result (1, 1) compiler.build.Results
end
%% Build installer(s)
for i = 1:size(runtimeDelivery, 1)
fprintf('Starting installer build: %s\n', runtimeDelivery(i, 1));
installer_opts = compiler.package.InstallerOptions(...
result, ...
'ApplicationName', '***REDACTED***', ...
'AuthorCompany', author_company, ...
'AuthorName', '***REDACTED***', ...
'AuthorEmail', '***REDACTED***', ...
"DefaultInstallationDir", fullfile(...
strtrim(install_folder), ...
author_company, ...
'***REDACTED***'), ...
'InstallerName', sprintf(...
'%s_%s', ...
result.Options.ExecutableName, ...
runtimeDelivery(i, 2)), ...
'OutputDir', result.Options.OutputDir, ...
'RuntimeDelivery', runtimeDelivery(i, 1), ...
'Summary', '***REDACTED***', ...
'Version', result.Options.ExecutableVersion);
try
compiler.package.installer(...
result, 'Options', installer_opts)
catch ME
switch ME.identifier
case 'Compiler:installer:couldNotFindRuntimeInstaller'
compiler.runtime.download
compiler.package.installer(...
result, 'Options', installer_opts);
otherwise
rethrow ME;
end
end
end
end
Not sure if this is intended but I would welcome it if there is/will be an option to set plan specific parameters as options to the plan or propagate values to next tasks.

Answers (1)

arushi
arushi on 23 Jan 2024
Hi Jonathan,
I understand that you want to propagate variables to the next build task. You are trying to automate the process of building and packaging a MATLAB application using MATLAB's buildtools functionality. Your current implementation has redundancy because you are building the application again in the packageTask even though it was already built in the buildTask. Ideally, you would want to build once and then use the result of that build to create the installer.
MATLAB's buildtools are designed to allow you to define a sequence of tasks that can depend on each other. One way to work around this limitation is to write the build result to a file during the buildTask and then read it back in during the packageTask. Here’s a conceptual approach to how you might implement this:
  1. Modify the buildTask to save the build result to a file.
  2. Modify the packageTask to read the build result from the file and use it to create the installer.
Ensure that you handle the build result file (buildResult.mat) properly, such as deleting it after the build and package process is completed to avoid any potential for using stale build results in future builds.
Hope this helps.

Categories

Find more on Build Automation in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!