-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal-parameter.ts
More file actions
61 lines (52 loc) · 2.27 KB
/
Copy pathglobal-parameter.ts
File metadata and controls
61 lines (52 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { getPty, ParameterSetting, SpawnStatus, StatefulParameter } from '@codifycli/plugin-core';
import { UvConfig } from './uv.js';
/**
* Manages the global default Python version exposed on PATH via uv.
*
* `uv python install <version> --default` installs unversioned `python` and
* `python3` symlinks into ~/.local/bin, making that version the system-wide
* default outside of any project context.
*
* To detect the current default, we read the symlink at ~/.local/bin/python
* and parse the cpython version string from the target path.
*/
export class UvGlobalParameter extends StatefulParameter<UvConfig, string> {
getSettings(): ParameterSetting {
return {
type: 'version',
};
}
override async refresh(): Promise<string | null> {
const $ = getPty();
// Check if ~/.local/bin/python exists and points to a uv-managed interpreter.
// `readlink` resolves the symlink target; if it contains cpython we know it
// was installed by uv with --default.
const { status, data } = await $.spawnSafe('readlink ~/.local/bin/python');
if (status === SpawnStatus.ERROR || !data.trim()) {
return null;
}
const { status: versionStatus, data: versionData } = await $.spawnSafe('python --version');
if (versionStatus === SpawnStatus.ERROR) {
return null;
}
const match = versionData.trim().match(/Python\s+(\S+)/);
return match ? match[1] ?? null : null;
}
override async add(version: string): Promise<void> {
const $ = getPty();
await $.spawnSafe(`uv python install ${version} --default`, { interactive: true });
await $.spawnSafe('uv python update-shell', { interactive: true })
}
override async modify(newVersion: string): Promise<void> {
const $ = getPty();
await $.spawn(`uv python install ${newVersion} --default`, { interactive: true });
}
override async remove(_version: string): Promise<void> {
const $ = getPty();
// uv has no "unset default" command. Remove the unversioned symlinks that
// --default created in ~/.local/bin so `python` / `python3` no longer
// resolve to this uv-managed interpreter. The versioned binary is left
// intact because it may still be listed in pythonVersions.
await $.spawnSafe('rm -f ~/.local/bin/python ~/.local/bin/python3');
}
}