|
| 1 | +import * as fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
1 | 4 | import { ToolRunner } from "@actions/exec/lib/toolrunner";
|
2 | 5 | import * as toolcache from "@actions/tool-cache";
|
3 | 6 | import { safeWhich } from "@chrisgavin/safe-which";
|
| 7 | +import { v4 as uuidV4 } from "uuid"; |
4 | 8 |
|
| 9 | +import { getTemporaryDirectory, runTool } from "./actions-util"; |
5 | 10 | import { Logger } from "./logging";
|
6 | 11 | import { assertNever } from "./util";
|
7 | 12 |
|
@@ -84,24 +89,77 @@ export async function isZstdAvailable(
|
84 | 89 | export type CompressionMethod = "gzip" | "zstd";
|
85 | 90 |
|
86 | 91 | export async function extract(
|
87 |
| - path: string, |
| 92 | + tarPath: string, |
88 | 93 | compressionMethod: CompressionMethod,
|
| 94 | + tarVersion: TarVersion | undefined, |
89 | 95 | ): Promise<string> {
|
90 | 96 | switch (compressionMethod) {
|
91 | 97 | case "gzip":
|
92 |
| - // While we could also ask tar to autodetect the compression method, |
93 |
| - // we defensively keep the gzip call identical as requesting a gzipped |
94 |
| - // bundle will soon be a fallback option. |
95 |
| - return await toolcache.extractTar(path); |
| 98 | + // Defensively continue to call the toolcache API as requesting a gzipped |
| 99 | + // bundle may be a fallback option. |
| 100 | + return await toolcache.extractTar(tarPath); |
96 | 101 | case "zstd":
|
97 |
| - // By specifying only the "x" flag, we ask tar to autodetect the |
98 |
| - // compression method. |
99 |
| - return await toolcache.extractTar(path, undefined, "x"); |
| 102 | + if (!tarVersion) { |
| 103 | + throw new Error( |
| 104 | + "Could not determine tar version, which is required to extract a Zstandard archive.", |
| 105 | + ); |
| 106 | + } |
| 107 | + return await extractTarZst(tarPath, tarVersion); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Extract a compressed tar archive |
| 113 | + * |
| 114 | + * @param file path to the tar |
| 115 | + * @param dest destination directory. Optional. |
| 116 | + * @returns path to the destination directory |
| 117 | + */ |
| 118 | +export async function extractTarZst( |
| 119 | + file: string, |
| 120 | + tarVersion: TarVersion, |
| 121 | +): Promise<string> { |
| 122 | + if (!file) { |
| 123 | + throw new Error("parameter 'file' is required"); |
| 124 | + } |
| 125 | + |
| 126 | + // Create dest |
| 127 | + const dest = await createExtractFolder(); |
| 128 | + |
| 129 | + // Initialize args |
| 130 | + const args = ["-x", "-v"]; |
| 131 | + |
| 132 | + let destArg = dest; |
| 133 | + let fileArg = file; |
| 134 | + if (process.platform === "win32" && tarVersion.type === "gnu") { |
| 135 | + args.push("--force-local"); |
| 136 | + destArg = dest.replace(/\\/g, "/"); |
| 137 | + |
| 138 | + // Technically only the dest needs to have `/` but for aesthetic consistency |
| 139 | + // convert slashes in the file arg too. |
| 140 | + fileArg = file.replace(/\\/g, "/"); |
| 141 | + } |
| 142 | + |
| 143 | + if (tarVersion.type === "gnu") { |
| 144 | + // Suppress warnings when using GNU tar to extract archives created by BSD tar |
| 145 | + args.push("--warning=no-unknown-keyword"); |
| 146 | + args.push("--overwrite"); |
100 | 147 | }
|
| 148 | + |
| 149 | + args.push("-C", destArg, "-f", fileArg); |
| 150 | + await runTool(`tar`, args); |
| 151 | + |
| 152 | + return dest; |
| 153 | +} |
| 154 | + |
| 155 | +async function createExtractFolder(): Promise<string> { |
| 156 | + const dest = path.join(getTemporaryDirectory(), uuidV4()); |
| 157 | + fs.mkdirSync(dest, { recursive: true }); |
| 158 | + return dest; |
101 | 159 | }
|
102 | 160 |
|
103 |
| -export function inferCompressionMethod(path: string): CompressionMethod { |
104 |
| - if (path.endsWith(".tar.gz")) { |
| 161 | +export function inferCompressionMethod(tarPath: string): CompressionMethod { |
| 162 | + if (tarPath.endsWith(".tar.gz")) { |
105 | 163 | return "gzip";
|
106 | 164 | }
|
107 | 165 | return "zstd";
|
|
0 commit comments