Skip to content

Commit

Permalink
feat(cache): support passing extra tar args
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Apr 2, 2024
1 parent 59e9d28 commit 8111aa2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
12 changes: 7 additions & 5 deletions packages/cache/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path'
import * as utils from './internal/cacheUtils'
import * as cacheHttpClient from './internal/cacheHttpClient'
import {createTar, extractTar, listTar} from './internal/tar'
import {DownloadOptions, UploadOptions} from './options'
import {DownloadOptions, ExtraTarArgs, UploadOptions} from './options'

export class ValidationError extends Error {
constructor(message: string) {
Expand Down Expand Up @@ -68,7 +68,8 @@ export async function restoreCache(
primaryKey: string,
restoreKeys?: string[],
options?: DownloadOptions,
enableCrossOsArchive = false
enableCrossOsArchive = false,
extraTarArgs: ExtraTarArgs = []
): Promise<string | undefined> {
checkPaths(paths)

Expand Down Expand Up @@ -129,7 +130,7 @@ export async function restoreCache(
)} MB (${archiveFileSize} B)`
)

await extractTar(archivePath, compressionMethod)
await extractTar(archivePath, compressionMethod, extraTarArgs)
core.info('Cache restored successfully')

return cacheEntry.cacheKey
Expand Down Expand Up @@ -166,7 +167,8 @@ export async function saveCache(
paths: string[],
key: string,
options?: UploadOptions,
enableCrossOsArchive = false
enableCrossOsArchive = false,
extraTarArgs: ExtraTarArgs = []
): Promise<number> {
checkPaths(paths)
checkKey(key)
Expand All @@ -193,7 +195,7 @@ export async function saveCache(
core.debug(`Archive Path: ${archivePath}`)

try {
await createTar(archiveFolder, cachePaths, compressionMethod)
await createTar(archiveFolder, cachePaths, compressionMethod, extraTarArgs)
if (core.isDebug()) {
await listTar(archivePath, compressionMethod)
}
Expand Down
25 changes: 20 additions & 5 deletions packages/cache/src/internal/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TarFilename,
ManifestFilename
} from './constants'
import {ExtraTarArgs} from '../options'

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

Expand Down Expand Up @@ -128,7 +129,8 @@ async function getTarArgs(
async function getCommands(
compressionMethod: CompressionMethod,
type: string,
archivePath = ''
archivePath = '',
extraTarArgs: ExtraTarArgs = []
): Promise<string[]> {
let args

Expand All @@ -139,6 +141,7 @@ async function getCommands(
type,
archivePath
)
tarArgs.push(...extraTarArgs)
const compressionArgs =
type !== 'create'
? await getDecompressionProgram(tarPath, compressionMethod, archivePath)
Expand Down Expand Up @@ -272,26 +275,38 @@ export async function listTar(
// Extract a tar
export async function extractTar(
archivePath: string,
compressionMethod: CompressionMethod
compressionMethod: CompressionMethod,
extraTarArgs: ExtraTarArgs = []
): Promise<void> {
// Create directory to extract tar into
const workingDirectory = getWorkingDirectory()
await io.mkdirP(workingDirectory)
const commands = await getCommands(compressionMethod, 'extract', archivePath)
const commands = await getCommands(
compressionMethod,
'extract',
archivePath,
extraTarArgs
)
await execCommands(commands)
}

// Create a tar
export async function createTar(
archiveFolder: string,
sourceDirectories: string[],
compressionMethod: CompressionMethod
compressionMethod: CompressionMethod,
extraTarArgs: ExtraTarArgs = []
): Promise<void> {
// Write source directories to manifest.txt to avoid command length limits
writeFileSync(
path.join(archiveFolder, ManifestFilename),
sourceDirectories.join('\n')
)
const commands = await getCommands(compressionMethod, 'create')
const commands = await getCommands(
compressionMethod,
'create',
undefined,
extraTarArgs
)
await execCommands(commands, archiveFolder)
}
6 changes: 6 additions & 0 deletions packages/cache/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export interface DownloadOptions {
lookupOnly?: boolean
}

/**
* Options to control tar commands
*/

export type ExtraTarArgs = string[]

/**
* Returns a copy of the upload options with defaults filled in.
*
Expand Down

0 comments on commit 8111aa2

Please sign in to comment.