Skip to content

Commit fd48475

Browse files
author
Kartik Raj
committed
Temp
1 parent c00ae65 commit fd48475

File tree

6 files changed

+56
-36
lines changed

6 files changed

+56
-36
lines changed

src/client/common/terminal/factory.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class TerminalServiceFactory implements ITerminalServiceFactory {
2828
const title = options?.title;
2929
const terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
3030
const interpreter = options?.interpreter;
31-
const id = this.getTerminalId(terminalTitle, resource, interpreter);
31+
const id = this.getTerminalId(terminalTitle, resource, interpreter, options);
3232
if (!this.terminalServices.has(id)) {
3333
const terminalService = new TerminalService(this.serviceContainer, options);
3434
this.terminalServices.set(id, terminalService);
@@ -46,13 +46,10 @@ export class TerminalServiceFactory implements ITerminalServiceFactory {
4646
title = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
4747
return new TerminalService(this.serviceContainer, { resource, title });
4848
}
49-
private getTerminalId(title: string, resource?: Uri, interpreter?: PythonEnvironment): string {
50-
if (!resource && !interpreter) {
51-
return title;
52-
}
49+
private getTerminalId(title: string, resource?: Uri, interpreter?: PythonEnvironment, options?: TerminalCreationOptions): string {
5350
const workspaceFolder = this.serviceContainer
5451
.get<IWorkspaceService>(IWorkspaceService)
5552
.getWorkspaceFolder(resource || undefined);
56-
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}`;
53+
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}:${options?.message}`;
5754
}
5855
}

src/client/common/terminal/service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class TerminalService implements ITerminalService, Disposable {
4242
this.terminalAutoActivator = this.serviceContainer.get<ITerminalAutoActivation>(ITerminalAutoActivation);
4343
this.terminalManager.onDidCloseTerminal(this.terminalCloseHandler, this, disposableRegistry);
4444
this.terminalActivator = this.serviceContainer.get<ITerminalActivator>(ITerminalActivator);
45+
this.ensureTerminal().ignoreErrors();
4546
}
4647
public dispose() {
4748
if (this.terminal) {
@@ -78,6 +79,7 @@ export class TerminalService implements ITerminalService, Disposable {
7879
name: this.options?.title || 'Python',
7980
env: this.options?.env,
8081
hideFromUser: this.options?.hideFromUser,
82+
message: this.options?.message,
8183
});
8284
this.terminalAutoActivator.disableAutoActivation(this.terminal);
8385

src/client/common/terminal/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export type TerminalCreationOptions = {
8484
* @type {boolean}
8585
*/
8686
hideFromUser?: boolean;
87+
/**
88+
* A message to write to the terminal on first launch, note that this is not sent to the
89+
* process but, rather written directly to the terminal. This supports escape sequences such
90+
* a setting text style.
91+
*/
92+
message?: string;
8793
};
8894

8995
export interface ITerminalServiceFactory {

src/client/common/utils/localize.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ export namespace Interpreters {
277277
'Interpreters.selectInterpreterTip',
278278
'Tip: you can change the Python interpreter used by the Python extension by clicking on the Python version in the status bar',
279279
);
280+
export const installPythonTerminalMessage = localize(
281+
'Interpreters.installPythonTerminalMessage',
282+
'Please try installing python package using your package manager. Alternatively you can also download it from https://www.python.org/downloads',
283+
);
280284
}
281285

282286
export namespace InterpreterQuickPickList {

src/client/interpreter/configuration/interpreterSelector/commands/installPython/installPythonViaTerminal.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import { ICommandManager } from '../../../../../common/application/types';
1414
import { sleep } from '../../../../../common/utils/async';
1515
import { OSType } from '../../../../../common/utils/platform';
1616
import { traceVerbose } from '../../../../../logging';
17+
import { Interpreters } from '../../../../../common/utils/localize';
18+
19+
enum PackageManagers {
20+
brew = 'brew',
21+
apt = 'apt',
22+
dnf = 'dnf',
23+
}
1724

1825
/**
1926
* Runs commands listed in walkthrough to install Python.
@@ -22,6 +29,12 @@ import { traceVerbose } from '../../../../../logging';
2229
export class InstallPythonViaTerminal implements IExtensionSingleActivationService {
2330
public readonly supportedWorkspaceTypes = { untrustedWorkspace: true, virtualWorkspace: false };
2431

32+
private readonly packageManagerCommands: Record<PackageManagers, string[]> = {
33+
brew: ['brew install python3'],
34+
dnf: ['sudo dnf install python3'],
35+
apt: ['sudo apt-get update', 'sudo apt-get install python3 python3-venv python3-pip'],
36+
};
37+
2538
constructor(
2639
@inject(ICommandManager) private readonly commandManager: ICommandManager,
2740
@inject(ITerminalServiceFactory) private readonly terminalServiceFactory: ITerminalServiceFactory,
@@ -42,36 +55,42 @@ export class InstallPythonViaTerminal implements IExtensionSingleActivationServi
4255
}
4356

4457
public async _installPythonOnUnix(os: OSType.Linux | OSType.OSX): Promise<void> {
45-
const terminalService = this.terminalServiceFactory.getTerminalService({});
46-
const commands = await getCommands(os);
58+
const commands = await this.getCommands(os);
59+
const terminalService = this.terminalServiceFactory.getTerminalService({
60+
message: commands.length ? undefined : Interpreters.installPythonTerminalMessage,
61+
});
4762
for (const command of commands) {
4863
await terminalService.sendText(command);
4964
await waitForCommandToProcess();
5065
}
5166
}
52-
}
5367

54-
async function getCommands(os: OSType.Linux | OSType.OSX) {
55-
if (os === OSType.OSX) {
56-
return ['brew install python3'];
68+
private async getCommands(os: OSType.Linux | OSType.OSX) {
69+
if (os === OSType.OSX) {
70+
return this.packageManagerCommands[PackageManagers.brew];
71+
}
72+
return this.getCommandsForLinux();
5773
}
58-
return getCommandsForLinux();
59-
}
6074

61-
async function getCommandsForLinux() {
62-
let isDnfAvailable = false;
63-
try {
64-
const which = require('which') as typeof whichTypes;
65-
const resolvedPath = await which('dnf');
66-
traceVerbose('Resolved path to dnf module:', resolvedPath);
67-
isDnfAvailable = resolvedPath.trim().length > 0;
68-
} catch (ex) {
69-
traceVerbose('Dnf not found', ex);
70-
isDnfAvailable = false;
75+
private async getCommandsForLinux() {
76+
for (const packageManager of [PackageManagers.apt, PackageManagers.dnf]) {
77+
let isPackageAvailable = false;
78+
try {
79+
const which = require('which') as typeof whichTypes;
80+
const resolvedPath = await which(packageManager);
81+
traceVerbose(`Resolved path to ${packageManager} module:`, resolvedPath);
82+
isPackageAvailable = resolvedPath.trim().length > 0;
83+
} catch (ex) {
84+
traceVerbose(`${packageManager} not found`, ex);
85+
isPackageAvailable = false;
86+
}
87+
isPackageAvailable = false;
88+
if (isPackageAvailable) {
89+
return this.packageManagerCommands[packageManager];
90+
}
91+
}
92+
return [];
7193
}
72-
return isDnfAvailable
73-
? ['sudo dnf install python3']
74-
: ['sudo apt-get update', 'sudo apt-get install python3 python3-venv python3-pip'];
7594
}
7695

7796
async function waitForCommandToProcess() {

src/client/pythonEnvironments/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,15 @@ function createNonWorkspaceLocators(ext: ExtensionState): ILocator<BasicEnvInfo>
134134
const locators: (ILocator<BasicEnvInfo> & Partial<IDisposable>)[] = [];
135135
locators.push(
136136
// OS-independent locators go here.
137-
new PyenvLocator(),
138-
new CondaEnvironmentLocator(),
139-
new GlobalVirtualEnvironmentLocator(),
140-
new CustomVirtualEnvironmentLocator(),
141137
);
142138

143139
if (getOSType() === OSType.Windows) {
144140
locators.push(
145141
// Windows specific locators go here.
146-
new WindowsRegistryLocator(),
147-
new WindowsStoreLocator(),
148-
new WindowsPathEnvVarLocator(),
149142
);
150143
} else {
151144
locators.push(
152145
// Linux/Mac locators go here.
153-
new PosixKnownPathsLocator(),
154146
);
155147
}
156148

@@ -179,7 +171,7 @@ function watchRoots(args: WatchRootsArgs): IDisposable {
179171

180172
function createWorkspaceLocator(ext: ExtensionState): WorkspaceLocators<BasicEnvInfo> {
181173
const locators = new WorkspaceLocators<BasicEnvInfo>(watchRoots, [
182-
(root: vscode.Uri) => [new WorkspaceVirtualEnvironmentLocator(root.fsPath), new PoetryLocator(root.fsPath)],
174+
(root: vscode.Uri) => [],
183175
// Add an ILocator factory func here for each kind of workspace-rooted locator.
184176
]);
185177
ext.disposables.push(locators);

0 commit comments

Comments
 (0)