Skip to content

Commit 49c8f68

Browse files
committed
Add support for SunOS-like platforms
1 parent 3c984bb commit 49c8f68

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

src/Cli/Microsoft.DotNet.Cli.Utils/FileNameSuffixes.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,11 @@ public static PlatformFileNameSuffixes CurrentPlatform
2424
{
2525
return OSX;
2626
}
27-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
28-
{
29-
return Linux;
30-
}
31-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
32-
{
33-
return FreeBSD;
34-
}
3527
else
3628
{
37-
throw new InvalidOperationException("Unknown Platform");
29+
// assume everything else is Unix to avoid modifying this file
30+
// everytime a new platform is introduced in runtime.
31+
return Unix;
3832
}
3933
}
4034
}
@@ -58,21 +52,19 @@ public static PlatformFileNameSuffixes CurrentPlatform
5852
public static PlatformFileNameSuffixes OSX { get; } = new PlatformFileNameSuffixes
5953
{
6054
DynamicLib = ".dylib",
61-
Exe = "",
55+
Exe = string.Empty,
6256
ProgramDatabase = ".pdb",
6357
StaticLib = ".a"
6458
};
6559

66-
public static PlatformFileNameSuffixes Linux { get; } = new PlatformFileNameSuffixes
60+
public static PlatformFileNameSuffixes Unix { get; } = new PlatformFileNameSuffixes
6761
{
6862
DynamicLib = ".so",
69-
Exe = "",
63+
Exe = string.Empty,
7064
ProgramDatabase = ".pdb",
7165
StaticLib = ".a"
7266
};
7367

74-
public static PlatformFileNameSuffixes FreeBSD { get; } = Linux;
75-
7668
public struct PlatformFileNameSuffixes
7769
{
7870
public string DynamicLib { get; internal set; }

src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ internal enum Platform
1313
Windows = 1,
1414
Linux = 2,
1515
Darwin = 3,
16-
FreeBSD = 4
16+
FreeBSD = 4,
17+
illumos = 5,
18+
Solaris = 6
1719
}
1820

1921
internal static class RuntimeEnvironment
@@ -36,15 +38,19 @@ private static string GetOSName()
3638
switch (GetOSPlatform())
3739
{
3840
case Platform.Windows:
39-
return "Windows";
41+
return nameof(Platform.Windows);
4042
case Platform.Linux:
41-
return GetDistroId() ?? "Linux";
43+
return GetDistroId() ?? nameof(Platform.Linux);
4244
case Platform.Darwin:
4345
return "Mac OS X";
4446
case Platform.FreeBSD:
45-
return "FreeBSD";
47+
return nameof(Platform.FreeBSD);
48+
case Platform.illumos:
49+
return GetDistroId() ?? nameof(Platform.illumos);
50+
case Platform.Solaris:
51+
return nameof(Platform.Solaris);
4652
default:
47-
return "Unknown";
53+
return nameof(Platform.Unknown);
4854
}
4955
}
5056

@@ -55,11 +61,16 @@ private static string GetOSVersion()
5561
case Platform.Windows:
5662
return Environment.OSVersion.Version.ToString(3);
5763
case Platform.Linux:
64+
case Platform.illumos:
5865
return GetDistroVersionId() ?? string.Empty;
5966
case Platform.Darwin:
6067
return Environment.OSVersion.Version.ToString(2);
6168
case Platform.FreeBSD:
6269
return GetFreeBSDVersion() ?? string.Empty;
70+
case Platform.Solaris:
71+
// RuntimeInformation.OSDescription example on Solaris 11.3: SunOS 5.11 11.3
72+
// we only need the major version; 11
73+
return RuntimeInformation.OSDescription.Split(' ')[2].Split('.')[0];
6374
default:
6475
return string.Empty;
6576
}
@@ -98,6 +109,19 @@ private static string GetDistroVersionId()
98109
}
99110

100111
private static DistroInfo LoadDistroInfo()
112+
{
113+
switch (GetOSPlatform())
114+
{
115+
case Platform.Linux:
116+
return LoadDistroInfoFromLinux();
117+
case Platform.illumos:
118+
return LoadDistroInfoFromIllumos();
119+
}
120+
121+
return null;
122+
}
123+
124+
private static DistroInfo LoadDistroInfoFromLinux()
101125
{
102126
DistroInfo result = null;
103127

@@ -138,6 +162,36 @@ private static DistroInfo LoadDistroInfo()
138162
return result;
139163
}
140164

165+
private static DistroInfo LoadDistroInfoFromIllumos()
166+
{
167+
DistroInfo result = null;
168+
// examples:
169+
// on OmniOS
170+
// SunOS 5.11 omnios-r151018-95eaa7e
171+
// on OpenIndiana Hipster:
172+
// SunOS 5.11 illumos-63878f749f
173+
// on SmartOS:
174+
// SunOS 5.11 joyent_20200408T231825Z
175+
var versionDescription = RuntimeInformation.OSDescription.Split(' ')[2];
176+
switch (versionDescription)
177+
{
178+
case string version when version.StartsWith("omnios"):
179+
result.Id = "OmniOS";
180+
result.VersionId = version.Substring("omnios-r".Length, 2); // e.g. 15
181+
break;
182+
case string version when version.StartsWith("joyent"):
183+
result.Id = "SmartOS";
184+
result.VersionId = version.Substring("joyent_".Length, 4); // e.g. 2020
185+
break;
186+
case string version when version.StartsWith("illumos"):
187+
result.Id = "OpenIndiana";
188+
// version-less
189+
break;
190+
}
191+
192+
return result;
193+
}
194+
141195
// For some distros, we don't want to use the full version from VERSION_ID. One example is
142196
// Red Hat Enterprise Linux, which includes a minor version in their VERSION_ID but minor
143197
// versions are backwards compatable.
@@ -179,10 +233,20 @@ private static Platform DetermineOSPlatform()
179233
{
180234
return Platform.Darwin;
181235
}
182-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
236+
#if NETCOREAPP
237+
if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
183238
{
184239
return Platform.FreeBSD;
185240
}
241+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("ILLUMOS")))
242+
{
243+
return Platform.illumos;
244+
}
245+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("SOLARIS")))
246+
{
247+
return Platform.Solaris;
248+
}
249+
#endif
186250

187251
return Platform.Unknown;
188252
}

0 commit comments

Comments
 (0)