diff --git a/.github/badges/tests.svg b/.github/badges/tests.svg index dc56878..0261c9f 100644 --- a/.github/badges/tests.svg +++ b/.github/badges/tests.svg @@ -1 +1 @@ -tests: 107 passedtests107 passed +tests: 110 passedtests110 passed diff --git a/README.md b/README.md index 7ede99d..8ce4086 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,14 @@ By default, a `LICENSE` file in the project root is included in the packaged too "RootFilesToPackage": ["LICENSE", "THIRD_PARTY_NOTICES.md"] ``` +The packaged toolbox is saved as `releases/_v.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 +"MltbxFileName": "MatBox" +``` + +A fixed file name gives the latest release a stable download link, `https://github.com///releases/latest/download/.mltbx`. + ## Project-Specific Task Wrappers Projects can keep their own CI entry points in `tools/tasks`. These wrapper functions are useful when a project needs non-default source folders, test folders, tag filters, report settings, or packaging options. diff --git a/code/+matbox/+tasks/packageToolbox.m b/code/+matbox/+tasks/packageToolbox.m index 04d1242..b58645f 100644 --- a/code/+matbox/+tasks/packageToolbox.m +++ b/code/+matbox/+tasks/packageToolbox.m @@ -17,6 +17,10 @@ % specify a top-level "RootFilesToPackage" list in MLToolboxInfo.json, % e.g. "RootFilesToPackage": ["LICENSE", "THIRD_PARTY_NOTICES.md"]. % +% The MLTBX file is named _v.mltbx. To use a fixed +% file name for every release, specify a top-level "MltbxFileName" in +% MLToolboxInfo.json, e.g. "MltbxFileName": "MatBox". +% % Adapted from: https://github.com/mathworks/climatedatastore/blob/main/buildUtilities/packageToolbox.m % See the THIRD_PARTY_NOTICES.md file distributed with MatBox for license terms. diff --git a/code/+matbox/+toolbox/createToolboxOptions.m b/code/+matbox/+toolbox/createToolboxOptions.m index db8420c..c229ba2 100644 --- a/code/+matbox/+toolbox/createToolboxOptions.m +++ b/code/+matbox/+toolbox/createToolboxOptions.m @@ -14,7 +14,8 @@ end % Read the toolbox info from MLToolboxInfo.json - [toolboxInfo, identifier] = matbox.toolbox.readToolboxInfo(projectRootDirectory); + [toolboxInfo, identifier, toolboxInfoFileContent] = ... + matbox.toolbox.readToolboxInfo(projectRootDirectory); % 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 @@ -67,15 +68,39 @@ end end - % Specify name for output .mltbx file. Every version separator, including - % the "+" before a build number, becomes an underscore. - versionNumber = replace(opts.ToolboxVersion, [".", "+"], "_"); - outputFileName = sprintf('%s_v%s.mltbx', MLTBX_NAME, versionNumber); + outputFileName = resolveOutputFileName(toolboxInfoFileContent, ... + MLTBX_NAME, opts.ToolboxVersion); opts.OutputFile = fullfile(projectRootDirectory, "releases", outputFileName); toolboxOptions = opts; end +function outputFileName = resolveOutputFileName(toolboxInfoFileContent, mltbxName, toolboxVersion) +% resolveOutputFileName - Get the file name of the packaged .mltbx file +% +% A non-empty top-level "MltbxFileName" field in MLToolboxInfo.json is used +% as the file name, with ".mltbx" appended if it is missing. Otherwise the +% name is _v.mltbx, where every version separator, +% including the "+" before a build number, becomes an underscore. + + if isfield(toolboxInfoFileContent, 'MltbxFileName') ... + && ~isempty(toolboxInfoFileContent.MltbxFileName) + outputFileName = string(toolboxInfoFileContent.MltbxFileName); + if ~isscalar(outputFileName) || contains(outputFileName, ["/", "\"]) + error('MatBox:Package:InvalidMltbxFileName', ... + ['The "MltbxFileName" field in MLToolboxInfo.json must be a ', ... + 'single file name without folders. The packaged toolbox is ', ... + 'always saved in the "releases" folder.']) + end + if ~endsWith(outputFileName, ".mltbx", "IgnoreCase", true) + outputFileName = outputFileName + ".mltbx"; + end + else + versionNumber = replace(toolboxVersion, [".", "+"], "_"); + outputFileName = sprintf("%s_v%s.mltbx", mltbxName, versionNumber); + end +end + function toolboxVersion = formatToolboxVersion(versionNumber) % formatToolboxVersion - Write a version number in the form ToolboxOptions accepts % diff --git a/docs/ci.md b/docs/ci.md index 8809f93..b6a93a5 100644 --- a/docs/ci.md +++ b/docs/ci.md @@ -216,7 +216,9 @@ 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 packaged toolbox. The `.mltbx` file is named + `_v.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 diff --git a/tools/tests/+matboxtools/+unittest/TasksTest.m b/tools/tests/+matboxtools/+unittest/TasksTest.m index d0d795b..8a609bc 100644 --- a/tools/tests/+matboxtools/+unittest/TasksTest.m +++ b/tools/tests/+matboxtools/+unittest/TasksTest.m @@ -140,6 +140,38 @@ function testPackageToolboxShadowedRootFile(testCase) testCase.verifyTrue(isfile(fullfile(pwd, 'code', 'LICENSE'))) end + function testMltbxFileNameDefaultsToVersionedName(testCase) + copyfile(matboxtools.projectdir(), pwd); + + toolboxOptions = matbox.toolbox.createToolboxOptions(pwd, "1.2.3", ... + "SourceFolderName", "code"); + [~, fileName, fileExtension] = fileparts(toolboxOptions.OutputFile); + testCase.verifyEqual(string(fileName) + fileExtension, "MatBox_v1_2_3.mltbx") + end + + function testMltbxFileNameFromToolboxInfo(testCase) + copyfile(matboxtools.projectdir(), pwd); + + fileNames = ["MatBox", "MatBox.mltbx"]; + for iFileName = fileNames + setMltbxFileName(pwd, iFileName) + toolboxOptions = matbox.toolbox.createToolboxOptions(pwd, "1.2.3", ... + "SourceFolderName", "code", "ToolboxShortName", "ShortName"); + testCase.verifyEqual(string(toolboxOptions.OutputFile), ... + fullfile(pwd, "releases", "MatBox.mltbx"), ... + sprintf('Unexpected output file for MltbxFileName "%s".', iFileName)) + end + end + + function testMltbxFileNameWithFolderErrors(testCase) + copyfile(matboxtools.projectdir(), pwd); + + setMltbxFileName(pwd, "subfolder/MatBox") + testCase.verifyError(... + @() matbox.toolbox.createToolboxOptions(pwd, "1.2.3", "SourceFolderName", "code"), ... + "MatBox:Package:InvalidMltbxFileName") + end + function testTestedWithBadgeCountsReleaseWithSkippedTests(testCase) % Tests filtered by an unmet assumption are reported as skipped % rather than failed, so the release is still tested with. @@ -179,6 +211,13 @@ function testTestedWithBadgeErrorsWhenNoReleasePassed(testCase) end end +function setMltbxFileName(projectRootDirectory, mltbxFileName) + toolboxInfoFile = fullfile(projectRootDirectory, 'tools', 'MLToolboxInfo.json'); + toolboxInfo = jsondecode(fileread(toolboxInfoFile)); + toolboxInfo.MltbxFileName = mltbxFileName; + matbox.utility.filewrite(toolboxInfoFile, jsonencode(toolboxInfo, "PrettyPrint", true)); +end + function writeTestResultsReport(projectRootDirectory, releaseName, options) % writeTestResultsReport - Write a minimal JUnit-style report for one release %