Skip to content

Commit 76f15af

Browse files
committed
generalize
1 parent f88e24a commit 76f15af

3 files changed

Lines changed: 74 additions & 54 deletions

File tree

+stdlib/matlab_git_version.m

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%% MATLAB_GIT_VERSION version of the Git2 library used by Matlab
2-
% general example of access shared library version info from Matlab
3-
% using loadlibrary() to call a function in a shared library without a MEX file.
2+
% version('-modules') doesn't reveal the version of Git2, so this function uses
3+
% loadlibrary() to call a function in the Git2 library without a MEX file.
44
%
55
% Input:
66
% libPath - Full path to the library (optional)
@@ -10,54 +10,13 @@
1010

1111
function v = matlab_git_version(libPath)
1212
arguments
13-
libPath {mustBeTextScalar, mustBeFile} = getMatlabGit2libPath()
13+
libPath {mustBeTextScalar} = getMatlabGit2libPath()
1414
end
1515

16-
if ispc()
17-
% Windows .NET
18-
fileInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(libPath);
19-
v = char(fileInfo.FileVersion);
20-
else
21-
% elseif ismac()
22-
% cmd = ['otool -L "' char(libPath) '"'];
23-
% pat = '(?<=current version\s+)[0-9][0-9.]+';
24-
% v = shell_regex(cmd, pat);
16+
v = library_version(libPath, fullfile(fileparts(mfilename("fullpath")), 'private/git2dummy.h'), 'git2');
2517

26-
cwd = fileparts(mfilename("fullpath"));
27-
hdr = fullfile(cwd, 'private/git2dummy.h');
28-
29-
if ~libisloaded('git2')
30-
if isfile(libPath)
31-
loadlibrary(libPath, hdr, alias='git2');
32-
else
33-
v = missing;
34-
return
35-
end
36-
end
37-
38-
maj = libpointer('int32Ptr', 0);
39-
min = libpointer('int32Ptr', 0);
40-
rev = libpointer('int32Ptr', 0);
41-
42-
calllib('git2', 'git_libgit2_version', maj, min, rev);
43-
v = sprintf('%d.%d.%d', maj.Value, min.Value, rev.Value);
4418
end
4519

46-
47-
end
48-
49-
% function v = shell_regex(cmd, pat)
50-
% [status, o] = system(cmd);
51-
% assert(status == 0, 'Failed to get library version from %s\nOutput: %s', cmd, o);
52-
%
53-
% v = regexp(o, pat, 'match', 'once');
54-
% if iscell(v)
55-
% v = v{1};
56-
% end
57-
% v = char(strtrim(v));
58-
% end
59-
60-
6120
function p = getMatlabGit2libPath()
6221
archDir = fullfile(matlabroot, 'bin', computer('arch'));
6322

+stdlib/private/library_version.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
%% LIBRARY_VERSION get version of shared library
2+
% general example of access shared library version info from Matlab
3+
% using loadlibrary() to call a function in a shared library without a MEX file.
4+
%
5+
% Input:
6+
% libPath - Full path to the library (optional)
7+
%
8+
% Output:
9+
% v - version string (e.g., '1.9.0')
10+
11+
function v = library_version(libPath, header, name)
12+
arguments
13+
libPath {mustBeTextScalar, mustBeFile}
14+
header {mustBeTextScalar, mustBeFile}
15+
name {mustBeTextScalar}
16+
end
17+
18+
19+
if ispc()
20+
% Windows .NET
21+
fileInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(libPath);
22+
v = char(fileInfo.FileVersion);
23+
else
24+
% elseif ismac()
25+
% cmd = ['otool -L "' char(libPath) '"'];
26+
% pat = '(?<=current version\s+)[0-9][0-9.]+';
27+
% v = shell_regex(cmd, pat);
28+
29+
if ~libisloaded(name)
30+
if isfile(libPath)
31+
loadlibrary(libPath, header, alias=name);
32+
else
33+
v = missing;
34+
return
35+
end
36+
end
37+
38+
switch name
39+
case 'git2'
40+
fun = 'git_libgit2_version';
41+
otherwise
42+
error('stdlib:library_version', 'Unknown library name: %s', name)
43+
end
44+
45+
maj = libpointer('int32Ptr', 0);
46+
min = libpointer('int32Ptr', 0);
47+
rev = libpointer('int32Ptr', 0);
48+
49+
calllib(name, fun, maj, min, rev);
50+
v = sprintf('%d.%d.%d', maj.Value, min.Value, rev.Value);
51+
52+
unloadlibrary(name)
53+
end
54+
55+
56+
end

scripts/installDotNet.m

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
else
1616

1717
if ~strlength(installDir)
18-
if ispc()
19-
installDir = getenv('USERPROFILE');
20-
else
21-
installDir = getenv('HOME');
22-
end
23-
installDir = fullfile(installDir, '.dotnet');
18+
installDir = default_installdir();
2419
end
2520
if ~isfolder(installDir)
2621
mkdir(installDir);
@@ -32,9 +27,7 @@
3227

3328
cmd = sprintf('sh -c "%s --install-dir %s"', scr, installDir);
3429

35-
if isunix()
36-
setPermissions(filePermissions(scr), "UserExecute", true);
37-
end
30+
setPermissions(filePermissions(scr), "UserExecute", true);
3831

3932
end
4033

@@ -64,3 +57,15 @@
6457
dotnetenv()
6558

6659
end
60+
61+
62+
function d = default_installdir()
63+
64+
if ispc()
65+
h = getenv('USERPROFILE');
66+
else
67+
h = getenv('HOME');
68+
end
69+
d = fullfile(h, '.dotnet');
70+
71+
end

0 commit comments

Comments
 (0)