Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/tests.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ By default, a `LICENSE` file in the project root is included in the packaged too
"RootFilesToPackage": ["LICENSE", "THIRD_PARTY_NOTICES.md"]
```

From R2026b, an `.mltbx` file is a package file with a readme and a package name. Set them with `Readme` and `PackageName` in the `ToolboxOptions` of `MLToolboxInfo.json`:

```json
"RootFilesToPackage": ["LICENSE", "README.md"],
"ToolboxOptions": {
"Readme": "README.md",
"PackageName": "MyToolbox"
}
```

- `Readme` is a path relative to the source folder, because the package contains only the files below that folder. List a project root file in `RootFilesToPackage` to copy it there. Without `Readme`, R2026b uses a `README.md` at the root of the source folder, or else writes `Description` to one.
- `PackageName` must be a valid MATLAB identifier. It names the installation folder and identifies the package in `mpmlist` and to packages that depend on it. Without it, R2026b derives the name from `ToolboxName`, dropping spaces and writing a hyphen as `0x2D`.
- Releases before R2026b package the readme as an ordinary file and have no package name. MatBox checks both fields on every release.

The packaged toolbox is saved as `releases/<ToolboxName>_v<version>.mltbx`, for example `MatBox_v1_2_3.mltbx`. To use the same file name for every release, add a top-level `MltbxFileName` to `MLToolboxInfo.json`. `.mltbx` is appended if it is missing, and the name must not contain folders:

```json
Expand Down
7 changes: 7 additions & 0 deletions code/+matbox/+tasks/packageToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
% specify a top-level "RootFilesToPackage" list in MLToolboxInfo.json,
% e.g. "RootFilesToPackage": ["LICENSE", "THIRD_PARTY_NOTICES.md"].
%
% From R2026b, "Readme" and "PackageName" in the "ToolboxOptions" of
% MLToolboxInfo.json set the readme file and the package name. "Readme" is
% a path relative to the source folder; list "README.md" in
% "RootFilesToPackage" to package the project README. Earlier releases
% package the readme as an ordinary file and have no package name. Both
% fields are checked on every release.
%
% The MLTBX file is named <ToolboxName>_v<version>.mltbx. To use a fixed
% file name for every release, specify a top-level "MltbxFileName" in
% MLToolboxInfo.json, e.g. "MltbxFileName": "MatBox".
Expand Down
78 changes: 75 additions & 3 deletions code/+matbox/+toolbox/createToolboxOptions.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
function toolboxOptions = createToolboxOptions(projectRootDirectory, versionNumber, options)
% Script for setting up toolbox (mltbx) options for openMINDS_MATLAB
% createToolboxOptions - Create the ToolboxOptions for packaging a project
%
% toolboxOptions = matbox.toolbox.createToolboxOptions(projectRootDirectory, versionNumber)
% returns a matlab.addons.toolbox.ToolboxOptions object for the given
% version, built from the project's MLToolboxInfo.json and
% requirements.txt.
%
% Example:
% toolboxOptions = getToolboxOptions('0.9.2')
% toolboxOptions = matbox.toolbox.createToolboxOptions(pwd, "0.9.2", "SourceFolderName", "code")

arguments
projectRootDirectory (1,1) string {mustBeFolder}
Expand All @@ -16,6 +21,20 @@
% Read the toolbox info from MLToolboxInfo.json
[toolboxInfo, identifier, toolboxInfoFileContent] = ...
matbox.toolbox.readToolboxInfo(projectRootDirectory);
toolboxFolder = fullfile(projectRootDirectory, options.SourceFolderName);

% ToolboxOptions has the Readme and PackageName properties from R2026b,
% and earlier releases reject them as unknown name-value arguments. They
% are therefore taken out before the constructor and set afterwards on
% the releases that have them.
[toolboxInfo, readme] = takeOptionalField(toolboxInfo, "Readme");
if readme ~= ""
readmeFile = resolveReadmeFile(toolboxFolder, readme);
end
[toolboxInfo, packageName] = takeOptionalField(toolboxInfo, "PackageName");
if packageName ~= ""
mustBeValidPackageName(packageName)
end

% Resolve folders to add to path for toolbox. This needs to be done
% before creating the ToolboxOptions object due to a bug in that class
Expand All @@ -32,9 +51,15 @@
end

