Operating System
Many plugin developers are familiar with the OperatingSystem internal API in Gradle. Unfortunately, this remains an internal API and is subject to change.
Grolifant offers a similar public API with a small number of API differences:
-
No
getFamilyNameandgetNativePrefixmethods. (A scan of the Gradle3.2.1codebase seem to yield no usage either). -
No public static fields called
WINDOWS,MACOSXetc. These are now a static field calledINSTANCEon each of the specific operating system implementations. -
getSharedLibrarySuffixandgetSharedLibraryNamehave been added. -
Support for NetBSD.
-
getHomeVarto get the name of the environment variable for home directories.
Example
OperatingSystem os = OperatingSystem.current() (1)
File findExe = os.findInPath('bash')
File findExe = os.findInPath('bash')
| 1 | Use current() to the operating system the code is being executed upon. |
Operating system detection
The logic in 5.8.1 to determine an operating system is
if (OS_NAME.contains('windows')) {
DefaultWindows.INSTANCE
} else if (OS_NAME.contains('mac os x') || OS_NAME.contains('darwin') || OS_NAME.contains('osx')) {
DefaultMacOsX.INSTANCE
} else if (OS_NAME.contains('linux')) {
DefaultLinux.INSTANCE
} else if (OS_NAME.contains('freebsd')) {
DefaultFreeBSD.INSTANCE
} else if (OS_NAME.contains('sunos') || OS_NAME.contains('solaris')) {
DefaultSolaris.INSTANCE
} else if (OS_NAME.contains('netbsd')) {
DefaultNetBSD.INSTANCE
} else {
// Not strictly true, but a good guess
DefaultGenericUnix.INSTANCE
}