Skip to content

Commit 3851bdd

Browse files
committed
Refactor
1 parent cebca53 commit 3851bdd

13 files changed

Lines changed: 105 additions & 76 deletions

File tree

‎src/managers/base/commands/version.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Pep440Version } from '@renovatebot/pep440';
12
import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand';
23

34
/**
@@ -7,5 +8,5 @@ import { BaseExecuteArgs, PackageManagerCommand } from './packageManagerCommand'
78
export abstract class VersionCommand extends PackageManagerCommand {
89
protected static readonly configSection = 'versionCommandArgs';
910

10-
abstract execute(executeArgs?: BaseExecuteArgs): Promise<string>;
11+
abstract execute(executeArgs?: BaseExecuteArgs): Promise<Pep440Version | undefined>;
1112
}

‎src/managers/builtin/commands/availableVersions.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
CommandConstructorOptions,
44
type AvailableVersionsExecuteArgs,
55
} from '../../base/commands/index';
6-
import { runPython, runUV } from '../helpers';
6+
import { runPython, runUV, shouldUseUv } from '../helpers';
77

88
/**
99
* Pip available versions command.
@@ -110,3 +110,12 @@ export class UvAvailableVersionsCommand extends AvailableVersionsCommand {
110110
return availableVersions;
111111
}
112112
}
113+
114+
export async function BuiltinAvailableVersionsCommandFactory(
115+
options: CommandConstructorOptions,
116+
): Promise<AvailableVersionsCommand> {
117+
if (await shouldUseUv(options.log, options.pythonExecutable)) {
118+
return new UvAvailableVersionsCommand(options);
119+
}
120+
return new PipAvailableVersionsCommand(options);
121+
}

‎src/managers/builtin/commands/install.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getConfiguration } from '../../../common/workspace.apis';
22
import { CommandConstructorOptions, InstallCommand, type InstallExecuteArgs } from '../../base/commands/index';
3-
import { runPython, runUV } from '../helpers';
3+
import { runPython, runUV, shouldUseUv } from '../helpers';
44
import { processEditableInstallArgs } from '../utils';
55

66
/**
@@ -87,3 +87,10 @@ export class UvInstallCommand extends InstallCommand {
8787
await runUV(args, undefined, this.log, executeArgs.cancellationToken, this.timeout);
8888
}
8989
}
90+
91+
export async function BuiltinInstallCommandFactory(options: CommandConstructorOptions): Promise<InstallCommand> {
92+
if (await shouldUseUv(options.log, options.pythonExecutable)) {
93+
return new UvInstallCommand(options);
94+
}
95+
return new PipInstallCommand(options);
96+
}

‎src/managers/builtin/commands/list.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PackageInfo } from '../../../api';
22
import { CommandConstructorOptions, ListCommand, type BaseExecuteArgs } from '../../base/commands/index';
3-
import { runPython, runUV } from '../helpers';
3+
import { runPython, runUV, shouldUseUv } from '../helpers';
44

55
/**
66
* Pip list command.
@@ -114,3 +114,10 @@ export class UvListCommand extends ListCommand {
114114
return packages;
115115
}
116116
}
117+
118+
export async function BuiltinListCommandFactory(options: CommandConstructorOptions): Promise<ListCommand> {
119+
if (await shouldUseUv(options.log, options.pythonExecutable)) {
120+
return new UvListCommand(options);
121+
}
122+
return new PipListCommand(options);
123+
}

‎src/managers/builtin/commands/listDirectNames.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommandConstructorOptions, ListDirectNamesCommand, type BaseExecuteArgs } from '../../base/commands/index';
2-
import { runPython, runUV } from '../helpers';
2+
import { runPython, runUV, shouldUseUv } from '../helpers';
33

44
/**
55
* Pip list direct names command.
@@ -99,3 +99,12 @@ export class UvListDirectNamesCommand extends ListDirectNamesCommand {
9999
return directNames;
100100
}
101101
}
102+
103+
export async function BuiltinListDirectNamesCommandFactory(
104+
options: CommandConstructorOptions,
105+
): Promise<ListDirectNamesCommand> {
106+
if (await shouldUseUv(options.log, options.pythonExecutable)) {
107+
return new UvListDirectNamesCommand(options);
108+
}
109+
return new PipListDirectNamesCommand(options);
110+
}

‎src/managers/builtin/commands/uninstall.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommandConstructorOptions, UninstallCommand, type UninstallExecuteArgs } from '../../base/commands/index';
2-
import { runPython, runUV } from '../helpers';
2+
import { runPython, runUV, shouldUseUv } from '../helpers';
33

44
/**
55
* Pip uninstall command.
@@ -52,3 +52,10 @@ export class UvUninstallCommand extends UninstallCommand {
5252
await runUV(args, undefined, this.log, executeArgs.cancellationToken, this.timeout);
5353
}
5454
}
55+
56+
export async function BuiltinUninstallCommandFactory(options: CommandConstructorOptions): Promise<UninstallCommand> {
57+
if (await shouldUseUv(options.log, options.pythonExecutable)) {
58+
return new UvUninstallCommand(options);
59+
}
60+
return new PipUninstallCommand(options);
61+
}

‎src/managers/builtin/commands/version.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { Pep440Version } from '@renovatebot/pep440';
2+
import { explain as parsePep440Version } from '@renovatebot/pep440';
13
import { CommandConstructorOptions, VersionCommand, type BaseExecuteArgs } from '../../base/commands/index';
2-
import { runPython, runUV } from '../helpers';
4+
import { runPython, runUV, shouldUseUv } from '../helpers';
35

46
/**
57
* Pip version command.
@@ -18,13 +20,13 @@ export class PipVersionCommand extends VersionCommand {
1820
return ['-m', 'pip', '--version'];
1921
}
2022

21-
async execute(executeArgs?: BaseExecuteArgs): Promise<string> {
22-
let versionString: string = '';
23+
async execute(executeArgs?: BaseExecuteArgs): Promise<Pep440Version | undefined> {
24+
let parsedVersion: Pep440Version | undefined;
2325

2426
const parser = (output: string): void => {
2527
// "pip X.Y.Z from /path/to/pip (python X.Y)"
2628
const match = output.match(/^pip\s+(\d+\.\d+(?:\.\d+)*)/);
27-
versionString = match ? match[1] : '';
29+
parsedVersion = match ? (parsePep440Version(match[1]) ?? undefined) : undefined;
2830
};
2931

3032
const args = this.buildCommand();
@@ -39,7 +41,7 @@ export class PipVersionCommand extends VersionCommand {
3941
);
4042

4143
parser(output);
42-
return versionString;
44+
return parsedVersion;
4345
}
4446
}
4547

@@ -61,20 +63,27 @@ export class UvVersionCommand extends VersionCommand {
6163
return ['--version'];
6264
}
6365

64-
async execute(executeArgs?: BaseExecuteArgs): Promise<string> {
65-
let versionString: string = '';
66+
async execute(executeArgs?: BaseExecuteArgs): Promise<Pep440Version | undefined> {
67+
let parsedVersion: Pep440Version | undefined;
6668

6769
const parser = (output: string): void => {
6870
// "uv X.Y.Z" format
6971
const match = output.match(/(\d+\.\d+(?:\.\d+)*)/);
70-
versionString = match ? match[1] : '';
72+
parsedVersion = match ? (parsePep440Version(match[1]) ?? undefined) : undefined;
7173
};
7274

7375
const args = this.buildCommand();
7476

7577
const output = await runUV(args, undefined, this.log, executeArgs?.cancellationToken, this.timeout);
7678

7779
parser(output);
78-
return versionString;
80+
return parsedVersion;
7981
}
8082
}
83+
84+
export async function BuiltinVersionCommandFactory(options: CommandConstructorOptions): Promise<VersionCommand> {
85+
if (await shouldUseUv(options.log, options.pythonExecutable)) {
86+
return new UvVersionCommand(options);
87+
}
88+
return new PipVersionCommand(options);
89+
}

‎src/managers/builtin/pipPackageManager.ts‎

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,13 @@ import {
2323
} from '../../api';
2424
import { CommandConstructorOptions } from '../base/commands/index';
2525
import { updatePackagesAndNotify } from '../common/packageChanges';
26-
import {
27-
PipAvailableVersionsCommand,
28-
PipInstallCommand,
29-
PipListCommand,
30-
PipListDirectNamesCommand,
31-
PipUninstallCommand,
32-
PipVersionCommand,
33-
UvAvailableVersionsCommand,
34-
UvInstallCommand,
35-
UvListCommand,
36-
UvListDirectNamesCommand,
37-
UvUninstallCommand,
38-
UvVersionCommand,
39-
} from './commands/index';
40-
import { shouldUseUv } from './helpers';
26+
import { BuiltinAvailableVersionsCommandFactory } from './commands/availableVersions';
27+
import { PipAvailableVersionsCommand } from './commands/index';
28+
import { BuiltinInstallCommandFactory } from './commands/install';
29+
import { BuiltinListCommandFactory } from './commands/list';
30+
import { BuiltinListDirectNamesCommandFactory } from './commands/listDirectNames';
31+
import { BuiltinUninstallCommandFactory } from './commands/uninstall';
32+
import { BuiltinVersionCommandFactory } from './commands/version';
4133
import { getWorkspacePackagesToInstall } from './pipUtils';
4234
import { normalizePackageName, parsePackageSpecs } from './utils';
4335
import { VenvManager } from './venvManager';
@@ -86,9 +78,6 @@ export class PipPackageManager implements PackageManager, Disposable {
8678
throw new Error('Unable to determine Python executable path');
8779
}
8880

89-
// Detect whether to use UV
90-
const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath);
91-
9281
// Centralize command options for install/uninstall operations
9382
const manageCommandOptions: CommandConstructorOptions = {
9483
pythonExecutable,
@@ -97,16 +86,14 @@ export class PipPackageManager implements PackageManager, Disposable {
9786

9887
// Execute uninstall if needed
9988
if (toUninstall.length > 0) {
100-
const UninstallCommand = useUv ? UvUninstallCommand : PipUninstallCommand;
101-
const uninstallCmd = new UninstallCommand(manageCommandOptions);
89+
const uninstallCmd = await BuiltinUninstallCommandFactory(manageCommandOptions);
10290
const packages = parsePackageSpecs(toUninstall);
10391
await uninstallCmd.executeWithProgress({ packages, showProgress: true }, 'Installing packages');
10492
}
10593

10694
// Execute install if needed
10795
if (toInstall.length > 0) {
108-
const InstallCommand = useUv ? UvInstallCommand : PipInstallCommand;
109-
const installCmd = new InstallCommand(manageCommandOptions);
96+
const installCmd = await BuiltinInstallCommandFactory(manageCommandOptions);
11097
const packages = parsePackageSpecs(toInstall);
11198
await installCmd.executeWithProgress(
11299
{ packages, upgrade: options.upgrade, showProgress: true },
@@ -158,10 +145,7 @@ export class PipPackageManager implements PackageManager, Disposable {
158145
if (!pythonExecutable) {
159146
return undefined;
160147
}
161-
162-
const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath);
163-
const ListCmd = useUv ? UvListCommand : PipListCommand;
164-
const listCmd = new ListCmd({
148+
const listCmd = await BuiltinListCommandFactory({
165149
pythonExecutable,
166150
log: this.log,
167151
});
@@ -179,15 +163,11 @@ export class PipPackageManager implements PackageManager, Disposable {
179163
if (!pythonExecutable) {
180164
return undefined;
181165
}
182-
183-
const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath);
184-
const VersionCmd = useUv ? UvVersionCommand : PipVersionCommand;
185-
const versionCmd = new VersionCmd({
166+
const versionCmd = await BuiltinVersionCommandFactory({
186167
pythonExecutable,
187168
log: this.log,
188169
});
189-
const versionString = await versionCmd.execute();
190-
return versionString ? (parse(versionString) ?? undefined) : undefined;
170+
return await versionCmd.execute();
191171
} catch {
192172
return undefined;
193173
}
@@ -208,15 +188,13 @@ export class PipPackageManager implements PackageManager, Disposable {
208188
return undefined;
209189
}
210190

211-
const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath);
212-
const AvailableVersionsCmd = useUv ? UvAvailableVersionsCommand : PipAvailableVersionsCommand;
213-
const availableVersionsCmd = new AvailableVersionsCmd({
191+
const availableVersionsCmd = await BuiltinAvailableVersionsCommandFactory({
214192
pythonExecutable,
215193
log: this.log,
216194
});
217195

218196
// For pip < 21.2.0, check version first
219-
if (!useUv) {
197+
if (availableVersionsCmd instanceof PipAvailableVersionsCommand) {
220198
const pipVersion = await this.getVersion(environment);
221199
if (!pipVersion || compare(pipVersion.public, '21.2.0') < 0) {
222200
// pip <= 20.3.4 - version picking is undefined; no reliable machine-readable API exists.
@@ -228,9 +206,7 @@ export class PipPackageManager implements PackageManager, Disposable {
228206
packageName,
229207
pythonVersion: environment.version,
230208
});
231-
return versionStrings
232-
.map((v) => parse(v))
233-
.filter((parsed): parsed is Pep440Version => parsed !== null);
209+
return versionStrings.map((v) => parse(v)).filter((parsed): parsed is Pep440Version => parsed !== null);
234210
} catch {
235211
return undefined;
236212
}
@@ -252,10 +228,7 @@ export class PipPackageManager implements PackageManager, Disposable {
252228
if (!pythonExecutable) {
253229
return undefined;
254230
}
255-
256-
const useUv = await shouldUseUv(this.log, environment.environmentPath.fsPath);
257-
const ListDirectNamesCmd = useUv ? UvListDirectNamesCommand : PipListDirectNamesCommand;
258-
const listDirectNamesCmd = new ListDirectNamesCmd({
231+
const listDirectNamesCmd = await BuiltinListDirectNamesCommandFactory({
259232
pythonExecutable,
260233
log: this.log,
261234
});

‎src/managers/builtin/pipUtils.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import { findFiles } from '../../common/workspace.apis';
1313
import { selectFromCommonPackagesToInstall, selectFromInstallableToInstall } from '../common/pickers';
1414
import { Installable } from '../common/types';
1515
import { mergePackages } from '../common/utils';
16+
import { BuiltinListCommandFactory } from './commands/list';
1617
import { normalizePackageName } from './utils';
17-
import { PipListCommand, UvListCommand } from './commands/index';
18-
import { shouldUseUv } from './helpers';
1918

2019
export interface PyprojectToml {
2120
project?: {
@@ -279,9 +278,7 @@ export async function getWorkspacePackagesToInstall(
279278
if (environment) {
280279
const pythonExecutable = environment.execInfo?.run?.executable;
281280
if (pythonExecutable) {
282-
const useUv = await shouldUseUv(log, environment.environmentPath.fsPath);
283-
const ListCmd = useUv ? UvListCommand : PipListCommand;
284-
const listCmd = new ListCmd({ pythonExecutable, log });
281+
const listCmd = await BuiltinListCommandFactory({ pythonExecutable, log });
285282
const data = await listCmd.executeWithProgress<{ name: string }[]>({ showProgress: true });
286283
installed = data?.map((pkg) => pkg.name);
287284
}

‎src/managers/conda/commands/version.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Pep440Version } from '@renovatebot/pep440';
2+
import { explain as parsePep440Version } from '@renovatebot/pep440';
13
import { CommandConstructorOptions, VersionCommand, type BaseExecuteArgs } from '../../base/commands/index';
24
import { runCondaExecutable } from '../condaUtils';
35

@@ -19,12 +21,12 @@ export class CondaVersionCommand extends VersionCommand {
1921
return ['--version'];
2022
}
2123

22-
async execute(executeArgs?: BaseExecuteArgs): Promise<string> {
24+
async execute(executeArgs?: BaseExecuteArgs): Promise<Pep440Version | undefined> {
2325
const args = this.buildCommand();
2426
const output = await runCondaExecutable(args, this.log, executeArgs?.cancellationToken);
2527

2628
// "conda X.Y.Z"
2729
const match = output.match(/conda\s+(\d+\.\d+(?:\.\d+)*)/i);
28-
return match ? match[1] : '';
30+
return match ? (parsePep440Version(match[1]) ?? undefined) : undefined;
2931
}
3032
}

0 commit comments

Comments
 (0)