瀏覽代碼

Expose Godot path as getGodotPath command (#586)

adds new command: "godotTools.getGodotPath"

---------

Co-authored-by: David Kincaid <[email protected]>
Butch Wesley 1 年之前
父節點
當前提交
0058ffa870
共有 2 個文件被更改,包括 44 次插入10 次删除
  1. 19 0
      src/extension.ts
  2. 25 10
      src/utils/project_utils.ts

+ 19 - 0
src/extension.ts

@@ -68,6 +68,7 @@ export function activate(context: vscode.ExtensionContext) {
 		register_command("copyResourcePath", copy_resource_path),
 		register_command("listGodotClasses", list_classes),
 		register_command("switchSceneScript", switch_scene_script),
+		register_command("getGodotPath", get_godot_path),
 	);
 
 	set_context("godotFiles", ["gdscript", "gdscene", "gdresource", "gdshader",]);
@@ -190,6 +191,24 @@ async function open_workspace_with_editor() {
 	}
 }
 
+/**
+ * Returns the executable path for Godot based on the current project's version.
+ * Created to allow other extensions to get the path without having to go
+ * through the steps of determining the version to get the proper configuration
+ * value (godotTools.editorPath.godot3/4).
+ * @returns
+ */
+async function get_godot_path(): Promise<string|undefined> {
+	const projectVersion = await get_project_version();
+	if (projectVersion === undefined) {
+		return undefined;
+	}
+	const settingName = `editorPath.godot${projectVersion[0]}`;
+	// Cleans up any surrounding quotes the user might put into the path.
+	const godotPath : string = get_configuration(settingName).replace(/^"/, "").replace(/"$/, "");
+	return godotPath;
+}
+
 class GodotEditorTerminal implements vscode.Pseudoterminal {
 	private writeEmitter = new vscode.EventEmitter<string>();
 	onDidWrite: vscode.Event<string> = this.writeEmitter.event;

+ 25 - 10
src/utils/project_utils.ts

@@ -3,19 +3,29 @@ import * as path from "path";
 import * as fs from "fs";
 import { execSync } from "child_process";
 
-let projectDir = undefined;
-let projectFile = undefined;
+let projectDir: string | undefined = undefined;
+let projectFile: string | undefined = undefined;
 
-export async function get_project_dir() {
+export async function get_project_dir(): Promise<string | undefined> {
 	let file = "";
 	if (vscode.workspace.workspaceFolders != undefined) {
 		const files = await vscode.workspace.findFiles("**/project.godot");
-		// if multiple project files, pick the top-most one
-		const best = files.reduce((a, b) => a.fsPath.length <= b.fsPath.length ? a : b);
-		if (best) {
-			file = best.fsPath;
+
+		if (files.length == 0) {
+			return undefined;
+		} else if (files.length == 1) {
+			file = files[0].fsPath;
 			if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
-				return;
+				return undefined;
+			}
+		} else if (files.length > 1) {
+			// if multiple project files, pick the top-most one
+			const best = files.reduce((a, b) => a.fsPath.length <= b.fsPath.length ? a : b);
+			if (best) {
+				file = best.fsPath;
+				if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
+					return undefined;
+				}
 			}
 		}
 	}
@@ -24,12 +34,17 @@ export async function get_project_dir() {
 	return projectDir;
 }
 
-let projectVersion = undefined;
+let projectVersion: string | undefined = undefined;
 
 export async function get_project_version(): Promise<string | undefined> {
 	if (projectDir === undefined || projectFile === undefined) {
 		await get_project_dir();
 	}
+
+	if (projectFile === undefined) {
+		return undefined;
+	}
+
 	let godotVersion = "3.x";
 	const document = await vscode.workspace.openTextDocument(projectFile);
 	const text = document.getText();
@@ -81,7 +96,7 @@ type VERIFY_RESULT = {
 export function verify_godot_version(godotPath: string, expectedVersion: "3" | "4" | string): VERIFY_RESULT {
 	try {
 		const output = execSync(`"${godotPath}" -h`).toString().trim();
-		const pattern = /^Godot Engine v(([34])\.([0-9]+)(?:\.[0-9]+)?)/;
+		const pattern = /^Godot Engine v(([34])\.([0-9]+)(?:\.[0-9]+)?)/m;
 		const match = output.match(pattern);
 		if (!match) {
 			return { status: "INVALID_EXE" };