Skip to content

[wip] desktop service host #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
461 changes: 285 additions & 176 deletions src/commands/LifecycleCommands.ts

Large diffs are not rendered by default.

164 changes: 157 additions & 7 deletions src/common/PQTestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { PQTestTask } from "./PowerQueryTask";
import { RpcRequestParamBase } from "../pqTestConnector/RpcClient";
import type { ValueEventEmitter } from "./ValueEventEmitter";

export interface GenericResult {
Expand Down Expand Up @@ -111,14 +112,62 @@ export interface CreateAuthState {
$$PASSWORD$$?: string;
}

export interface ParsedDocumentState {
DocumentKind: "Section" | "Expression";
Queries: Array<{
nameIdentifier: {
argumentFragmentKind:
| "Date"
| "DateTime"
| "DateTimeZone"
| "Duration"
| "FieldAccess"
| "Identifier"
| "Logical"
| "NaN"
| "NegativeInfinity"
| "Null"
| "Number"
| "PositiveInfinity"
| "Text"
| "Time";
fragmentKind:
| "Aggregation"
| "ArbitrarySubstring"
| "Argument"
| "ArgumentRecord"
| "ArithmeticBinaryTransform"
| "ArithmeticFunctionTransform"
| "BinaryContents"
| "BoundarySubstring"
| string;
Name: string;
value: string;
};
queryScript: string;
steps: unknown[];
}>;
Errors: Array<{
Kind: unknown;
Location: unknown;
ErrorRange: unknown;
Message: string;
}>;
}

export interface ResolveResourceChallengeState {
DocumentScript: string;
QueryName?: string;
ResourceKind?: string;
ResourcePath?: string;
}

export interface GetPreviewRequest {
DocumentScript: string;
QueryName?: string;
}

export interface IPQTestService {
readonly pqTestReady: boolean;
readonly pqTestLocation: string;
readonly pqTestFullPath: string;
readonly currentExtensionInfos: ValueEventEmitter<ExtensionInfo[]>;
readonly currentCredentials: ValueEventEmitter<Credential[]>;
readonly onPowerQueryTestLocationChanged: () => void;
readonly ExecuteBuildTaskAndAwaitIfNeeded: () => Promise<void>;
readonly DeleteCredential: () => Promise<GenericResult>;
readonly DisplayExtensionInfo: () => Promise<ExtensionInfo[]>;
readonly ListCredentials: () => Promise<Credential[]>;
Expand All @@ -132,6 +181,107 @@ export interface IPQTestService {
readonly TestConnection: () => Promise<GenericResult>;
}

export interface PqServiceHostRequestParamBase extends RpcRequestParamBase {
ExtensionPaths?: string[];
KeyVaultSecretName?: string;
DataSourceKind?: string;
DataSourcePath?: string;
PrettyPrint?: boolean;
EnvironmentConfigurationFile?: string;
EnvironmentSetting?: string[];
ApplicationPropertyFile?: string;
ApplicationProperties?: string[];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}

export interface PqServiceHostCreateAuthRequest extends PqServiceHostRequestParamBase {
AuthenticationKind: string;
TemplateValueKey?: string;
TemplateValueUsername?: string;
TemplateValuePassword?: string;
TemplateValueAccessToken?: string;
TemplateValueRefreshToken?: string;
AllowUserInteraction?: string;
}

export interface PqServiceHostDeleteCredentialRequest extends PqServiceHostRequestParamBase {
AllCredentials?: boolean;
}

export interface PqServiceHostRunTestRequest extends PqServiceHostRequestParamBase {
LogMashupEngineTraceLevel?: string;
FailOnFoldingFailure?: boolean;
AutomaticFileCredentials?: boolean;
RunAsAction?: boolean;
}

export interface PqServiceHostRunTestRequest extends PqServiceHostRequestParamBase {
LogMashupEngineTraceLevel?: string;
FailOnFoldingFailure?: boolean;
AutomaticFileCredentials?: boolean;
RunAsAction?: boolean;
}

export interface PqServiceHostSetCredentialRequest extends PqServiceHostRequestParamBase {
AuthenticationKind?: string;
AllowUserInteraction?: boolean;
UseLegacyBrowser?: boolean;
UseSystemBrowser?: boolean;
InputTemplateString?: string;
}

export interface PqServiceHostTestConnectionRequest extends PqServiceHostRequestParamBase {
LogMashupEngineTraceLevel?: string;
}

export interface PqServiceHostValidateRequest extends PqServiceHostRequestParamBase {
LogMashupEngineTraceLevel?: string;
}

export type PqServiceHostResolveResourceChallengeRequest = PqServiceHostRequestParamBase &
ResolveResourceChallengeState;

export type PqServiceHostGetPreviewRequest = PqServiceHostRequestParamBase & GetPreviewRequest;

export interface IHealthService {
readonly ForceShutdown: () => Promise<number>;
readonly Ping: () => Promise<number>;
}

export interface IDocumentService {
readonly TryParseDocumentScript: (documentScript: string) => Promise<ParsedDocumentState>;
}

export interface IEvaluationService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly GetPreviewAsync: (state: GetPreviewRequest) => Promise<any>;
}

export interface IPQTestClient {
readonly pqTestReady: boolean;
readonly pqTestLocation: string;
readonly pqTestFullPath: string;
readonly currentExtensionInfos: ValueEventEmitter<ExtensionInfo[]>;
readonly currentCredentials: ValueEventEmitter<Credential[]>;
readonly onPowerQueryTestLocationChangedByConfig: (config: { PQTestLocation: string | undefined }) => void;
readonly ExecuteBuildTaskAndAwaitIfNeeded: () => Promise<void>;
readonly pqTestService: IPQTestService;
}

export interface IPQServiceHostClient extends IPQTestClient {
readonly healthService: IHealthService;
readonly pqTestService: IPQTestService & {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly RunTestBatteryFromContent: (pathToQueryFile?: string) => Promise<any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly ResolveResourceChallengeAsync: (state: ResolveResourceChallengeState) => Promise<any>;
};
readonly documentService: IDocumentService;
readonly evaluationService: IEvaluationService;
}

const CommonArgs: string[] = ["--prettyPrint"];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 1 addition & 1 deletion src/common/promises/fromEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { cancelable } from "./cancelable";
import { CancellationToken } from "./CancellationToken";
import { noop } from "./noop";
import { once } from "./once";
import WebSocket from "ws";
import type WebSocket from "ws";

export type AnyEventListener = (type: string, callback: AnyFunction) => void;

Expand Down
13 changes: 9 additions & 4 deletions src/common/sockets/JsonRpcSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
SocketMessageReader,
SocketMessageWriter,
} from "vscode-jsonrpc/node";
import { fibonacciNumbers } from "../iterables/FibonacciNumbers";
import { NumberGenerator } from "../iterables/NumberIterator";
import { EventEmitter } from "events";

