Skip to content

Commit

Permalink
Update semver usage (#10416)
Browse files Browse the repository at this point in the history
* only use semver types on Web.

* fix compile
  • Loading branch information
rebornix authored Jun 10, 2022
1 parent 797af53 commit d6471f9
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/intellisense/intellisenseProvider.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { LanguageServer } from './languageServer.node';
import { IVSCodeNotebookController } from '../notebooks/controllers/types';
import { getComparisonKey } from '../platform/vscode-path/resources';
import { CompletionRequest } from 'vscode-languageclient';
import { NotebookPythonPathService } from './notebookPythonPathService';
import { NotebookPythonPathService } from './notebookPythonPathService.node';
import { isJupyterNotebook } from '../platform/common/utils';

const EmptyWorkspaceKey = '';
Expand Down
2 changes: 1 addition & 1 deletion src/intellisense/serviceRegistry.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IServiceManager } from '../platform/ioc/types';
import { NotebookCellLanguageService } from './cellLanguageService';
import { NotebookCellBangInstallDiagnosticsProvider } from './diagnosticsProvider';
import { EmptyNotebookCellLanguageService } from './emptyNotebookCellLanguageService';
import { NotebookPythonPathService } from './notebookPythonPathService';
import { NotebookPythonPathService } from './notebookPythonPathService.node';
import { PythonKernelCompletionProvider } from './pythonKernelCompletionProvider';
import { PythonKernelCompletionProviderRegistration } from './pythonKernelCompletionProviderRegistration';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { inject, injectable } from 'inversify';
import { SemVer } from 'semver';
import { CancellationToken } from 'vscode';
import { parseSemVer } from '../../../platform/common/utils';
import { parseSemVer } from '../../../platform/common/utils.node';
import { PythonEnvironment } from '../../../platform/pythonEnvironments/info';
import { ResourceSet } from '../../../platform/vscode-path/map';
import { JupyterCommands } from '../../../webviews/webview-side/common/constants';
Expand Down
5 changes: 0 additions & 5 deletions src/platform/common/platform/platformService.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

import { injectable } from 'inversify';
import { SemVer } from 'semver';
import { OSType } from '../utils/platform';
import { IPlatformService } from './types';

Expand All @@ -16,16 +15,12 @@ export class PlatformService implements IPlatformService {
return undefined;
}
public readonly osType: OSType = OSType.Unknown;
public version?: SemVer;
public get pathVariableName() {
return '';
}
public get virtualEnvBinName() {
return this.isWindows ? 'Scripts' : 'bin';
}
public async getVersion(): Promise<SemVer> {
throw new Error('Not Supported');
}

public get isWindows(): boolean {
return this.osType === OSType.Windows;
Expand Down
2 changes: 0 additions & 2 deletions src/platform/common/platform/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { SemVer } from 'semver';
import * as vscode from 'vscode';
import { OSType } from '../utils/platform';

Expand All @@ -21,7 +20,6 @@ export interface IPlatformService {
readonly isMac: boolean;
readonly isLinux: boolean;
readonly is64bit: boolean;
getVersion(): Promise<SemVer>;
readonly homeDir: vscode.Uri | undefined;
readonly tempDir: vscode.Uri | undefined;
}
Expand Down
12 changes: 12 additions & 0 deletions src/platform/common/utils.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import * as path from '../../platform/vscode-path/path';
import * as fsExtra from 'fs-extra';
import { SemVer, parse } from 'semver';
import { Uri } from 'vscode';
import { fsPathToUri } from '../vscode-path/utils';
import { IWorkspaceService } from './application/types';
Expand Down Expand Up @@ -70,3 +71,14 @@ export async function calculateWorkingDirectory(
}
return workingDir;
}

// For the given string parse it out to a SemVer or return undefined
export function parseSemVer(versionString: string): SemVer | undefined {
const versionMatch = /^\s*(\d+)\.(\d+)\.(.+)\s*$/.exec(versionString);
if (versionMatch && versionMatch.length > 2) {
const major = parseInt(versionMatch[1], 10);
const minor = parseInt(versionMatch[2], 10);
const build = parseInt(versionMatch[3], 10);
return parse(`${major}.${minor}.${build}`, true) ?? undefined;
}
}
12 changes: 0 additions & 12 deletions src/platform/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import type * as nbformat from '@jupyterlab/nbformat';
import * as uriPath from '../../platform/vscode-path/resources';
import { SemVer, parse } from 'semver';
import { NotebookData, NotebookDocument, TextDocument, Uri, workspace } from 'vscode';
import { sendTelemetryEvent } from '../../telemetry';
import { getTelemetrySafeLanguage } from '../../telemetry/helpers';
Expand Down Expand Up @@ -156,17 +155,6 @@ export function generateNewNotebookUri(
}
}

// For the given string parse it out to a SemVer or return undefined
export function parseSemVer(versionString: string): SemVer | undefined {
const versionMatch = /^\s*(\d+)\.(\d+)\.(.+)\s*$/.exec(versionString);
if (versionMatch && versionMatch.length > 2) {
const major = parseInt(versionMatch[1], 10);
const minor = parseInt(versionMatch[2], 10);
const build = parseInt(versionMatch[3], 10);
return parse(`${major}.${minor}.${build}`, true) ?? undefined;
}
}

export function sendNotebookOrKernelLanguageTelemetry(
telemetryEvent: Telemetry.SwitchToExistingKernel | Telemetry.NotebookLanguage,
language?: string
Expand Down
2 changes: 1 addition & 1 deletion src/platform/pythonEnvironments/info/interpreter.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Uri } from 'vscode';
import { InterpreterInformation } from '.';
import { interpreterInfo as getInterpreterInfoCommand, PythonEnvInfo } from '../../common/process/internal/scripts/index.node';
import { copyPythonExecInfo, PythonExecInfo } from '../exec';
import { parsePythonVersion } from './pythonVersion';
import { parsePythonVersion } from './pythonVersion.node';


