Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/managers/base/commands/availableVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Pep440Version } from '@renovatebot/pep440';
import { explain as parsePep440Version } from '@renovatebot/pep440';
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';

/**
* Arguments for available versions command execution (change per execution).
*/
export interface AvailableVersionsExecuteArgs extends BaseExecuteArgs {
packageName: string;
pythonVersion: string;
includePrerelease?: boolean;
Comment thread
edvilme marked this conversation as resolved.
}

/**
* Template class for availableVersions commands.
* Subclasses implement concrete package-manager-specific logic.
*/
export abstract class AvailableVersionsCommand extends PackageManagerCommand {
protected static readonly configSection = 'availableVersionsCommandArgs';
protected abstract buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[];
protected parseVersions(versions: string[], includePrerelease?: boolean): Pep440Version[] {
let parsed = Array.from(new Set(versions))
.map((version) => parsePep440Version(version.trim()))
.filter((version): version is Pep440Version => version !== null);
if (includePrerelease === false) {
parsed = parsed.filter((version) => !version.is_prerelease);
}
return parsed;
}
abstract execute(executeArgs: AvailableVersionsExecuteArgs): Promise<Pep440Version[]>;
}
7 changes: 7 additions & 0 deletions src/managers/base/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { AvailableVersionsCommand, type AvailableVersionsExecuteArgs } from './availableVersions';
export { InstallCommand, type InstallExecuteArgs } from './install';
export { ListCommand } from './list';
export { ListDirectNamesCommand } from './listDirectNames';
export { BaseExecuteArgs, CommandConstructorOptions, PackageManagerCommand } from './packageManagerCommand';
export { UninstallCommand, type UninstallExecuteArgs } from './uninstall';
export { VersionCommand } from './version';
19 changes: 19 additions & 0 deletions src/managers/base/commands/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';

/**
* Arguments for install command execution (change per execution).
*/
export interface InstallExecuteArgs extends BaseExecuteArgs {
packages: { packageName: string; version?: string }[];
upgrade?: boolean;
}

/**
* Template class for install commands.
* Subclasses implement concrete package-manager-specific logic.
*/
export abstract class InstallCommand extends PackageManagerCommand {
protected static readonly configSection = 'installCommandArgs';
protected abstract buildCommand(executeArgs: InstallExecuteArgs): string[];
abstract execute(executeArgs: InstallExecuteArgs): Promise<void>;
}
12 changes: 12 additions & 0 deletions src/managers/base/commands/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PackageInfo } from '../../../api';
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';

/**
* Template class for list commands.
* Subclasses implement concrete package-manager-specific logic.
*/
export abstract class ListCommand extends PackageManagerCommand {
protected static readonly configSection = 'listCommandArgs';
protected timeout = 30000;
abstract execute(executeArgs?: BaseExecuteArgs): Promise<PackageInfo[]>;
}
11 changes: 11 additions & 0 deletions src/managers/base/commands/listDirectNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';

/**
* Template class for listDirectNames commands.
* Subclasses implement concrete package-manager-specific logic.
*/
export abstract class ListDirectNamesCommand extends PackageManagerCommand {
protected static readonly configSection = 'listDirectNamesCommandArgs';
protected timeout = 30000;
abstract execute(executeArgs?: BaseExecuteArgs): Promise<Set<string>>;
}
46 changes: 46 additions & 0 deletions src/managers/base/commands/packageManagerCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { CancellationToken, LogOutputChannel, WorkspaceConfiguration } from 'vscode';
import { getConfiguration } from '../../../common/workspace.apis';

/**
* Base interface for all command execute arguments.
* Provides optional cancellation token that all commands can use.
*/
export interface BaseExecuteArgs {
cancellationToken?: CancellationToken;
}

/**
* Constructor options shared by all package manager commands.
*/
export interface CommandConstructorOptions {
pythonExecutable: string;
Comment thread
edvilme marked this conversation as resolved.
cwd?: string;
log?: LogOutputChannel;
}

/**
* Base class for all package manager commands.
* Provides common properties and minimal interface for subclasses.
*/
export abstract class PackageManagerCommand {
protected static readonly configSection?: string;

protected pythonExecutable: string;
protected cwd?: string;
protected log?: LogOutputChannel;
protected timeout: number = 300000;
Comment thread
edvilme marked this conversation as resolved.
protected config?: WorkspaceConfiguration;

constructor(options: CommandConstructorOptions) {
this.pythonExecutable = options.pythonExecutable;
this.cwd = options.cwd;
this.log = options.log;
const configSection = (this.constructor as typeof PackageManagerCommand).configSection;
this.config = configSection ? getConfiguration(`python-envs.packageManager.${configSection}`) : undefined;
}

/**
* Subclasses implement to build the command arguments.
*/
protected abstract buildCommand(executeArgs: BaseExecuteArgs): string[];
}
18 changes: 18 additions & 0 deletions src/managers/base/commands/uninstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';

/**
* Arguments for uninstall command execution (change per execution).
*/
export interface UninstallExecuteArgs extends BaseExecuteArgs {
packages: { packageName: string; version?: string }[];
}

/**
* Template class for uninstall commands.
* Subclasses implement concrete package-manager-specific logic.
*/
export abstract class UninstallCommand extends PackageManagerCommand {
protected static readonly configSection = 'uninstallCommandArgs';
protected abstract buildCommand(executeArgs: UninstallExecuteArgs): string[];
abstract execute(executeArgs: UninstallExecuteArgs): Promise<void>;
}
11 changes: 11 additions & 0 deletions src/managers/base/commands/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Pep440Version } from '@renovatebot/pep440';
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';