% Initialize the ToolboxOptions from the code folder and initial metadata
toolboxFolder = fullfile(projectRootDirectory, options.SourceFolderName);
opts = matlab.addons.toolbox.ToolboxOptions(toolboxFolder, identifier, toolboxInfo);

if readme ~= "" && isprop(opts, "Readme")
opts.Readme = readmeFile;
end
if packageName ~= "" && isprop(opts, "PackageName")
opts.PackageName = packageName;
end

% Set the toolbox version
opts.ToolboxVersion = formatToolboxVersion(versionNumber);

Expand Down Expand Up @@ -116,6 +141,53 @@
end
end

function [toolboxInfo, value] = takeOptionalField(toolboxInfo, fieldName)
% takeOptionalField - Remove a field from the toolbox info and return its value
%
% The value is "" when the field is absent or empty.

value = "";
if isfield(toolboxInfo, fieldName)
if ~isempty(toolboxInfo.(fieldName))
value = string(toolboxInfo.(fieldName));
end
toolboxInfo = rmfield(toolboxInfo, fieldName);
end
if ~isscalar(value)
error("MatBox:Package:InvalidToolboxInfoField", ...
'"%s" in MLToolboxInfo.json must be a single text value.', fieldName)
end
end

function readmeFile = resolveReadmeFile(toolboxFolder, readme)
% resolveReadmeFile - Get the full path of the readme named in MLToolboxInfo.json
%
% The package contains only the files below the source folder, so the
% readme is given relative to that folder. A project root file such as
% README.md is copied there by listing it in RootFilesToPackage.

