-
Notifications
You must be signed in to change notification settings - Fork 56
Introduce package manager command classes #1621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edvilme
wants to merge
3
commits into
main
Choose a base branch
from
package-manager-command-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+945
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * 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[]>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[]>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
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; | ||
|
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[]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 []; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
edvilme marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.