/**
* Template class for version commands.
* Subclasses implement concrete package-manager-specific logic.
*/
export abstract class VersionCommand extends PackageManagerCommand {
protected static readonly configSection = 'versionCommandArgs';
abstract execute(executeArgs?: BaseExecuteArgs): Promise<Pep440Version | undefined>;
}
93 changes: 93 additions & 0 deletions src/managers/builtin/commands/availableVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import type { Pep440Version } from '@renovatebot/pep440';
import { AvailableVersionsCommand, type AvailableVersionsExecuteArgs } from '../../base/commands/index';
import { runPython, runUV } from '../helpers';

/**
* Pip available versions command.
* Parsed command: `python -m pip index versions <package> --json --python-version <version>`
* Official documentation: https://pip.pypa.io/en/stable/cli/pip_index/
*/
export class PipAvailableVersionsCommand extends AvailableVersionsCommand {
protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] {
return [
'-m',
'pip',
'index',
'versions',
executeArgs.packageName,
'--json',
'--python-version',
executeArgs.pythonVersion,
];
}

async execute(executeArgs: AvailableVersionsExecuteArgs): Promise<Pep440Version[]> {
const output = await runPython(
this.pythonExecutable,
this.buildCommand(executeArgs),
undefined,
this.log,
executeArgs.cancellationToken,
this.timeout,
);
const match = output.match(/{[\s\S]*}/);
if (!match) {
return [];
}

try {
const parsed = JSON.parse(match[0]) as { versions?: string[] };
return this.parseVersions(
Array.isArray(parsed.versions) ? parsed.versions : [],
executeArgs.includePrerelease,
);
} catch {
return [];
}
}
}

/**
* UV available versions command.
* Parsed command: `uv tool run pip index versions <package> --json --python-version <version>`
* Official documentation: https://docs.astral.sh/uv/pip/
*/
export class UvAvailableVersionsCommand extends AvailableVersionsCommand {
protected buildCommand(executeArgs: AvailableVersionsExecuteArgs): string[] {
return [
'tool',
'run',
'pip',
'index',
'versions',
executeArgs.packageName,
'--json',
'--python-version',
executeArgs.pythonVersion,
];
}

async execute(executeArgs: AvailableVersionsExecuteArgs): Promise<Pep440Version[]> {
const output = await runUV(
this.buildCommand(executeArgs),
undefined,
this.log,
executeArgs.cancellationToken,
this.timeout,
);
const match = output.match(/{[\s\S]*}/);
if (!match) {
return [];
}

try {
const parsed = JSON.parse(match[0]) as { versions?: string[] };
return this.parseVersions(
Array.isArray(parsed.versions) ? parsed.versions : [],
executeArgs.includePrerelease,
);
} catch {
return [];
}
}
}
13 changes: 13 additions & 0 deletions src/managers/builtin/commands/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CommandConstructorOptions } from '../../base/commands/index';
import { shouldUseUv } from '../helpers';

type CommandConstructor<T> = new (options: CommandConstructorOptions) => T;

export async function createPipOrUvCommand<T, P extends T, U extends T>(
options: CommandConstructorOptions,
environmentPath: string,
PipCommand: CommandConstructor<P>,
UvCommand: CommandConstructor<U>,
): Promise<T> {
return (await shouldUseUv(options.log, environmentPath)) ? new UvCommand(options) : new PipCommand(options);
}
6 changes: 6 additions & 0 deletions src/managers/builtin/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { PipAvailableVersionsCommand, UvAvailableVersionsCommand } from './availableVersions';
export { PipInstallCommand, UvInstallCommand } from './install';
export { PipListCommand, UvListCommand } from './list';
export { PipListDirectNamesCommand, UvListDirectNamesCommand } from './listDirectNames';
export { PipUninstallCommand, UvUninstallCommand } from './uninstall';
export { PipVersionCommand, UvVersionCommand } from './version';
52 changes: 52 additions & 0 deletions src/managers/builtin/commands/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { InstallCommand, type InstallExecuteArgs } from '../../base/commands/index';
import { runPython, runUV } from '../helpers';
import { processEditableInstallArgs } from '../utils';

/**
* Pip install command.
* Parsed command: `python -m pip install [--upgrade] <package>`
* Official documentation: https://pip.pypa.io/en/stable/cli/pip_install/
*/
export class PipInstallCommand extends InstallCommand {
protected buildCommand(executeArgs: InstallExecuteArgs): string[] {
let args = ['-m', 'pip', 'install'];
if (executeArgs.upgrade) {
args.push('--upgrade');
}
const processedArgs = processEditableInstallArgs(executeArgs.packages.map((pkg) => pkg.packageName));
args.push(...processedArgs);
return args;
}

async execute(executeArgs: InstallExecuteArgs): Promise<void> {
await runPython(
this.pythonExecutable,
this.buildCommand(executeArgs),
undefined,
this.log,
executeArgs.cancellationToken,
this.timeout,
);
}
}

/**
* UV install command.
* Parsed command: `uv pip install --python <path> [--upgrade] <package>`
* Official documentation: https://docs.astral.sh/uv/pip/
*/
export class UvInstallCommand extends InstallCommand {
protected buildCommand(executeArgs: InstallExecuteArgs): string[] {
let args = ['pip', 'install', '--python', this.pythonExecutable];
if (executeArgs.upgrade) {
args.push('--upgrade');
}
const processedArgs = processEditableInstallArgs(executeArgs.packages.map((pkg) => pkg.packageName));
args.push(...processedArgs);
return args;
}

async execute(executeArgs: InstallExecuteArgs): Promise<void> {
await runUV(this.buildCommand(executeArgs), undefined, this.log, executeArgs.cancellationToken, this.timeout);
}
Comment thread
edvilme marked this conversation as resolved.
}
Loading
Loading