2021-09-14 17:23:12 +08:00
|
|
|
import { find, downloadTool, cacheDir } from "@actions/tool-cache"
|
|
|
|
import { addPath, group, startGroup, endGroup } from "@actions/core"
|
2021-09-14 17:23:38 +08:00
|
|
|
import { join } from "path"
|
2021-09-14 17:23:12 +08:00
|
|
|
import { existsSync } from "fs"
|
|
|
|
import * as hasha from "hasha"
|
|
|
|
|
2021-09-14 17:26:33 +08:00
|
|
|
/** A type that describes a package */
|
2021-09-14 17:23:12 +08:00
|
|
|
export type PackageInfo = {
|
2021-09-14 17:27:55 +08:00
|
|
|
/** Url to download the package */
|
2021-09-14 17:23:12 +08:00
|
|
|
url: string
|
2021-09-14 17:26:33 +08:00
|
|
|
/** The top folder name once it is extracted. It can be `""` if there is no top folder */
|
2021-09-14 17:23:38 +08:00
|
|
|
extractedFolderName: string
|
2021-09-14 17:26:33 +08:00
|
|
|
/** The relative directory in which the binary is located. It can be `""` if the exe is in the top folder */
|
|
|
|
binRelativeDir: string
|
2021-09-14 17:27:55 +08:00
|
|
|
/** The function to extract the downloaded archive. It can be `undefined`, if the binary itself is downloaded directly. */
|
|
|
|
extractFunction?: {
|
2021-09-14 17:23:12 +08:00
|
|
|
(url: string, outputPath: string): Promise<string>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A function that downloads and installs a tool. Then it caches it in the tool-cache. */
|
|
|
|
export async function setupBin(
|
|
|
|
name: string,
|
|
|
|
version: string,
|
2021-09-14 17:23:38 +08:00
|
|
|
getPackageInfo: (version: string, platform: NodeJS.Platform) => PackageInfo
|
2021-09-14 17:23:12 +08:00
|
|
|
): Promise<string> {
|
|
|
|
// Build artifact name
|
|
|
|
const binName = process.platform === "win32" ? `${name}.exe` : name
|
|
|
|
|
|
|
|
// Restore from cache (if found).
|
|
|
|
const dir = find(name, version)
|
|
|
|
if (dir) {
|
|
|
|
addPath(dir)
|
|
|
|
return join(dir, binName)
|
|
|
|
}
|
|
|
|
|
2021-09-14 17:23:38 +08:00
|
|
|
const { url, binRelativeDir, extractedFolderName, extractFunction } = getPackageInfo(version, process.platform)
|
2021-09-14 17:23:12 +08:00
|
|
|
|
|
|
|
// Get an unique output directory name from the URL.
|
|
|
|
const key: string = await hasha.async(url)
|
2021-09-14 18:11:42 +08:00
|
|
|
const workDir = join(process.env.SETUP_CPP_DIR ?? "~/setup_cpp", key)
|
2021-09-14 17:23:12 +08:00
|
|
|
|
|
|
|
/** The directory which the tool is installed to */
|
2021-09-14 17:23:38 +08:00
|
|
|
const binDir = join(workDir, extractedFolderName, binRelativeDir)
|
2021-09-14 17:23:12 +08:00
|
|
|
|
|
|
|
if (!existsSync(workDir)) {
|
|
|
|
await group(`Download and extract ${name}`, async () => {
|
|
|
|
const downloaded = await downloadTool(url)
|
2021-09-14 17:27:55 +08:00
|
|
|
await extractFunction?.(downloaded, workDir)
|
2021-09-14 17:23:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
startGroup(`Add ${name} to PATH`)
|
|
|
|
addPath(binDir)
|
|
|
|
} finally {
|
|
|
|
endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
await cacheDir(workDir, name, version)
|
|
|
|
|
|
|
|
return join(binDir, binName)
|
|
|
|
}
|