2019-12-13 17:24:37 -05:00
|
|
|
import { exec } from "@actions/exec";
|
|
|
|
import * as io from "@actions/io";
|
2020-03-20 13:02:11 -07:00
|
|
|
import { existsSync, writeFileSync } from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
|
2020-04-22 16:36:34 -04:00
|
|
|
import { CompressionMethod } from "./constants";
|
|
|
|
import * as utils from "./utils/actionUtils";
|
2020-04-09 10:29:33 -04:00
|
|
|
|
|
|
|
async function getTarPath(args: string[]): Promise<string> {
|
2019-12-13 17:24:37 -05:00
|
|
|
// Explicitly use BSD Tar on Windows
|
|
|
|
const IS_WINDOWS = process.platform === "win32";
|
|
|
|
if (IS_WINDOWS) {
|
|
|
|
const systemTar = `${process.env["windir"]}\\System32\\tar.exe`;
|
|
|
|
if (existsSync(systemTar)) {
|
|
|
|
return systemTar;
|
2020-04-22 16:36:34 -04:00
|
|
|
} else if (await utils.useGnuTar()) {
|
2020-04-09 10:29:33 -04:00
|
|
|
args.push("--force-local");
|
2019-12-13 17:24:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return await io.which("tar", true);
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:02:11 -07:00
|
|
|
async function execTar(args: string[], cwd?: string): Promise<void> {
|
2019-12-13 17:24:37 -05:00
|
|
|
try {
|
2020-04-30 15:28:04 -04:00
|
|
|
await exec(`"${await getTarPath(args)}"`, args, { cwd: cwd });
|
2019-12-13 17:24:37 -05:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Tar failed with error: ${error?.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:02:11 -07:00
|
|
|
function getWorkingDirectory(): string {
|
|
|
|
return process.env["GITHUB_WORKSPACE"] ?? process.cwd();
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:28:04 -04:00
|
|
|
function isOS64(): boolean {
|
|
|
|
return process.platform != "win32" || process.arch === "x64";
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:36:34 -04:00
|
|
|
export async function extractTar(
|
|
|
|
archivePath: string,
|
|
|
|
compressionMethod: CompressionMethod
|
|
|
|
): Promise<void> {
|
2019-12-13 17:24:37 -05:00
|
|
|
// Create directory to extract tar into
|
2020-03-20 13:02:11 -07:00
|
|
|
const workingDirectory = getWorkingDirectory();
|
|
|
|
await io.mkdirP(workingDirectory);
|
2020-04-30 15:28:04 -04:00
|
|
|
// --d: Decompress.
|
|
|
|
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
|
2020-04-09 10:29:33 -04:00
|
|
|
const args = [
|
2020-04-22 16:36:34 -04:00
|
|
|
...(compressionMethod == CompressionMethod.Zstd
|
2020-04-30 15:28:04 -04:00
|
|
|
? [
|
|
|
|
"--use-compress-program",
|
|
|
|
isOS64() ? "zstd -d --long=31" : "zstd -d --long=30"
|
|
|
|
]
|
2020-04-22 16:36:34 -04:00
|
|
|
: ["-z"]),
|
|
|
|
"-xf",
|
2020-04-13 12:20:27 -04:00
|
|
|
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
|
2020-04-09 10:29:33 -04:00
|
|
|
"-P",
|
|
|
|
"-C",
|
2020-04-13 12:20:27 -04:00
|
|
|
workingDirectory.replace(new RegExp("\\" + path.sep, "g"), "/")
|
2020-04-09 10:29:33 -04:00
|
|
|
];
|
2019-12-13 17:24:37 -05:00
|
|
|
await execTar(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createTar(
|
2020-03-20 13:02:11 -07:00
|
|
|
archiveFolder: string,
|
2020-04-22 16:36:34 -04:00
|
|
|
sourceDirectories: string[],
|
|
|
|
compressionMethod: CompressionMethod
|
2019-12-13 17:24:37 -05:00
|
|
|
): Promise<void> {
|
2020-03-20 13:02:11 -07:00
|
|
|
// Write source directories to manifest.txt to avoid command length limits
|
|
|
|
const manifestFilename = "manifest.txt";
|
2020-04-22 16:36:34 -04:00
|
|
|
const cacheFileName = utils.getCacheFileName(compressionMethod);
|
2020-03-20 13:02:11 -07:00
|
|
|
writeFileSync(
|
|
|
|
path.join(archiveFolder, manifestFilename),
|
|
|
|
sourceDirectories.join("\n")
|
|
|
|
);
|
2020-04-22 16:36:34 -04:00
|
|
|
// -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores.
|
2020-04-30 15:28:04 -04:00
|
|
|
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
|
2020-03-20 13:02:11 -07:00
|
|
|
const workingDirectory = getWorkingDirectory();
|
|
|
|
const args = [
|
2020-04-22 16:36:34 -04:00
|
|
|
...(compressionMethod == CompressionMethod.Zstd
|
2020-04-30 15:28:04 -04:00
|
|
|
? [
|
|
|
|
"--use-compress-program",
|
|
|
|
isOS64() ? "zstd -T0 --long=31" : "zstd -T0 --long=30"
|
|
|
|
]
|
2020-04-22 16:36:34 -04:00
|
|
|
: ["-z"]),
|
|
|
|
"-cf",
|
|
|
|
cacheFileName.replace(new RegExp("\\" + path.sep, "g"), "/"),
|
2020-04-08 10:58:38 -04:00
|
|
|
"-P",
|
2020-03-20 13:02:11 -07:00
|
|
|
"-C",
|
2020-04-13 12:20:27 -04:00
|
|
|
workingDirectory.replace(new RegExp("\\" + path.sep, "g"), "/"),
|
2020-03-20 13:02:11 -07:00
|
|
|
"--files-from",
|
|
|
|
manifestFilename
|
|
|
|
];
|
|
|
|
await execTar(args, archiveFolder);
|
2019-12-13 17:24:37 -05:00
|
|
|
}
|