2020-04-09 16:29:33 +02:00
|
|
|
import * as core from "@actions/core";
|
2019-12-13 23:24:37 +01:00
|
|
|
import { exec } from "@actions/exec";
|
|
|
|
import * as io from "@actions/io";
|
2020-03-20 21:02:11 +01:00
|
|
|
import { existsSync, writeFileSync } from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
|
|
|
|
import { CacheFilename } from "./constants";
|
2019-12-13 23:24:37 +01:00
|
|
|
|
2020-04-09 16:29:33 +02:00
|
|
|
export async function isGnuTar(): Promise<boolean> {
|
|
|
|
core.debug("Checking tar --version");
|
|
|
|
let versionOutput = "";
|
|
|
|
await exec("tar --version", [], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true,
|
|
|
|
listeners: {
|
|
|
|
stdout: (data: Buffer): string =>
|
|
|
|
(versionOutput += data.toString()),
|
|
|
|
stderr: (data: Buffer): string => (versionOutput += data.toString())
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
core.debug(versionOutput.trim());
|
|
|
|
return versionOutput.toUpperCase().includes("GNU TAR");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTarPath(args: string[]): Promise<string> {
|
2019-12-13 23:24:37 +01: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-09 16:29:33 +02:00
|
|
|
} else if (isGnuTar()) {
|
|
|
|
args.push("--force-local");
|
2019-12-13 23:24:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return await io.which("tar", true);
|
|
|
|
}
|
|
|
|
|
2020-03-20 21:02:11 +01:00
|
|
|
async function execTar(args: string[], cwd?: string): Promise<void> {
|
2019-12-13 23:24:37 +01:00
|
|
|
try {
|
2020-04-09 16:29:33 +02:00
|
|
|
await exec(`"${await getTarPath(args)}"`, args, { cwd: cwd });
|
2019-12-13 23:24:37 +01:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Tar failed with error: ${error?.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 21:02:11 +01:00
|
|
|
function getWorkingDirectory(): string {
|
|
|
|
return process.env["GITHUB_WORKSPACE"] ?? process.cwd();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function extractTar(archivePath: string): Promise<void> {
|
2019-12-13 23:24:37 +01:00
|
|
|
// Create directory to extract tar into
|
2020-03-20 21:02:11 +01:00
|
|
|
const workingDirectory = getWorkingDirectory();
|
|
|
|
await io.mkdirP(workingDirectory);
|
2020-04-09 16:29:33 +02:00
|
|
|
const args = [
|
|
|
|
"-xz",
|
|
|
|
"-f",
|
2020-04-13 18:20:27 +02:00
|
|
|
archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"),
|
2020-04-09 16:29:33 +02:00
|
|
|
"-P",
|
|
|
|
"-C",
|
2020-04-13 18:20:27 +02:00
|
|
|
workingDirectory.replace(new RegExp("\\" + path.sep, "g"), "/")
|
2020-04-09 16:29:33 +02:00
|
|
|
];
|
2019-12-13 23:24:37 +01:00
|
|
|
await execTar(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createTar(
|
2020-03-20 21:02:11 +01:00
|
|
|
archiveFolder: string,
|
|
|
|
sourceDirectories: string[]
|
2019-12-13 23:24:37 +01:00
|
|
|
): Promise<void> {
|
2020-03-20 21:02:11 +01:00
|
|
|
// Write source directories to manifest.txt to avoid command length limits
|
|
|
|
const manifestFilename = "manifest.txt";
|
|
|
|
writeFileSync(
|
|
|
|
path.join(archiveFolder, manifestFilename),
|
|
|
|
sourceDirectories.join("\n")
|
|
|
|
);
|
|
|
|
|
|
|
|
const workingDirectory = getWorkingDirectory();
|
|
|
|
const args = [
|
|
|
|
"-cz",
|
|
|
|
"-f",
|
2020-04-13 18:20:27 +02:00
|
|
|
CacheFilename.replace(new RegExp("\\" + path.sep, "g"), "/"),
|
2020-04-08 16:58:38 +02:00
|
|
|
"-P",
|
2020-03-20 21:02:11 +01:00
|
|
|
"-C",
|
2020-04-13 18:20:27 +02:00
|
|
|
workingDirectory.replace(new RegExp("\\" + path.sep, "g"), "/"),
|
2020-03-20 21:02:11 +01:00
|
|
|
"--files-from",
|
|
|
|
manifestFilename
|
|
|
|
];
|
|
|
|
await execTar(args, archiveFolder);
|
2019-12-13 23:24:37 +01:00
|
|
|
}
|