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

Add support for Windows .cmd and .bat files #570

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 35 additions & 7 deletions src/common/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fsapi from "fs-extra";
import * as vscode from "vscode";
import { platform } from "os";
import { Disposable, l10n, LanguageStatusSeverity, LogOutputChannel } from "vscode";
import { State } from "vscode-languageclient";
import {
Expand Down Expand Up @@ -44,18 +45,45 @@ export type IInitializationOptions = {
globalSettings: ISettings;
};

/**
* Check if shell mode is required for execFile.
* Note: This is only the case for Windows OS when using .cmd instead of .exe extension to start the interpreter.
* @param file
* @returns
*/
tonka3000 marked this conversation as resolved.
Show resolved Hide resolved
export function execFileShellModeRequired(file: string) {
return platform() === "win32" && file.toLowerCase().endsWith(".cmd");
tonka3000 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Quote given file if shell mode is required
* @param file
* @returns
*/
tonka3000 marked this conversation as resolved.
Show resolved Hide resolved
function quoteFilename(file: string) {
if (execFileShellModeRequired(file)) {
return `"${file}"`;
}
return file;
}

/**
* Function to execute a command and return the stdout.
*/
function executeFile(file: string, args: string[] = []): Promise<string> {
return new Promise((resolve, reject) => {
execFile(file, args, (error, stdout, stderr) => {
if (error) {
reject(new Error(stderr || error.message));
} else {
resolve(stdout);
}
});
execFile(
quoteFilename(file),
args,
{ shell: execFileShellModeRequired(file) },
(error, stdout, stderr) => {
if (error) {
reject(new Error(stderr || error.message));
} else {
resolve(stdout);
}
},
);
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from "vscode";
import * as path from "path";
import { platform } from "os";

const EXTENSION_ID = "charliermarsh.ruff";

Expand All @@ -26,3 +27,7 @@ export const getDocumentPath = (p: string) => {
export const getDocumentUri = (p: string) => {
return vscode.Uri.file(getDocumentPath(p));
};

export const isWindows = () => {
return platform() === "win32";
};
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions src/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as assert from "assert";
import { execFileShellModeRequired } from "../common/server";
import { isWindows } from "./helper";

suite("Utils tests", () => {
test("Check execFile shell mode", () => {
assert.strictEqual(
execFileShellModeRequired("/use/random/python"),
false,
"Shell mode should not be required for unix paths",
);
assert.strictEqual(
execFileShellModeRequired("C:\\random\\python.exe"),
false,
"Shell mode should not be required for .exe files",
);
assert.strictEqual(
execFileShellModeRequired("C:\\random\\python.cmd"),
isWindows() ? true : false,
"Shell mode should be required for .cmd files",
);
});
});