Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Jul 27, 2022
1 parent c00ae65 commit fd48475
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 deletions.
9 changes: 3 additions & 6 deletions src/client/common/terminal/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TerminalServiceFactory implements ITerminalServiceFactory {
const title = options?.title;
const terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
const interpreter = options?.interpreter;
const id = this.getTerminalId(terminalTitle, resource, interpreter);
const id = this.getTerminalId(terminalTitle, resource, interpreter, options);
if (!this.terminalServices.has(id)) {
const terminalService = new TerminalService(this.serviceContainer, options);
this.terminalServices.set(id, terminalService);
Expand All @@ -46,13 +46,10 @@ export class TerminalServiceFactory implements ITerminalServiceFactory {
title = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python';
return new TerminalService(this.serviceContainer, { resource, title });
}
private getTerminalId(title: string, resource?: Uri, interpreter?: PythonEnvironment): string {
if (!resource && !interpreter) {
return title;
}
private getTerminalId(title: string, resource?: Uri, interpreter?: PythonEnvironment, options?: TerminalCreationOptions): string {
const workspaceFolder = this.serviceContainer
.get<IWorkspaceService>(IWorkspaceService)
.getWorkspaceFolder(resource || undefined);
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}`;
return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}:${options?.message}`;
}
}
2 changes: 2 additions & 0 deletions src/client/common/terminal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class TerminalService implements ITerminalService, Disposable {
this.terminalAutoActivator = this.serviceContainer.get<ITerminalAutoActivation>(ITerminalAutoActivation);
this.terminalManager.onDidCloseTerminal(this.terminalCloseHandler, this, disposableRegistry);
this.terminalActivator = this.serviceContainer.get<ITerminalActivator>(ITerminalActivator);
this.ensureTerminal().ignoreErrors();
}
public dispose() {
if (this.terminal) {
Expand Down Expand Up @@ -78,6 +79,7 @@ export class TerminalService implements ITerminalService, Disposable {
name: this.options?.title || 'Python',
env: this.options?.env,
hideFromUser: this.options?.hideFromUser,
message: this.options?.message,
});
this.terminalAutoActivator.disableAutoActivation(this.terminal);

Expand Down
6 changes: 6 additions & 0 deletions src/client/common/terminal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export type TerminalCreationOptions = {
* @type {boolean}
*/
hideFromUser?: boolean;
/**
* A message to write to the terminal on first launch, note that this is not sent to the
* process but, rather written directly to the terminal. This supports escape sequences such
* a setting text style.
*/
message?: string;
};

export interface ITerminalServiceFactory {
Expand Down
4 changes: 4 additions & 0 deletions src/client/common/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ export namespace Interpreters {
'Interpreters.selectInterpreterTip',
'Tip: you can change the Python interpreter used by the Python extension by clicking on the Python version in the status bar',
);
export const installPythonTerminalMessage = localize(
'Interpreters.installPythonTerminalMessage',
'Please try installing python package using your package manager. Alternatively you can also download it from https://www.python.org/downloads',
);
}

export namespace InterpreterQuickPickList {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { ICommandManager } from '../../../../../common/application/types';
import { sleep } from '../../../../../common/utils/async';
import { OSType } from '../../../../../common/utils/platform';
import { traceVerbose } from '../../../../../logging';
import { Interpreters } from '../../../../../common/utils/localize';

enum PackageManagers {
brew = 'brew',
apt = 'apt',
dnf = 'dnf',
}

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

private readonly packageManagerCommands: Record<PackageManagers, string[]> = {
brew: ['brew install python3'],
dnf: ['sudo dnf install python3'],
apt: ['sudo apt-get update', 'sudo apt-get install python3 python3-venv python3-pip'],
};

constructor(
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(ITerminalServiceFactory) private readonly terminalServiceFactory: ITerminalServiceFactory,
Expand All @@ -42,36 +55,42 @@ export class InstallPythonViaTerminal implements IExtensionSingleActivationServi
}

public async _installPythonOnUnix(os: OSType.Linux | OSType.OSX): Promise<void> {
const terminalService = this.terminalServiceFactory.getTerminalService({});
const commands = await getCommands(os);
const commands = await this.getCommands(os);
const terminalService = this.terminalServiceFactory.getTerminalService({
message: commands.length ? undefined : Interpreters.installPythonTerminalMessage,
});
for (const command of commands) {
await terminalService.sendText(command);
await waitForCommandToProcess();
}
}
}

async function getCommands(os: OSType.Linux | OSType.OSX) {
if (os === OSType.OSX) {
return ['brew install python3'];
private async getCommands(os: OSType.Linux | OSType.OSX) {
if (os === OSType.OSX) {
return this.packageManagerCommands[PackageManagers.brew];
}
return this.getCommandsForLinux();
}
return getCommandsForLinux();
}

async function getCommandsForLinux() {
let isDnfAvailable = false;
try {
const which = require('which') as typeof whichTypes;
const resolvedPath = await which('dnf');
traceVerbose('Resolved path to dnf module:', resolvedPath);
isDnfAvailable = resolvedPath.trim().length > 0;
} catch (ex) {
traceVerbose('Dnf not found', ex);
isDnfAvailable = false;
private async getCommandsForLinux() {
for (const packageManager of [PackageManagers.apt, PackageManagers.dnf]) {
let isPackageAvailable = false;
try {
const which = require('which') as typeof whichTypes;
const resolvedPath = await which(packageManager);
traceVerbose(`Resolved path to ${packageManager} module:`, resolvedPath);
isPackageAvailable = resolvedPath.trim().length > 0;
} catch (ex) {
traceVerbose(`${packageManager} not found`, ex);
isPackageAvailable = false;
}
isPackageAvailable = false;
if (isPackageAvailable) {
return this.packageManagerCommands[packageManager];
}
}
return [];
}
return isDnfAvailable
? ['sudo dnf install python3']
: ['sudo apt-get update', 'sudo apt-get install python3 python3-venv python3-pip'];
}

async function waitForCommandToProcess() {
Expand Down
10 changes: 1 addition & 9 deletions src/client/pythonEnvironments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,15 @@ function createNonWorkspaceLocators(ext: ExtensionState): ILocator<BasicEnvInfo>
const locators: (ILocator<BasicEnvInfo> & Partial<IDisposable>)[] = [];
locators.push(
// OS-independent locators go here.
new PyenvLocator(),
new CondaEnvironmentLocator(),
new GlobalVirtualEnvironmentLocator(),
new CustomVirtualEnvironmentLocator(),
);

if (getOSType() === OSType.Windows) {
locators.push(
// Windows specific locators go here.
new WindowsRegistryLocator(),
new WindowsStoreLocator(),
new WindowsPathEnvVarLocator(),
);
} else {
locators.push(
// Linux/Mac locators go here.
new PosixKnownPathsLocator(),
);
}

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

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

0 comments on commit fd48475

Please sign in to comment.