Skip to content

ATL-972: Can customize the launch.json location. #22

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

Merged
merged 2 commits into from
Mar 3, 2021
Merged
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
30 changes: 22 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import { promises as fs } from 'fs';
import { spawnSync } from 'child_process';
import deepEqual from 'deep-equal';
import WebRequest from 'web-request';
Expand Down Expand Up @@ -35,6 +34,11 @@ interface DebugConfig {
readonly name?: string;
}
readonly sketchPath: string;
/**
* Location where the `launch.config` will be created on the fly before starting every debug session.
* If not defined, it falls back to `sketchPath/.vscode/launch.json`.
*/
readonly configPath?: string;
}

interface DebugInfo {
Expand Down Expand Up @@ -140,13 +144,10 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo

let customDebugConfig = {};
try {
const raw = await promisify(fs.readFile)(path.join(config.sketchPath, 'debug_custom.json'), { encoding: 'utf8' });
const raw = await fs.readFile(path.join(config.sketchPath, 'debug_custom.json'), { encoding: 'utf8' });
customDebugConfig = JSON.parse(raw);
} catch { }
const mergedDebugConfig = deepmerge(defaultDebugConfig, customDebugConfig);

// Create the `launch.json` if it does not exist. Otherwise, update the existing.
const configuration = vscode.workspace.getConfiguration();
const launchConfig = {
version: '0.2.0',
'configurations': [
Expand All @@ -155,7 +156,7 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
}
]
};
await configuration.update('launch', launchConfig, false);
await updateLaunchConfig(config, launchConfig);
return vscode.debug.startDebugging(undefined, mergedDebugConfig);
}

Expand Down Expand Up @@ -199,7 +200,7 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
let logPath: string | undefined = undefined;
if (typeof log === 'string') {
try {
const stats = await promisify(fs.stat)(log);
const stats = await fs.stat(log);
if (stats.isDirectory()) {
logPath = log;
}
Expand Down Expand Up @@ -248,3 +249,16 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
}
);
}

/**
* Instead of writing the `launch.json` to the workspace, the file is written to the temporary binary output location.
*/
async function updateLaunchConfig(debugConfig: DebugConfig, launchConfig: object): Promise<void> {
if (debugConfig.configPath) {
await fs.mkdir(debugConfig.configPath, { recursive: true });
await fs.writeFile(path.join(debugConfig.configPath, 'launch.json'), JSON.stringify(launchConfig, null, 2));
} else {
const configuration = vscode.workspace.getConfiguration();
await configuration.update('launch', launchConfig, false);
}
}