/**
Expand Down
47 changes: 47 additions & 0 deletions src/platform/pythonEnvironments/info/pythonVersion.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Convert a Python version string.
*
* The supported formats are:
*
* * MAJOR.MINOR.MICRO-RELEASE_LEVEL
*
* (where RELEASE_LEVEL is one of {alpha,beta,candidate,final})
*
* Everything else, including an empty string, results in `undefined`.
*/
// Eventually we will want to also support the release serial
// (e.g. beta1, candidate3) and maybe even release abbreviations

import { SemVer } from 'semver';
import { PythonVersion } from './pythonVersion';

// (e.g. 3.9.2b1, 3.8.10rc3).
export function parsePythonVersion(raw: string): PythonVersion | undefined {
if (!raw || raw.trim().length === 0) {
return;
}
const versionParts = (raw || '')
.split('.')
.map((item) => item.trim())
.filter((item) => item.length > 0)
.filter((_, index) => index < 4);

if (versionParts.length > 0 && versionParts[versionParts.length - 1].indexOf('-') > 0) {
const lastPart = versionParts[versionParts.length - 1];
versionParts[versionParts.length - 1] = lastPart.split('-')[0].trim();
versionParts.push(lastPart.split('-')[1].trim());
}
while (versionParts.length < 4) {
versionParts.push('');
}
// Exclude PII from `version_info` to ensure we don't send this up via telemetry.
for (let index = 0; index < 3; index += 1) {
versionParts[index] = /^\d+$/.test(versionParts[index]) ? versionParts[index] : '0';
}
if (['alpha', 'beta', 'candidate', 'final'].indexOf(versionParts[3]) === -1) {
versionParts.pop();
}
const numberParts = `${versionParts[0]}.${versionParts[1]}.${versionParts[2]}`;
const rawVersion = versionParts.length === 4 ? `${numberParts}-${versionParts[3]}` : numberParts;
return new SemVer(rawVersion);
}
46 changes: 0 additions & 46 deletions src/platform/pythonEnvironments/info/pythonVersion.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { SemVer } from 'semver';

/**
* A representation of a Python runtime's version.
*
Expand All @@ -27,47 +25,3 @@ export type PythonVersion = {
build: string[];
prerelease: string[];
};

/**
* Convert a Python version string.
*
* The supported formats are:
*
* * MAJOR.MINOR.MICRO-RELEASE_LEVEL
*
* (where RELEASE_LEVEL is one of {alpha,beta,candidate,final})
*
* Everything else, including an empty string, results in `undefined`.
*/
// Eventually we will want to also support the release serial
// (e.g. beta1, candidate3) and maybe even release abbreviations
// (e.g. 3.9.2b1, 3.8.10rc3).
export function parsePythonVersion(raw: string): PythonVersion | undefined {
if (!raw || raw.trim().length === 0) {
return;
}
const versionParts = (raw || '')
.split('.')
.map((item) => item.trim())
.filter((item) => item.length > 0)
.filter((_, index) => index < 4);

if (versionParts.length > 0 && versionParts[versionParts.length - 1].indexOf('-') > 0) {
const lastPart = versionParts[versionParts.length - 1];
versionParts[versionParts.length - 1] = lastPart.split('-')[0].trim();
versionParts.push(lastPart.split('-')[1].trim());
}
while (versionParts.length < 4) {
versionParts.push('');
}
// Exclude PII from `version_info` to ensure we don't send this up via telemetry.
for (let index = 0; index < 3; index += 1) {
versionParts[index] = /^\d+$/.test(versionParts[index]) ? versionParts[index] : '0';
}
if (['alpha', 'beta', 'candidate', 'final'].indexOf(versionParts[3]) === -1) {
versionParts.pop();
}
const numberParts = `${versionParts[0]}.${versionParts[1]}.${versionParts[2]}`;
const rawVersion = versionParts.length === 4 ? `${numberParts}-${versionParts[3]}` : numberParts;
return new SemVer(rawVersion);
}
2 changes: 1 addition & 1 deletion src/test/interpreters/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BufferDecoder } from '../../platform/common/process/decoder.node';
import { PythonEnvInfo } from '../../platform/common/process/internal/scripts/index.node';
import { ProcessService } from '../../platform/common/process/proc.node';
import { PythonEnvironment } from '../../platform/pythonEnvironments/info';
import { parsePythonVersion } from '../../platform/pythonEnvironments/info/pythonVersion';
import { parsePythonVersion } from '../../platform/pythonEnvironments/info/pythonVersion.node';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../constants.node';
import { isCondaEnvironment } from './condaLocator.node';
import { getCondaEnvironment, getCondaFile, isCondaAvailable } from './condaService.node';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Cancellation, createPromiseFromCancellation } from '../../../platform/c
import { traceWarning } from '../../../platform/logging';
import { IPythonExecutionFactory } from '../../../platform/common/process/types.node';
import { IsCodeSpace } from '../../../platform/common/types';
import { parseSemVer } from '../../../platform/common/utils';
import { parseSemVer } from '../../../platform/common/utils.node';
import { DataScience, Common } from '../../../platform/common/utils/localize';
import { IInterpreterService } from '../../../platform/interpreter/contracts';
import { PythonEnvironment } from '../../../platform/pythonEnvironments/info';
Expand Down

0 comments on commit d6471f9

Please sign in to comment.