Skip to content

Commit

Permalink
Add platform info utilities to @actions/core (#1551)
Browse files Browse the repository at this point in the history
* Introduce platform utilities into @actions/core

* Add tests for the platform helper

* Update README.md

* Update README.md with more details
  • Loading branch information
nikolai-laevskii authored Nov 14, 2023
1 parent fe3e7ce commit 20f826b
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,28 @@ toPlatformPath('/foo/bar') // => \foo\bar
// On a Linux runner.
toPlatformPath('\\foo\\bar') // => /foo/bar
```

#### Platform helper

Provides shorthands for getting information about platform action is running on.

```js
import { platform } from '@actions/core'
/* equals to a call of os.platform() */
platform.platform // 'win32' | 'darwin' | 'linux' | 'freebsd' | 'openbsd' | 'android' | 'cygwin' | 'sunos'
/* equals to a call of os.arch() */
platform.arch // 'x64' | 'arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 'riscv64' | 's390' | 's390x'
/* common shorthands for platform-specific logic */
platform.isWindows // true
platform.isMacOS // false
platform.isLinux // false
/* run platform-specific script to get more details about the exact platform, works on Windows, MacOS and Linux */
const {
name, // Microsoft Windows 11 Enterprise
version, // 10.0.22621
} = await platform.getDetails()
```
29 changes: 29 additions & 0 deletions packages/core/__tests__/platform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os from 'os'
import {platform} from '../src/core'

describe('getInfo', () => {
it('returns the platform info', async () => {
const info = await platform.getDetails()
expect(info).toEqual({
name: expect.any(String),
platform: expect.any(String),
arch: expect.any(String),
version: expect.any(String),
isWindows: expect.any(Boolean),
isMacOS: expect.any(Boolean),
isLinux: expect.any(Boolean)
})
})

it('returns the platform info with the correct name', async () => {
const isWindows = os.platform() === 'win32'
const isMacOS = os.platform() === 'darwin'
const isLinux = os.platform() === 'linux'

const info = await platform.getDetails()
expect(info.platform).toEqual(os.platform())
expect(info.isWindows).toEqual(isWindows)
expect(info.isMacOS).toEqual(isMacOS)
expect(info.isLinux).toEqual(isLinux)
})
})
27 changes: 27 additions & 0 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"url": "https://github.com/actions/toolkit/issues"
},
"dependencies": {
"@actions/exec": "^1.1.1",
"@actions/http-client": "^2.0.1",
"uuid": "^8.3.2"
},
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,8 @@ export {markdownSummary} from './summary'
* Path exports
*/
export {toPosixPath, toWin32Path, toPlatformPath} from './path-utils'

/**
* Platform utilities exports
*/
export * as platform from './platform'
87 changes: 87 additions & 0 deletions packages/core/src/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os from 'os'
import * as exec from '@actions/exec'

const getWindowsInfo = async (): Promise<{name: string; version: string}> => {
const {stdout: version} = await exec.getExecOutput(
'powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Version"',
undefined,
{
silent: true
}
)

const {stdout: name} = await exec.getExecOutput(
'powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Caption"',
undefined,
{
silent: true
}
)

return {
name: name.trim(),
version: version.trim()
}
}

const getMacOsInfo = async (): Promise<{
name: string
version: string
}> => {
const {stdout} = await exec.getExecOutput('sw_vers', undefined, {
silent: true
})

const version = stdout.match(/ProductVersion:\s*(.+)/)?.[1] ?? ''
const name = stdout.match(/ProductName:\s*(.+)/)?.[1] ?? ''

return {
name,
version
}
}

const getLinuxInfo = async (): Promise<{
name: string
version: string
}> => {
const {stdout} = await exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], {
silent: true
})

const [name, version] = stdout.trim().split('\n')

return {
name,
version
}
}

export const platform = os.platform()
export const arch = os.arch()
export const isWindows = platform === 'win32'
export const isMacOS = platform === 'darwin'
export const isLinux = platform === 'linux'

export async function getDetails(): Promise<{
name: string
platform: string
arch: string
version: string
isWindows: boolean
isMacOS: boolean
isLinux: boolean
}> {
return {
...(await (isWindows
? getWindowsInfo()
: isMacOS
? getMacOsInfo()
: getLinuxInfo())),
platform,
arch,
isWindows,
isMacOS,
isLinux
}
}

0 comments on commit 20f826b

Please sign in to comment.