Skip to content

Commit

Permalink
[IMP] vscode: Client code cleaning + Python extension integration
Browse files Browse the repository at this point in the history
Changes:
- Remove pythonPath field from Config view
- Integrate Python extension
- Properly use async/await in the code.
- Use of global.d.ts for Global variables.
- Wait for LanguageClient to stop before starting it again
- Queue Configuration change when LanguageClient is still stopping
- Properly hide Crash notification test command in Production
- outputChannel is now stored globally instead of being passed to all
functions
- Logging is now done through LanguageClient
- Add a Bash shebang to build_package.sh
  • Loading branch information
Louciole authored and delvsola committed Nov 22, 2023
1 parent d5735a2 commit 960903f
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 381 deletions.
2 changes: 2 additions & 0 deletions vscode/build_package.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions vscode/client/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as path from 'path';

const folderName = path.basename(__dirname);
export const EXTENSION_ROOT_DIR =
folderName === 'common' ? path.dirname(path.dirname(__dirname)) : path.dirname(__dirname);
export const BUNDLED_PYTHON_SCRIPTS_DIR = path.join(EXTENSION_ROOT_DIR, 'server');
export const SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, '__main__.py');
export const DEBUG_SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, `_debug_server.py`);
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import {EventEmitter} from "vscode";

export const selectedConfigurationChange = new EventEmitter();
export const ConfigurationsChange = new EventEmitter();
export const clientStopped = new EventEmitter();
87 changes: 87 additions & 0 deletions vscode/client/common/python.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/* eslint-disable @typescript-eslint/naming-convention */
import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode';
import { PythonExtension, ResolvedEnvironment, VersionInfo } from '@vscode/python-extension';

export interface IInterpreterDetails {
path?: string[];
resource?: Uri;
}

const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;

let _api: PythonExtension | undefined;
async function getPythonExtensionAPI(): Promise<PythonExtension | undefined> {
if (_api) {
return _api;
}
_api = await PythonExtension.api();
return _api;
}

export async function initializePython(disposables: Disposable[]): Promise<void> {
try {
const api = await getPythonExtensionAPI();

if (api) {
disposables.push(
api.environments.onDidChangeActiveEnvironmentPath((e) => {
onDidChangePythonInterpreterEvent.fire({ path: [e.path], resource: e.resource?.uri });
}),
);

console.log('Waiting for interpreter from python extension.');
onDidChangePythonInterpreterEvent.fire(await getInterpreterDetails());
}
} catch (error) {
console.error('Error initializing python: ', error);
}
}

export async function resolveInterpreter(interpreter: string[]): Promise<ResolvedEnvironment | undefined> {
const api = await getPythonExtensionAPI();
return api?.environments.resolveEnvironment(interpreter[0]);
}

export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
const api = await getPythonExtensionAPI();
const environment = await api?.environments.resolveEnvironment(
api?.environments.getActiveEnvironmentPath(resource),
);
if (environment?.executable.uri && checkVersion(environment)) {
return { path: [environment?.executable.uri.fsPath], resource };
}
return { path: undefined, resource };
}

export async function getDebuggerPath(): Promise<string | undefined> {
const api = await getPythonExtensionAPI();
return api?.debug.getDebuggerPackagePath();
}

export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
await getPythonExtensionAPI();
return await commands.executeCommand(command, ...rest);
}

export function checkVersion(resolved: ResolvedEnvironment | undefined): boolean {
const version = resolved?.version;
if (version?.major === 3 && version?.minor >= 8) {
return true;
}
console.error(`Python version ${version?.major}.${version?.minor} is not supported.`);
console.error(`Selected python path: ${resolved?.executable.uri?.fsPath}`);
console.error('Supported versions are 3.8 and above.');
return false;
}

export async function getPythonVersion(): Promise<VersionInfo> {
let interpreterDetails = await getInterpreterDetails();
let resolved = await resolveInterpreter(interpreterDetails.path);
let version = resolved.version;

return version;
}
11 changes: 1 addition & 10 deletions vscode/client/utils/utils.ts → vscode/client/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { execSync } from "child_process";
import { ExtensionContext, Uri, Webview } from "vscode";

/**
Expand All @@ -25,17 +24,9 @@ export function getNonce() {
return text;
}

export function getPythonVersion(context: ExtensionContext, pythonPath: String = "python3") {
try {
return execSync(`${pythonPath} --version`, {encoding: 'utf8'})
} catch {
return null;
}
}

// Config related utils

export function getCurrentConfig(context: ExtensionContext) {
export async function getCurrentConfig(context: ExtensionContext) {
const configs: any = context.globalState.get("Odoo.configurations");
const activeConfig: number = Number(context.workspaceState.get('Odoo.selectedConfiguration'));
return (configs && activeConfig > -1 ? configs[activeConfig] : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export function getConfigurationStructure(id: number = 0) {
"name": `New Configuration ${id}`,
"odooPath": "",
"addons": [],
"pythonPath": "python3",
}
}

0 comments on commit 960903f

Please sign in to comment.