Skip to content
Prev Previous commit
Next Next commit
clean up windows/nix logic
  • Loading branch information
Danny McCormick committed Jun 5, 2019
commit 2d58290b81ab579c52f132314ee5e481abe4a816
54 changes: 28 additions & 26 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,38 +188,40 @@ export async function extractZip(file: string, dest?: string): Promise<string> {
dest = dest || (await _createExtractFolder(dest))

if (IS_WINDOWS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should introduce a pattern for OS splitting—rather than this IS_WINDOWS branch, does it make sense to do something like:

IS_WINDOWS ? extractZipWin() : extractZipNix()

Do you think this would improve readabilty of these branches?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it here, not sure how useful I see it being in other places - I think most of these splits are pretty small amounts of code compared to this one.

I also ditched the ? : syntax in favor of traditional if else because it felt a little crowded with the awaits thrown in.

// build the powershell command
const escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, '') // double-up single quotes, remove double quotes and newlines
const escapedDest = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '')
const command = `$ErrorActionPreference = 'Stop' ; try { Add-Type -AssemblyName System.IO.Compression.FileSystem } catch { } ; [System.IO.Compression.ZipFile]::ExtractToDirectory('${escapedFile}', '${escapedDest}')`

// run powershell
const powershellPath = await io.which('powershell')
const args = [
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
]
await exec(`"${powershellPath}"`, args)
await extractZipWin(file, dest)
} else {
const unzipPath = path.join(
__dirname,
'..',
'scripts',
'externals',
'unzip'
)
await exec(`"${unzipPath}"`, [file], {cwd: dest})
await extractZipNix(file, dest)
}

return dest
}

async function extractZipWin(file: string, dest: string): Promise<void> {
// build the powershell command
const escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, '') // double-up single quotes, remove double quotes and newlines
const escapedDest = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '')
const command = `$ErrorActionPreference = 'Stop' ; try { Add-Type -AssemblyName System.IO.Compression.FileSystem } catch { } ; [System.IO.Compression.ZipFile]::ExtractToDirectory('${escapedFile}', '${escapedDest}')`

// run powershell
const powershellPath = await io.which('powershell')
const args = [
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
]
await exec(`"${powershellPath}"`, args)
}

async function extractZipNix(file: string, dest: string): Promise<void> {
const unzipPath = path.join(__dirname, '..', 'scripts', 'externals', 'unzip')
await exec(`"${unzipPath}"`, [file], {cwd: dest})
}

/**
* Caches a directory and installs it into the tool cacheDir
*
Expand Down