isInsideSourceFolder = ~any(split(readme, ["/", "\"]) == "..");
readmeFile = fullfile(toolboxFolder, readme);
if ~isInsideSourceFolder || ~isfile(readmeFile)
error("MatBox:Package:ReadmeNotFound", ...
['The file "%s" given as "Readme" in MLToolboxInfo.json is not in ', ...
'the source folder "%s". Give its path relative to the source ', ...
'folder, or list a project root file in "RootFilesToPackage" to ', ...
'copy it there.'], readme, toolboxFolder)
end
end

function mustBeValidPackageName(packageName)
% mustBeValidPackageName - Check the package name, which must be a MATLAB identifier
if ~isvarname(packageName)
error("MatBox:Package:InvalidPackageName", ...
['"PackageName" in MLToolboxInfo.json is "%s", which is not a ', ...
'valid MATLAB identifier. Use a letter followed by letters, ', ...
'digits or underscores, at most %d characters.'], ...
packageName, namelengthmax)
end
end

function opts = addRequirementsToToolboxOptions(opts, requirements)
% Add GitHub repository requirements as Additional Software
isGithubRequirement = strcmp({requirements.Type}, 'GitHub');
Expand Down
7 changes: 4 additions & 3 deletions docs/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@ The reusable workflow chains these jobs:
`matbox.tasks.packageToolbox` and creates a draft GitHub release.
Project root files declared in the top-level `RootFilesToPackage`
list in `MLToolboxInfo.json` (by default `LICENSE`) are included in
the packaged toolbox. The `.mltbx` file is named
`<ToolboxName>_v<version>.mltbx`, or by the top-level `MltbxFileName`
in `MLToolboxInfo.json` when that field is set
the packaged toolbox. On R2026b, `Readme` and `PackageName` in its
`ToolboxOptions` set the package readme and package name. The `.mltbx`
file is named `<ToolboxName>_v<version>.mltbx`, or by the top-level
`MltbxFileName` in `MLToolboxInfo.json` when that field is set
5. **Verify installation** — installs the packaged toolbox and confirms it
loads

Expand Down
86 changes: 86 additions & 0 deletions tools/tests/+matboxtools/+unittest/TasksTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,55 @@ function testMltbxFileNameWithFolderErrors(testCase)
"MatBox:Package:InvalidMltbxFileName")
end

function testPackageToolboxWithReadmeAndPackageName(testCase)
% Releases before R2026b have neither property, and must still
% package the readme as an ordinary file.
archiveFolder = packageWithReadmeAndPackageName();

packagedReadmeFile = fullfile(archiveFolder, "fsroot", "README.md");
testCase.verifyTrue(isfile(packagedReadmeFile))
testCase.verifyEqual(fileread(packagedReadmeFile), ...
fileread(fullfile(pwd, "README.md")))
testCase.verifyFalse(isfile(fullfile(pwd, "code", "README.md")))
end

function testPackageToolboxWritesReadmeAndPackageNameToMpackage(testCase)
testCase.assumeFalse(isMATLABReleaseOlderThan("R2026b"), ...
"ToolboxOptions has Readme and PackageName only from R2026b.")

archiveFolder = packageWithReadmeAndPackageName();

mpackage = jsondecode(fileread( ...
fullfile(archiveFolder, "fsroot", "resources", "mpackage.json")));
testCase.verifyEqual(string(mpackage.readme), "README.md")
testCase.verifyEqual(string(mpackage.name), "MatBoxTest")
end

function testCreateToolboxOptionsReadmeNotInSourceFolder(testCase)
copyfile(matboxtools.projectdir(), pwd);

% README.md is in the project root, but not staged
setToolboxInfoFields(pwd, struct("Readme", "README.md"))
testCase.verifyError( ...
@() matbox.toolbox.createToolboxOptions(pwd, "1.0.0", "SourceFolderName", "code"), ...
"MatBox:Package:ReadmeNotFound")

% The file exists, but outside the source folder
setToolboxInfoFields(pwd, struct("Readme", "../README.md"))
testCase.verifyError( ...
@() matbox.toolbox.createToolboxOptions(pwd, "1.0.0", "SourceFolderName", "code"), ...
"MatBox:Package:ReadmeNotFound")
end

function testCreateToolboxOptionsInvalidPackageName(testCase)
copyfile(matboxtools.projectdir(), pwd);

setToolboxInfoFields(pwd, struct("PackageName", "MatBox-Test"))
testCase.verifyError( ...
@() matbox.toolbox.createToolboxOptions(pwd, "1.0.0", "SourceFolderName", "code"), ...
"MatBox:Package:InvalidPackageName")
end

function testTestedWithBadgeCountsReleaseWithSkippedTests(testCase)
% Tests filtered by an unmet assumption are reported as skipped
% rather than failed, so the release is still tested with.
Expand Down Expand Up @@ -257,3 +306,40 @@ function writeTestResultsReport(projectRootDirectory, releaseName, options)
"Expected 'tested with' badge file was not created.")
badgeInfo = jsondecode(fileread(badgeFile));
end

function archiveFolder = packageWithReadmeAndPackageName()
% packageWithReadmeAndPackageName - Package a copy of MatBox that sets Readme and PackageName
%
% The project README.md is staged through RootFilesToPackage. Returns the
% folder the packaged toolbox is unzipped to.

copyfile(matboxtools.projectdir(), pwd);
setToolboxInfoFields(pwd, struct("Readme", "README.md", "PackageName", "MatBoxTest"), ...
{'LICENSE'; 'README.md'})

[~, toolboxFile] = matbox.tasks.packageToolbox(pwd, "build", "", "SourceFolderName", "code");
archiveFolder = fullfile(pwd, "toolbox-archive");
unzip(toolboxFile, archiveFolder)
end

function setToolboxInfoFields(projectRootDirectory, toolboxOptionsFields, rootFilesToPackage)
% setToolboxInfoFields - Set fields of the ToolboxOptions in the project's MLToolboxInfo.json
%
% A non-empty rootFilesToPackage replaces the top-level RootFilesToPackage.

arguments
projectRootDirectory (1,1) string
toolboxOptionsFields (1,1) struct
rootFilesToPackage (:,1) cell = {}
end

toolboxInfoFile = fullfile(projectRootDirectory, "tools", "MLToolboxInfo.json");
toolboxInfo = jsondecode(fileread(toolboxInfoFile));
for fieldName = string(fieldnames(toolboxOptionsFields))'
toolboxInfo.ToolboxOptions.(fieldName) = toolboxOptionsFields.(fieldName);
end
if ~isempty(rootFilesToPackage)
toolboxInfo.RootFilesToPackage = rootFilesToPackage;
end
matbox.utility.filewrite(toolboxInfoFile, jsonencode(toolboxInfo, "PrettyPrint", true));
end