From 99b90ede27f0fb224db92b0cafca79ebebb3fb17 Mon Sep 17 00:00:00 2001 From: Erik Lieben Date: Thu, 15 Aug 2019 23:24:20 +0200 Subject: [PATCH] feat(vscode): allow multi-root & enable extension only when @aurelia/runtime is in package.json --- client/src/extension.ts | 78 ++++++++++++++++++++++++++++++++++++----- package.json | 5 ++- server/src/server.ts | 3 +- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/client/src/extension.ts b/client/src/extension.ts index 8cdb1989..6017acd6 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -1,27 +1,87 @@ -/* -------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - * ------------------------------------------------------------------------------------------ */ import { workspace as Workspace, ExtensionContext, - WorkspaceFolder, - Uri, window as Window } from 'vscode'; +import * as path from 'path'; import { - LanguageClient + LanguageClient, TransportKind, LanguageClientOptions, ServerOptions } from 'vscode-languageclient'; -let defaultClient: LanguageClient; let clients: Map = new Map(); export function activate(context: ExtensionContext) { + const channel = Window.createOutputChannel('aurelia'); - channel.appendLine('extension activated'); + channel.appendLine('checking workspaces for Aurelia workspaces'); + + workspacePluginLoader(channel, context); + } export function deactivate(): Thenable { return Promise.resolve(); } + +/** + * Load the plugin for each seperate workspace (Proof of Concept) + */ +function workspacePluginLoader(channel, context) { + const serverModule = context.asAbsolutePath(path.join('server', 'dist', 'server.js')); + + // Initial check of workspaces + Workspace.workspaceFolders.forEach(async (folder) => { + + channel.appendLine("processing workspace: " + folder.uri.toString()); + + const workspacePath = folder.uri.fsPath; + const packageJsonPath = path.join(workspacePath, 'package.json'); + const document = await Workspace.openTextDocument(packageJsonPath); + let packageJson = JSON.parse(document.getText()); + + // TODO: figure out if this is the right way to detect it's an Aurelia 2 workspace + if (!packageJson.dependencies.hasOwnProperty('@aurelia/runtime')) { + return; + } + + channel.appendLine("activating Aurelia plugin for workspace: " + folder.uri.toString()); + + const debugOptions = { execArgv: ["--nolazy", `--inspect=${6011 + clients.size}`] }; + const serverOptions: ServerOptions = { + run: { module: serverModule, transport: TransportKind.ipc }, + debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions} + }; + const clientOptions: LanguageClientOptions = { + documentSelector: [ + { scheme: 'file', language: 'plaintext', pattern: `${folder.uri.fsPath}/**/*` } + ], + diagnosticCollectionName: 'aurelia|' + workspacePath, + workspaceFolder: folder, + outputChannel: channel + } + + const client = new LanguageClient('aurelia', serverOptions, clientOptions); + client.start(); + clients.set(workspacePath, client); + + }); + + Workspace.onDidChangeWorkspaceFolders((event) => { + + // add client if workspace is added + for (let folder of event.removed) { + channel.appendLine("workspace added :" + folder.uri.fsPath); + } + + // remove client when workspace is removed + for (let folder of event.removed) { + let client = clients.get(folder.uri.fsPath); + channel.appendLine("workspace deleted :" + folder.uri.fsPath); + if (client) { + clients.delete(folder.uri.toString()); + client.stop(); + } + } + }); +} diff --git a/package.json b/package.json index a0336b79..7364e42e 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,7 @@ "icon": "images/logo.png", "keywords": [ "aurelia", - "vscode", - "extension" + "multi-root ready" ], "homepage": "http://aurelia.io", "bugs": { @@ -33,7 +32,7 @@ "Snippets" ], "activationEvents": [ - "workspaceContains:**/aurelia_project/aurelia.json" + "workspaceContains:package.json" ], "main": "./client/dist/extension", "contributes": {}, diff --git a/server/src/server.ts b/server/src/server.ts index 3c679041..f4c02d97 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -7,12 +7,13 @@ let documents = new TextDocuments(); let workspaceFolder: string | null; documents.onDidOpen((event) => { - connection.console.log(`[Server ${workspaceFolder}] Document opened: ${event.document.uri}`); + connection.console.log(`[Server(${process.pid}) ${workspaceFolder}] Document opened: ${event.document.uri}`); }) documents.listen(connection); connection.onInitialize((params) => { workspaceFolder = params.rootUri; + connection.console.log(`[Server(${process.pid}) ${workspaceFolder}] Started and initialize received`); return { capabilities: { textDocumentSync: {