Skip to content

Commit

Permalink
Re-added preference for specifying path to ActiveState's State Tool.
Browse files Browse the repository at this point in the history
Use a more descriptive name.
  • Loading branch information
mitchell-as committed Feb 7, 2023
1 parent 97f6740 commit b5595b2
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@
],
"configuration": {
"properties": {
"python.activeStateToolPath": {
"default": "state",
"description": "%python.activeStateToolPath.description%",
"scope": "machine-overridable",
"type": "string"
},
"python.autoComplete.extraPaths": {
"default": [],
"description": "%python.autoComplete.extraPaths.description%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"python.command.python.launchTensorBoard.title": "Launch TensorBoard",
"python.command.python.refreshTensorBoard.title": "Refresh TensorBoard",
"python.menu.createNewFile.title": "Python File",
"python.activeStateToolPath.description": "Path to the State Tool executable for ActiveState runtimes (version 0.36+).",
"python.autoComplete.extraPaths.description": "List of paths to libraries and the like that need to be imported by auto complete engine. E.g. when using Google App SDK, the paths are not in system path, hence need to be added into this list.",
"python.condaPath.description": "Path to the conda executable to use for activation (version 4.4+).",
"python.defaultInterpreterPath.description": "Path to default Python to use when extension loads up for the first time, no longer used once an interpreter is selected for the workspace. See [here](https://aka.ms/AAfekmf) to understand when this is used",
Expand Down
1 change: 1 addition & 0 deletions resources/report_issue_user_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"envFile": "placeholder",
"venvPath": "placeholder",
"venvFolders": "placeholder",
"activeStateToolPath": "placeholder",
"condaPath": "placeholder",
"pipenvPath": "placeholder",
"poetryPath": "placeholder",
Expand Down
7 changes: 7 additions & 0 deletions src/client/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export class PythonSettings implements IPythonSettings {

public venvFolders: string[] = [];

public activeStateToolPath = '';

public condaPath = '';

public pipenvPath = '';
Expand Down Expand Up @@ -254,6 +256,11 @@ export class PythonSettings implements IPythonSettings {

this.venvPath = systemVariables.resolveAny(pythonSettings.get<string>('venvPath'))!;
this.venvFolders = systemVariables.resolveAny(pythonSettings.get<string[]>('venvFolders'))!;
const activeStateToolPath = systemVariables.resolveAny(pythonSettings.get<string>('activeStateToolPath'))!;
this.activeStateToolPath =
activeStateToolPath && activeStateToolPath.length > 0
? getAbsolutePath(activeStateToolPath, workspaceRoot)
: activeStateToolPath;
const condaPath = systemVariables.resolveAny(pythonSettings.get<string>('condaPath'))!;
this.condaPath = condaPath && condaPath.length > 0 ? getAbsolutePath(condaPath, workspaceRoot) : condaPath;
const pipenvPath = systemVariables.resolveAny(pythonSettings.get<string>('pipenvPath'))!;
Expand Down
1 change: 1 addition & 0 deletions src/client/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export interface IPythonSettings {
readonly pythonPath: string;
readonly venvPath: string;
readonly venvFolders: string[];
readonly activeStateToolPath: string;
readonly condaPath: string;
readonly pipenvPath: string;
readonly poetryPath: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@

import * as path from 'path';
import { dirname } from 'path';
import { arePathsSame, pathExists, shellExecute } from '../externalDependencies';
import {
arePathsSame,
getPythonSetting,
onDidChangePythonSetting,
pathExists,
shellExecute,
} from '../externalDependencies';
import { cache } from '../../../common/utils/decorators';
import { traceError, traceVerbose } from '../../../logging';
import { getOSType, getUserHomeDir, OSType } from '../../../common/utils/platform';

export const ACTIVESTATETOOLPATH_SETTING_KEY = 'activeStateToolPath';

const STATE_GENERAL_TIMEOUT = 5000;

export type ProjectInfo = {
Expand All @@ -35,6 +43,12 @@ export class ActiveState {
return ActiveState.statePromise;
}

constructor() {
onDidChangePythonSetting(ACTIVESTATETOOLPATH_SETTING_KEY, () => {
ActiveState.statePromise = undefined;
});
}

public static getStateToolDir(): string | undefined {
const home = getUserHomeDir();
if (!home) {
Expand All @@ -47,7 +61,9 @@ export class ActiveState {

private static async locate(): Promise<ActiveState | undefined> {
const stateToolDir = this.getStateToolDir();
if (stateToolDir && (await pathExists(stateToolDir))) {
const stateCommand =
getPythonSetting<string>(ACTIVESTATETOOLPATH_SETTING_KEY) ?? ActiveState.defaultStateCommand;
if (stateToolDir && ((await pathExists(stateToolDir)) || stateCommand !== this.defaultStateCommand)) {
return new ActiveState();
}
return undefined;
Expand All @@ -57,13 +73,15 @@ export class ActiveState {
return this.getProjectsCached();
}

private static readonly stateCommand: string = 'state';
private static readonly defaultStateCommand: string = 'state';

@cache(30_000, true, 10_000)
// eslint-disable-next-line class-methods-use-this
private async getProjectsCached(): Promise<ProjectInfo[] | undefined> {
try {
const result = await shellExecute(`${ActiveState.stateCommand} projects -o editor`, {
const stateCommand =
getPythonSetting<string>(ACTIVESTATETOOLPATH_SETTING_KEY) ?? ActiveState.defaultStateCommand;
const result = await shellExecute(`${stateCommand} projects -o editor`, {
timeout: STATE_GENERAL_TIMEOUT,
});
if (!result) {
Expand All @@ -74,7 +92,7 @@ export class ActiveState {
// '\0' is a record separator.
output = output.substring(0, output.length - 1);
}
traceVerbose(`${ActiveState.stateCommand} projects -o editor: ${output}`);
traceVerbose(`${stateCommand} projects -o editor: ${output}`);
const projects = JSON.parse(output);
ActiveState.setCachedProjectInfo(projects);
return projects;
Expand Down
17 changes: 12 additions & 5 deletions src/test/common/configSettings/configSettings.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ suite('Python Settings', async () => {
for (const name of [
'pythonPath',
'venvPath',
'activeStateToolPath',
'condaPath',
'pipenvPath',
'envFile',
Expand Down Expand Up @@ -139,11 +140,17 @@ suite('Python Settings', async () => {
}

suite('String settings', async () => {
['venvPath', 'condaPath', 'pipenvPath', 'envFile', 'poetryPath', 'defaultInterpreterPath'].forEach(
async (settingName) => {
testIfValueIsUpdated(settingName, 'stringValue');
},
);
[
'venvPath',
'activeStateToolPath',
'condaPath',
'pipenvPath',
'envFile',
'poetryPath',
'defaultInterpreterPath',
].forEach(async (settingName) => {
testIfValueIsUpdated(settingName, 'stringValue');
});
});

suite('Boolean settings', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ suite('ActiveState Locator', () => {
sinon.stub(fsapi, 'pathExists').callsFake((dir: string) => dir === stateToolDir);
}

sinon.stub(externalDependencies, 'getPythonSetting').returns(undefined);

sinon.stub(externalDependencies, 'shellExecute').callsFake((command: string) => {
if (command === 'state projects -o editor') {
return Promise.resolve<ExecutionResult<string>>({
Expand Down

0 comments on commit b5595b2

Please sign in to comment.