2019-12-14 05:24:37 +07:00
|
|
|
import { exec } from "@actions/exec";
|
|
|
|
import * as io from "@actions/io";
|
2020-03-21 03:02:11 +07:00
|
|
|
import { existsSync, writeFileSync } from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
|
|
|
|
import { CacheFilename } from "./constants";
|
2019-12-14 05:24:37 +07:00
|
|
|
|
|
|
|
async function getTarPath(): Promise<string> {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await io.which("tar", true);
|
|
|
|
}
|
|
|
|
|
2020-03-21 03:02:11 +07:00
|
|
|
async function execTar(args: string[], cwd?: string): Promise<void> {
|
2019-12-14 05:24:37 +07:00
|
|
|
try {
|
2020-03-21 03:02:11 +07:00
|
|
|
await exec(`"${await getTarPath()}"`, args, { cwd: cwd });
|
2019-12-14 05:24:37 +07:00
|
|
|
} catch (error) {
|
|
|
|
const IS_WINDOWS = process.platform === "win32";
|
|
|
|
if (IS_WINDOWS) {
|
|
|
|
throw new Error(
|
|
|
|
`Tar failed with error: ${error?.message}. Ensure BSD tar is installed and on the PATH.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw new Error(`Tar failed with error: ${error?.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 03:02:11 +07:00
|
|
|
function getWorkingDirectory(): string {
|
|
|
|
return process.env["GITHUB_WORKSPACE"] ?? process.cwd();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function extractTar(archivePath: string): Promise<void> {
|
2019-12-14 05:24:37 +07:00
|
|
|
// Create directory to extract tar into
|
2020-03-21 03:02:11 +07:00
|
|
|
const workingDirectory = getWorkingDirectory();
|
|
|
|
await io.mkdirP(workingDirectory);
|
|
|
|
const args = ["-xz", "-f", archivePath, "-P", "-C", workingDirectory];
|
2019-12-14 05:24:37 +07:00
|
|
|
await execTar(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createTar(
|
2020-03-21 03:02:11 +07:00
|
|
|
archiveFolder: string,
|
|
|
|
sourceDirectories: string[]
|
2019-12-14 05:24:37 +07:00
|
|
|
): Promise<void> {
|
2020-03-21 03:02:11 +07: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",
|
|
|
|
CacheFilename,
|
2020-04-08 21:58:38 +07:00
|
|
|
"-P",
|
2020-03-21 03:02:11 +07:00
|
|
|
"-C",
|
|
|
|
workingDirectory,
|
|
|
|
"--files-from",
|
|
|
|
manifestFilename
|
|
|
|
];
|
|
|
|
await execTar(args, archiveFolder);
|
2019-12-14 05:24:37 +07:00
|
|
|
}
|