- Notifications
You must be signed in to change notification settings - Fork669
Centralize the logic about wsl#55
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
8 changes: 4 additions & 4 deletionssrc/commands/list.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletionssrc/commands/session.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletionssrc/commands/show.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletionssrc/commands/submit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletionssrc/commands/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionssrc/extension.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletionssrc/leetCodeExecutor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"use strict"; | ||
import * as cp from "child_process"; | ||
import * as opn from "opn"; | ||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils"; | ||
import { DialogOptions } from "./utils/uiUtils"; | ||
import * as wsl from "./utils/wslUtils"; | ||
export interface ILeetCodeExecutor { | ||
meetRequirements(): Promise<boolean>; | ||
getLeetCodeBinaryPath(): Promise<string>; | ||
/* section for user command */ | ||
getUserInfo(): Promise<string>; | ||
signOut(): Promise<string>; | ||
// TODO: implement login when leetcode-cli support login in batch mode. | ||
// signIn(): Promise<string>; | ||
/* section for problem command */ | ||
listProblems(showLocked: boolean): Promise<string>; | ||
showProblem(id: string, language: string, outdir: string): Promise<string>; | ||
/* section for session command */ | ||
listSessions(): Promise<string>; | ||
enableSession(name: string): Promise<string>; | ||
createSession(name: string): Promise<string>; | ||
/* section for solution command */ | ||
submitSolution(filePath: string): Promise<string>; | ||
testSolution(filePath: string, testString?: string): Promise<string>; | ||
} | ||
class LeetCodeExecutor implements ILeetCodeExecutor { | ||
private leetCodeBinaryPath: string; | ||
private leetCodeBinaryPathInWsl: string; | ||
constructor() { | ||
this.leetCodeBinaryPath = path.join(__dirname, "..", "..", "node_modules", "leetcode-cli", "bin", "leetcode"); | ||
this.leetCodeBinaryPathInWsl = ""; | ||
} | ||
public async getLeetCodeBinaryPath(): Promise<string> { | ||
if (wsl.useWsl()) { | ||
if (!this.leetCodeBinaryPathInWsl) { | ||
this.leetCodeBinaryPathInWsl = `${await wsl.toWslPath(this.leetCodeBinaryPath)}`; | ||
} | ||
return `"${this.leetCodeBinaryPathInWsl}"`; | ||
} | ||
return `"${this.leetCodeBinaryPath}"`; | ||
} | ||
public async meetRequirements(): Promise<boolean> { | ||
try { | ||
await this.executeCommandEx("node", ["-v"]); | ||
return true; | ||
} catch (error) { | ||
const choice: vscode.MessageItem | undefined = await vscode.window.showErrorMessage( | ||
"LeetCode extension needs Node.js installed in environment path", | ||
DialogOptions.open, | ||
); | ||
if (choice === DialogOptions.open) { | ||
opn("https://nodejs.org"); | ||
} | ||
return false; | ||
} | ||
} | ||
public async getUserInfo(): Promise<string> { | ||
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "user"]); | ||
} | ||
public async signOut(): Promise<string> { | ||
return await await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "user", "-L"]); | ||
} | ||
public async listProblems(showLocked: boolean): Promise<string> { | ||
return await this.executeCommandEx("node", showLocked ? | ||
[await this.getLeetCodeBinaryPath(), "list"] : | ||
[await this.getLeetCodeBinaryPath(), "list", "-q", "L"], | ||
); | ||
} | ||
public async showProblem(id: string, language: string, outdir: string): Promise<string> { | ||
return await this.executeCommandWithProgressEx("Fetching problem data...", "node", [await this.getLeetCodeBinaryPath(), "show", id, "-gx", "-l", language, "-o", `"${outdir}"`]); | ||
} | ||
public async listSessions(): Promise<string> { | ||
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session"]); | ||
} | ||
public async enableSession(name: string): Promise<string> { | ||
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session", "-e", name]); | ||
} | ||
public async createSession(name: string): Promise<string> { | ||
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session", "-c", name]); | ||
} | ||
public async submitSolution(filePath: string): Promise<string> { | ||
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "submit", `"${filePath}"`]); | ||
} | ||
public async testSolution(filePath: string, testString?: string): Promise<string> { | ||
if (testString) { | ||
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `"${testString}"`]); | ||
} | ||
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]); | ||
} | ||
private async executeCommandEx(command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> { | ||
if (wsl.useWsl()) { | ||
return await executeCommand("wsl", [command].concat(args), options); | ||
} | ||
return await executeCommand(command, args, options); | ||
} | ||
private async executeCommandWithProgressEx(message: string, command: string, args: string[], options: cp.SpawnOptions = { shell: true }): Promise<string> { | ||
if (wsl.useWsl()) { | ||
return await executeCommandWithProgress(message, "wsl", [command].concat(args), options); | ||
} | ||
return await executeCommandWithProgress(message, command, args, options); | ||
} | ||
} | ||
export const leetCodeExecutor: ILeetCodeExecutor = new LeetCodeExecutor(); |
9 changes: 5 additions & 4 deletionssrc/leetCodeManager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletionssrc/shared.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletionssrc/utils/cpUtils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletionssrc/utils/nodeUtils.ts
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.