Skip to content

Commit

Permalink
Relative links in tasks output, closing and truncated export cmd in t…
Browse files Browse the repository at this point in the history
…erminal (#337)

* prefer shell execution that pseudo terminal

* rm export set cmds use strict env terminals

* fix monitor not closing

* rm timeout
  • Loading branch information
brianignacio5 authored Mar 3, 2021
1 parent 5bff08b commit 2df42de
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 215 deletions.
13 changes: 6 additions & 7 deletions src/build/buildTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import * as vscode from "vscode";
import * as idfConf from "../idfConfiguration";
import { appendIdfAndToolsToPath, isBinInPath } from "../utils";
import { TaskManager } from "../taskManager";
import EspIdfCustomTerminal from "../espIdfCustomTerminal";
import { SpawnOptions } from "child_process";

export class BuildTask {
public static isBuilding: boolean;
Expand All @@ -45,10 +43,11 @@ export class BuildTask {
}
}

public getShellExecution(args: string[], options?: SpawnOptions) {
return new vscode.CustomExecution(
async () => new EspIdfCustomTerminal("cmake", args, options)
);
public getShellExecution(
args: string[],
options?: vscode.ShellExecutionOptions
) {
return new vscode.ShellExecution(`cmake ${args.join(" ")}`, options);
}

public async build() {
Expand Down Expand Up @@ -79,7 +78,7 @@ export class BuildTask {
if (canAccessCMake === "" || canAccessNinja === "") {
throw new Error("CMake or Ninja executables not found");
}
const options: SpawnOptions = {
const options: vscode.ShellExecutionOptions = {
cwd: this.curWorkspace,
env: modifiedEnv,
};
Expand Down
145 changes: 0 additions & 145 deletions src/espIdfCustomTerminal.ts

This file was deleted.

76 changes: 20 additions & 56 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,7 @@ export async function activate(context: vscode.ExtensionContext) {
debugAdapterManager.stop();
if (isMonitorLaunchedByDebug) {
isMonitorLaunchedByDebug = false;
monitorTerminal.processId.then((monitorPid) => {
kill(monitorPid, "SIGKILL");
monitorTerminal.dispose();
});
monitorTerminal.dispose();
}
});

Expand All @@ -346,6 +343,15 @@ export async function activate(context: vscode.ExtensionContext) {
});
context.subscriptions.push(sdkDeleteWatchDisposable);

vscode.window.onDidCloseTerminal(async (terminal: vscode.Terminal) => {
const terminalPid = await terminal.processId;
const monitorTerminalPid = (await monitorTerminal?.processId) || -1;
if (monitorTerminalPid === terminalPid) {
monitorTerminal = undefined;
kill(monitorTerminalPid);
}
});

registerIDFCommand("espIdf.createFiles", async () => {
PreCheck.perform([openFolderCheck], async () => {
try {
Expand Down Expand Up @@ -2298,13 +2304,8 @@ const flash = () => {
}

if (monitorTerminal) {
Logger.warnNotify("ESP-IDF Monitor was closed.");
const monitorPid = await monitorTerminal.processId;
kill(monitorPid, "SIGKILL");
Logger.warnNotify("ESP-IDF Monitor was open. Closing it.");
monitorTerminal.dispose();
setTimeout(() => {
monitorTerminal = undefined;
}, 200);
}

const idfPathDir = idfConf.readParameter("idf.espIdfPath");
Expand Down Expand Up @@ -2432,13 +2433,8 @@ const buildFlashAndMonitor = (runMonitor: boolean = true) => {
return;
}
if (monitorTerminal) {
Logger.warnNotify("ESP-IDF Monitor was closed.");
const monitorPid = await monitorTerminal.processId;
kill(monitorPid, "SIGKILL");
Logger.warnNotify("ESP-IDF Monitor was open. Closing it.");
monitorTerminal.dispose();
setTimeout(() => {
monitorTerminal = undefined;
}, 200);
}
const buildTask = new BuildTask(workspaceRoot.fsPath);
const buildPath = path.join(workspaceRoot.fsPath, "build");
Expand Down Expand Up @@ -2613,14 +2609,17 @@ function createMonitor() {
return reject(new Error("NOT_SELECTED_PORT"));
}
if (typeof monitorTerminal === "undefined") {
const shellExecutable = path.basename(vscode.env.shell);
monitorTerminal = vscode.window.createTerminal({
name: "ESP-IDF Monitor",
env: modifiedEnv,
cwd: workspaceRoot.fsPath,
shellArgs: [],
shellPath: shellExecutable,
strictEnv: true,
});
}
monitorTerminal.show();
overrideVscodeTerminalWithIdfEnv(monitorTerminal, modifiedEnv);
const osRelease = release();
const kernelMatch = osRelease.toLowerCase().match(/(.*)-(.*)-(.*)/);
let isWsl2Kernel: number = -1; // WSL 2 is implemented on Microsoft Linux Kernel >=4.19
Expand Down Expand Up @@ -2665,58 +2664,23 @@ function createMonitor() {
function createIdfTerminal() {
PreCheck.perform([webIdeCheck, openFolderCheck], () => {
const modifiedEnv = utils.appendIdfAndToolsToPath();
const shellExecutable = path.basename(vscode.env.shell);
const espIdfTerminal = vscode.window.createTerminal({
name: "ESP-IDF Terminal",
env: modifiedEnv,
cwd: workspaceRoot.fsPath || modifiedEnv.IDF_PATH || process.cwd(),
strictEnv: true,
shellArgs: [],
shellPath: shellExecutable,
});
espIdfTerminal.show();
overrideVscodeTerminalWithIdfEnv(espIdfTerminal, modifiedEnv);
});
}

function getTxtCmd(variable: string, modifiedEnv: { [key: string]: string }) {
const shellExecutable = path.basename(vscode.env.shell);
switch (shellExecutable) {
case "cmd.exe":
return `set "${variable}=${modifiedEnv[variable]}"`;
case "powershell.exe":
case "pwsh.exe":
return `$Env:${variable}="${modifiedEnv[variable]}"`;
case "pwsh":
return `$Env:${variable}="${modifiedEnv[variable]}"`;
default:
return `export ${variable}="${modifiedEnv[variable]}"`;
}
}

function overrideVscodeTerminalWithIdfEnv(
terminal: vscode.Terminal,
modifiedEnv: { [key: string]: string }
) {
const pathVar = process.platform === "win32" ? "Path" : "PATH";
const envVars = [
pathVar,
"IDF_PATH",
"IDF_TARGET",
"PYTHON",
"IDF_PYTHON_ENV_PATH",
];
for (const envVar of envVars) {
terminal.sendText(`${getTxtCmd(envVar, modifiedEnv)}`);
}
const clearCmd = process.platform === "win32" ? "cls" : "clear";
terminal.sendText(clearCmd);
}

export function deactivate() {
Telemetry.dispose();
if (monitorTerminal) {
monitorTerminal.processId.then((monitorPid) => {
kill(monitorPid, "SIGKILL");
monitorTerminal.dispose();
});
monitorTerminal.dispose();
}
OutputChannel.end();

Expand Down
13 changes: 6 additions & 7 deletions src/flash/flashTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import {
extensionContext,
} from "../utils";
import { TaskManager } from "../taskManager";
import { SpawnOptions } from "child_process";
import EspIdfCustomTerminal from "../espIdfCustomTerminal";

export class FlashTask {
public static isFlashing: boolean;
Expand Down Expand Up @@ -86,7 +84,7 @@ export class FlashTask {
if (kernelMatch && kernelMatch.length) {
isWsl2Kernel = compareVersion(kernelMatch[1], "4.19");
}
let flashExecution: vscode.CustomExecution;
let flashExecution: vscode.ShellExecution;
if (
process.platform === "linux" &&
osRelease.toLowerCase().indexOf("microsoft") !== -1 &&
Expand Down Expand Up @@ -141,7 +139,7 @@ export class FlashTask {
flashFile.binFilePath.replace(/\//g, "\\")
);
}
const options: SpawnOptions = {
const options: vscode.ShellExecutionOptions = {
cwd: this.buildDir,
env: modifiedEnv,
};
Expand Down Expand Up @@ -173,13 +171,14 @@ export class FlashTask {
for (const flashFile of this.model.flashSections) {
flasherArgs.push(flashFile.address, flashFile.binFilePath);
}
const options: SpawnOptions = {
const options: vscode.ShellExecutionOptions = {
cwd: this.buildDir,
env: modifiedEnv,
};
const pythonBinPath = idfConf.readParameter("idf.pythonBinPath") as string;
return new vscode.CustomExecution(
async () => new EspIdfCustomTerminal(pythonBinPath, flasherArgs, options)
return new vscode.ShellExecution(
`${pythonBinPath} ${flasherArgs.join(" ")}`,
options
);
}
}

0 comments on commit 2df42de

Please sign in to comment.