enable latest cache by calling getLatestReleaseVersion

This commit is contained in:
pollenjp 2024-11-30 00:48:57 +09:00
parent 8bdd012be5
commit 8c90a15bfd
4 changed files with 57 additions and 175 deletions

92
dist/setup/index.js generated vendored
View file

@ -89685,62 +89685,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.downloadLatest = downloadLatest; exports.getLatestReleaseVersion = getLatestReleaseVersion;
const core = __importStar(__nccwpck_require__(7484));
const tc = __importStar(__nccwpck_require__(3472));
const exec = __importStar(__nccwpck_require__(5236));
const path = __importStar(__nccwpck_require__(6760));
const node_fs_1 = __nccwpck_require__(3024);
const checksum_1 = __nccwpck_require__(5391);
const constants_1 = __nccwpck_require__(6156); const constants_1 = __nccwpck_require__(6156);
function downloadLatest(platform, arch, checkSum, githubToken) { const github = __importStar(__nccwpck_require__(3228));
function getLatestReleaseVersion(githubToken) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const artifact = `uv-${arch}-${platform}`; const octokit = github.getOctokit(githubToken);
let extension = ".tar.gz"; const { data: latestRelease } = yield octokit.rest.repos.getLatestRelease({
if (platform === "pc-windows-msvc") { owner: constants_1.OWNER,
extension = ".zip"; repo: constants_1.REPO,
}
const downloadUrl = `https://github.com/${constants_1.OWNER}/${constants_1.REPO}/releases/latest/download/${artifact}${extension}`;
core.info(`Downloading uv from "${downloadUrl}" ...`);
const downloadPath = yield tc.downloadTool(downloadUrl, undefined, githubToken);
let uvExecutablePath;
let uvDir;
if (platform === "pc-windows-msvc") {
const fullPathWithExtension = `${downloadPath}${extension}`;
yield node_fs_1.promises.copyFile(downloadPath, fullPathWithExtension);
uvDir = yield tc.extractZip(fullPathWithExtension);
// On windows extracting the zip does not create an intermediate directory
uvExecutablePath = path.join(uvDir, "uv.exe");
}
else {
const extractedDir = yield tc.extractTar(downloadPath);
uvDir = path.join(extractedDir, artifact);
uvExecutablePath = path.join(uvDir, "uv");
}
const version = yield getVersion(uvExecutablePath);
yield (0, checksum_1.validateChecksum)(checkSum, downloadPath, arch, platform, version);
const cachedToolDir = yield tc.cacheDir(uvDir, constants_1.TOOL_CACHE_NAME, version, arch);
return { cachedToolDir, version };
}); });
if (latestRelease) {
return latestRelease.tag_name;
} }
function getVersion(uvExecutablePath) { throw new Error("No releases found for this repository.");
return __awaiter(this, void 0, void 0, function* () {
// Parse the output of `uv --version` to get the version
// The output looks like
// uv 0.3.1 (be17d132a 2024-08-21)
const options = {
silent: !core.isDebug(),
};
const execArgs = ["--version"];
let output = "";
options.listeners = {
stdout: (data) => {
output += data.toString();
},
};
yield exec.exec(uvExecutablePath, execArgs, options);
const parts = output.split(" ");
return parts[1].trim();
}); });
} }
@ -89787,6 +89745,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.tryGetFromToolCache = tryGetFromToolCache; exports.tryGetFromToolCache = tryGetFromToolCache;
exports.downloadVersion = downloadVersion; exports.downloadVersion = downloadVersion;
exports.resolveVersion = resolveVersion;
const core = __importStar(__nccwpck_require__(7484)); const core = __importStar(__nccwpck_require__(7484));
const tc = __importStar(__nccwpck_require__(3472)); const tc = __importStar(__nccwpck_require__(3472));
const path = __importStar(__nccwpck_require__(6760)); const path = __importStar(__nccwpck_require__(6760));
@ -90045,27 +90004,16 @@ function run() {
} }
function setupUv(platform, arch, versionInput, checkSum, githubToken) { function setupUv(platform, arch, versionInput, checkSum, githubToken) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let installedPath; const resolvedVersion = yield (0, download_version_1.resolveVersion)(versionInput === "latest"
let cachedToolDir; ? yield (0, download_latest_1.getLatestReleaseVersion)(githubToken)
let version; : versionInput, githubToken);
if (versionInput === "latest") { const toolCacheResult = (0, download_version_1.tryGetFromToolCache)(arch, resolvedVersion);
const latestResult = yield (0, download_latest_1.downloadLatest)(platform, arch, checkSum, githubToken); if (toolCacheResult.installedPath) {
version = latestResult.version; core.info(`Found uv in tool-cache for ${resolvedVersion}`);
cachedToolDir = latestResult.cachedToolDir; return { uvDir: toolCacheResult.installedPath, version: resolvedVersion };
} }
else { const versionResult = yield (0, download_version_1.downloadVersion)(platform, arch, resolvedVersion, checkSum, githubToken);
const toolCacheResult = (0, download_version_1.tryGetFromToolCache)(arch, versionInput); return { uvDir: versionResult.cachedToolDir, version: versionResult.version };
version = toolCacheResult.version;
installedPath = toolCacheResult.installedPath;
if (installedPath) {
core.info(`Found uv in tool-cache for ${versionInput}`);
return { uvDir: installedPath, version };
}
const versionResult = yield (0, download_version_1.downloadVersion)(platform, arch, versionInput, checkSum, githubToken);
cachedToolDir = versionResult.cachedToolDir;
version = versionResult.version;
}
return { uvDir: cachedToolDir, version };
}); });
} }
function addUvToPath(cachedPath) { function addUvToPath(cachedPath) {

View file

@ -1,73 +1,16 @@
import * as core from "@actions/core"; import { OWNER, REPO } from "../utils/constants";
import * as tc from "@actions/tool-cache"; import * as github from "@actions/github";
import * as exec from "@actions/exec";
import * as path from "node:path";
import { promises as fs } from "node:fs";
import type { Architecture, Platform } from "../utils/platforms";
import { validateChecksum } from "./checksum/checksum";
import { OWNER, REPO, TOOL_CACHE_NAME } from "../utils/constants";
export async function downloadLatest( export async function getLatestReleaseVersion(githubToken: string) {
platform: Platform, const octokit = github.getOctokit(githubToken);
arch: Architecture,
checkSum: string | undefined, const { data: latestRelease } = await octokit.rest.repos.getLatestRelease({
githubToken: string | undefined, owner: OWNER,
): Promise<{ cachedToolDir: string; version: string }> { repo: REPO,
const artifact = `uv-${arch}-${platform}`; });
let extension = ".tar.gz";
if (platform === "pc-windows-msvc") { if (latestRelease) {
extension = ".zip"; return latestRelease.tag_name;
} }
const downloadUrl = `https://github.com/${OWNER}/${REPO}/releases/latest/download/${artifact}${extension}`; throw new Error("No releases found for this repository.");
core.info(`Downloading uv from "${downloadUrl}" ...`);
const downloadPath = await tc.downloadTool(
downloadUrl,
undefined,
githubToken,
);
let uvExecutablePath: string;
let uvDir: string;
if (platform === "pc-windows-msvc") {
const fullPathWithExtension = `${downloadPath}${extension}`;
await fs.copyFile(downloadPath, fullPathWithExtension);
uvDir = await tc.extractZip(fullPathWithExtension);
// On windows extracting the zip does not create an intermediate directory
uvExecutablePath = path.join(uvDir, "uv.exe");
} else {
const extractedDir = await tc.extractTar(downloadPath);
uvDir = path.join(extractedDir, artifact);
uvExecutablePath = path.join(uvDir, "uv");
}
const version = await getVersion(uvExecutablePath);
await validateChecksum(checkSum, downloadPath, arch, platform, version);
const cachedToolDir = await tc.cacheDir(
uvDir,
TOOL_CACHE_NAME,
version,
arch,
);
return { cachedToolDir, version };
}
async function getVersion(uvExecutablePath: string): Promise<string> {
// Parse the output of `uv --version` to get the version
// The output looks like
// uv 0.3.1 (be17d132a 2024-08-21)
const options: exec.ExecOptions = {
silent: !core.isDebug(),
};
const execArgs = ["--version"];
let output = "";
options.listeners = {
stdout: (data: Buffer) => {
output += data.toString();
},
};
await exec.exec(uvExecutablePath, execArgs, options);
const parts = output.split(" ");
return parts[1].trim();
} }

View file

@ -70,7 +70,7 @@ export async function downloadVersion(
return { version: resolvedVersion, cachedToolDir }; return { version: resolvedVersion, cachedToolDir };
} }
async function resolveVersion( export async function resolveVersion(
version: string, version: string,
githubToken: string, githubToken: string,
): Promise<string> { ): Promise<string> {

View file

@ -3,10 +3,11 @@ import * as path from "node:path";
import { import {
downloadVersion, downloadVersion,
tryGetFromToolCache, tryGetFromToolCache,
resolveVersion,
} from "./download/download-version"; } from "./download/download-version";
import { restoreCache } from "./cache/restore-cache"; import { restoreCache } from "./cache/restore-cache";
import { downloadLatest } from "./download/download-latest"; import { getLatestReleaseVersion } from "./download/download-latest";
import { import {
type Architecture, type Architecture,
getArch, getArch,
@ -69,38 +70,28 @@ async function setupUv(
checkSum: string | undefined, checkSum: string | undefined,
githubToken: string, githubToken: string,
): Promise<{ uvDir: string; version: string }> { ): Promise<{ uvDir: string; version: string }> {
let installedPath: string | undefined; const resolvedVersion = await resolveVersion(
let cachedToolDir: string; versionInput === "latest"
let version: string; ? await getLatestReleaseVersion(githubToken)
if (versionInput === "latest") { : versionInput,
const latestResult = await downloadLatest(
platform,
arch,
checkSum,
githubToken, githubToken,
); );
version = latestResult.version;
cachedToolDir = latestResult.cachedToolDir; const toolCacheResult = tryGetFromToolCache(arch, resolvedVersion);
} else { if (toolCacheResult.installedPath) {
const toolCacheResult = tryGetFromToolCache(arch, versionInput); core.info(`Found uv in tool-cache for ${resolvedVersion}`);
version = toolCacheResult.version; return { uvDir: toolCacheResult.installedPath, version: resolvedVersion };
installedPath = toolCacheResult.installedPath;
if (installedPath) {
core.info(`Found uv in tool-cache for ${versionInput}`);
return { uvDir: installedPath, version };
} }
const versionResult = await downloadVersion( const versionResult = await downloadVersion(
platform, platform,
arch, arch,
versionInput, resolvedVersion,
checkSum, checkSum,
githubToken, githubToken,
); );
cachedToolDir = versionResult.cachedToolDir;
version = versionResult.version;
}
return { uvDir: cachedToolDir, version }; return { uvDir: versionResult.cachedToolDir, version: versionResult.version };
} }
function addUvToPath(cachedPath: string): void { function addUvToPath(cachedPath: string): void {