Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
feat: add zstd support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Jan 18, 2022
1 parent ba1f5f6 commit 3135027
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
22 changes: 17 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"devDependencies": {
"@commitlint/cli": "16.0.2",
"@commitlint/config-conventional": "16.0.0",
"@types/semver": "^7.3.9",
"@types/tmp": "^0.2.3",
"@typescript-eslint/eslint-plugin": "4.33.0",
"@typescript-eslint/parser": "4.33.0",
Expand All @@ -76,6 +77,7 @@
"@actions/github": "5.0.0",
"@actions/glob": "0.2.0",
"@google-cloud/storage": "5.17.0",
"semver": "^7.3.5",
"tmp-promise": "3.0.3"
}
}
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as github from '@actions/github';
import { Storage, File, Bucket } from '@google-cloud/storage';
import { withFile as withTemporaryFile } from 'tmp-promise';

import { getInputs } from './inputs';
import { CacheHitKindState, saveState } from './state';
import { extractTar } from './tar-utils';

async function getBestMatch(
bucket: Bucket,
Expand Down Expand Up @@ -75,7 +75,7 @@ async function main() {
});

console.log('🗜️ Extracting cache archive...');
await exec.exec('tar', ['-xzf', tmpFile.path, '-P', '-C', workspace]);
await extractTar(tmpFile.path, workspace);

saveState({
cacheHitKind: bestMatchKind,
Expand Down
13 changes: 2 additions & 11 deletions src/post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as exec from '@actions/exec';
import * as github from '@actions/github';
import * as glob from '@actions/glob';
import { Storage } from '@google-cloud/storage';
Expand All @@ -7,6 +6,7 @@ import { withFile as withTemporaryFile } from 'tmp-promise';

import { getInputs } from './inputs';
import { getState } from './state';
import { createTar } from './tar-utils';

async function main() {
const inputs = getInputs();
Expand Down Expand Up @@ -43,16 +43,7 @@ async function main() {

return withTemporaryFile(async (tmpFile) => {
console.log('🗜️ Creating cache archive...');

await exec.exec('tar', [
'--posix',
'-czf',
tmpFile.path,
'-P',
'-C',
workspace,
...paths,
]);
await createTar(tmpFile.path, paths, workspace);

console.log('🌐 Uploading cache archive to bucket...');
await bucket.upload(tmpFile.path, {
Expand Down
81 changes: 81 additions & 0 deletions src/tar-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable sonarjs/no-duplicate-string */

import * as exec from '@actions/exec';
import * as semver from 'semver';

enum CompressionMethod {
GZIP,
ZSTD_WITHOUT_LONG,
ZSTD,
}

async function getTarCompressionMethod(): Promise<CompressionMethod> {
if (process.platform === 'win32') {
return CompressionMethod.GZIP;
}

const [zstdOutput, zstdVersion] = await exec
.getExecOutput('zstd', ['--version'], {
ignoreReturnCode: true,
silent: true,
})
.then((out) => out.stdout.trim())
.then((out) => [out, semver.clean(out)]);

if (!zstdOutput?.toLowerCase().includes('zstd command line interface')) {
return CompressionMethod.GZIP;
} else if (!zstdVersion || semver.lt(zstdVersion, 'v1.3.2')) {
return CompressionMethod.ZSTD_WITHOUT_LONG;
} else {
return CompressionMethod.ZSTD;
}
}

export async function createTar(
archivePath: string,
paths: string[],
cwd: string,
): Promise<number> {
const compressionMethod = await getTarCompressionMethod();

const compressionArgs =
compressionMethod === CompressionMethod.GZIP
? ['-z']
: compressionMethod === CompressionMethod.ZSTD_WITHOUT_LONG
? ['--use-compress-program', 'zstd -T0 --long=30']
: ['--use-compress-program', 'zstd -T0'];

return exec.exec('tar', [
'-c',
...compressionArgs,
'--posix',
'-f',
archivePath,
'-C',
cwd,
...paths,
]);
}

export async function extractTar(
archivePath: string,
cwd: string,
): Promise<number> {
const compressionMethod = await getTarCompressionMethod();

const compressionArgs =
compressionMethod === CompressionMethod.GZIP
? ['-z']
: compressionMethod === CompressionMethod.ZSTD_WITHOUT_LONG
? ['--use-compress-program', 'zstd -d --long=30']
: ['--use-compress-program', 'zstd -d'];

return exec.exec('tar', [
'-x',
...compressionArgs,
'-f',
archivePath,
'-C',
cwd,
]);
}

0 comments on commit 3135027

Please sign in to comment.