| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  | import * as core from "@actions/core"; | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -05:00
										 |  |  | import { exec } from "@actions/exec"; | 
					
						
							|  |  |  | import * as io from "@actions/io"; | 
					
						
							|  |  |  | import { existsSync } from "fs"; | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  | import * as path from "path"; | 
					
						
							| 
									
										
										
										
											2020-05-12 16:36:56 -04:00
										 |  |  | import * as tar from "./tar"; | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04: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> { | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -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-05-12 16:36:56 -04:00
										 |  |  |         } else if (await tar.isGnuTar()) { | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |             args.push("--force-local"); | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return await io.which("tar", true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function execTar(args: string[]): Promise<void> { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |         await exec(`"${await getTarPath(args)}"`, args); | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -05:00
										 |  |  |     } catch (error) { | 
					
						
							|  |  |  |         throw new Error(`Tar failed with error: ${error?.message}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function extractTar( | 
					
						
							|  |  |  |     archivePath: string, | 
					
						
							|  |  |  |     targetDirectory: string | 
					
						
							|  |  |  | ): Promise<void> { | 
					
						
							|  |  |  |     // Create directory to extract tar into
 | 
					
						
							|  |  |  |     await io.mkdirP(targetDirectory); | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |     const args = [ | 
					
						
							|  |  |  |         "-xz", | 
					
						
							|  |  |  |         "-f", | 
					
						
							| 
									
										
										
										
											2020-04-13 12:20:27 -04:00
										 |  |  |         archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"), | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |         "-C", | 
					
						
							| 
									
										
										
										
											2020-04-13 12:20:27 -04:00
										 |  |  |         targetDirectory.replace(new RegExp("\\" + path.sep, "g"), "/") | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |     ]; | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -05:00
										 |  |  |     await execTar(args); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function createTar( | 
					
						
							|  |  |  |     archivePath: string, | 
					
						
							|  |  |  |     sourceDirectory: string | 
					
						
							|  |  |  | ): Promise<void> { | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |     const args = [ | 
					
						
							|  |  |  |         "-cz", | 
					
						
							|  |  |  |         "-f", | 
					
						
							| 
									
										
										
										
											2020-04-13 12:20:27 -04:00
										 |  |  |         archivePath.replace(new RegExp("\\" + path.sep, "g"), "/"), | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |         "-C", | 
					
						
							| 
									
										
										
										
											2020-04-13 12:20:27 -04:00
										 |  |  |         sourceDirectory.replace(new RegExp("\\" + path.sep, "g"), "/"), | 
					
						
							| 
									
										
										
										
											2020-04-09 10:29:33 -04:00
										 |  |  |         "." | 
					
						
							|  |  |  |     ]; | 
					
						
							| 
									
										
										
										
											2020-01-06 13:36:33 -05:00
										 |  |  |     await execTar(args); | 
					
						
							|  |  |  | } |