mirror of
https://github.com/astral-sh/setup-uv.git
synced 2026-03-13 12:05:15 +00:00
feat: add venv-path input for activate-environment (#746)
Allow customizing the venv location while preserving working-directory semantics via --directory. Supersedes: #736
This commit is contained in:
committed by
GitHub
parent
99b0f0474b
commit
3511ff7054
@@ -24,6 +24,7 @@ import {
|
||||
resolutionStrategy,
|
||||
toolBinDir,
|
||||
toolDir,
|
||||
venvPath,
|
||||
versionFile as versionFileInput,
|
||||
version as versionInput,
|
||||
workingDirectory,
|
||||
@@ -269,12 +270,10 @@ async function activateEnvironment(): Promise<void> {
|
||||
"UV_NO_MODIFY_PATH and activate-environment cannot be used together.",
|
||||
);
|
||||
}
|
||||
const execArgs = ["venv", ".venv", "--directory", workingDirectory];
|
||||
|
||||
core.info("Activating python venv...");
|
||||
await exec.exec("uv", execArgs);
|
||||
core.info(`Creating and activating python venv at ${venvPath}...`);
|
||||
await exec.exec("uv", ["venv", venvPath, "--directory", workingDirectory]);
|
||||
|
||||
const venvPath = path.resolve(`${workingDirectory}${path.sep}.venv`);
|
||||
let venvBinPath = `${venvPath}${path.sep}bin`;
|
||||
if (process.platform === "win32") {
|
||||
venvBinPath = `${venvPath}${path.sep}Scripts`;
|
||||
|
||||
@@ -14,6 +14,7 @@ export const version = core.getInput("version");
|
||||
export const versionFile = getVersionFile();
|
||||
export const pythonVersion = core.getInput("python-version");
|
||||
export const activateEnvironment = core.getBooleanInput("activate-environment");
|
||||
export const venvPath = getVenvPath();
|
||||
export const checkSum = core.getInput("checksum");
|
||||
export const enableCache = getEnableCache();
|
||||
export const restoreCache = core.getInput("restore-cache") === "true";
|
||||
@@ -45,6 +46,18 @@ function getVersionFile(): string {
|
||||
return versionFileInput;
|
||||
}
|
||||
|
||||
function getVenvPath(): string {
|
||||
const venvPathInput = core.getInput("venv-path");
|
||||
if (venvPathInput !== "") {
|
||||
if (!activateEnvironment) {
|
||||
core.warning("venv-path is only used when activate-environment is true");
|
||||
}
|
||||
const tildeExpanded = expandTilde(venvPathInput);
|
||||
return normalizePath(resolveRelativePath(tildeExpanded));
|
||||
}
|
||||
return normalizePath(resolveRelativePath(".venv"));
|
||||
}
|
||||
|
||||
function getEnableCache(): boolean {
|
||||
const enableCacheInput = core.getInput("enable-cache");
|
||||
if (enableCacheInput === "auto") {
|
||||
@@ -194,6 +207,19 @@ function expandTilde(input: string): string {
|
||||
return input;
|
||||
}
|
||||
|
||||
function normalizePath(inputPath: string): string {
|
||||
const normalized = path.normalize(inputPath);
|
||||
const root = path.parse(normalized).root;
|
||||
|
||||
// Remove any trailing path separators, except when the whole path is the root.
|
||||
let trimmed = normalized;
|
||||
while (trimmed.length > root.length && trimmed.endsWith(path.sep)) {
|
||||
trimmed = trimmed.slice(0, -1);
|
||||
}
|
||||
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function resolveRelativePath(inputPath: string): string {
|
||||
const hasNegation = inputPath.startsWith("!");
|
||||
const pathWithoutNegation = hasNegation ? inputPath.substring(1) : inputPath;
|
||||
|
||||
Reference in New Issue
Block a user