Skip to content

Commit

Permalink
Merge pull request #15 from davelopez/support_both_extension_modes
Browse files Browse the repository at this point in the history
Support both extension modes
  • Loading branch information
davelopez authored Mar 7, 2022
2 parents 805f9f3 + f3f428e commit ce3f1ac
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 198 deletions.
37 changes: 25 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,42 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Client",
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
],
"smartStep": true,
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/client/dist/**/*.js"
],
"preLaunchTask": {
"type": "npm",
"script": "compile"
},
"sourceMapPathOverrides": {
"webpack://?:*/*": "${workspaceFolder}/client/*"
}
},
{
"name": "Launch Extension in Webworker",
"type": "pwa-extensionHost",
"debugWebWorkerHost": true,
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"--extensionDevelopmentKind=web"
],
"smartStep": true,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/client/dist/**/*.js"
"${workspaceRoot}/client/dist/web/**/*.js"
],
"preLaunchTask": {
"type": "npm",
"script": "watch-web"
"script": "compile"
}
},
{
Expand All @@ -32,14 +54,5 @@
"${workspaceRoot}/server/dist/**/*.js"
]
}
],
"compounds": [
{
"name": "Client + Server",
"configurations": [
"Launch Client",
"Attach to Server"
]
}
]
}
19 changes: 1 addition & 18 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"tasks": [
{
"type": "npm",
"script": "compile-web",
"script": "compile",
"group": "build",
"presentation": {
"panel": "dedicated",
Expand All @@ -15,23 +15,6 @@
"$ts-webpack",
"$tslint-webpack"
]
},
{
"type": "npm",
"script": "watch-web",
"isBackground": true,
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"panel": "dedicated",
"reveal": "never"
},
"problemMatcher": [
"$ts-webpack-watch",
"$tslint-webpack-watch"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ TBA
```
- Build
```sh
npm run compile-web
npm run compile
```
- Run the `Launch Client` configuration from the `Run and Debug` action bar (or press F5).
- Run the `Launch Extension` configuration from the `Run and Debug` action bar (or press F5).
50 changes: 7 additions & 43 deletions client/src/browser/extension.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
import { ExtensionContext, Uri } from "vscode";
import { LanguageClientOptions } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/browser";
import { setupCommands } from "../commands/setup";
import { Constants } from "../constants";
import { CleanWorkflowDocumentProvider } from "../providers/cleanWorkflowDocumentProvider";
import { buildLanguageClientOptions, initExtension } from "../common";

export function activate(context: ExtensionContext) {
console.log(`${context.extension.id} is now active in the web extension host.`);
const client = buildWebLanguageClient(context);

const client = startLanguageClient(context);

setupProviders(context, client);

setupCommands(context, client);
initExtension(context, client);
}

export function deactivate() {}

function startLanguageClient(context: ExtensionContext) {
const documentSelector = [{ language: Constants.NATIVE_WORKFLOW_LANGUAGE_ID }];

// Options to control the language client
const clientOptions: LanguageClientOptions = {
documentSelector,
synchronize: {},
initializationOptions: {},
};

const client = createWorkerLanguageClient(context, clientOptions);

const disposable = client.start();
context.subscriptions.push(disposable);

client.onReady().then(() => {
console.log(`${context.extension.id} server is ready.`);
});
return client;
}

function createWorkerLanguageClient(context: ExtensionContext, clientOptions: LanguageClientOptions) {
const worker = createServerWorker(context);
function buildWebLanguageClient(context: ExtensionContext) {
const clientOptions: LanguageClientOptions = buildLanguageClientOptions();
const serverPath = Uri.joinPath(context.extensionUri, "server/dist/web/nativeServer.js");
const worker = new Worker(serverPath.toString());
return new LanguageClient("galaxy-workflow-language-client-native", "Galaxy Workflows LS", clientOptions, worker);
}

function createServerWorker(context: ExtensionContext) {
// The worker main file implements the language server.
const serverMain = Uri.joinPath(context.extensionUri, "server/dist/browserServerMain.js");
const worker = new Worker(serverMain.toString());
return worker;
}

function setupProviders(context: ExtensionContext, client: LanguageClient) {
CleanWorkflowDocumentProvider.register(context, client);
}
8 changes: 4 additions & 4 deletions client/src/commands/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { commands, Disposable } from "vscode";
import { LanguageClient } from "vscode-languageclient/browser";
import { CommonLanguageClient } from "vscode-languageclient";

/**
* Gets the fully-qualified identifier of the command by prefixing
Expand All @@ -17,9 +17,9 @@ export function getCommandFullIdentifier(command: string) {
*/
export abstract class CommandContext {
/** Allows to access language features. */
protected client: LanguageClient;
protected client: CommonLanguageClient;

constructor(client: LanguageClient) {
constructor(client: CommonLanguageClient) {
this.client = client;
}
}
Expand All @@ -30,7 +30,7 @@ export abstract class CommandContext {
*/
export abstract class CustomCommand extends CommandContext {
abstract readonly identifier: string;
constructor(client: LanguageClient) {
constructor(client: CommonLanguageClient) {
super(client);
}

Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionContext } from "vscode";
import { LanguageClient } from "vscode-languageclient/browser";
import { CommonLanguageClient } from "vscode-languageclient";
import { CompareCleanWithWorkflowsCommand } from "./compareCleanWith";
import { CompareCleanWorkflowsCommand } from "./compareCleanWorkflows";
import { PreviewCleanWorkflowCommand } from "./previewCleanWorkflow";
Expand All @@ -9,7 +9,7 @@ import { PreviewCleanWorkflowCommand } from "./previewCleanWorkflow";
* @param context The extension context
* @param client The language client
*/
export function setupCommands(context: ExtensionContext, client: LanguageClient) {
export function setupCommands(context: ExtensionContext, client: CommonLanguageClient) {
context.subscriptions.push(new PreviewCleanWorkflowCommand(client).register());
context.subscriptions.push(new CompareCleanWorkflowsCommand(client).register());
context.subscriptions.push(new CompareCleanWithWorkflowsCommand(client).register());
Expand Down
33 changes: 33 additions & 0 deletions client/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ExtensionContext } from "vscode";
import { CommonLanguageClient, LanguageClientOptions } from "vscode-languageclient";
import { setupCommands } from "./commands/setup";
import { Constants } from "./constants";
import { CleanWorkflowDocumentProvider } from "./providers/cleanWorkflowDocumentProvider";

export function buildLanguageClientOptions() {
const documentSelector = [{ language: Constants.NATIVE_WORKFLOW_LANGUAGE_ID }];

// Options to control the language client
const clientOptions: LanguageClientOptions = {
documentSelector,
synchronize: {},
initializationOptions: {},
};
return clientOptions;
}

export function initExtension(context: ExtensionContext, client: CommonLanguageClient) {
setupProviders(context, client);
setupCommands(context, client);

const disposable = client.start();
context.subscriptions.push(disposable);

client.onReady().then(() => {
console.log(`${context.extension.id} server is ready.`);
});
}

function setupProviders(context: ExtensionContext, client: CommonLanguageClient) {
CleanWorkflowDocumentProvider.register(context, client);
}
44 changes: 44 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as path from "path";
import { ExtensionContext } from "vscode";
import { LanguageClientOptions } from "vscode-languageclient";
import { LanguageClient, ServerOptions, TransportKind } from "vscode-languageclient/node";
import { buildLanguageClientOptions, initExtension } from "./common";

export function activate(context: ExtensionContext) {
const client = buildLanguageClient(context);

initExtension(context, client);
}

export function deactivate() {}

function buildLanguageClient(context: ExtensionContext) {
const clientOptions: LanguageClientOptions = buildLanguageClientOptions();
const serverOptions: ServerOptions = buildServerOptions(context);
return new LanguageClient(
"galaxy-workflow-language-client-native",
"Galaxy Workflows LS",
serverOptions,
clientOptions
);
}

function buildServerOptions(context: ExtensionContext) {
// The server is implemented in node
const serverModule = context.asAbsolutePath(path.join("server", "dist", "nativeServer.js"));
// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
const debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: { module: serverModule, transport: TransportKind.ipc },
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions,
},
};
return serverOptions;
}
6 changes: 3 additions & 3 deletions client/src/providers/cleanWorkflowDocumentProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter, ExtensionContext, TextDocumentContentProvider, Uri, workspace } from "vscode";
import { LanguageClient, RequestType } from "vscode-languageclient/browser";
import { CommonLanguageClient, RequestType } from "vscode-languageclient";
import { Constants, LSRequestIdentifiers } from "../constants";
import { getWorkspaceScheme, replaceUriScheme } from "../utils";

Expand All @@ -19,14 +19,14 @@ namespace CleanWorkflowDocumentRequest {
}

export class CleanWorkflowDocumentProvider implements TextDocumentContentProvider {
public static register(context: ExtensionContext, client: LanguageClient) {
public static register(context: ExtensionContext, client: CommonLanguageClient) {
const provider = new CleanWorkflowDocumentProvider(client);
context.subscriptions.push(
workspace.registerTextDocumentContentProvider(Constants.CLEAN_WORKFLOW_DOCUMENT_SCHEME, provider)
);
}

constructor(private readonly languageClient: LanguageClient) {}
constructor(private readonly languageClient: CommonLanguageClient) {}

onDidChangeEmitter = new EventEmitter<Uri>();
onDidChange = this.onDidChangeEmitter.event;
Expand Down
2 changes: 1 addition & 1 deletion client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"WebWorker"
],
"rootDir": "src",
"sourceMap": true
"sourceMap": true,
},
"include": [
"src"
Expand Down
43 changes: 43 additions & 0 deletions client/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */

//@ts-check
/** @typedef {import('webpack').Configuration} WebpackConfig **/

//@ts-check
("use strict");

const withDefaults = require("../shared.webpack.config");
const path = require("path");

/** @type WebpackConfig */
const nodeExtensionConfig = withDefaults({
context: path.join(__dirname),
mode: "none",
target: "node", // regular extensions run in a node context
entry: {
extension: "./src/extension.ts",
},
output: {
filename: "[name].js",
path: path.join(__dirname, "dist"),
libraryTarget: "commonjs",
},
});

/** @type WebpackConfig */
const webExtensionConfig = withDefaults({
context: path.join(__dirname),
mode: "none",
target: "webworker", // web extensions run in a webworker context
entry: {
extension: "./src/browser/extension.ts",
},
output: {
filename: "[name].js",
path: path.join(__dirname, "dist", "web"),
libraryTarget: "commonjs",
},
});

module.exports = [nodeExtensionConfig, webExtensionConfig];
Loading

0 comments on commit ce3f1ac

Please sign in to comment.