Skip to content

Commit

Permalink
plugin-dev - fix run/debug flow to work on windows
Browse files Browse the repository at this point in the history
and some code clean

Signed-off-by: Amiram Wingarten <amiram.wingarten@sap.com>
  • Loading branch information
amiramw committed Jun 28, 2019
1 parent dcdea4a commit d45d633
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/filesystem/src/common/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export interface FileSystem extends JsonRpcServer<FileSystemClient> {
* If the URI is not a file URI, undefined is returned.
*
* USE WITH CAUTION: You should always prefer URIs to paths if possible, as they are
* portable and platform independent. Pathes should only be used in cases you directly
* portable and platform independent. Paths should only be used in cases you directly
* interact with the OS, e.g. when running a command on the shell.
*/
getFsPath(uri: string): Promise<string | undefined>
Expand Down
107 changes: 55 additions & 52 deletions packages/plugin-dev/package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
{
"name": "@theia/plugin-dev",
"version": "0.8.0",
"description": "Theia - Plugin Development Extension",
"main": "lib/common/index.js",
"typings": "lib/common/index.d.ts",
"dependencies": {
"@theia/core": "^0.8.0",
"@theia/plugin-ext": "^0.8.0",
"@theia/preferences": "^0.8.0",
"ps-tree": "1.1.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"backend": "lib/node/plugin-dev-backend-module",
"backendElectron": "lib/node-electron/plugin-dev-electron-backend-module",
"frontend": "lib/browser/plugin-dev-frontend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/theia-ide/theia.git"
},
"bugs": {
"url": "https://github.com/theia-ide/theia/issues"
},
"homepage": "https://github.com/theia-ide/theia",
"files": [
"lib",
"src"
],
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "theiaext clean",
"build": "theiaext build",
"watch": "theiaext watch",
"test": "theiaext test"
},
"devDependencies": {
"@theia/ext-scripts": "^0.8.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
{
"name": "@theia/plugin-dev",
"version": "0.8.0",
"description": "Theia - Plugin Development Extension",
"main": "lib/common/index.js",
"typings": "lib/common/index.d.ts",
"dependencies": {
"@theia/core": "^0.8.0",
"@theia/workspace": "^0.8.0",
"@theia/filesystem": "^0.8.0",
"@theia/plugin-ext": "^0.8.0",
"@theia/debug": "^0.8.0",
"@theia/preferences": "^0.8.0",
"ps-tree": "1.1.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"backend": "lib/node/plugin-dev-backend-module",
"backendElectron": "lib/node-electron/plugin-dev-electron-backend-module",
"frontend": "lib/browser/plugin-dev-frontend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/theia-ide/theia.git"
},
"bugs": {
"url": "https://github.com/theia-ide/theia/issues"
},
"homepage": "https://github.com/theia-ide/theia",
"files": [
"lib",
"src"
],
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "theiaext clean",
"build": "theiaext build",
"watch": "theiaext watch",
"test": "theiaext test"
},
"devDependencies": {
"@theia/ext-scripts": "^0.8.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
8 changes: 4 additions & 4 deletions packages/plugin-dev/src/browser/hosted-plugin-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class HostedPluginController implements FrontendApplicationContribution {
this.hostedPluginManagerClient.onStateChanged(e => {
if (e.state === HostedInstanceState.STARTING) {
this.onHostedPluginStarting();
} else if (e.state === HostedInstanceState.RUNNNING) {
} else if (e.state === HostedInstanceState.RUNNING) {
this.onHostedPluginRunning();
} else if (e.state === HostedInstanceState.STOPPED) {
this.onHostedPluginStopped();
Expand Down Expand Up @@ -149,7 +149,7 @@ export class HostedPluginController implements FrontendApplicationContribution {
* Display status bar element for running plugin.
*/
protected async onHostedPluginRunning(): Promise<void> {
this.pluginState = HostedInstanceState.RUNNNING;
this.pluginState = HostedInstanceState.RUNNING;

let entryText: string;
if (this.hostedPluginPreferences['hosted-plugin.watchMode'] && this.watcherSuccess) {
Expand Down Expand Up @@ -216,7 +216,7 @@ export class HostedPluginController implements FrontendApplicationContribution {
* @param event hosted instance state change event
*/
protected async handleWatchers(event: HostedInstanceData): Promise<void> {
if (event.state === HostedInstanceState.RUNNNING) {
if (event.state === HostedInstanceState.RUNNING) {
if (this.hostedPluginPreferences['hosted-plugin.watchMode']) {
await this.runWatchCompilation(event.pluginLocation.toString());
// update status bar
Expand Down Expand Up @@ -289,7 +289,7 @@ export class HostedPluginController implements FrontendApplicationContribution {
commands
});

if (this.pluginState === HostedInstanceState.RUNNNING) {
if (this.pluginState === HostedInstanceState.RUNNING) {
this.addCommandsForRunningPlugin(commands, menu);
} else if (this.pluginState === HostedInstanceState.STOPPED || this.pluginState === HostedInstanceState.FAILED) {
this.addCommandsForStoppedPlugin(commands, menu);
Expand Down
13 changes: 3 additions & 10 deletions packages/plugin-dev/src/browser/hosted-plugin-manager-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export namespace HostedPluginCommands {
export enum HostedInstanceState {
STOPPED = 'stopped',
STARTING = 'starting',
RUNNNING = 'running',
RUNNING = 'running',
STOPPING = 'stopping',
FAILED = 'failed'
}
Expand Down Expand Up @@ -128,13 +128,6 @@ export class HostedPluginManagerClient {
}
}

get lastPluginLocation(): string | undefined {
if (this.pluginLocation) {
return this.pluginLocation.toString();
}
return undefined;
}

async start(debugConfig?: HostedDebugConfig): Promise<void> {
if (await this.hostedPluginServer.isHostedPluginInstanceRunning()) {
this.messageService.warn('Hosted instance is already running.');
Expand All @@ -161,7 +154,7 @@ export class HostedPluginManagerClient {
await this.openPluginWindow();

this.messageService.info('Hosted instance is running at: ' + this.pluginInstanceURL);
this.stateChanged.fire({ state: HostedInstanceState.RUNNNING, pluginLocation: this.pluginLocation });
this.stateChanged.fire({ state: HostedInstanceState.RUNNING, pluginLocation: this.pluginLocation });
} catch (error) {
this.messageService.error('Failed to run hosted plugin instance: ' + this.getErrorMessage(error));
this.stateChanged.fire({ state: HostedInstanceState.FAILED, pluginLocation: this.pluginLocation });
Expand Down Expand Up @@ -224,7 +217,7 @@ export class HostedPluginManagerClient {
this.pluginInstanceURL = await this.hostedPluginServer.runHostedPluginInstance(this.pluginLocation!.toString());
await this.openPluginWindow();
this.messageService.info('Hosted instance is running at: ' + this.pluginInstanceURL);
this.stateChanged.fire({ state: HostedInstanceState.RUNNNING, pluginLocation: this.pluginLocation! });
this.stateChanged.fire({ state: HostedInstanceState.RUNNING, pluginLocation: this.pluginLocation! });
return;
} catch (error) {
lastError = error;
Expand Down
29 changes: 17 additions & 12 deletions packages/plugin-dev/src/node/hosted-instance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { FileUri } from '@theia/core/lib/node/file-uri';
import { LogType } from '@theia/plugin-ext/lib/common/types';
import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin';
import { MetadataScanner } from '@theia/plugin-ext/lib/hosted/node/metadata-scanner';
import { FileSystem } from '@theia/filesystem/lib/common';

const processTree = require('ps-tree');

export const HostedInstanceManager = Symbol('HostedInstanceManager');
Expand Down Expand Up @@ -97,8 +99,7 @@ delete PROCESS_OPTIONS.env.ELECTRON_RUN_AS_NODE;
@injectable()
export abstract class AbstractHostedInstanceManager implements HostedInstanceManager {
protected hostedInstanceProcess: cp.ChildProcess;
protected processOptions: cp.SpawnOptions;
protected isPluginRunnig: boolean = false;
protected isPluginRunning: boolean = false;
protected instanceUri: URI;
protected pluginUri: URI;
protected instanceOptions: object;
Expand All @@ -109,8 +110,11 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
@inject(MetadataScanner)
protected readonly metadata: MetadataScanner;

@inject(FileSystem)
protected readonly fileSystem: FileSystem;

isRunning(): boolean {
return this.isPluginRunnig;
return this.isPluginRunning;
}

async run(pluginUri: URI, port?: number): Promise<URI> {
Expand All @@ -122,7 +126,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
}

private async doRun(pluginUri: URI, port?: number, debugConfig?: DebugConfiguration): Promise<URI> {
if (this.isPluginRunnig) {
if (this.isPluginRunning) {
this.hostedPluginSupport.sendLog({ data: 'Hosted plugin instance is already running.', type: LogType.Info });
throw new Error('Hosted instance is already running.');
}
Expand All @@ -131,7 +135,8 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
let processOptions: cp.SpawnOptions;
if (pluginUri.scheme === 'file') {
processOptions = { ...PROCESS_OPTIONS };
processOptions.env.HOSTED_PLUGIN = pluginUri.path.toString();
// get filesystem path that work cross operating systems
processOptions.env.HOSTED_PLUGIN = await this.fileSystem.getFsPath(pluginUri.toString());

// Disable all the other plugins on this instance
processOptions.env.THEIA_PLUGINS = '';
Expand All @@ -154,7 +159,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
}

terminate(): void {
if (this.isPluginRunnig) {
if (this.isPluginRunning) {
// tslint:disable-next-line:no-any
processTree(this.hostedInstanceProcess.pid, (err: Error, children: Array<any>) => {
// tslint:disable-next-line:no-any
Expand All @@ -168,14 +173,14 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
}

getInstanceURI(): URI {
if (this.isPluginRunnig) {
if (this.isPluginRunning) {
return this.instanceUri;
}
throw new Error('Hosted plugin instance is not running.');
}

getPluginURI(): URI {
if (this.isPluginRunnig) {
if (this.isPluginRunning) {
return this.pluginUri;
}
throw new Error('Hosted plugin instance is not running.');
Expand Down Expand Up @@ -290,7 +295,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
}

protected runHostedPluginTheiaInstance(command: string[], options: cp.SpawnOptions): Promise<URI> {
this.isPluginRunnig = true;
this.isPluginRunning = true;
return new Promise((resolve, reject) => {
let started = false;
const outputListener = (data: string | Buffer) => {
Expand All @@ -304,8 +309,8 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
};

this.hostedInstanceProcess = cp.spawn(command.shift()!, command, options);
this.hostedInstanceProcess.on('error', () => { this.isPluginRunnig = false; });
this.hostedInstanceProcess.on('exit', () => { this.isPluginRunnig = false; });
this.hostedInstanceProcess.on('error', () => { this.isPluginRunning = false; });
this.hostedInstanceProcess.on('exit', () => { this.isPluginRunning = false; });
this.hostedInstanceProcess.stdout.addListener('data', outputListener);

this.hostedInstanceProcess.stdout.addListener('data', data => {
Expand All @@ -318,7 +323,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
setTimeout(() => {
if (!started) {
this.terminate();
this.isPluginRunnig = false;
this.isPluginRunning = false;
reject(new Error('Timeout.'));
}
}, HOSTED_INSTANCE_START_TIMEOUT_MS);
Expand Down

0 comments on commit d45d633

Please sign in to comment.