Skip to content

Commit

Permalink
feat(vscode): allow multi-root & enable extension only when @aurelia/…
Browse files Browse the repository at this point in the history
…runtime is in package.json
  • Loading branch information
erik-lieben committed Aug 15, 2019
1 parent b7b9905 commit 99b90ed
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
78 changes: 69 additions & 9 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -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<string, LanguageClient> = 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<void> {
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();
}
}
});
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"icon": "images/logo.png",
"keywords": [
"aurelia",
"vscode",
"extension"
"multi-root ready"
],
"homepage": "http://aurelia.io",
"bugs": {
Expand All @@ -33,7 +32,7 @@
"Snippets"
],
"activationEvents": [
"workspaceContains:**/aurelia_project/aurelia.json"
"workspaceContains:package.json"
],
"main": "./client/dist/extension",
"contributes": {},
Expand Down
3 changes: 2 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 99b90ed

Please sign in to comment.