Skip to content

Commit

Permalink
Remove startSessionCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kimushu committed Nov 9, 2017
1 parent 7483ff5 commit 05bd110
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 442 deletions.
14 changes: 0 additions & 14 deletions out/src/debug/rubicDebugHelper.nls.ja.json

This file was deleted.

6 changes: 2 additions & 4 deletions out/src/processes/rubicHostProcess.nls.ja.json
Expand Up @@ -3,14 +3,12 @@
"はい",
"いいえ",
"ファイル {0} は編集中で保存されていません。",
"スケッチのロードに失敗しました: {0}",
"デバッグセッションを開始できません"
"スケッチのロードに失敗しました: {0}"
],
"keys": [
"yes",
"no",
"file-x-dirty",
"sketch-load-failed-x",
"cannot-start-debug"
"sketch-load-failed-x"
]
}
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -142,7 +142,6 @@
"label": "%debug.rubic.label%",
"program": "./out/src/debug/rubicDebugSession.js",
"runtime": "node",
"startSessionCommand": "extension.rubic.startDebugSession",
"configurationAttributes": {
"launch": {
"required": [
Expand Down Expand Up @@ -253,7 +252,6 @@
"markdown-it": "^8.4.0",
"md5": "^2.2.1",
"mruby-native": "github:kimushu/node-mruby-native#1.2.0-build3",
"node-ipc": "^9.1.1",
"pify": "^3.0.0",
"promise.prototype.finally": "^3.0.1",
"request": "^2.83.0",
Expand All @@ -275,7 +273,6 @@
"@types/md5": "^2.1.32",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.45",
"@types/node-ipc": "^9.1.0",
"@types/promise.prototype.finally": "^2.0.2",
"@types/request": "^2.0.6",
"@types/rimraf": "2.0.2",
Expand Down
86 changes: 84 additions & 2 deletions src/debug/rubicDebugConfigProvider.ts
Expand Up @@ -2,14 +2,69 @@ import {
CancellationToken,
DebugConfiguration, DebugConfigurationProvider,
ProviderResult,
WorkspaceFolder
WorkspaceFolder,
window, workspace
} from "vscode";
import * as path from "path";
import { RubicDebugHook, RubicProcess } from "../processes/rubicProcess";

/**
* Substitute variables for VSCode
* @param input Input string
*/
function substituteVariables(input: string): string {
let editor = window.activeTextEditor;
let fileName = (editor != null) ? editor.document.fileName : null;
return input.replace(/\$\{(\w+)\}/g, (match, name) => {
switch (name) {
case "workspaceRoot":
return workspace.workspaceFolders[0].uri.fsPath;
case "workspaceRootFolderName":
return path.basename(workspace.workspaceFolders[0].name);
case "file":
if (fileName != null) {
return fileName;
}
break;
case "relativeFile":
if (fileName != null) {
return path.relative(workspace.rootPath, fileName);
}
break;
case "fileBasename":
if (fileName != null) {
return path.basename(fileName);
}
break;
case "fileBasenameNoExtension":
if (fileName != null) {
return path.basename(fileName, ".*");
}
break;
case "fileDirname":
if (fileName != null) {
return path.dirname(fileName);
}
break;
case "fileExtname":
if (fileName != null) {
return path.extname(fileName);
}
break;
default:
return "${" + name + "}";
}
return "";
});
}

/**
* Debug configuration provider for "rubic" type debugger
*/
export class RubicDebugConfigProvider implements DebugConfigurationProvider {
constructor(private _debugHooks: RubicDebugHook[]) {
}

resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
if (!config.type && !config.request && !config.name) {
// launch.json is missing or empty
Expand All @@ -18,6 +73,33 @@ export class RubicDebugConfigProvider implements DebugConfigurationProvider {
const { initialConfigurations } = rubicDebugger;
Object.assign(config, initialConfigurations[0]);
}
return config;

// Add private data to debug adapter process
let { workspaceRoot, extensionRoot } = RubicProcess.self;
config.__private = { workspaceRoot, extensionRoot };

if ((this._debugHooks == null) || (config.request === "attach")) {
return config;
}

// Substitute variables
if (config.program != null) {
config.program = substituteVariables(config.program);
}

// Invoke hooks
return this._debugHooks.reduce((promise, hook) => {
return promise
.then((continueDebug) => {
return hook.onDebugStart(config);
});
}, Promise.resolve(true))
.then((continueDebug) => {
if (continueDebug) {
return config;
}
// Abort debugging
return undefined;
});
}
}
84 changes: 79 additions & 5 deletions src/debug/rubicDebugSession.ts
Expand Up @@ -2,7 +2,7 @@
import * as nls from "vscode-nls";
const localize = nls.config(process.env.VSCODE_NLS_CONFIG)(__filename);

import { DebugSession, OutputEvent, TerminatedEvent } from "vscode-debugadapter";
import { DebugSession, Event, OutputEvent, TerminatedEvent } from "vscode-debugadapter";
import { DebugProtocol } from "vscode-debugprotocol";
import { RubicDebugProcess } from "../processes/rubicDebugProcess";
import { SketchLoadResult } from "../sketch";
Expand Down Expand Up @@ -43,6 +43,13 @@ interface WriteFirmwareArguments extends BoardPathArguments {
fullPath: string;
}

interface HostRequestQueue {
[id: string]: {
resolve: (value: any) => void;
reject: (reason: any) => void;
};
}

/**
* Debug session for Rubic
*/
Expand All @@ -62,6 +69,9 @@ class RubicDebugSession extends DebugSession {
/** Resolver for debug stream */
private _debugStreamResolver: (s: BoardDebugStream) => void;

/** Sent host requests (with response) */
private _hostRequests: HostRequestQueue = {};

/**
* Execute normal program launch on board
* @param response Response
Expand Down Expand Up @@ -153,12 +163,74 @@ class RubicDebugSession extends DebugSession {
});
}

/**
* Receiver for custom request
* @param command Event name
* @param response Response
* @param args Arguments
*/
protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
response.command = command;
switch (command) {
case "host.response":
// Event response from HostProcess
let { id, result, reason } = args;
let req = this._hostRequests[id];
if (req != null) {
delete this._hostRequests[id];
if (reason != null) {
req.reject(reason);
} else {
req.resolve(result);
}
}
this.sendResponse(response);
return;
case "stop":
// Stop debug session
this.sendResponse(response);
this.sendEvent(new TerminatedEvent());
return;
}

// Process custom debug requests
this.rubicDebugRequest(command, args)
.then((result) => {
response.body = { result };
this.sendResponse(response);
}, (reason) => {
response.body = { reason: `${reason}` };
this.sendResponse(response);
});
}

/**
* Send custom request to host process
* @param request Request name
* @param args Arguments
* @param withResponse Wait response (if false, returned promise will soon resolved with undefined)
*/
sendHostRequest(request: string, args: any, withResponse: boolean): Thenable<any> {
let id: string;
let promise: Promise<any>;
if (withResponse) {
id = Math.random().toString(16).substr(2);
promise = new Promise((resolve, reject) => {
this._hostRequests[id] = { resolve, reject };
});
} else {
promise = Promise.resolve();
}
this.sendEvent(new Event("host.request", { id, request, args }));
return promise;
}

/**
* Process Rubic custom debug request
* @param request Request name
* @param args Arguments
*/
rubicDebugRequest(request: string, args: any): Thenable<any> {
protected rubicDebugRequest(request: string, args: any): Thenable<any> {
switch (request) {
case "board.getInfo":
return this._getInfo(args);
Expand Down Expand Up @@ -230,9 +302,11 @@ class RubicDebugSession extends DebugSession {
* Disconnect from board
*/
private _disconnectBoard(): Promise<void> {
this._report(
localize("disconnecting-board", "Disconnecting from board")
);
if (this._board.isConnected) {
this._report(
localize("disconnecting-board", "Disconnecting from board")
);
}
return this._board.disconnect();
}

Expand Down

1 comment on commit 05bd110

@kimushu
Copy link
Owner Author

@kimushu kimushu commented on 05bd110 Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #24

Please sign in to comment.