Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to allow web extension #328

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions build-web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*********************************************************************
* Copyright (c) 2024 Renesas Electronics Corporation and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*********************************************************************/
// @ts-check
const esbuild = require('esbuild');
const path = require('node:path');
const { nodeExternalsPlugin } = require('esbuild-node-externals');

/** @typedef {import('esbuild').BuildOptions} BuildOptions */

const args = process.argv.slice(2);
const optionWatch = args.includes('--watch');
const optionNoMinify = args.includes('--no-minify');
const sourceFolder = path.join(__dirname, 'src');
const distFolder = path.join(__dirname, 'dist');

/** @type {BuildOptions[]} */
const buildConfigurations = [
{
target: ['es2015'],
platform: 'browser',
format: 'cjs',
minify: !optionNoMinify,
bundle: true,
sourcemap: true,
entryPoints: [path.join(sourceFolder, 'web.ts')],
outfile: path.join(distFolder, 'browser', 'web.js'),
mainFields: ['browser', 'modules', 'main'],
alias: {
os: 'os-browserify',
path: 'path-browserify',
stream: 'stream-browserify',
},
plugins: [nodeExternalsPlugin()],
},
];

if (optionWatch) {
(async function watch() {
await Promise.all([
...buildConfigurations.map((config) =>
esbuild.context(config).then((context) => context.watch())
),
]);
})();
} else {
(async function build() {
await Promise.all([
...buildConfigurations.map((config) => esbuild.build(config)),
]);
})();
}
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "cdt-gdb-adapter",
"version": "0.0.33-next",
"version": "1.0.1-next",
"description": "gdb adapter implementing the debug adapter protocol",
"main": "dist/index.js",
"browser": "dist/browser/web.js",
"types": "dist/index.d.ts",
"bin": {
"cdtDebugAdapter": "./dist/debugAdapter.js",
Expand All @@ -12,8 +13,12 @@
"install": "node install.js",
"nativebuild": "node-gyp rebuild",
"prepublish": "yarn build",
"build": "tsc",
"watch": "tsc -w",
"build-tsc": "tsc",
"build-web": "node build-web.js",
"build": "run-s build-tsc build-web",
"watch-tsc": "tsc -w",
"watch-web": "node build-web.js --watch",
"watch": "run-p watch-tsc watch-web",
"lint": "eslint . --ext .ts,.tsx",
"format": "prettier --write .",
"format-check": "prettier --check .",
Expand Down Expand Up @@ -77,18 +82,24 @@
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@vscode/debugadapter-testsupport": "^1.59.0",
"browserify": "^17.0.0",
"chai": "^4.3.7",
"chai-string": "^1.5.0",
"cross-env": "^7.0.3",
"esbuild": "^0.21.5",
"esbuild-node-externals": "^1.13.1",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"mocha": "^10.2.0",
"mocha-jenkins-reporter": "^0.4.8",
"node-gyp": "^8.4.1",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"prettier": "2.8.4",
"sinon": "^17.0.0",
"stream-browserify": "^3.0.0",
"tmp": "^0.2.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
Expand Down
4 changes: 2 additions & 2 deletions src/MIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*********************************************************************/
import { Readable } from 'stream';
import { logger } from '@vscode/debugadapter/lib/logger';
import { GDBBackend } from './GDBBackend';
import { IGDBBackend } from './types/gdb';
import * as utf8 from 'utf8';

type CommandQueue = {
Expand All @@ -23,7 +23,7 @@ export class MIParser {
protected commandQueue: CommandQueue = {};
protected waitReady?: (value?: void | PromiseLike<void>) => void;

constructor(protected gdb: GDBBackend) {}
constructor(protected gdb: IGDBBackend) {}

public parse(stream: Readable): Promise<void> {
return new Promise((resolve) => {
Expand Down
10 changes: 8 additions & 2 deletions src/debugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
* SPDX-License-Identifier: EPL-2.0
*********************************************************************/
import { logger } from '@vscode/debugadapter/lib/logger';
import { GDBDebugSession } from './GDBDebugSession';
import { GDBDebugSession } from './desktop/GDBDebugSession';

process.on('uncaughtException', (err: any) => {
logger.error(JSON.stringify(err));
});

GDBDebugSession.run(GDBDebugSession);
class GDBDebugSessionToRun extends GDBDebugSession {
constructor() {
super();
}
}

GDBDebugSessionToRun.run(GDBDebugSessionToRun);
10 changes: 8 additions & 2 deletions src/debugTargetAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
* SPDX-License-Identifier: EPL-2.0
*********************************************************************/
import { logger } from '@vscode/debugadapter/lib/logger';
import { GDBTargetDebugSession } from './GDBTargetDebugSession';
import { GDBTargetDebugSession } from './desktop/GDBTargetDebugSession';

process.on('uncaughtException', (err: any) => {
logger.error(JSON.stringify(err));
});

GDBTargetDebugSession.run(GDBTargetDebugSession);
class GDBTargetDebugSessionToRun extends GDBTargetDebugSession {
constructor() {
super();
}
}

GDBTargetDebugSessionToRun.run(GDBTargetDebugSessionToRun);
96 changes: 96 additions & 0 deletions src/desktop/GDBDebugSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*********************************************************************
* Copyright (c) 2018 QNX Software Systems and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*********************************************************************/
import * as fs from 'fs';
import { DebugSession, logger } from '@vscode/debugadapter';
import {
LaunchRequestArguments,
AttachRequestArguments,
} from '../types/session';
import { GDBDebugSessionBase } from '../gdb/GDBDebugSessionBase';
import { GDBBackendFactory } from './factories/GDBBackendFactory';
import { IGDBBackendFactory } from '../types/gdb';

export class GDBDebugSession extends GDBDebugSessionBase {
/**
* Initial (aka default) configuration for launch/attach request
* typically supplied with the --config command line argument.
*/
protected static defaultRequestArguments?: any;

/**
* Frozen configuration for launch/attach request
* typically supplied with the --config-frozen command line argument.
*/
protected static frozenRequestArguments?: { request?: string };
constructor(backendFactory?: IGDBBackendFactory) {
super(backendFactory || new GDBBackendFactory());
this.logger = logger;
}

/**
* Main entry point
*/
public static run(debugSession: typeof DebugSession) {
GDBDebugSession.processArgv(process.argv.slice(2));
DebugSession.run(debugSession);
}

/**
* Parse an optional config file which is a JSON string of launch/attach request arguments.
* The config can be a response file by starting with an @.
*/
public static processArgv(args: string[]) {
args.forEach(function (val, _index, _array) {
const configMatch = /^--config(-frozen)?=(.*)$/.exec(val);
if (configMatch) {
let configJson;
const configStr = configMatch[2];
if (configStr.startsWith('@')) {
const configFile = configStr.slice(1);
configJson = JSON.parse(
fs.readFileSync(configFile).toString('utf8')
);
} else {
configJson = JSON.parse(configStr);
}
if (configMatch[1]) {
GDBDebugSession.frozenRequestArguments = configJson;
} else {
GDBDebugSession.defaultRequestArguments = configJson;
}
}
});
}

/**
* Apply the initial and frozen launch/attach request arguments.
* @param request the default request type to return if request type is not frozen
* @param args the arguments from the user to apply initial and frozen arguments to.
* @returns resolved request type and the resolved arguments
*/
protected applyRequestArguments(
request: 'launch' | 'attach',
args: LaunchRequestArguments | AttachRequestArguments
): ['launch' | 'attach', LaunchRequestArguments | AttachRequestArguments] {
const frozenRequest = GDBDebugSession.frozenRequestArguments?.request;
if (frozenRequest === 'launch' || frozenRequest === 'attach') {
request = frozenRequest;
}

return [
request,
{
...GDBDebugSession.defaultRequestArguments,
...args,
...GDBDebugSession.frozenRequestArguments,
},
];
}
}
Loading
Loading