Expose Godot path as getGodotPath command (#586)

adds new command: "godotTools.getGodotPath"

---------

Co-authored-by: David Kincaid <daelonsuzuka@gmail.com>
This commit is contained in:
Butch Wesley
2024-02-21 13:21:35 -05:00
committed by GitHub
parent bf1d739d07
commit 0058ffa870
2 changed files with 44 additions and 10 deletions

View File

@@ -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;

View File

@@ -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" };