Skip to content

support WSL #46

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 5 commits into from
Jul 24, 2018
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@
"default": true,
"scope": "window",
"description": "Show a hint to set the default language."
},
"leetcode.useWsl": {
"type": "boolean",
"default": false,
"scope": "window",
"description": "Use Node.js inside the Windows Subsystem for Linux."
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IQuickItemEx, languages, leetCodeBinaryPath, ProblemState } from "../sh
import { executeCommand } from "../utils/cpUtils";
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
import * as wsl from "../utils/wslUtils";
import * as list from "./list";

export async function showProblem(channel: vscode.OutputChannel, node?: LeetCodeNode): Promise<void> {
Expand Down Expand Up @@ -53,7 +54,9 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
const reg: RegExp = /\* Source Code:\s*(.*)/;
const match: RegExpMatchArray | null = result.match(reg);
if (match && match.length >= 2) {
await vscode.window.showTextDocument(vscode.Uri.file(match[1].trim()), { preview: false });
const filePath = wsl.useWsl() ? wsl.toWinPath(match[1].trim()) : match[1].trim();

await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false });
} else {
throw new Error("Failed to fetch the problem information.");
}
Expand Down
7 changes: 6 additions & 1 deletion src/leetCodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UserStatus } from "./shared";
import { leetCodeBinaryPath } from "./shared";
import { executeCommand } from "./utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";

export interface ILeetCodeManager extends EventEmitter {
getLoginStatus(channel: vscode.OutputChannel): void;
Expand Down Expand Up @@ -43,7 +44,11 @@ class LeetCodeManager extends EventEmitter implements ILeetCodeManager {
try {
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
let result: string = "";
const childProc: cp.ChildProcess = cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });

const childProc: cp.ChildProcess = wsl.useWsl()
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });

childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
result = result.concat(data);
Expand Down
9 changes: 8 additions & 1 deletion src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import * as path from "path";
import * as vscode from "vscode";
import * as wsl from "./utils/wslUtils";

export const leetCodeBinaryPath: string = `"${path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode")}"`;
let binPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode");

if (wsl.useWsl()) {
binPath = wsl.toWslPath(binPath);
}

export const leetCodeBinaryPath: string = `"${binPath}"`;

export interface IQuickItemEx<T> extends vscode.QuickPickItem {
value: T;
Expand Down
6 changes: 5 additions & 1 deletion src/utils/cpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import * as cp from "child_process";
import * as vscode from "vscode";
import * as wsl from "./wslUtils";

export async function executeCommand(channel: vscode.OutputChannel, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> {
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
let result: string = "";
const childProc: cp.ChildProcess = cp.spawn(command, args, options);

const childProc: cp.ChildProcess = wsl.useWsl()
? cp.spawn("wsl", [command].concat(args), options)
: cp.spawn(command, args, options);

childProc.stdout.on("data", (data: string | Buffer) => {
data = data.toString();
Expand Down
8 changes: 6 additions & 2 deletions src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import * as wsl from "./wslUtils";

export async function selectWorkspaceFolder(): Promise<string> {
let folder: vscode.WorkspaceFolder | undefined;
Expand All @@ -15,7 +16,10 @@ export async function selectWorkspaceFolder(): Promise<string> {
folder = vscode.workspace.workspaceFolders[0];
}
}
return folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");

const workFolder = folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");

return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder;
}

export async function getActivefilePath(uri?: vscode.Uri): Promise<string | undefined> {
Expand All @@ -33,5 +37,5 @@ export async function getActivefilePath(uri?: vscode.Uri): Promise<string | unde
vscode.window.showWarningMessage("Please save the solution file first.");
return undefined;
}
return textEditor.document.uri.fsPath;
return wsl.useWsl() ? wsl.toWslPath(textEditor.document.uri.fsPath) : textEditor.document.uri.fsPath;
}
18 changes: 18 additions & 0 deletions src/utils/wslUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

import * as cp from "child_process";
import * as vscode from "vscode";

export function useWsl(): boolean {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");

return process.platform === "win32" && leetCodeConfig.get<boolean>("useWsl") === true;
}

export function toWslPath(path: string): string {
return cp.execFileSync("wsl", ["wslpath", "-u", `${path.replace(/\\/g, "/")}`]).toString().trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use " to wrap the path instead of replace \, will it be workable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not Work :(

function toWslPath(path) {
    return cp.execFileSync("wsl", ["wslpath", "-u", `"${path}"`]).toString().trim();
}
module.js:549
    throw err;
    ^

Error: Cannot find module '/mnt/y/env/VSCode-win32-x64/Y:/env/VSCode-win32-x64/.vscode/extensions/shengchen.vscode-leetcode-0.7.0/node_modules/leetcode-cli/bin/leetcode'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With pathUtils my point of view is not unnecessary at this time.

For this program, we just need do good job with file system and shell command for cross-platform. I think the cpUtils and workspaceUtils is sufficiently abstract.

We can try to change these next time if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could focus on cpUtils and workspaceUtils provide service for others.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@purocean That is because by default the option shell of execFileSync is false. If add {shell: true} to the argument list, then I think it should work.

Another thing I'm concerning is that here we use execFileSync(). Personally, I prefer use async/await wherever possible. Take a look at this: https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
So can we use executeCommand in the cpUtils?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{shell: true} work perfectly.

https://github.com/purocean/vscode-leetcode/blob/d330913ad843179e12d0248a155f8a1f570e9f6f/src/shared.ts#L7

We need a lot of works if we use async/await. Abstract leetcode command is a better option.

But we could improve step by step. Half a loaf is better than no bread.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, make sense. Tracking issue: #48

}

export function toWinPath(path: string): string {
return cp.execFileSync("wsl", ["wslpath", "-w", path]).toString().trim();
}