Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tool-cache: Support for extracting xar compatible archives #207

Merged
merged 15 commits into from
Jul 15, 2020
Binary file added packages/tool-cache/__tests__/data/test.xar
Binary file not shown.
34 changes: 34 additions & 0 deletions packages/tool-cache/__tests__/tool-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ process.env['RUNNER_TOOL_CACHE'] = cachePath
import * as tc from '../src/tool-cache'

const IS_WINDOWS = process.platform === 'win32'
const IS_MAC = process.platform === 'darwin'

describe('@actions/tool-cache', function() {
beforeAll(function() {
Expand Down Expand Up @@ -198,6 +199,39 @@ describe('@actions/tool-cache', function() {
}
})
} else {
if (IS_MAC) {
it('extract .xar', async () => {
const tempDir = path.join(tempPath, 'test-install.xar')
fwal marked this conversation as resolved.
Show resolved Hide resolved

await io.mkdirP(tempDir)

// copy the .xar file to the test dir
const _xarFile: string = path.join(tempDir, 'test.xar')
await io.cp(path.join(__dirname, 'data', 'test.xar'), _xarFile)

// extract/cache
const extPath: string = await tc.extractXar(_xarFile, undefined, '-x')
await tc.cacheDir(extPath, 'my-xar-contents', '1.1.0')
const toolPath: string = tc.find('my-xar-contents', '1.1.0')

expect(fs.existsSync(toolPath)).toBeTruthy()
expect(fs.existsSync(`${toolPath}.complete`)).toBeTruthy()
expect(fs.existsSync(path.join(toolPath, 'file.txt'))).toBeTruthy()
expect(
fs.existsSync(path.join(toolPath, 'file-with-ç-character.txt'))
).toBeTruthy()
expect(
fs.existsSync(path.join(toolPath, 'folder', 'nested-file.txt'))
).toBeTruthy()
expect(
fs.readFileSync(
path.join(toolPath, 'folder', 'nested-file.txt'),
'utf8'
)
).toBe('folder/nested-file.txt contents')
})
}

it('extract .tar.gz', async () => {
const tempDir = path.join(tempPath, 'test-install-tar.gz')

Expand Down
24 changes: 24 additions & 0 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class HTTPError extends Error {
}

const IS_WINDOWS = process.platform === 'win32'
const IS_MAC = process.platform === 'darwin'
const userAgent = 'actions/tool-cache'

// On load grab temp directory and cache directory and remove them from env (currently don't want to expose this)
Expand Down Expand Up @@ -206,6 +207,29 @@ export async function extractTar(
return dest
}

/**
* Extract a xar compatible archive
*
* @param file path to the archive
* @param dest destination directory. Optional.
* @param flags flags for the xar. Optional.
* @returns path to the destination directory
*/
export async function extractXar(
thboop marked this conversation as resolved.
Show resolved Hide resolved
file: string,
dest?: string,
flags: string = '-x'
): Promise<string> {
ok(IS_MAC, 'extractXar() not supported on current OS')
ok(file, 'parameter "file" is required')

dest = dest || (await _createExtractFolder(dest))
fwal marked this conversation as resolved.
Show resolved Hide resolved
const xarPath: string = await io.which('xar', true)
await exec(`"${xarPath}"`, [flags, '-C', dest, '-f', file])
fwal marked this conversation as resolved.
Show resolved Hide resolved

return dest
}

/**
* Extract a zip
*
Expand Down