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
4 changes: 4 additions & 0 deletions packages/tool-cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ if (process.platform === 'win32') {
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-win-x64.7z');
const node12ExtractedFolder = await tc.extract7z(node12Path, 'path/to/extract/to');
}
else if (process.platform === 'darwin') {
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0.pkg');
const node12ExtractedFolder = await tc.extractXar(node12Path, 'path/to/extract/to');
}
else {
const node12Path = await tc.downloadTool('https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.gz');
const node12ExtractedFolder = await tc.extractTar(node12Path, 'path/to/extract/to');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file-with-�-character.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file.txt contents
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
folder/nested-file.txt contents
Binary file not shown.
35 changes: 35 additions & 0 deletions packages/tool-cache/__tests__/tool-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -346,6 +347,40 @@ describe('@actions/tool-cache', function() {
await io.rmRF(tempDir)
}
})
} else if (IS_MAC) {
it('extract .xar', async () => {
const tempDir = path.join(tempPath, 'test-install.xar')
const sourcePath = path.join(__dirname, 'data', 'archive-content')
const targetPath = path.join(tempDir, 'test.xar')
fwal marked this conversation as resolved.
Show resolved Hide resolved
await io.mkdirP(tempDir)

// Create test archive
const xarPath = await io.which('xar', true)
await exec.exec(`${xarPath}`, ['-cf', targetPath, '.'], {
cwd: sourcePath
})

// extract/cache
const extPath: string = await tc.extractXar(targetPath, 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 () => {
Expand Down
30 changes: 30 additions & 0 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class HTTPError extends Error {
}

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

/**
Expand Down Expand Up @@ -254,6 +255,35 @@ 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 args = [flags, '-C', dest, '-f', file]

if (core.isDebug() && !flags.includes('v')) {
args.push('-v')
}
fwal marked this conversation as resolved.
Show resolved Hide resolved

const xarPath: string = await io.which('xar', true)
await exec(`"${xarPath}"`, args)

return dest
}

/**
* Extract a zip
*
Expand Down