Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 52ee36a

Browse files
Merge pull request #1 from guzman-raphael/master
Add flexible version resolution and remove project file in package operation
2 parents d0ee58c + eac50f8 commit 52ee36a

File tree

7 files changed

+49
-10
lines changed

7 files changed

+49
-10
lines changed

+ghtb/install.m

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ function install(target, varargin)
66
% force the install.
77
% Inputs:
88
% target[required]: (string) Toolbox repo (e.g. 'org1/repo1') or local path to *.mltbx
9-
% version[optional, default='latest']: (string) Version to be installed e.g. '1.0.0'
9+
% version[optional, default='latest']: (string||handle) Version to be installed e.g.
10+
% '1.0.0' or version handle of the form @(v) that
11+
% resolves to acceptable versions. Handle approach
12+
% should expect v to be a cell array of version
13+
% candidates and return a logical array of result.
1014
% override[optional, default=false]: (boolean) Flag to indicate if should override an
1115
% existing install
1216
% Assumptions:
@@ -20,6 +24,8 @@ function install(target, varargin)
2024
% ghtb.install('guzman-raphael/compareVersions')
2125
% ghtb.install('guzman-raphael/compareVersions', 'version', '1.0.7')
2226
% ghtb.install('guzman-raphael/compareVersions', 'version', '1.0.6', 'override', true)
27+
% ghtb.install('guzman-raphael/compareVersions', 'version', ...
28+
% @(v) cellfun(@(x) contains(x, '1.0.'), v, 'uni', true))
2329
% ghtb.install('compareVersions.mltbx') %install from local toolbox file
2430
clear('functions'); %needed for uninstall of mex-based toolboxes
2531
s = settings;
@@ -38,15 +44,22 @@ function install(target, varargin)
3844
'application/vnd.github.v3.raw'}, ...
3945
'ContentType', 'json', ...
4046
'Timeout', 60);
41-
if strcmp(version, 'latest')
47+
if ~isa(version,'function_handle') && strcmp(version, 'latest')
4248
url = [GitHubAPI '/repos/' target '/releases/latest'];
4349
data = webread(url, options);
4450
else
4551
url = [GitHubAPI '/repos/' target '/releases'];
4652
data = webread(url, options);
47-
% need to optimize to break out on first match
48-
data = data(arrayfun(@(x) strcmp(x.tag_name, version), data, ...
49-
'UniformOutput', true));
53+
[~,index] = sort({data.published_at});
54+
data = data(flip(index).*~[data.prerelease]);
55+
if isa(version,'function_handle')
56+
index = version({data.tag_name});
57+
else
58+
index = find(ismember({data.tag_name}, version));
59+
end
60+
% assumes descending order
61+
data = data(index);
62+
data = data(1);
5063
end
5164
if length(data.assets) == 0
5265
error('GHToolbox:Release:NotFound', ...

+ghtb/package.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,5 @@ function package(toolboxName, toolboxAuthor, toolboxContact, toolboxSummary, ...
112112
% build ToolBox
113113
matlab.addons.toolbox.packageToolbox([toolboxProjectDir '/package.prj'], ...
114114
[toolboxProjectDir '/' toolboxName]);
115+
delete([toolboxProjectDir '/package.prj']);
115116
end

+ghtb/require.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ function require(requiredToolboxes, varargin)
99
% resolved with installation. Each cell should contain a
1010
% struct with fields: Name[required, string],
1111
% ResolveTarget[required, string],
12-
% Version[optional, string]. Version specified 'pins' it
13-
% i.e. version equals operation. See below for examples.
12+
% Version[optional, string||handle]. For Version
13+
% specification, see below for examples or
14+
% see help(ghtb.install) for accepted Version assignment.
1415
% prompt[optional, default=true]: (boolean) Whether to silently install or use prompts.
1516
% resolveGHToolboxDeps[optional, default=true]: (boolean) Resolve dependencies related
1617
% to GHToolbox.
@@ -24,6 +25,11 @@ function require(requiredToolboxes, varargin)
2425
% 'Name', 'compareVersions', ...
2526
% 'ResolveTarget', 'guzman-raphael/compareVersions', ...
2627
% 'Version', '1.0.8'...
28+
% ), ...
29+
% struct(...
30+
% 'Name', 'mym', ...
31+
% 'ResolveTarget', 'datajoint/mym', ...
32+
% 'Version', @(v) cellfun(@(x) contains(x, '2.7.'), v, 'uni', true)...
2733
% )...
2834
% };
2935
% ghtb.require(requiredToolboxes) % require with prompts
@@ -54,15 +60,15 @@ function require(requiredToolboxes, varargin)
5460
end
5561
installPromptMsg = {
5662
'Toolbox ''%s'' did not meet the minimum requirements.'
57-
'Would you like to proceed with an upgrade?'
63+
'Would you like to proceed with the install?'
5864
};
5965
for tb = requiredToolboxes
6066
matched = toolboxes(strcmp(tb{1}.Name, {toolboxes.Name}));
6167
if ~isfield(tb{1}, 'Version') && any(arrayfun(@(x) strcmp(x.Name, tb{1}.Name), ...
6268
matched, 'uni', true))
6369
% toolbox found
6470
elseif isfield(tb{1}, 'Version') && any(arrayfun(@(x) strcmp(x.Name, tb{1}.Name) && ...
65-
compareVersions({x.Version}, tb{1}.Version), matched, 'uni', true))
71+
tb{1}.Version({x.Version}), matched, 'uni', true))
6672
% toolbox found with appropriate version
6773
elseif ~isfield(tb{1}, 'Version') && ~isempty(tb{1}.ResolveTarget) && ...
6874
(~prompt || strcmpi('yes', ...

+ghtb/version.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
% https://www.github.com/datajoint/GHToolbox.git
1212
% License:
1313
% MIT (use/copy/change/redistribute at your own risk)
14-
v = '1.0.15';
14+
v = '1.0.16';
1515
if nargout
1616
varargout{1}=v;
1717
else

GHToolbox.mltbx

432 Bytes
Binary file not shown.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Here are some examples on how to invoke it once installed or saved to path:
2222
ghtb.install('guzman-raphael/compareVersions') % default: version='latest' and override=false
2323
ghtb.install('guzman-raphael/compareVersions', 'version', '1.0.7') % default: override=false
2424
ghtb.install('guzman-raphael/compareVersions', 'version', '1.0.6', 'override', true)
25+
ghtb.install('guzman-raphael/compareVersions', 'version', ...
26+
@(v) cellfun(@(x) contains(x, '1.0.'), v, 'uni', true))
2527
ghtb.install('compareVersions.mltbx') %install from local toolbox file
2628
ghtb.uninstall('compareVersions') % uninstalls all versions of Toolbox
2729
requiredToolboxes = {...
@@ -33,6 +35,11 @@ requiredToolboxes = {...
3335
'Name', 'compareVersions', ...
3436
'ResolveTarget', 'guzman-raphael/compareVersions', ...
3537
'Version', '1.0.8'...
38+
), ...
39+
struct(...
40+
'Name', 'mym', ...
41+
'ResolveTarget', 'datajoint/mym', ...
42+
'Version', @(v) cellfun(@(x) contains(x, '2.7.'), v, 'uni', true)...
3643
)...
3744
};
3845
ghtb.require(requiredToolboxes) % require with prompts

tests/TestManage.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ function TestManage_testOverride(testCase)
2020
'UniformOutput', true)) < latest_ver);
2121
ghtb.uninstall('compareVersions');
2222
end
23+
function TestManage_testVersionHandle(testCase)
24+
st = dbstack;
25+
disp(['---------------' st(1).name '---------------']);
26+
version_handle = @(v) cellfun(@(x) strcmp(x, '1.0.9'), v, 'uni', true);
27+
ghtb.install(...
28+
'guzman-raphael/compareVersions', 'version', version_handle, 'override', true);
29+
toolboxes = matlab.addons.toolbox.installedToolboxes;
30+
tb_version = toolboxes(strcmp('compareVersions', {toolboxes.Name})).Version;
31+
testCase.verifyTrue(compareVersions({tb_version}, '1.0.9', ...
32+
@(curr_v,ref_v) curr_v==ref_v));
33+
ghtb.uninstall('compareVersions');
34+
end
2335
function TestManage_initialize(testCase)
2436
st = dbstack;
2537
disp(['---------------' st(1).name '---------------']);

0 commit comments

Comments
 (0)