Skip to content

Commit e38f9ab

Browse files
committed
Fix usages of string.split(sep, limit)
* Since the limit just removes further matches in JavaScript.
1 parent 7c2bf72 commit e38f9ab

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

common.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export const windows = (os.platform() === 'win32')
1111
// Extract to SSD on Windows, see https://github.com/ruby/setup-ruby/pull/14
1212
export const drive = (windows ? (process.env['GITHUB_WORKSPACE'] || 'C')[0] : undefined)
1313

14+
export function partition(string, separator) {
15+
const i = string.indexOf(separator)
16+
if (i === -1) {
17+
throw new Error(`No separator ${separator} in string ${string}`)
18+
}
19+
return [string.slice(0, i), string.slice(i + separator.length, string.length)]
20+
}
21+
1422
export async function measure(name, block) {
1523
return await core.group(name, async () => {
1624
const start = performance.now()
@@ -95,7 +103,7 @@ export function getToolCacheRubyPrefix(platform, version) {
95103
export function win2nix(path) {
96104
if (/^[A-Z]:/i.test(path)) {
97105
// path starts with drive
98-
path = `/${path[0].toLowerCase()}${path.split(':', 2)[1]}`
106+
path = `/${path[0].toLowerCase()}${partition(path, ':')[1]}`
99107
}
100108
return path.replace(/\\/g, '/').replace(/ /g, '\\ ')
101109
}

dist/index.js

Lines changed: 13 additions & 4 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function parseRubyEngineAndVersion(rubyVersion) {
106106
} else if (rubyVersion === '.tool-versions') { // Read from .tool-versions
107107
const toolVersions = fs.readFileSync('.tool-versions', 'utf8').trim()
108108
const rubyLine = toolVersions.split(/\r?\n/).filter(e => e.match(/^ruby\s/))[0]
109-
rubyVersion = rubyLine.split(/\s+/, 2)[1]
109+
rubyVersion = rubyLine.match(/^ruby\s+(.+)$/)[1]
110110
console.log(`Using ${rubyVersion} as input from file .tool-versions`)
111111
}
112112

@@ -118,7 +118,7 @@ function parseRubyEngineAndVersion(rubyVersion) {
118118
engine = rubyVersion
119119
version = '' // Let the logic in validateRubyEngineAndVersion() find the version
120120
} else { // engine-X.Y.Z
121-
[engine, version] = rubyVersion.split('-', 2)
121+
[engine, version] = common.partition(rubyVersion, '-')
122122
}
123123

124124
return [engine, version]

windows.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function addVCVARSEnv() {
120120
let newSet = cp.execSync(cmd).toString().trim().split(/\r?\n/)
121121
newSet = newSet.filter(line => line.match(/\S=\S/))
122122
newSet.forEach(s => {
123-
let [k,v] = s.split('=', 2)
123+
let [k,v] = common.partition(s, '=')
124124
newEnv.set(k,v)
125125
})
126126

0 commit comments

Comments
 (0)