Skip to content

Commit

Permalink
Merge pull request #303 from mihai-vlc/master
Browse files Browse the repository at this point in the history
Adds support for enabling WSL Mode for the shell language only
  • Loading branch information
twibiral authored Dec 15, 2023
2 parents 69249d7 + c2d1c4c commit b940cb3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ esbuild.build({
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'src/main.js',
outfile: 'main.js',
}).catch(() => process.exit(1));
20 changes: 15 additions & 5 deletions src/executors/NonInteractiveCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Executor from "./Executor";
import {Outputter} from "src/Outputter";
import {LanguageId} from "src/main";
import { ExecutorSettings } from "../settings/Settings.js";
import { sep } from "path";
import { join } from "path/posix";
import windowsPathToWsl from "../transforms/windowsPathToWsl.js";

export default class NonInteractiveCodeExecutor extends Executor {
Expand Down Expand Up @@ -38,13 +36,13 @@ export default class NonInteractiveCodeExecutor extends Executor {

fs.promises.writeFile(tempFileName, codeBlockContent).then(() => {
const args = cmdArgs ? cmdArgs.split(" ") : [];
if (this.settings.wslMode) {

if (this.isWSLEnabled()) {
args.unshift("-e", cmd);
cmd = "wsl";
args.push(windowsPathToWsl(tempFileName));
} else {
args.push(tempFileName);
args.push(tempFileName);
}

const child = child_process.spawn(cmd, args, {env: process.env, shell: this.usesShell});
Expand All @@ -63,6 +61,18 @@ export default class NonInteractiveCodeExecutor extends Executor {
});
}

private isWSLEnabled(): boolean {
if (this.settings.wslMode) {
return true;
}

if (this.language == 'shell' && this.settings.shellWSLMode) {
return true;
}

return false;
}

/**
* Handles the output of a child process and redirects stdout and stderr to the given {@link Outputter} element.
* Removes the temporary file after the code execution. Creates a new Notice after the code execution.
Expand Down
2 changes: 2 additions & 0 deletions src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ExecutorSettings {
timeout: number;
allowInput: boolean;
wslMode: boolean;
shellWSLMode: boolean;
onlyCurrentBlock: boolean;
nodePath: string;
nodeArgs: string;
Expand Down Expand Up @@ -175,6 +176,7 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
timeout: 10000,
allowInput: true,
wslMode: false,
shellWSLMode: false,
onlyCurrentBlock: false,
nodePath: "node",
nodeArgs: "",
Expand Down
14 changes: 13 additions & 1 deletion src/settings/per-lang/makeShellSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ export default (tab: SettingsTab, containerEl: HTMLElement) => {
console.log('Shell file extension set to: ' + value);
await tab.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Shell WSL mode')
.setDesc('Run the shell script in Windows Subsystem for Linux. This option is used if the global "WSL Mode" is disabled.')
.addToggle((toggle) =>
toggle
.setValue(tab.plugin.settings.shellWSLMode)
.onChange(async (value) => {
tab.plugin.settings.shellWSLMode = value;
await tab.plugin.saveSettings();
})
);
tab.makeInjectSetting(containerEl, "shell");
}
}

0 comments on commit b940cb3

Please sign in to comment.