import { CLOSED, OPEN, SocketClient, SocketConnectionError } from "./SocketClient";
import { AnyFunction } from "../promises/types";
import { BaseError } from "../errors";
import { noop } from "../promises/noop";
import { fibonacciNumbers } from "../iterables/FibonacciNumbers";
import { NumberGenerator } from "../iterables/NumberIterator";

const JSON_RPC_VERSION: string = "2.0";

Expand Down Expand Up @@ -78,6 +78,7 @@ function defaultOnMessage(message: any): void {
export type DeferredJsonRpcTask = { resolve: AnyFunction; reject: AnyFunction };

export class JsonRpcSocketClient extends SocketClient {
public readonly notificationMessageEmitter: EventEmitter = new EventEmitter();
private readonly _handle: (message: any) => Promise<any>;
private readonly _deferredDictionary: Map<number, DeferredJsonRpcTask> = new Map();
private reader?: SocketMessageReader;
Expand Down Expand Up @@ -116,7 +117,11 @@ export class JsonRpcSocketClient extends SocketClient {

return this._getDeferred(rawJsonRpcResponseMessage.id)?.resolve(rawJsonRpcResponseMessage.result);
} else if (Message.isNotification(rawJsonRpcMessage)) {
return this._handle(rawJsonRpcMessage).catch(noop);
Array.isArray(rawJsonRpcMessage.params)
? this.notificationMessageEmitter.emit(rawJsonRpcMessage.method, ...rawJsonRpcMessage.params)
: this.notificationMessageEmitter.emit(rawJsonRpcMessage.method, rawJsonRpcMessage.params);

return Promise.resolve(rawJsonRpcMessage);
} else {
return this._handle(rawJsonRpcMessage).then((result: ResponseBase) => result.result);
}
Expand Down
4 changes: 2 additions & 2 deletions src/constants/PowerQuerySdkExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ const PublicMsftPqSdkToolsNugetName: string = InternalMsftPqSdkToolsNugetName;
/**
* 2.114 or 2.114.x wil limit the version of the sdkTool seized beneath 2.115
*/
const MaximumPqTestNugetVersion: string = "2.114.x" as const;
const MaximumPqTestNugetVersion: string = "2.117.x" as const;
/**
* A suggestedPqTestNugetVersion that would be used as the initially tried pqTest version
* thus, make sure it is lower than `MaximumPqTestNugetVersion` if it were specified
*/
const SuggestedPqTestNugetVersion: string = "2.114.4" as const;
const SuggestedPqTestNugetVersion: string = "2.117.361" as const;

const PqTestSubPath: string[] = [
`${InternalMsftPqSdkToolsNugetName}.${SuggestedPqTestNugetVersion}`,
Expand Down
12 changes: 7 additions & 5 deletions src/debugAdaptor/MQueryDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@vscode/debugadapter";
import { DebugProtocol } from "@vscode/debugprotocol";

import { DISCONNECTED, PqServiceHostClientLite, READY } from "../pqTestConnector/PqServiceHostClientLite";
import { DISCONNECTED, READY } from "../pqTestConnector/RpcClient";
import { extensionI18n, resolveI18nTemplate } from "../i18n/extension";
import { ExtensionInfo, GenericResult } from "../common/PQTestService";
import {
Expand All @@ -27,6 +27,7 @@ import {
import { DeferredValue } from "../common/DeferredValue";
import { ExtensionConfigurations } from "../constants/PowerQuerySdkConfiguration";
import { fromEvents } from "../common/promises/fromEvents";
import { PqServiceHostClientLite } from "../pqTestConnector/PqServiceHostClientLite";
import { stringifyJson } from "../utils/strings";
import { WaitNotify } from "../common/WaitNotify";

Expand Down Expand Up @@ -178,7 +179,7 @@ export class MQueryDebugSession extends LoggingDebugSession {
private async doLaunchRequest(args: ILaunchRequestArguments): Promise<void> {
if (this.useServiceHost && this.pqServiceHostClientLite) {
// activate pqServiceHostClientLite and make it connect to pqServiceHost
this.pqServiceHostClientLite.onPowerQueryTestLocationChanged();
this.pqServiceHostClientLite.onPowerQueryTestLocationChangedByConfig(ExtensionConfigurations);

try {
// wait for the pqServiceHostClientLite's socket got ready
Expand All @@ -189,7 +190,7 @@ export class MQueryDebugSession extends LoggingDebugSession {
switch (theOperation) {
case "info": {
const displayExtensionInfoResult: ExtensionInfo[] =
await this.pqServiceHostClientLite.DisplayExtensionInfo();
await this.pqServiceHostClientLite.pqTestService.DisplayExtensionInfo();

this.appendInfoLine(
resolveI18nTemplate("PQSdk.lifecycle.command.display.extension.info.result", {
Expand All @@ -204,7 +205,8 @@ export class MQueryDebugSession extends LoggingDebugSession {
}

case "test-connection": {
const testConnectionResult: GenericResult = await this.pqServiceHostClientLite.TestConnection();
const testConnectionResult: GenericResult =
await this.pqServiceHostClientLite.pqTestService.TestConnection();

this.appendInfoLine(
resolveI18nTemplate("PQSdk.lifecycle.command.test.connection.result", {
Expand All @@ -217,7 +219,7 @@ export class MQueryDebugSession extends LoggingDebugSession {

case "run-test": {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: any = await this.pqServiceHostClientLite.RunTestBatteryFromContent(
const result: any = await this.pqServiceHostClientLite.pqTestService.RunTestBatteryFromContent(
path.resolve(args.program),
);

Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import * as vscode from "vscode";

import { convertExtensionInfoToLibraryJson, ExtensionInfo, IPQTestService } from "./common/PQTestService";
import { convertExtensionInfoToLibraryJson, ExtensionInfo, IPQTestClient } from "./common/PQTestService";
import { getFirstWorkspaceFolder, maybeHandleNewWorkspaceCreated } from "./utils/vscodes";
import { activateMQueryDebug } from "./debugAdaptor/activateMQueryDebug";
import { ExtensionConfigurations } from "./constants/PowerQuerySdkConfiguration";
Expand Down Expand Up @@ -42,7 +42,7 @@ export function activate(vscExtCtx: vscode.ExtensionContext): void {
pqSdkOutputChannel,
);

const disposablePqTestServices: IPQTestService & IDisposable = useServiceHost
const disposablePqTestServices: IPQTestClient & IDisposable = useServiceHost
? new PqServiceHostClient(globalEventBus, pqSdkOutputChannel)
: new PqTestExecutableTaskQueue(vscExtCtx, globalEventBus, pqSdkOutputChannel);

Expand Down
16 changes: 8 additions & 8 deletions src/features/PowerQueryTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as path from "path";
import * as vscode from "vscode";

import { buildPqTestArgs, IPQTestService } from "../common/PQTestService";
import { buildPqTestArgs, IPQTestClient } from "../common/PQTestService";
import { ExtensionConfigurations } from "../constants/PowerQuerySdkConfiguration";
import { ExtensionConstants } from "../constants/PowerQuerySdkExtension";
import { extensionI18n } from "../i18n/extension";
Expand Down Expand Up @@ -135,19 +135,19 @@ export class PowerQueryTaskProvider implements vscode.TaskProvider {
});
}

constructor(protected readonly pqTestService: IPQTestService) {
constructor(protected readonly pqTestClient: IPQTestClient) {
// noop
}

public provideTasks(_token: vscode.CancellationToken): vscode.ProviderResult<vscode.Task[]> {
const result: vscode.Task[] = [];

if (!this.pqTestService.pqTestReady) {
if (!this.pqTestClient.pqTestReady) {
return result;
}

result.push(PowerQueryTaskProvider.buildMsbuildTask());
result.push(PowerQueryTaskProvider.buildMakePQXCompileTask(this.pqTestService.pqTestLocation));
result.push(PowerQueryTaskProvider.buildMakePQXCompileTask(this.pqTestClient.pqTestLocation));

const useServiceHost: boolean = ExtensionConfigurations.featureUseServiceHost;

Expand All @@ -158,7 +158,7 @@ export class PowerQueryTaskProvider implements vscode.TaskProvider {
} else {
pqTestOperations.forEach((taskDef: PowerQueryTaskDefinition) => {
result.push(
PowerQueryTaskProvider.getTaskForPQTestTaskDefinition(taskDef, this.pqTestService.pqTestFullPath),
PowerQueryTaskProvider.getTaskForPQTestTaskDefinition(taskDef, this.pqTestClient.pqTestFullPath),
);
});
}
Expand All @@ -169,7 +169,7 @@ export class PowerQueryTaskProvider implements vscode.TaskProvider {
public resolveTask(task: vscode.Task, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Task> {
const taskDef: PowerQueryTaskDefinition = task.definition as PowerQueryTaskDefinition;

const pqtestExe: string = this.pqTestService.pqTestFullPath;
const pqtestExe: string = this.pqTestClient.pqTestFullPath;

if (taskDef.operation === "msbuild") {
const msbuildFullPath: string | undefined = ExtensionConfigurations.msbuildPath;
Expand All @@ -185,11 +185,11 @@ export class PowerQueryTaskProvider implements vscode.TaskProvider {
const currentWorkingFolder: string | undefined = getFirstWorkspaceFolder()?.uri.fsPath;

const makePQXExe: string = path.join(
this.pqTestService.pqTestLocation,
this.pqTestClient.pqTestLocation,
ExtensionConstants.MakePQXExecutableName,
);

if (currentWorkingFolder && this.pqTestService.pqTestLocation && !token.isCancellationRequested) {
if (currentWorkingFolder && this.pqTestClient.pqTestLocation && !token.isCancellationRequested) {
const args: string[] = buildPqTestArgs(taskDef);
args.push(currentWorkingFolder);
const processExecution: vscode.ProcessExecution = new vscode.ProcessExecution(makePQXExe, args);
Expand Down
Loading