Skip to content

Commit bb14b95

Browse files
committed
os_version: choose_method
1 parent b20e500 commit bb14b95

File tree

7 files changed

+69
-17
lines changed

7 files changed

+69
-17
lines changed

+stdlib/+dotnet/os_version.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function [os, version] = os_version()
2+
3+
v = System.Environment.OSVersion.VersionString;
4+
% https://learn.microsoft.com/en-us/dotnet/api/system.operatingsystem.versionstring
5+
vs = split(string(v), ' ');
6+
version = vs(end);
7+
os = join(vs(1:end-1));
8+
9+
end

+stdlib/+java/os_version.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function [os, version] = os_version()
2+
3+
os = javaMethod("getProperty", "java.lang.System", "os.name");
4+
version = javaMethod("getProperty", "java.lang.System", "os.version");
5+
os = string(os);
6+
version = string(version);
7+
8+
end

+stdlib/+python/get_owner.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
try
77
n = string(py.str(py.pathlib.Path(p).owner()));
88
catch e
9-
warning(e.identifier, "get_owner(%s) failed: %s", p, e.message);
9+
warning(e.identifier, "get_owner(%s) failed: %s", p, e.message)
1010
end
1111

1212
end

+stdlib/+python/os_version.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function [os, version] = os_version()
2+
3+
try
4+
os = string(py.platform.system());
5+
version = string(py.platform.version());
6+
catch e
7+
warning(e.identifier, 'Failed to get OS version using Python: %s', e.message)
8+
os = string.empty();
9+
version = string.empty();
10+
end
11+
12+
end

+stdlib/+sys/os_version.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function [os, version] = os_version()
2+
3+
if ispc()
4+
cmd1 = 'pwsh -c "(Get-CimInstance -ClassName Win32_OperatingSystem).Caption"';
5+
cmd2 = 'pwsh -c "(Get-CimInstance -ClassName Win32_OperatingSystem).Version"';
6+
else
7+
cmd1 = "uname -s";
8+
cmd2 = "uname -r";
9+
end
10+
11+
[s, os] = system(cmd1);
12+
if s == 0
13+
os = strtrim(os);
14+
else
15+
error("Failed to get OS name: %s", os);
16+
end
17+
18+
[s, version] = system(cmd2);
19+
if s == 0
20+
version = strtrim(version);
21+
else
22+
error("Failed to get OS version: %s", version);
23+
end
24+
25+
end

+stdlib/os_version.m

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44
% instead of Windows 10.
55
% Ref: https://bugs.openjdk.org/browse/JDK-8274840
66

7-
function [os, version] = os_version()
8-
9-
if stdlib.has_dotnet()
10-
v = System.Environment.OSVersion.VersionString;
11-
% https://learn.microsoft.com/en-us/dotnet/api/system.operatingsystem.versionstring
12-
vs = split(string(v), ' ');
13-
version = vs(end);
14-
os = join(vs(1:end-1));
15-
else
16-
os = javaMethod("getProperty", "java.lang.System", "os.name");
17-
version = javaMethod("getProperty", "java.lang.System", "os.version");
18-
os = string(os);
19-
version = string(version);
7+
function [os, version] = os_version(method)
8+
arguments
9+
method (1,:) string = ["sys", "python", "dotnet", "java"]
2010
end
2111

12+
fun = choose_method(method, "os_version");
13+
[os, version] = fun();
14+
2215
end

test/TestSys.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
cpu_arch_fun = {'java', 'dotnet', 'native'}
1111
ia_fun = {'sys', 'dotnet', 'java', 'python'}
1212
cr_method = {'sys', 'java', 'python'}
13+
os_m = {'java', 'python', 'dotnet', 'sys'}
1314
end
1415

1516
methods(TestClassSetup)
@@ -108,10 +109,14 @@ function test_has_python(tc)
108109
tc.verifyTrue(all(v >= [3, 8, 0]), "expected Python >= 3.8")
109110
end
110111

111-
function test_os_version(tc)
112-
tc.assumeTrue(stdlib.has_dotnet() || stdlib.has_java())
112+
function test_os_version(tc, os_m)
113+
try
114+
[os, ver] = stdlib.os_version(os_m);
115+
catch e
116+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
117+
return
118+
end
113119

114-
[os, ver] = stdlib.os_version();
115120
tc.verifyGreaterThan(strlength(os), 0, "expected non-empty os")
116121
tc.verifyGreaterThan(strlength(ver), 0, "expected non-empty version")
117122
end

0 commit comments

Comments
 (0)