Skip to content

Commit 152ff00

Browse files
committed
Extract utility methods to common.js
1 parent d4f9058 commit 152ff00

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed

common.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const os = require('os')
2+
const fs = require('fs')
3+
4+
export function getVirtualEnvironmentName() {
5+
const platform = os.platform()
6+
if (platform === 'linux') {
7+
return `ubuntu-${findUbuntuVersion()}`
8+
} else if (platform === 'darwin') {
9+
return 'macos-latest'
10+
} else if (platform === 'win32') {
11+
return 'windows-latest'
12+
} else {
13+
throw new Error(`Unknown platform ${platform}`)
14+
}
15+
}
16+
17+
function findUbuntuVersion() {
18+
const lsb_release = fs.readFileSync('/etc/lsb-release', 'utf8')
19+
const match = lsb_release.match(/^DISTRIB_RELEASE=(\d+\.\d+)$/m)
20+
if (match) {
21+
return match[1]
22+
} else {
23+
throw new Error('Could not find Ubuntu version')
24+
}
25+
}

dist/index.js

Lines changed: 37 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const fs = require('fs')
33
const path = require('path')
44
const core = require('@actions/core')
55
const exec = require('@actions/exec')
6+
const common = require('./common')
67

78
export async function run() {
89
try {
9-
const platform = getVirtualEnvironmentName()
10+
const platform = common.getVirtualEnvironmentName()
1011
const [engine, version] = parseRubyEngineAndVersion(core.getInput('ruby-version'))
1112

1213
let installer
@@ -171,27 +172,4 @@ function isHeadVersion(rubyVersion) {
171172
return rubyVersion === 'head' || rubyVersion === 'mingw' || rubyVersion === 'mswin'
172173
}
173174

174-
function getVirtualEnvironmentName() {
175-
const platform = os.platform()
176-
if (platform === 'linux') {
177-
return `ubuntu-${findUbuntuVersion()}`
178-
} else if (platform === 'darwin') {
179-
return 'macos-latest'
180-
} else if (platform === 'win32') {
181-
return 'windows-latest'
182-
} else {
183-
throw new Error(`Unknown platform ${platform}`)
184-
}
185-
}
186-
187-
function findUbuntuVersion() {
188-
const lsb_release = fs.readFileSync('/etc/lsb-release', 'utf8')
189-
const match = lsb_release.match(/^DISTRIB_RELEASE=(\d+\.\d+)$/m)
190-
if (match) {
191-
return match[1]
192-
} else {
193-
throw new Error('Could not find Ubuntu version')
194-
}
195-
}
196-
197175
if (__filename.endsWith('index.js')) { run() }

0 commit comments

Comments
 (0)